diff --git a/source/client/CMakeLists.txt b/source/client/CMakeLists.txt index 8fb0953..e524304 100644 --- a/source/client/CMakeLists.txt +++ b/source/client/CMakeLists.txt @@ -13,6 +13,7 @@ set( SOURCES client.cpp server.cpp net.cpp + filesystem.cpp ) # Set defaults for ttrts variables diff --git a/source/client/filesystem.cpp b/source/client/filesystem.cpp new file mode 100644 index 0000000..59ac6b8 --- /dev/null +++ b/source/client/filesystem.cpp @@ -0,0 +1,222 @@ +#include "filesystem.h" + + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +// time for waiting between file stats +static const std::chrono::milliseconds sk_waitTime = std::chrono::milliseconds(100); + +// Check if a file exists +inline bool FileExists( const std::string& name ) +{ + struct stat buffer; + return (stat (name.c_str(), &buffer) == 0); +} + +// Wait for a file to exist +inline void WaitForFile( const std::string& name, const std::chrono::milliseconds& time ) +{ + while( !FileExists(name) ) std::this_thread::sleep_for(time); +} + +bool OutputGameStateFile(CTTRTSGame &game, const std::string &gameDir) +{ + char turnFileName[128]; + snprintf(turnFileName,128,"%s/Turn_%i.txt",gameDir.c_str(),game.GetTurn()); + std::ofstream turnFile(turnFileName, std::ios_base::trunc); // truncate to overwrite if a file exists + + if ( turnFile.bad() ) + { + return false; + } + + // Output the turn description + std::string turnDescriptor = GetStringFromGame(game); + + turnFile<(file)),std::istreambuf_iterator()); + + if( gameDescriptor.size() == 0 ) + { + std::cerr<<"Error: failed to read in any information from "<>input; + if( !input.size() || std::tolower(input[0]) != 'y' ) + { + std::cerr<<"Aborting..."<(turnFile)),std::istreambuf_iterator()); + + // Issue the orders to the game + if( game.IssueOrders(player, orders) ) + std::cerr<<"Warning: Orders for player "<<(int) player <<" failed to correctly parse"< + +int runFromFilesystem( const std::string& directory, const std::string gamefile ); + +#endif \ No newline at end of file diff --git a/source/client/main.cpp b/source/client/main.cpp index d5df140..8c31966 100644 --- a/source/client/main.cpp +++ b/source/client/main.cpp @@ -1,5 +1,5 @@ #include "game.h" -#include "server.h" +#include "filesystem.h" #include #include @@ -60,6 +60,6 @@ int main(int argc, char* argv[]) return -1; } - return runServer(ttrts_games_dir, gameFile); + return runFromFilesystem(ttrts_games_dir, gameFile); }; diff --git a/source/client/server.cpp b/source/client/server.cpp index 678d4f1..e9ea187 100644 --- a/source/client/server.cpp +++ b/source/client/server.cpp @@ -1,221 +1 @@ -#include "server.h" - -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -// time for waiting between file stats -static const std::chrono::milliseconds sk_waitTime = std::chrono::milliseconds(100); - -// Check if a file exists -inline bool FileExists( const std::string& name ) -{ - struct stat buffer; - return (stat (name.c_str(), &buffer) == 0); -} - -// Wait for a file to exist -inline void WaitForFile( const std::string& name, const std::chrono::milliseconds& time ) -{ - while( !FileExists(name) ) std::this_thread::sleep_for(time); -} - -bool OutputGameStateFile(CTTRTSGame &game, const std::string &gameDir) -{ - char turnFileName[128]; - snprintf(turnFileName,128,"%s/Turn_%i.txt",gameDir.c_str(),game.GetTurn()); - std::ofstream turnFile(turnFileName, std::ios_base::trunc); // truncate to overwrite if a file exists - - if ( turnFile.bad() ) - { - return false; - } - - // Output the turn description - std::string turnDescriptor = GetStringFromGame(game); - - turnFile<(file)),std::istreambuf_iterator()); - - if( gameDescriptor.size() == 0 ) - { - std::cerr<<"Error: failed to read in any information from "<>input; - if( !input.size() || std::tolower(input[0]) != 'y' ) - { - std::cerr<<"Aborting..."<(turnFile)),std::istreambuf_iterator()); - - // Issue the orders to the game - if( game.IssueOrders(player, orders) ) - std::cerr<<"Warning: Orders for player "<<(int) player <<" failed to correctly parse"< - -int runServer(const std::string& dir,const std::string& file); - #endif \ No newline at end of file