From 34d1bc168b8c73b9b85a1d71a10eafaa06a65941 Mon Sep 17 00:00:00 2001 From: Cybiosphere - Ecosystem simulation - Ecology Date: Sun, 3 Mar 2024 18:49:43 +0100 Subject: [PATCH] Start server with scenario Allows to define a scenario in ini file --- WpClanNetwork/inc/clan_server.h | 14 +---- WpClanNetwork/src/clan_client_main.cpp | 44 ++++++-------- WpClanNetwork/src/clan_server.cpp | 17 +++++- WpClanNetwork/src/clan_server_main.cpp | 51 ++++------------ WpCommon/inc/Helpers.h | 84 ++++++++++++++++++++++++++ WpEnvironment/inc/CBiotop.h | 1 + WpEnvironment/src/CBiotop.cpp | 10 ++- 7 files changed, 144 insertions(+), 77 deletions(-) diff --git a/WpClanNetwork/inc/clan_server.h b/WpClanNetwork/inc/clan_server.h index 8d02790..845a81e 100644 --- a/WpClanNetwork/inc/clan_server.h +++ b/WpClanNetwork/inc/clan_server.h @@ -1,18 +1,9 @@ #pragma once -//FRED #include -//FRED #include - -//#include "Network/precomp.h" -//#include "API/Network/NetGame/event.h" -//#include "API/Core/Text/string_format.h" -//#include "API/Network/NetGame/event_dispatcher.h" -//#include "API/Network/NetGame/server.h" -//#include "Network/NetGame/connection_impl.h" - #include "API/core.h" #include "API/network.h" #include "CBiotop.h" +#include "CScenarioPlayer.h" #include "event_manager.h" using namespace clan; @@ -23,7 +14,7 @@ class ServerCoprocessor; class Server { public: - Server(std::string portStr, CBiotop* pBiotop); + Server(std::string portStr, CBiotop* pBiotop, CScenarioPlayer* pScenarioPlayer); ~Server(); // start only @@ -96,6 +87,7 @@ class Server std::string serverPortStr; CBiotop* m_pBiotop; + CScenarioPlayer* m_pScenarioPlayer; int next_user_id; int nb_users_connected; float m_biotopSpeed; // set 1.0 for real time speed. Biotp update every 1sec diff --git a/WpClanNetwork/src/clan_client_main.cpp b/WpClanNetwork/src/clan_client_main.cpp index 8fcd7d6..4be9f31 100644 --- a/WpClanNetwork/src/clan_client_main.cpp +++ b/WpClanNetwork/src/clan_client_main.cpp @@ -3,6 +3,7 @@ using namespace clan; #include "clan_client.h" +#include "Helpers.h" //FRED #ifdef WIN32 //FRED int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) @@ -10,42 +11,35 @@ using namespace clan; int main(int argc, char* argv[]) //FRED #endif { - try - { - ConsoleWindow console("Client Console", 160, 1000); - ConsoleLogger logger; - - // Get Server info in ini file - string ServerAddrStr; - string ServerPortStr; - string LogInStr; - char resuBuffer[512]; + try + { + ConsoleWindow console("Client Console", 160, 1000); + ConsoleLogger logger; string fileIni = "Cybiosphere.ini"; + if (argc == 2) { fileIni = argv[1]; log_event("Start ", "Use specific ini file " + fileIni); } - int resu = getStringSectionFromFile("CYBIOSPHERE", "ServerAddr", "localhost", resuBuffer, 512, fileIni); - ServerAddrStr = resuBuffer; - resu = getStringSectionFromFile("CYBIOSPHERE", "ServerPort", "4556", resuBuffer, 512, fileIni); - ServerPortStr = resuBuffer; - resu = getStringSectionFromFile("CYBIOSPHERE", "Login", "Player", resuBuffer, 512, fileIni); - LogInStr = resuBuffer; - Client client(ServerAddrStr, ServerPortStr, LogInStr); - client.exec(); + string serverAddrStr = getIpServerAddrFromIniFile(fileIni); + string serverPortStr = getIpPortStrFromIniFile(fileIni); + string logInStr = getIpServerLoginFromIniFile(fileIni); + + Client client(serverAddrStr, serverPortStr, logInStr); + client.exec(); - return 0; - } - catch (Exception e) - { + return 0; + } + catch (Exception e) + { #ifdef WIN32 //FRED MessageBox(0, e.get_message_and_stack_trace().c_str(), TEXT("Unhandled Exception"), MB_OK); #else - Console::write_line("Unhandled exception: %1", e.get_message_and_stack_trace()); + Console::write_line("Unhandled exception: %1", e.get_message_and_stack_trace()); #endif - return 1; - } + return 1; + } } diff --git a/WpClanNetwork/src/clan_server.cpp b/WpClanNetwork/src/clan_server.cpp index b15384c..ee8592d 100644 --- a/WpClanNetwork/src/clan_server.cpp +++ b/WpClanNetwork/src/clan_server.cpp @@ -20,9 +20,10 @@ CommandHandler_t ServerCmdNameList[SERVER_CMD_NUMBER] = }; -Server::Server(std::string portStr, CBiotop* pBiotop) +Server::Server(std::string portStr, CBiotop* pBiotop, CScenarioPlayer* pScenarioPlayer) : serverPortStr{ portStr }, m_pBiotop{ pBiotop }, +m_pScenarioPlayer{ pScenarioPlayer }, next_user_id(1), nb_users_connected(0), m_biotopSpeed(1.0), @@ -106,6 +107,7 @@ void Server::exec() { std::chrono::time_point curTick; std::chrono::time_point lastRunTick = std::chrono::system_clock::now(); + int currentScenarioScore = 0; network_server.start(serverPortStr); @@ -129,7 +131,18 @@ void Server::exec() { send_event_new_second_start(); // Next second in biotop - m_pBiotop->nextSecond(); + if ((m_pScenarioPlayer != nullptr) && m_pScenarioPlayer->NextCmdNextSecond()) + { + if (m_pScenarioPlayer->m_totalScore > currentScenarioScore) + { + currentScenarioScore = m_pScenarioPlayer->m_totalScore; + log_event(labelServer, "Scenario score success: %1 / %2", m_pScenarioPlayer->m_successScore, m_pScenarioPlayer->m_totalScore); + } + } + else + { + m_pBiotop->nextSecond(); + } send_event_new_second_end(); } } diff --git a/WpClanNetwork/src/clan_server_main.cpp b/WpClanNetwork/src/clan_server_main.cpp index 9283850..6f24e98 100644 --- a/WpClanNetwork/src/clan_server_main.cpp +++ b/WpClanNetwork/src/clan_server_main.cpp @@ -1,6 +1,8 @@ #include "clan_server.h" #include "event_definitions.h" #include "CBiotop.h" +#include "CScenarioPlayer.h" +#include "Helpers.h" //#ifdef WIN32 //int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) @@ -12,55 +14,28 @@ int main(int argc, char* argv[]) { ConsoleWindow console("Server Console", 160, 1000); ConsoleLogger logger; - - CBiotop* pBiotop; - char resuBuffer[512]; - string resuStr; + CBiotop* pBiotop{nullptr}; + CScenarioPlayer* pScenarioPlayer{nullptr}; string fileIni = "Cybiosphere.ini"; + if (argc == 2) { fileIni = argv[1]; log_event(labelServer, "Open ini file " + fileIni); } - string serverPortStr; - if (getStringSectionFromFile("CYBIOSPHERE", "ServerPort", "4556", resuBuffer, 512, fileIni) > 0) - { - serverPortStr = resuBuffer; - } - else - { - serverPortStr = "4556"; - } + string serverPortStr = getIpPortStrFromIniFile(fileIni); + createBiotopAndScenarioFromIniFile(fileIni, &pBiotop, &pScenarioPlayer); - int resu = getStringSectionFromFile("CYBIOSPHERE", "Biotop", "", resuBuffer, 512, fileIni); - resuStr = resuBuffer; - - if (resuStr != "") - { - string resuDataPath; - pBiotop = new CBiotop(0,0,0); - bool resu = getStringSectionFromFile("CYBIOSPHERE", "DataPath", "", resuBuffer, 512, fileIni); - resuDataPath = resuBuffer; - if (resuDataPath != "") - pBiotop->loadFromXmlFile(resuStr, resuDataPath); - else - pBiotop->loadFromXmlFile(resuStr, "..\\dataXml\\"); - log_event(labelServer, "Biotop loaded"); - } - else - { - pBiotop = new CBiotop(80,40,3); - pBiotop->initGridDefaultLayerType(); - pBiotop->initGridDefaultAltitude(); - pBiotop->initGridEntity(); - pBiotop->setDefaultEntitiesForTest(); - log_event(labelServer, "Default empty biotop created"); - } + if (pBiotop != nullptr) + log_event(labelServer, "Biotop loaded: %1", pBiotop->getLabel()); + if (pScenarioPlayer != nullptr) + log_event(labelServer, "Scenario loaded"); - Server server(serverPortStr, pBiotop); + Server server(serverPortStr, pBiotop, pScenarioPlayer); server.exec(); delete pBiotop; + delete pScenarioPlayer; return 0; } catch (Exception e) diff --git a/WpCommon/inc/Helpers.h b/WpCommon/inc/Helpers.h index 99797b4..3059ce7 100644 --- a/WpCommon/inc/Helpers.h +++ b/WpCommon/inc/Helpers.h @@ -39,6 +39,90 @@ distribution. // Functions //=========================================================================== +inline string getIpServerAddrFromIniFile(const string& fileIni) +{ + string ServerAddrStr; + char resuBuffer[512]; + getStringSectionFromFile("CYBIOSPHERE", "ServerAddr", "localhost", resuBuffer, 512, fileIni); + ServerAddrStr = resuBuffer; + return ServerAddrStr; +} + +inline string getIpServerLoginFromIniFile(const string& fileIni) +{ + string LogInStr; + char resuBuffer[512]; + getStringSectionFromFile("CYBIOSPHERE", "Login", "Player", resuBuffer, 512, fileIni); + LogInStr = resuBuffer; + return LogInStr; +} + +inline string getIpPortStrFromIniFile(const string& fileIni) +{ + string serverPortStr; + char resuBuffer[512]; + if (getStringSectionFromFile("CYBIOSPHERE", "ServerPort", "4556", resuBuffer, 512, fileIni) > 0) + { + serverPortStr = resuBuffer; + } + else + { + serverPortStr = "4556"; + } + return serverPortStr; +} + +inline void createBiotopAndScenarioFromIniFile(const string& fileIni, CBiotop** pNewBiotop, CScenarioPlayer** pNewScenarioPlayer) +{ + CBiotop* pBiotop{ nullptr }; + CScenarioPlayer* pScenarioPlayer{ nullptr }; + char resuBuffer[512]; + string resuStr; + + int resu = getStringSectionFromFile("CYBIOSPHERE", "Biotop", "", resuBuffer, 512, fileIni); + resuStr = resuBuffer; + if (resuStr != "") + { + string resuDataPath; + pBiotop = new CBiotop(0, 0, 0); + bool resu = getStringSectionFromFile("CYBIOSPHERE", "DataPath", "", resuBuffer, 512, fileIni); + resuDataPath = resuBuffer; + if (resuDataPath != "") + pBiotop->loadFromXmlFile(resuStr, resuDataPath); + else + pBiotop->loadFromXmlFile(resuStr, "..\\dataXml\\"); + } + else + { + pBiotop = new CBiotop(50, 50, 3); + pBiotop->initGridDefaultLayerType(); + pBiotop->initGridDefaultAltitude(); + pBiotop->initGridEntity(); + pBiotop->setDefaultEntitiesForTest(); + } + + resu = getStringSectionFromFile("CYBIOSPHERE", "Scenario", "", resuBuffer, 512, fileIni); + resuStr = resuBuffer; + if (resuStr != "") + { + string resuDataPath; + pScenarioPlayer = new CScenarioPlayer(pBiotop); + bool resu = getStringSectionFromFile("CYBIOSPHERE", "DataPath", "", resuBuffer, 512, fileIni); + resuDataPath = resuBuffer; + if (resuDataPath != "") + pScenarioPlayer->ReadScenarioFile(resuStr, resuDataPath); + else + pScenarioPlayer->ReadScenarioFile(resuStr, "..\\dataXml\\"); + + // Start reading scenario twice to update biotop + pScenarioPlayer->NextCmdNextSecond(); + pScenarioPlayer->NextCmdNextSecond(); + } + + *pNewBiotop = pBiotop; + *pNewScenarioPlayer = pScenarioPlayer; +} + inline size_t computeMaxSpeedStepfactor(CBiotop* pBiotop, bool isMaxSpeed) { if (pBiotop == NULL) diff --git a/WpEnvironment/inc/CBiotop.h b/WpEnvironment/inc/CBiotop.h index 67ad177..303848d 100644 --- a/WpEnvironment/inc/CBiotop.h +++ b/WpEnvironment/inc/CBiotop.h @@ -424,6 +424,7 @@ class DLL_CYBIOCORE_API CBiotop private: std::map& getBiotopEventMapCurrent(); std::map& getBiotopEventMapPrevious(); + void clearEventMaps(); //--------------------------------------------------------------------------- // Save/Load in file diff --git a/WpEnvironment/src/CBiotop.cpp b/WpEnvironment/src/CBiotop.cpp index 956c0bb..c54cb6f 100644 --- a/WpEnvironment/src/CBiotop.cpp +++ b/WpEnvironment/src/CBiotop.cpp @@ -2285,6 +2285,12 @@ std::map& CBiotop::getBiotopEventMapPrevious() } } +void CBiotop::clearEventMaps() +{ + m_tEventsOdd.clear(); + m_tEventsEven.clear(); +} + //=========================================================================== // Save/Load in file //=========================================================================== @@ -2431,7 +2437,6 @@ bool CBiotop::saveInXmlFile(TiXmlDocument *pXmlDoc, string pathNameForEntities, bool CBiotop::loadFromXmlFile(string fileName, string pathName) { - TiXmlDocument xmlDoc(pathName + fileName); if (xmlDoc.LoadFile() == false) { @@ -2445,6 +2450,8 @@ bool CBiotop::loadFromXmlFile(string fileName, string pathName) loadFromXmlFile(&xmlDoc, pathName); CYBIOCORE_LOG_TIME(m_BioTime); CYBIOCORE_LOG("BIOTOP - File load complete\n"); + + m_Label = fileName; return true; } @@ -2475,6 +2482,7 @@ bool CBiotop::loadFromXmlFile(TiXmlDocument *pXmlDoc, string pathNameForEntities deleteAllEntities(); deleteAllMeasures(); deleteGrid(); + clearEventMaps(); m_Dimension.x = sizeX; m_Dimension.y = sizeY;