/* 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 "YOGGameServer.h" #include "NetTestSuite.h" #include YOGGameServer::YOGGameServer(YOGLoginPolicy loginPolicy, YOGGamePolicy gamePolicy) : loginPolicy(loginPolicy), gamePolicy(gamePolicy) { nl.startListening(YOG_SERVER_PORT); } void YOGGameServer::update() { //First attempt connections with new players shared_ptr nc(new NetConnection); while(nl.attemptConnection(*nc)) { Uint16 id = chooseNewPlayerID(); players[id]=shared_ptr(new YOGPlayer(nc, id, *this)); nc.reset(new NetConnection); } //Call update to all of the players for(std::map >::iterator i=players.begin(); i!=players.end(); ++i) { i->second->update(); } //Call update to all of the games for(std::map >::iterator i=games.begin(); i!=games.end(); ++i) { i->second->update(); } //Remove all of the players that have disconnected. for(std::map >::iterator i=players.begin(); i!=players.end();) { if(!i->second->isConnected()) { playerHasLoggedOut(i->second->getPlayerID()); std::map >::iterator to_erase=i; i++; players.erase(to_erase); } else { i++; } } //Remove old games for(std::map >::iterator i=games.begin(); i!=games.end();) { if(i->second->isEmpty()) { removeGameInfo(i->second->getGameID()); std::map >::iterator to_erase=i; i++; games.erase(to_erase); } else { i++; } } if(broadcaster) broadcaster->update(); if(!broadcaster && isBroadcasting && gameList.size()) { LANGameInformation info; info.getGameInformation() = *gameList.begin(); broadcaster.reset(new NetBroadcaster(info)); } } int YOGGameServer::run() { NetTestSuite tests; bool cont = tests.runAllTests(); if(!cont) return 1; std::cout<<"Server started successfully."<& YOGGameServer::getGameList() const { return gameList; } const std::list& YOGGameServer::getPlayerList() const { return playerList; } void YOGGameServer::propogateMessage(boost::shared_ptr message, boost::shared_ptr sender) { shared_ptr nmessage(new NetSendYOGMessage(message)); if(message->getMessageType() == YOGNormalMessage) { for(std::map >::iterator i=players.begin(); i!=players.end(); ++i) { if(i->second != sender) { i->second->sendMessage(nmessage); } } } else if(message->getMessageType() == YOGGameMessage) { for(std::map >::iterator i=players.begin(); i!=players.end(); ++i) { if(i->second != sender && sender->getGameID() == i->second->getGameID()) { i->second->sendMessage(nmessage); } } } } void YOGGameServer::playerHasLoggedIn(const std::string& username, Uint16 id) { playerList.push_back(YOGPlayerInfo(username, id)); } void YOGGameServer::playerHasLoggedOut(Uint16 playerID) { for(std::list::iterator i=playerList.begin(); i!=playerList.end(); ++i) { if(i->getPlayerID() == playerID) { playerList.erase(i); break; } } } YOGGameCreateRefusalReason YOGGameServer::canCreateNewGame(const std::string& game) { //not implemented return YOGCreateRefusalUnknown; } Uint16 YOGGameServer::createNewGame(const std::string& name) { //choose the new game ID Uint16 newID=1; while(true) { bool found=false; for(std::list::iterator i=gameList.begin(); i!=gameList.end(); ++i) { if(i->getGameID() == newID) { found=true; break; } } if(found) newID+=1; else break; } gameList.push_back(YOGGameInfo(name, newID)); games[newID] = shared_ptr(new YOGGame(newID, *this)); return newID; } YOGGameJoinRefusalReason YOGGameServer::canJoinGame(Uint16 gameID) { //not implemented return YOGJoinRefusalUnknown; } shared_ptr YOGGameServer::getGame(Uint16 gameID) { return games[gameID]; } shared_ptr YOGGameServer::getPlayer(Uint16 playerID) { return players[playerID]; } void YOGGameServer::enableLANBroadcasting() { if(gameList.size()) { LANGameInformation info; info.getGameInformation() = *gameList.begin(); broadcaster.reset(new NetBroadcaster(info)); } isBroadcasting = true; } void YOGGameServer::disableLANBroadcasting() { broadcaster.reset(); isBroadcasting = false; } YOGGameInfo& YOGGameServer::getGameInfo(Uint16 gameID) { for(std::list::iterator i=gameList.begin(); i!=gameList.end(); ++i) { if(i->getGameID() == gameID) { return *i; } } } Uint16 YOGGameServer::chooseNewPlayerID() { //choose the new player ID. Uint16 newID=1; while(true) { bool found=false; if(players.find(newID) != players.end()) found=true; if(found) newID+=1; else break; } return newID; } void YOGGameServer::removeGameInfo(Uint16 gameID) { for(std::list::iterator i=gameList.begin(); i!=gameList.end(); ++i) { if(i->getGameID() == gameID) { gameList.erase(i); return; } } }