Gobligine/metaserver/games.cpp

239 lines
5.1 KiB
C++
Raw Normal View History

2005-04-17 12:39:41 -06:00
// _________ __ __
// / _____// |_____________ _/ |______ ____ __ __ ______
// \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/
// / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ |
// /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ >
// \/ \/ \//_____/ \/
// ______________________ ______________________
// T H E W A R B E G I N S
// Stratagus - A free fantasy real time strategy game engine
//
2005-09-17 20:18:31 -06:00
/**@name games.cpp - Basic games query process. */
2005-04-17 12:39:41 -06:00
//
// (c) Copyright 2005 by Jimmy Salmon
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; only version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
//
//@{
/*----------------------------------------------------------------------------
-- Includes
----------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stratagus.h"
#include "games.h"
#include "netdriver.h"
/*----------------------------------------------------------------------------
-- Variables
----------------------------------------------------------------------------*/
2005-09-25 10:32:15 -06:00
static GameData *Games;
2005-04-17 12:39:41 -06:00
int GameID;
/*----------------------------------------------------------------------------
-- Functions
----------------------------------------------------------------------------*/
/**
** Create a game
*/
2005-09-23 21:08:56 -06:00
void CreateGame(Session *session, char *description, char *map,
char *players, char *ip, char *port, char *password)
2005-04-17 12:39:41 -06:00
{
2005-09-23 21:08:56 -06:00
GameData *game;
2005-04-17 12:39:41 -06:00
2005-09-25 10:32:15 -06:00
game = new GameData;
2005-04-17 12:39:41 -06:00
strcpy(game->IP, ip);
strcpy(game->Port, port);
strcpy(game->Description, description);
strcpy(game->Map, map);
game->MaxSlots = atoi(players);
game->OpenSlots = game->MaxSlots - 1;
2005-04-26 19:46:20 -06:00
if (password) {
strcpy(game->Password, password);
} else {
game->Password[0] = '\0';
}
2005-04-17 12:39:41 -06:00
game->NumSessions = 1;
game->Sessions[0] = session;
game->ID = GameID++;
game->Started = 0;
game->GameName = session->UserData.GameName;
game->Version = session->UserData.Version;
if (Games) {
Games->Prev = game;
}
game->Next = Games;
game->Prev = NULL;
Games = game;
2005-09-25 10:32:15 -06:00
session->Game = game;
2005-04-17 12:39:41 -06:00
}
/**
** Cancel a game
*/
2005-09-23 21:08:56 -06:00
int CancelGame(Session *session)
2005-04-17 12:39:41 -06:00
{
2005-09-23 21:08:56 -06:00
GameData *game;
2005-04-17 12:39:41 -06:00
int i;
2005-09-25 10:32:15 -06:00
game = session->Game;
2005-04-17 12:39:41 -06:00
if (game->Sessions[0] != session) {
return -1; // Not the host
}
if (game->Next) {
game->Next->Prev = game->Prev;
}
if (game->Prev) {
game->Prev->Next = game->Next;
}
if (Games == game) {
Games = game->Next;
}
for (i = 0; i < game->NumSessions; ++i) {
2005-09-25 10:32:15 -06:00
game->Sessions[i]->Game = NULL;
2005-04-17 12:39:41 -06:00
}
2005-09-25 10:32:15 -06:00
delete game;
2005-04-17 12:39:41 -06:00
return 0;
}
/**
** Start a game
*/
2005-09-23 21:08:56 -06:00
int StartGame(Session *session)
2005-04-17 12:39:41 -06:00
{
2005-09-25 10:32:15 -06:00
if (session->Game->Sessions[0] != session) {
2005-04-17 12:39:41 -06:00
return -1; // Not the host
}
2005-09-25 10:32:15 -06:00
session->Game->Started = 1;
2005-04-17 12:39:41 -06:00
return 0;
}
/**
** Join a game
*/
2005-09-23 21:08:56 -06:00
int JoinGame(Session *session, int id, char *password)
2005-04-17 12:39:41 -06:00
{
2005-09-23 21:08:56 -06:00
GameData *game;
2005-04-17 12:39:41 -06:00
2005-09-25 10:32:15 -06:00
if (session->Game) {
2005-04-17 12:39:41 -06:00
return -1; // Already in a game
}
game = Games;
while (game) {
if (game->ID == id) {
break;
}
game = game->Next;
}
if (!game) {
return -2; // ID not found
}
2005-04-26 19:46:20 -06:00
if (game->Password[0]) {
if (!password || strcmp(game->Password, password)) {
return -3; // Wrong password
}
}
2005-04-17 12:39:41 -06:00
if (!game->OpenSlots) {
2005-04-26 19:46:20 -06:00
return -4; // Game full
2005-04-17 12:39:41 -06:00
}
game->Sessions[game->NumSessions++] = session;
2005-09-25 10:32:15 -06:00
session->Game = game;
2005-04-17 12:39:41 -06:00
return 0;
}
/**
** Leave a game
*/
2005-09-23 21:08:56 -06:00
int PartGame(Session *session)
2005-04-17 12:39:41 -06:00
{
2005-09-23 21:08:56 -06:00
GameData *game;
2005-04-17 12:39:41 -06:00
int i;
2005-09-25 10:32:15 -06:00
game = session->Game;
2005-04-17 12:39:41 -06:00
if (!game) {
return -1; // Not in a game
}
if (game->Started) {
return -2;
}
if (game->Sessions[0] == session) {
// The host left, cancel the game
CancelGame(session);
return 0;
}
for (i = 1; i < game->NumSessions; ++i) {
if (game->Sessions[i] == session) {
for (; i < game->NumSessions - 1; ++i) {
game->Sessions[i] = game->Sessions[i + 1];
}
game->NumSessions--;
break;
}
}
2005-09-25 10:32:15 -06:00
session->Game = NULL;
2005-04-17 12:39:41 -06:00
return 0;
}
2005-09-23 21:08:56 -06:00
static int MatchGameType(Session *session, GameData *game)
2005-04-17 12:39:41 -06:00
{
return (!*game->GameName || !strcmp(session->UserData.GameName, game->GameName)) &&
(!*game->Version || !strcmp(session->UserData.Version, game->Version));
}
/**
** List games
*/
2005-09-23 21:08:56 -06:00
void ListGames(Session *session)
2005-04-17 12:39:41 -06:00
{
2005-09-23 21:08:56 -06:00
GameData *game;
2005-04-17 12:39:41 -06:00
char buf[1024];
game = Games;
while (game) {
if (!game->Started && MatchGameType(session, game)) {
sprintf(buf, "LISTGAMES %d \"%s\" \"%s\" %d %d %s %s\n",
game->ID, game->Description, game->Map,
game->OpenSlots, game->MaxSlots, game->IP, game->Port);
Send(session, buf);
}
game = game->Next;
}
2005-04-17 13:11:10 -06:00
}