/* -*- Objc -*- */ /* * $Id: NetGameList.m,v 1.1 2003/05/26 10:21:38 thunder Exp $ * * Copyright (C) 2003 Free Software Foundation, Inc. * * This file is part of GNU Hégémonie. * * 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; either version 2 * of the License, or (at your option) any later version. * * 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. */ #include #include #include #include #include #include "Network/NetProtocol.h" #include "Network/NetGameList.h" #include "Network/NetGameID.h" #include "Network/NetClient.h" #include "Network/NetServer.h" #include "Network/NetParty.h" #include "Network/NetConnection.h" #include "GameEngine/HgPlayerState.h" @interface NSPort (Hack) - (NSHost*) host; @end /** * This class represents the list of existing games on the local network and * allows join a game. */ @implementation NetGameList /** * Initialise NetGameList with the existing games. These games are researched * on the local network. */ - (id) init { self = [super init]; if (self != nil) { _gameList = AUTORELEASE ([NSMutableArray array]); [self searchGames]; } return self; } /** * Release NetGameList. */ - (void) dealloc { RELEASE (_gameList); [super dealloc]; } /** * Creates and adds an gameID in the current list with an existing NetParty * and its NSHost associated. */ - (void) _addGameIDWithParty: (NetParty *)party withHost: (NSHost *)host { NSParameterAssert (party); NSParameterAssert (host); NetGameID *game = [[NetGameID alloc] initWithParty: party withHost: host]; [_gameList addObject: game]; } /** * Clear all the games in the list. */ - (void) _clearGameList { [_gameList removeAllObjects]; } /** * Research games on the local network to actualise the list and returns * this NetGameID list. */ /**---------pour l'instant recherche sur un seul serveur---------------**/ - (NSArray *) searchGames { id server; server = (id)[NetConnection rootProxyForConnectionWithRegisteredName: @"GameList" host: @"*"]; [self _clearGameList]; if (server != nil) { NSArray *partyList = [[server partyList] allValues]; unsigned int i; for (i = 1; i <= [partyList count]; i++) [self _addGameIDWithParty: [partyList objectAtIndex: i] withHost: [[[NetConnection currentConnection] sendPort] host]]; } return _gameList; } /** * Join an existing game, passing the player's name. * On success, returns the NetClient created. * Else, if this player's name is already use by an other player, returns nil. */ - (NetClient *) connectToGame: (NetGameID *)gameID withName: (NSString *)playerName { NSParameterAssert (gameID); NSParameterAssert (playerName); HgGame *game = [[HgGame alloc] init]; /* FIXME - error message without cast */ NSPort *receivePort = (NSPort *)[NSPort port]; NSPort *sendPort = [[gameID party] connectWithName: playerName withPort: receivePort initGame: game]; if (sendPort != nil) { // NetInfosClient *inf = [[gameID party] clientWithName: playerName]; // HgPlayerState *playerState; // playerState = [inf playerState]; NetConnection *cnx = [NetConnection connectionWithReceivePort: receivePort sendPort: sendPort]; NSDistantObject *serverInfosClient = [cnx rootProxy]; NetClient *client = AUTORELEASE ([[NetClient alloc] initWithProxy: serverInfosClient withGame: game]); [cnx setRootObject: client]; return client; } else { return nil; } } @end // End of NetGameList class implementation