/* Copyright (C) 2007 Bradley Arsenault 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 3 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 "YOGClient.h" #include #include "MultiplayerGame.h" #include "MapAssembler.h" YOGClient::YOGClient(const std::string& server) { connect(server); } YOGClient::YOGClient() { initialize(); } void YOGClient::initialize() { connectionState = NotConnected; loginPolicy = YOGUnknownLoginPolicy; gamePolicy = YOGUnknownGamePolicy; loginState = YOGLoginUnknown; playerID=0; listener=NULL; wasConnected=false; } void YOGClient::connect(const std::string& server) { initialize(); nc.openConnection(server, YOG_SERVER_PORT); connectionState = NeedToSendClientInformation; if(nc.isConnected()) wasConnected=true; } bool YOGClient::isConnected() { return nc.isConnected(); } void YOGClient::update() { if(server) server->update(); if(!nc.isConnected() && wasConnected) { if(listener) { shared_ptr event(new YOGConnectionLostEvent); listener->handleYOGEvent(event); } wasConnected=false; } if(nc.isConnected()) { //If we need to send client information, send it if(connectionState == NeedToSendClientInformation) { shared_ptr message(new NetSendClientInformation); nc.sendMessage(message); connectionState = WaitingForServerInformation; } //Parse incoming messages and generate events shared_ptr message = nc.getMessage(); if(!message) return; Uint8 type = message->getMessageType(); //This recieves the server information if(type==MNetSendServerInformation) { shared_ptr info = static_pointer_cast(message); loginPolicy = info->getLoginPolicy(); gamePolicy = info->getGamePolicy(); playerID = info->getPlayerID(); if(listener) { shared_ptr event(new YOGConnectedEvent); listener->handleYOGEvent(event); } connectionState = WaitingForLoginInformation; } //This recieves a login acceptance message if(type==MNetLoginSuccessful) { shared_ptr info = static_pointer_cast(message); connectionState = ClientOnStandby; loginState = YOGLoginSuccessful; if(listener) { shared_ptr event(new YOGLoginAcceptedEvent); listener->handleYOGEvent(event); } } //This recieves a login refusal message if(type==MNetRefuseLogin) { shared_ptr info = static_pointer_cast(message); connectionState = WaitingForLoginInformation; loginState = info->getRefusalReason(); if(listener) { shared_ptr event(new YOGLoginRefusedEvent(info->getRefusalReason())); listener->handleYOGEvent(event); } } //This recieves a registration acceptance message if(type==MNetAcceptRegistration) { shared_ptr info = static_pointer_cast(message); connectionState = ClientOnStandby; loginState = YOGLoginSuccessful; if(listener) { shared_ptr event(new YOGLoginAcceptedEvent); listener->handleYOGEvent(event); } } //This recieves a regisration refusal message if(type==MNetRefuseRegistration) { shared_ptr info = static_pointer_cast(message); connectionState = WaitingForLoginInformation; loginState = info->getRefusalReason(); if(listener) { shared_ptr event(new YOGLoginRefusedEvent(info->getRefusalReason())); listener->handleYOGEvent(event); } } ///This recieves a game list update message if(type==MNetUpdateGameList) { shared_ptr info = static_pointer_cast(message); info->applyDifferences(games); if(listener) { shared_ptr event(new YOGGameListUpdatedEvent); listener->handleYOGEvent(event); } } ///This recieves a player list update message if(type==MNetUpdatePlayerList) { shared_ptr info = static_pointer_cast(message); info->applyDifferences(players); if(listener) { shared_ptr event(new YOGPlayerListUpdatedEvent); listener->handleYOGEvent(event); } } ///This recieves a YOGMessage list update message if(type==MNetSendYOGMessage) { shared_ptr yogmessage = static_pointer_cast(message); messages.push(yogmessage->getMessage()); } if(type==MNetCreateGameAccepted) { joinedGame->recieveMessage(message); } if(type==MNetCreateGameRefused) { joinedGame->recieveMessage(message); } if(type==MNetGameJoinAccepted) { joinedGame->recieveMessage(message); } if(type==MNetGameJoinRefused) { joinedGame->recieveMessage(message); } if(type==MNetSendMapHeader) { joinedGame->recieveMessage(message); } if(type==MNetSendGameHeader) { joinedGame->recieveMessage(message); } if(type==MNetPlayerJoinsGame) { joinedGame->recieveMessage(message); } if(type==MNetPlayerLeavesGame) { if(joinedGame) joinedGame->recieveMessage(message); } if(type==MNetStartGame) { joinedGame->recieveMessage(message); } if(type==MNetSendOrder) { //ignore orders for when there is no joined game, //say, the leftover orders in transit after a player //quits a game if(joinedGame) joinedGame->recieveMessage(message); } if(type==MNetRequestMap) { joinedGame->recieveMessage(message); } if(type==MNetKickPlayer) { joinedGame->recieveMessage(message); } if(type==MNetReadyToLaunch) { joinedGame->recieveMessage(message); } if(type==MNetSendFileInformation) { assembler->handleMessage(message); } if(type==MNetRequestNextChunk) { assembler->handleMessage(message); } if(type==MNetSendFileChunk) { assembler->handleMessage(message); } } } YOGClient::ConnectionState YOGClient::getConnectionState() const { return connectionState; } YOGLoginPolicy YOGClient::getLoginPolicy() const { return loginPolicy; } YOGGamePolicy YOGClient::getGamePolicy() const { return gamePolicy; } Uint16 YOGClient::getPlayerID() const { return playerID; } void YOGClient::attemptLogin(const std::string& nusername, const std::string& password) { username = nusername; shared_ptr message(new NetAttemptLogin(username, password)); nc.sendMessage(message); connectionState = WaitingForLoginReply; } void YOGClient::attemptRegistration(const std::string& username, const std::string& password) { shared_ptr message(new NetAttemptRegistration(username, password)); nc.sendMessage(message); connectionState = WaitingForRegistrationReply; } YOGLoginState YOGClient::getLoginState() const { return loginState; } const std::list& YOGClient::getGameList() const { return games; } const std::list& YOGClient::getPlayerList() const { return players; } std::string YOGClient::findPlayerName(Uint16 playerID) { for(std::list::iterator i = players.begin(); i != players.end(); ++i) { if(i->getPlayerID() == playerID) return i->getPlayerName(); } return ""; } void YOGClient::requestGameListUpdate() { //unimplemented } void YOGClient::requestPlayerListUpdate() { //unimplemented } void YOGClient::disconnect() { shared_ptr message(new NetDisconnect); nc.sendMessage(message); nc.closeConnection(); connectionState = NotConnected; wasConnected=false; } void YOGClient::sendMessage(boost::shared_ptr mmessage) { messages.push(mmessage); shared_ptr message(new NetSendYOGMessage(mmessage)); nc.sendMessage(message); } boost::shared_ptr YOGClient::getMessage() { if(messages.size()) { boost::shared_ptr m = messages.front(); messages.pop(); return m; } return boost::shared_ptr(); } std::string YOGClient::getUsername() const { return username; } void YOGClient::createGame(const std::string& name) { shared_ptr message(new NetCreateGame(name)); nc.sendMessage(message); } void YOGClient::setMultiplayerGame(boost::shared_ptr game) { joinedGame=game; } boost::shared_ptr YOGClient::getMultiplayerGame() { return joinedGame; } void YOGClient::sendNetMessage(boost::shared_ptr message) { nc.sendMessage(message); } void YOGClient::setMapAssembler(boost::shared_ptr nassembler) { assembler=nassembler; } boost::shared_ptr YOGClient::getMapAssembler() { return assembler; } void YOGClient::setEventListener(YOGEventListener* nlistener) { listener=nlistener; } void YOGClient::attachGameServer(boost::shared_ptr nserver) { server = nserver; }