From: <tz...@us...> - 2006-11-22 03:56:48
|
Revision: 1953 http://svn.sourceforge.net/freeorion/revision/?rev=1953&view=rev Author: tzlaine Date: 2006-11-21 19:56:47 -0800 (Tue, 21 Nov 2006) Log Message: ----------- More XML to plain text message conversions, and a slight refinement of the existing set of messages. The new messages are more natural and explicit. Modified Paths: -------------- trunk/FreeOrion/UI/IntroScreen.cpp trunk/FreeOrion/UI/MultiplayerLobbyWnd.cpp trunk/FreeOrion/client/human/HumanClientApp.cpp trunk/FreeOrion/network/Message.cpp trunk/FreeOrion/network/Message.h trunk/FreeOrion/network/NetworkCore.h trunk/FreeOrion/server/ServerApp.cpp Modified: trunk/FreeOrion/UI/IntroScreen.cpp =================================================================== --- trunk/FreeOrion/UI/IntroScreen.cpp 2006-11-22 03:10:41 UTC (rev 1952) +++ trunk/FreeOrion/UI/IntroScreen.cpp 2006-11-22 03:56:47 UTC (rev 1953) @@ -229,7 +229,7 @@ if (!failed) { ClientUI::GetClientUI()->ScreenNewGame(); - HumanClientApp::GetApp()->NetworkCore().SendMessage(HostGameMessage(HumanClientApp::GetApp()->PlayerID(), game_parameters)); + HumanClientApp::GetApp()->NetworkCore().SendMessage(HostSPGameMessage(HumanClientApp::GetApp()->PlayerID(), game_parameters)); } } else { failed = true; @@ -284,7 +284,7 @@ ClientUI::GetClientUI()->ScreenIntro(); } else { HumanClientApp::GetApp()->NetworkCore().SendMessage(server_connect_wnd.Result().second == "HOST GAME SELECTED" ? - HostGameMessage(HumanClientApp::GetApp()->PlayerID(), server_connect_wnd.Result().first) : + HostMPGameMessage(HumanClientApp::GetApp()->PlayerID(), server_connect_wnd.Result().first) : JoinGameMessage(server_connect_wnd.Result().first)); MultiplayerLobbyWnd multiplayer_lobby_wnd(server_connect_wnd.Result().second == "HOST GAME SELECTED"); multiplayer_lobby_wnd.Run(); Modified: trunk/FreeOrion/UI/MultiplayerLobbyWnd.cpp =================================================================== --- trunk/FreeOrion/UI/MultiplayerLobbyWnd.cpp 2006-11-22 03:10:41 UTC (rev 1952) +++ trunk/FreeOrion/UI/MultiplayerLobbyWnd.cpp 2006-11-22 03:56:47 UTC (rev 1953) @@ -415,7 +415,7 @@ void MultiplayerLobbyWnd::StartGameClicked() { - HumanClientApp::GetApp()->NetworkCore().SendMessage(HostGameMessage(HumanClientApp::GetApp()->PlayerID(), HumanClientApp::GetApp()->PlayerName())); + HumanClientApp::GetApp()->NetworkCore().SendMessage(StartMPGameMessage(HumanClientApp::GetApp()->PlayerID())); m_result = true; CUIWnd::CloseClicked(); } Modified: trunk/FreeOrion/client/human/HumanClientApp.cpp =================================================================== --- trunk/FreeOrion/client/human/HumanClientApp.cpp 2006-11-22 03:10:41 UTC (rev 1952) +++ trunk/FreeOrion/client/human/HumanClientApp.cpp 2006-11-22 03:56:47 UTC (rev 1953) @@ -253,11 +253,9 @@ m_empire_id = -1; m_player_name = "Happy_Player"; - // HACK! send the multiplayer form of the HostGameMessage, since it establishes us as the host, and the single-player - // LOAD_GAME message will establish us as a single-player game - XMLDoc parameters; - parameters.root_node.AppendChild(XMLElement("host_player_name", std::string("Happy_Player"))); - NetworkCore().SendMessage(HostGameMessage(NetworkCore::HOST_PLAYER_ID, parameters)); + // HACK! Send HostMPGameMessage, since it establishes us as the host, and the LOAD_GAME message will + // establish us as a single-player game. + NetworkCore().SendMessage(HostMPGameMessage(NetworkCore::HOST_PLAYER_ID, "Happy_Player")); NetworkCore().SendMessage(HostLoadGameMessage(NetworkCore::HOST_PLAYER_ID, filename)); return true; @@ -545,9 +543,10 @@ break; } - case Message::HOST_GAME: { + case Message::HOST_SP_GAME: + case Message::HOST_MP_GAME: { if (msg.Sender() == -1 && msg.GetText() == "ACK") - Logger().debugStream() << "HumanClientApp::HandleMessageImpl : Received HOST_GAME acknowledgement"; + Logger().debugStream() << "HumanClientApp::HandleMessageImpl : Received " << (msg.Type() == Message::HOST_SP_GAME ? "HOST_SP_GAME" : "HOST_MP_GAME") << " acknowledgement"; break; } Modified: trunk/FreeOrion/network/Message.cpp =================================================================== --- trunk/FreeOrion/network/Message.cpp 2006-11-22 03:10:41 UTC (rev 1952) +++ trunk/FreeOrion/network/Message.cpp 2006-11-22 03:56:47 UTC (rev 1953) @@ -55,12 +55,14 @@ GG_ENUM_MAP_INSERT(Message::UNDEFINED) GG_ENUM_MAP_INSERT(Message::DEBUG) GG_ENUM_MAP_INSERT(Message::SERVER_STATUS) - GG_ENUM_MAP_INSERT(Message::HOST_GAME) + GG_ENUM_MAP_INSERT(Message::HOST_SP_GAME) + GG_ENUM_MAP_INSERT(Message::HOST_MP_GAME) GG_ENUM_MAP_INSERT(Message::JOIN_GAME) GG_ENUM_MAP_INSERT(Message::LOBBY_UPDATE) GG_ENUM_MAP_INSERT(Message::LOBBY_CHAT) GG_ENUM_MAP_INSERT(Message::LOBBY_HOST_ABORT) GG_ENUM_MAP_INSERT(Message::LOBBY_EXIT) + GG_ENUM_MAP_INSERT(Message::START_MP_GAME) GG_ENUM_MAP_INSERT(Message::SAVE_GAME) GG_ENUM_MAP_INSERT(Message::LOAD_GAME) GG_ENUM_MAP_INSERT(Message::GAME_START) @@ -269,16 +271,14 @@ //////////////////////////////////////////////// // Message-Creation Free Functions //////////////////////////////////////////////// -Message HostGameMessage(int player_id, const XMLDoc& doc) +Message HostSPGameMessage(int player_id, const XMLDoc& doc) { - return Message(Message::HOST_GAME, player_id, -1, Message::CORE, doc); + return Message(Message::HOST_SP_GAME, player_id, -1, Message::CORE, doc); } -Message HostGameMessage(int player_id, const std::string& host_player_name) +Message HostMPGameMessage(int player_id, const std::string& host_player_name) { - XMLDoc doc; - doc.root_node.AppendChild(XMLElement("host_player_name", host_player_name)); - return Message(Message::HOST_GAME, player_id, -1, Message::CORE, doc); + return Message(Message::HOST_MP_GAME, player_id, -1, Message::CORE, host_player_name); } Message JoinGameMessage(const std::string& player_name) @@ -296,11 +296,16 @@ return Message(Message::GAME_START, -1, player_id, Message::CORE, data); } -Message HostAckMessage(int player_id) +Message HostSPAckMessage(int player_id) { - return Message(Message::HOST_GAME, -1, player_id, Message::CORE, "ACK"); + return Message(Message::HOST_SP_GAME, -1, player_id, Message::CORE, "ACK"); } +Message HostMPAckMessage(int player_id) +{ + return Message(Message::HOST_MP_GAME, -1, player_id, Message::CORE, "ACK"); +} + Message JoinAckMessage(int player_id) { return Message(Message::JOIN_GAME, -1, player_id, Message::CORE, boost::lexical_cast<std::string>(player_id)); @@ -328,7 +333,7 @@ Message TurnProgressMessage(int player_id, Message::TurnProgressPhase phase_id, int empire_id) { - /// Turn progres message sends down message ID instead of text for faster transfer + /// Turn progress message sends down message ID instead of text for faster transfer /// The data is a number indicating the phase being started and the empire ID XMLDoc doc; @@ -446,3 +451,8 @@ { return Message(Message::LOBBY_EXIT, sender, receiver, Message::CLIENT_LOBBY_MODULE, ""); } + +Message StartMPGameMessage(int player_id) +{ + return Message(Message::START_MP_GAME, player_id, -1, Message::CORE, ""); +} Modified: trunk/FreeOrion/network/Message.h =================================================================== --- trunk/FreeOrion/network/Message.h 2006-11-22 03:10:41 UTC (rev 1952) +++ trunk/FreeOrion/network/Message.h 2006-11-22 03:56:47 UTC (rev 1953) @@ -35,13 +35,15 @@ UNDEFINED, DEBUG, ///< used to send special messages used for debugging purposes SERVER_STATUS, ///< sent to the client when requested, and when the server first recieves a connection from a client - HOST_GAME, ///< sent when a client wishes to establish a game at the server + HOST_SP_GAME, ///< sent when a client wishes to establish a single player game at the server + HOST_MP_GAME, ///< sent when a client wishes to establish a multiplayer game at the server JOIN_GAME, ///< sent when a client wishes to join a game being established at the server RENAME_PLAYER, ///< sent when the server must assign a new name to a player, because another player already has her desired name LOBBY_UPDATE, ///< used to synchronize multiplayer lobby dialogs among different players, when a user changes a setting, or the server updates the state LOBBY_CHAT, ///< used to send chat messages in the multiplayer lobby LOBBY_HOST_ABORT, ///< sent to server (by the "host" client only) when a multiplayer game is to be cancelled while it is still being set up in the multiplayer lobby LOBBY_EXIT, ///< sent to server (by a non-"host" client only) when a player leaves the multiplayer lobby + START_MP_GAME, ///< sent to server (by the "host" client only) when the settings in the MP lobby are satisfactory and it is time to start the game SAVE_GAME, ///< sent to server (by the "host" client only) when a game is to be saved, or from the server to the clients when the game is being saved LOAD_GAME, ///< sent to server (by the "host" client only) when a game is to be loaded, or from the server to the clients when the game is being loaded GAME_START, ///< sent to each client before the first turn of a new or newly loaded game, instead of a TURN_UPDATE @@ -141,11 +143,11 @@ std::ostream& operator<<(std::ostream& os, const Message& msg); -/** creates a HOST_GAME message*/ -Message HostGameMessage(int player_id, const XMLDoc& doc); +/** creates a HOST_SP_GAME message*/ +Message HostSPGameMessage(int player_id, const XMLDoc& doc); -/** creates a minimal HOST_GAME message used to enter and finalize the multiplayer "lobby" setup*/ -Message HostGameMessage(int player_id, const std::string& host_player_name); +/** creates a minimal HOST_MP_GAME message used to initiate multiplayer "lobby" setup*/ +Message HostMPGameMessage(int player_id, const std::string& host_player_name); /** creates a JOIN_GAME message. The sender's player name is sent in the message.*/ Message JoinGameMessage(const std::string& player_name); @@ -156,10 +158,14 @@ /** creates a GAME_START message. Contains the initial game state visible to player \a player_id.*/ Message GameStartMessage(int player_id, const std::string& data); -/** creates a HOST_GAME acknowledgement message. The \a player_id is the ID of the receiving player. This message +/** creates a HOST_SP_GAME acknowledgement message. The \a player_id is the ID of the receiving player. This message should only be sent by the server.*/ -Message HostAckMessage(int player_id); +Message HostSPAckMessage(int player_id); +/** creates a HOST_MP_GAME acknowledgement message. The \a player_id is the ID of the receiving player. This message + should only be sent by the server.*/ +Message HostMPAckMessage(int player_id); + /** creates a JOIN_GAME acknowledgement message. The \a player_id is the ID of the receiving player. This message should only be sent by the server.*/ Message JoinAckMessage(int player_id); @@ -255,4 +261,7 @@ /** creates an LOBBY_EXIT message. This message should only be sent by the server.*/ Message ServerLobbyExitMessage(int sender, int receiver); +/** creates a START_MP_GAME used to finalize the multiplayer "lobby" setup*/ +Message StartMPGameMessage(int player_id); + #endif // _Message_h_ Modified: trunk/FreeOrion/network/NetworkCore.h =================================================================== --- trunk/FreeOrion/network/NetworkCore.h 2006-11-22 03:10:41 UTC (rev 1952) +++ trunk/FreeOrion/network/NetworkCore.h 2006-11-22 03:56:47 UTC (rev 1953) @@ -26,9 +26,9 @@ /** the states the server may be in at various points during its execution*/ enum ServerState { - SERVER_IDLE, ///< there is no game yet and no one has send a HOST_GAME Message yet; this is the initial state + SERVER_IDLE, ///< there is no game yet and no one has send a HOST_*_GAME Message yet; this is the initial state SERVER_MP_LOBBY, ///< the host and possibly other players are in the multiplayer lobby, preparing to start a game - SERVER_GAME_SETUP, ///< a HOST_GAME Message has been received, and a game is being set up (the server is waiting for all players to join) + SERVER_GAME_SETUP, ///< a HOST_*_GAME Message has been received, and a game is being set up (the server is waiting for all players to join) SERVER_WAITING, ///< a game is in progress and currently the server is waiting for players to finish their turns SERVER_PROCESSING, ///< the server is processing a turn SERVER_DISCONNECT, ///< the server has encountered a disconnect error and is dealing with it Modified: trunk/FreeOrion/server/ServerApp.cpp =================================================================== --- trunk/FreeOrion/server/ServerApp.cpp 2006-11-22 03:10:41 UTC (rev 1952) +++ trunk/FreeOrion/server/ServerApp.cpp 2006-11-22 03:56:47 UTC (rev 1953) @@ -213,86 +213,75 @@ void ServerApp::HandleMessage(const Message& msg) { switch (msg.Type()) { - case Message::HOST_GAME: { // this should only be received at the end of MP setup - std::string host_player_name = msg.GetText(); - std::map<int, PlayerInfo>::const_iterator it = m_network_core.Players().find(msg.Sender()); - bool spoofed_host = (m_state == SERVER_MP_LOBBY && it != m_network_core.Players().end() && it->second.name == host_player_name); - if (!spoofed_host) { - if (m_lobby_data.m_new_game) { // new game - m_galaxy_size = m_lobby_data.m_size; - m_galaxy_shape = m_lobby_data.m_shape; - m_galaxy_age = m_lobby_data.m_age; - m_starlane_freq = m_lobby_data.m_starlane_freq; - m_planet_density = m_lobby_data.m_planet_density; - m_specials_freq = m_lobby_data.m_specials_freq; - m_expected_players = m_network_core.Players().size() + m_lobby_data.m_AIs.size(); - for (std::map<int, PlayerInfo>::const_iterator it = m_network_core.Players().begin(); it != m_network_core.Players().end(); ++it) { - if (it->first != msg.Sender()) - m_network_core.SendMessage(Message(Message::GAME_START, -1, it->first, Message::CLIENT_LOBBY_MODULE, "")); + case Message::START_MP_GAME: { + if (m_lobby_data.m_new_game) { // new game + m_galaxy_size = m_lobby_data.m_size; + m_galaxy_shape = m_lobby_data.m_shape; + m_galaxy_age = m_lobby_data.m_age; + m_starlane_freq = m_lobby_data.m_starlane_freq; + m_planet_density = m_lobby_data.m_planet_density; + m_specials_freq = m_lobby_data.m_specials_freq; + m_expected_players = m_network_core.Players().size() + m_lobby_data.m_AIs.size(); + for (std::map<int, PlayerInfo>::const_iterator it = m_network_core.Players().begin(); it != m_network_core.Players().end(); ++it) { + if (it->first != msg.Sender()) + m_network_core.SendMessage(Message(Message::GAME_START, -1, it->first, Message::CLIENT_LOBBY_MODULE, "")); + } + m_state = SERVER_GAME_SETUP; + CreateAIClients(m_lobby_data.m_AIs); + m_player_save_game_data.clear(); + m_log_category.debugStream() << "ServerApp::HandleMessage : Server now in mode " << SERVER_GAME_SETUP << " (SERVER_GAME_SETUP)."; + if (m_expected_players == static_cast<int>(m_network_core.Players().size())) { + NewGameInit(); + m_state = SERVER_WAITING; + m_log_category.debugStream() << "ServerApp::HandleMessage : Server now in mode " << SERVER_WAITING << " (SERVER_WAITING)."; + } + } else { // load game + std::map<int, PlayerInfo>::const_iterator sender_it = m_network_core.Players().find(msg.Sender()); + if (sender_it != m_network_core.Players().end() && sender_it->second.host) { + m_empires.RemoveAllEmpires(); + m_single_player_game = false; + + std::string load_filename = (GetLocalDir() / "save" / m_lobby_data.m_save_games[m_lobby_data.m_save_file_index]).native_file_string(); + std::ifstream ifs(load_filename.c_str(), std::ios_base::binary); + { + boost::archive::xml_iarchive ia(ifs); + ia >> BOOST_SERIALIZATION_NVP(m_current_turn); + Universe::s_encoding_empire = ALL_EMPIRES; + ia >> BOOST_SERIALIZATION_NVP(m_player_save_game_data); + ia >> BOOST_SERIALIZATION_NVP(m_universe); } - m_state = SERVER_GAME_SETUP; - CreateAIClients(m_lobby_data.m_AIs); - m_player_save_game_data.clear(); - m_log_category.debugStream() << "ServerApp::HandleMessage : Server now in mode " << SERVER_GAME_SETUP << " (SERVER_GAME_SETUP)."; - if (m_expected_players == static_cast<int>(m_network_core.Players().size())) { - NewGameInit(); - m_state = SERVER_WAITING; - m_log_category.debugStream() << "ServerApp::HandleMessage : Server now in mode " << SERVER_WAITING << " (SERVER_WAITING)."; - } - } else { // load game - std::map<int, PlayerInfo>::const_iterator sender_it = m_network_core.Players().find(msg.Sender()); - if (sender_it != m_network_core.Players().end() && sender_it->second.host) { - m_empires.RemoveAllEmpires(); - m_single_player_game = false; + ifs.close(); + m_expected_players = m_player_save_game_data.size(); - std::string load_filename = (GetLocalDir() / "save" / m_lobby_data.m_save_games[m_lobby_data.m_save_file_index]).native_file_string(); - std::ifstream ifs(load_filename.c_str(), std::ios_base::binary); - { - boost::archive::xml_iarchive ia(ifs); - ia >> BOOST_SERIALIZATION_NVP(m_current_turn); - Universe::s_encoding_empire = ALL_EMPIRES; - ia >> BOOST_SERIALIZATION_NVP(m_player_save_game_data); - ia >> BOOST_SERIALIZATION_NVP(m_universe); - } - ifs.close(); - m_expected_players = m_player_save_game_data.size(); - - m_empires.RemoveAllEmpires(); - for (unsigned int i = 0; i < m_player_save_game_data.size(); ++i) { - for (unsigned int j = 0; j < m_lobby_data.m_players.size(); ++j) { - assert(m_player_save_game_data[i].m_empire); - if (m_lobby_data.m_players[j].m_save_game_empire_id == m_player_save_game_data[i].m_empire->EmpireID()) { - std::map<int, PlayerInfo>::const_iterator player_it = m_network_core.Players().begin(); - std::advance(player_it, j); - m_player_save_game_data[i].m_name = player_it->second.name; - m_player_save_game_data[i].m_empire->SetPlayerName(player_it->second.name); - } + m_empires.RemoveAllEmpires(); + for (unsigned int i = 0; i < m_player_save_game_data.size(); ++i) { + for (unsigned int j = 0; j < m_lobby_data.m_players.size(); ++j) { + assert(m_player_save_game_data[i].m_empire); + if (m_lobby_data.m_players[j].m_save_game_empire_id == m_player_save_game_data[i].m_empire->EmpireID()) { + std::map<int, PlayerInfo>::const_iterator player_it = m_network_core.Players().begin(); + std::advance(player_it, j); + m_player_save_game_data[i].m_name = player_it->second.name; + m_player_save_game_data[i].m_empire->SetPlayerName(player_it->second.name); } - m_empires.InsertEmpire(m_player_save_game_data[i].m_empire); } + m_empires.InsertEmpire(m_player_save_game_data[i].m_empire); + } - for (std::map<int, PlayerInfo>::const_iterator it = m_network_core.Players().begin(); it != m_network_core.Players().end(); ++it) { - if (it != sender_it) - m_network_core.SendMessage(Message(Message::GAME_START, -1, it->first, Message::CLIENT_LOBBY_MODULE, "")); - } + for (std::map<int, PlayerInfo>::const_iterator it = m_network_core.Players().begin(); it != m_network_core.Players().end(); ++it) { + if (it != sender_it) + m_network_core.SendMessage(Message(Message::GAME_START, -1, it->first, Message::CLIENT_LOBBY_MODULE, "")); + } - int AI_clients = m_expected_players - m_network_core.Players().size(); - CreateAIClients(std::vector<PlayerSetupData>(AI_clients)); - m_state = SERVER_GAME_SETUP; + int AI_clients = m_expected_players - m_network_core.Players().size(); + CreateAIClients(std::vector<PlayerSetupData>(AI_clients)); + m_state = SERVER_GAME_SETUP; - if (!AI_clients) - LoadGameInit(); - } else { - m_log_category.errorStream() << "Player #" << msg.Sender() << " attempted to initiate a game load, but is not the host, or is " - "not found in the player list."; - } + if (!AI_clients) + LoadGameInit(); + } else { + m_log_category.errorStream() << "Player #" << msg.Sender() << " attempted to initiate a game load, but is not the host, or is " + "not found in the player list."; } - } else { - const char* socket_hostname = SDLNet_ResolveIP(const_cast<IPaddress*>(&m_network_core.Players().find(msg.Sender())->second.address)); - m_log_category.errorStream() << "ServerApp::HandleMessage : A human player attempted to host " - "a new MP game with the wrong player name, or while one was not being setup. Terminating connection to " << - (socket_hostname ? socket_hostname : "[unknown host]") << " (player #" << msg.Sender() << ")"; - m_network_core.DumpPlayer(msg.Sender()); } break; } @@ -579,56 +568,70 @@ void ServerApp::HandleNonPlayerMessage(const Message& msg, const PlayerInfo& connection) { switch (msg.Type()) { - case Message::HOST_GAME: { + case Message::HOST_SP_GAME: { if (m_network_core.Players().empty() && m_expected_ai_players.empty()) { std::stringstream stream(msg.GetText()); XMLDoc doc; doc.ReadDoc(stream); std::string host_player_name = doc.root_node.Child("host_player_name").Text(); + PlayerInfo host_player_info(connection.socket, connection.address, host_player_name, true); + int player_id = NetworkCore::HOST_PLAYER_ID; + // immediately start a new game with the given parameters + m_single_player_game = true; + m_expected_players = boost::lexical_cast<int>(doc.root_node.Child("num_players").Text()); + m_galaxy_size = boost::lexical_cast<int>(doc.root_node.Child("universe_params").Child("size").Text()); + m_galaxy_shape = boost::lexical_cast<Shape>(doc.root_node.Child("universe_params").Child("shape").Text()); + m_galaxy_age = boost::lexical_cast<Age>(doc.root_node.Child("universe_params").Child("age").Text()); + m_starlane_freq = boost::lexical_cast<StarlaneFrequency>(doc.root_node.Child("universe_params").Child("starlane_freq").Text()); + m_planet_density = boost::lexical_cast<PlanetDensity>(doc.root_node.Child("universe_params").Child("planet_density").Text()); + m_specials_freq = boost::lexical_cast<SpecialsFrequency>(doc.root_node.Child("universe_params").Child("specials_freq").Text()); + CreateAIClients(doc.root_node); + m_player_save_game_data.clear(); + m_lobby_data.m_players.clear(); + m_lobby_data.m_players.push_back(PlayerSetupData()); + m_lobby_data.m_players.back().m_player_id = 0; + m_lobby_data.m_players.back().m_player_name = "Happy_Player"; + m_lobby_data.m_players.back().m_empire_name = doc.root_node.Child("empire_name").Text(); + m_lobby_data.m_players.back().m_empire_color = XMLToClr(doc.root_node.Child("empire_color").Child("GG::Clr")); + m_state = SERVER_GAME_SETUP; + if (m_network_core.EstablishPlayer(connection.socket, player_id, host_player_info)) { + m_network_core.SendMessage(HostSPAckMessage(player_id)); + m_network_core.SendMessage(JoinAckMessage(player_id)); + } + m_log_category.debugStream() << "ServerApp::HandleNonPlayerMessage : Server now in mode " << SERVER_GAME_SETUP << " (SERVER_GAME_SETUP)."; + m_log_category.debugStream() << "ServerApp::HandleNonPlayerMessage : Universe size set to " << m_galaxy_size << " systems (SERVER_GAME_SETUP)."; + m_log_category.debugStream() << "ServerApp::HandleNonPlayerMessage : Universe shape set to " << m_galaxy_shape << " (SERVER_GAME_SETUP)."; + } else { + const char* socket_hostname = SDLNet_ResolveIP(const_cast<IPaddress*>(&connection.address)); + m_log_category.errorStream() << "ServerApp::HandleNonPlayerMessage : A human player attempted to host " + "a new game but there was already one in progress or one being setup. Terminating connection to " << + (socket_hostname ? socket_hostname : "[unknown host]") << " on socket " << connection.socket; + m_network_core.DumpConnection(connection.socket); + } + break; + } + + case Message::HOST_MP_GAME: { + if (m_network_core.Players().empty() && m_expected_ai_players.empty()) { + std::string host_player_name = msg.GetText(); PlayerInfo host_player_info(connection.socket, connection.address, host_player_name, true); int player_id = NetworkCore::HOST_PLAYER_ID; - if (!doc.root_node.ContainsChild("universe_params")) { // start an MP lobby situation so that game settings can be established - m_single_player_game = false; - m_state = SERVER_MP_LOBBY; - m_lobby_data = MultiplayerLobbyData(true); - m_log_category.debugStream() << "ServerApp::HandleNonPlayerMessage : Server now in mode " << SERVER_MP_LOBBY << " (SERVER_MP_LOBBY)."; - if (m_network_core.EstablishPlayer(connection.socket, player_id, host_player_info)) { - m_network_core.SendMessage(HostAckMessage(player_id)); - m_network_core.SendMessage(JoinAckMessage(player_id)); - m_lobby_data.m_players.push_back(PlayerSetupData()); - m_lobby_data.m_players.back().m_player_id = player_id; - m_lobby_data.m_players.back().m_player_name = host_player_name; - m_lobby_data.m_players.back().m_empire_color = EmpireColors().at(0); - } - m_network_core.SendMessage(ServerLobbyUpdateMessage(player_id, LobbyUpdate())); - } else { // immediately start a new game with the given parameters - m_single_player_game = true; - m_expected_players = boost::lexical_cast<int>(doc.root_node.Child("num_players").Text()); - m_galaxy_size = boost::lexical_cast<int>(doc.root_node.Child("universe_params").Child("size").Text()); - m_galaxy_shape = boost::lexical_cast<Shape>(doc.root_node.Child("universe_params").Child("shape").Text()); - m_galaxy_age = boost::lexical_cast<Age>(doc.root_node.Child("universe_params").Child("age").Text()); - m_starlane_freq = boost::lexical_cast<StarlaneFrequency>(doc.root_node.Child("universe_params").Child("starlane_freq").Text()); - m_planet_density = boost::lexical_cast<PlanetDensity>(doc.root_node.Child("universe_params").Child("planet_density").Text()); - m_specials_freq = boost::lexical_cast<SpecialsFrequency>(doc.root_node.Child("universe_params").Child("specials_freq").Text()); - CreateAIClients(doc.root_node); - m_player_save_game_data.clear(); - m_lobby_data.m_players.clear(); + // start an MP lobby situation so that game settings can be established + m_single_player_game = false; + m_state = SERVER_MP_LOBBY; + m_lobby_data = MultiplayerLobbyData(true); + m_log_category.debugStream() << "ServerApp::HandleNonPlayerMessage : Server now in mode " << SERVER_MP_LOBBY << " (SERVER_MP_LOBBY)."; + if (m_network_core.EstablishPlayer(connection.socket, player_id, host_player_info)) { + m_network_core.SendMessage(HostMPAckMessage(player_id)); + m_network_core.SendMessage(JoinAckMessage(player_id)); m_lobby_data.m_players.push_back(PlayerSetupData()); - m_lobby_data.m_players.back().m_player_id = 0; - m_lobby_data.m_players.back().m_player_name = "Happy_Player"; - m_lobby_data.m_players.back().m_empire_name = doc.root_node.Child("empire_name").Text(); - m_lobby_data.m_players.back().m_empire_color = XMLToClr(doc.root_node.Child("empire_color").Child("GG::Clr")); - m_state = SERVER_GAME_SETUP; - if (m_network_core.EstablishPlayer(connection.socket, player_id, host_player_info)) { - m_network_core.SendMessage(HostAckMessage(player_id)); - m_network_core.SendMessage(JoinAckMessage(player_id)); - } - m_log_category.debugStream() << "ServerApp::HandleNonPlayerMessage : Server now in mode " << SERVER_GAME_SETUP << " (SERVER_GAME_SETUP)."; - m_log_category.debugStream() << "ServerApp::HandleNonPlayerMessage : Universe size set to " << m_galaxy_size << " systems (SERVER_GAME_SETUP)."; - m_log_category.debugStream() << "ServerApp::HandleNonPlayerMessage : Universe shape set to " << m_galaxy_shape << " (SERVER_GAME_SETUP)."; + m_lobby_data.m_players.back().m_player_id = player_id; + m_lobby_data.m_players.back().m_player_name = host_player_name; + m_lobby_data.m_players.back().m_empire_color = EmpireColors().at(0); } + m_network_core.SendMessage(ServerLobbyUpdateMessage(player_id, LobbyUpdate())); } else { const char* socket_hostname = SDLNet_ResolveIP(const_cast<IPaddress*>(&connection.address)); m_log_category.errorStream() << "ServerApp::HandleNonPlayerMessage : A human player attempted to host " @@ -636,10 +639,9 @@ (socket_hostname ? socket_hostname : "[unknown host]") << " on socket " << connection.socket; m_network_core.DumpConnection(connection.socket); } - break; } - + case Message::JOIN_GAME: { std::string player_name = msg.GetText(); |