More refactoring of functionality into seperate files, with stubs for server and client
This commit is contained in:
		
							parent
							
								
									8835bfb82a
								
							
						
					
					
						commit
						2281bcb6cd
					
				
					 7 changed files with 101 additions and 45 deletions
				
			
		|  | @ -1 +1,7 @@ | ||||||
| #include "client.h" | #include "client.h" | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | int runClient( const std::string& host_address ) | ||||||
|  | { | ||||||
|  |     return 0; | ||||||
|  | } | ||||||
|  | @ -1,4 +1,8 @@ | ||||||
| #ifndef _TTRTS_CLIENT_H_ | #ifndef _TTRTS_CLIENT_H_ | ||||||
| #define _TTRTS_CLIENT_H_ | #define _TTRTS_CLIENT_H_ | ||||||
| 
 | 
 | ||||||
|  | #include <string> | ||||||
|  | 
 | ||||||
|  | int runClient( const std::string& host_address ); | ||||||
|  | 
 | ||||||
| #endif | #endif | ||||||
|  | @ -13,18 +13,19 @@ | ||||||
| #include <unistd.h> | #include <unistd.h> | ||||||
| #include <stdlib.h> | #include <stdlib.h> | ||||||
| 
 | 
 | ||||||
|  | // =====================================================================================================================
 | ||||||
| // time for waiting between file stats
 | // time for waiting between file stats
 | ||||||
| static const std::chrono::milliseconds sk_waitTime = std::chrono::milliseconds(100); | static const std::chrono::milliseconds sk_waitTime = std::chrono::milliseconds(100); | ||||||
| 
 | 
 | ||||||
| // Check if a file exists
 | // Check if a file exists
 | ||||||
| inline bool FileExists( const std::string& name ) | bool FileExists( const std::string& name ) | ||||||
| { | { | ||||||
|     struct stat buffer; |     struct stat buffer; | ||||||
|     return (stat (name.c_str(), &buffer) == 0); |     return (stat (name.c_str(), &buffer) == 0); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Wait for a file to exist
 | // Wait for a file to exist
 | ||||||
| inline void WaitForFile( const std::string& name, const std::chrono::milliseconds& time ) | void WaitForFile( const std::string& name, const std::chrono::milliseconds& time ) | ||||||
| { | { | ||||||
|     while( !FileExists(name) ) std::this_thread::sleep_for(time); |     while( !FileExists(name) ) std::this_thread::sleep_for(time); | ||||||
| } | } | ||||||
|  | @ -49,8 +50,59 @@ bool OutputGameStateFile(CTTRTSGame &game, const std::string &gameDir) | ||||||
|     return true; |     return true; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| int runFromFilesystem( const std::string& directory, const std::string gamefile ) | std::string getMapsDir() | ||||||
| { | { | ||||||
|  |     std::string maps = STRINGIFY(TTRTS_MAPS); | ||||||
|  |     if( getenv("TTRTS_MAPS") ) | ||||||
|  |     { | ||||||
|  |         maps = getenv("TTRTS_MAPS"); | ||||||
|  | 
 | ||||||
|  |         // Additional trailing slash
 | ||||||
|  |         if( maps.back() != '/' ) | ||||||
|  |             maps += "/"; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     return maps; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | std::string getGamesDir() | ||||||
|  | { | ||||||
|  |     std::string dir = STRINGIFY(TTRTS_GAMES); | ||||||
|  |     if( getenv("TTRTS_GAMES") ) | ||||||
|  |     { | ||||||
|  |         dir = getenv("TTRTS_GAMES"); | ||||||
|  | 
 | ||||||
|  |         // Additional trailing slash
 | ||||||
|  |         if( dir.back() != '/' ) | ||||||
|  |             dir += "/"; | ||||||
|  |     } | ||||||
|  |     return dir; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // =====================================================================================================================
 | ||||||
|  | int runFromFilesystem( const std::string& gamestring ) | ||||||
|  | { | ||||||
|  |     std::string gamefile = gamestring; | ||||||
|  | 
 | ||||||
|  |     // Default for maps
 | ||||||
|  |     std::string ttrts_maps_dir = getMapsDir(); | ||||||
|  | 
 | ||||||
|  |     // Default for games
 | ||||||
|  |     std::string ttrts_games_dir = getGamesDir(); | ||||||
|  | 
 | ||||||
|  |     // If file path is not local path and file doesn't exist
 | ||||||
|  |     if( gamefile.find("/") == std::string::npos | ||||||
|  |             && access( gamefile.c_str(), F_OK ) == -1 ) | ||||||
|  |     { | ||||||
|  |         gamefile = ttrts_maps_dir + gamefile; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     // If still not good
 | ||||||
|  |     if( access( gamefile.c_str(), F_OK ) == -1 ) | ||||||
|  |     { | ||||||
|  |         std::cerr<<"Error: "<< gamefile <<" file not found"<<std::endl; | ||||||
|  |         return -1; | ||||||
|  |     } | ||||||
| 
 | 
 | ||||||
|     std::ifstream file(gamefile); |     std::ifstream file(gamefile); | ||||||
| 
 | 
 | ||||||
|  | @ -79,7 +131,7 @@ int runFromFilesystem( const std::string& directory, const std::string gamefile | ||||||
|     auto players = game.GetPlayers(); |     auto players = game.GetPlayers(); | ||||||
| 
 | 
 | ||||||
|     // Current game directory
 |     // Current game directory
 | ||||||
|     std::string gameDir = directory + game.GetName(); |     std::string gameDir = ttrts_games_dir + game.GetName(); | ||||||
| 
 | 
 | ||||||
|     // Empty the current game directory
 |     // Empty the current game directory
 | ||||||
|     struct stat info; |     struct stat info; | ||||||
|  |  | ||||||
|  | @ -2,7 +2,22 @@ | ||||||
| #define _TTRTS_FILESYSTEM_H_ | #define _TTRTS_FILESYSTEM_H_ | ||||||
| 
 | 
 | ||||||
| #include <string> | #include <string> | ||||||
|  | #include <chrono> | ||||||
| 
 | 
 | ||||||
| int runFromFilesystem( const std::string& directory, const std::string gamefile ); | #include "game.h" | ||||||
|  | 
 | ||||||
|  | #define STRINGIFY(x) _STRINGIFY(x) | ||||||
|  | #define _STRINGIFY(x) #x | ||||||
|  | 
 | ||||||
|  | bool FileExists( const std::string& name ); | ||||||
|  | 
 | ||||||
|  | void WaitForFile( const std::string& name, const std::chrono::milliseconds& time ); | ||||||
|  | 
 | ||||||
|  | int runFromFilesystem( const std::string& gamefile ); | ||||||
|  | 
 | ||||||
|  | bool OutputGameStateFile(CTTRTSGame &game, const std::string &gameDir); | ||||||
|  | 
 | ||||||
|  | std::string getMapsDir(); | ||||||
|  | std::string getGamesDir(); | ||||||
| 
 | 
 | ||||||
| #endif | #endif | ||||||
|  | @ -1,11 +1,9 @@ | ||||||
| #include "game.h" | #include "game.h" | ||||||
| #include "filesystem.h" | #include "filesystem.h" | ||||||
|  | #include "server.h" | ||||||
|  | #include "client.h" | ||||||
| 
 | 
 | ||||||
| #include <iostream> | #include <iostream> | ||||||
| #include <unistd.h> |  | ||||||
| 
 |  | ||||||
| #define STRINGIFY(x) _STRINGIFY(x) |  | ||||||
| #define _STRINGIFY(x) #x |  | ||||||
| 
 | 
 | ||||||
| static const char* sk_usage = | static const char* sk_usage = | ||||||
| #include "usage.h" | #include "usage.h" | ||||||
|  | @ -22,44 +20,18 @@ int main(int argc, char* argv[]) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	// Attempt to open the game file
 | 	// Attempt to open the game file
 | ||||||
| 	std::string gameFile = argv[1]; | 	std::string arg1 = argv[1]; | ||||||
| 
 | 
 | ||||||
| 	// Default for maps
 | 	if( arg1 == "client" ) | ||||||
| 	std::string ttrts_maps_dir = STRINGIFY(TTRTS_MAPS); |  | ||||||
| 	if( getenv("TTRTS_MAPS") ) |  | ||||||
| 	{ | 	{ | ||||||
| 		ttrts_maps_dir = getenv("TTRTS_MAPS"); | 		if( argc == 3 ) | ||||||
| 
 | 			return runClient(argv[2]); | ||||||
| 		// Additional trailing slash
 |  | ||||||
| 		if( ttrts_maps_dir.back() != '/' ) |  | ||||||
| 			ttrts_maps_dir += "/"; |  | ||||||
| 	} | 	} | ||||||
| 
 | 	else if ( arg1 == "server" ) | ||||||
| 	// Default for games
 |  | ||||||
| 	std::string ttrts_games_dir = STRINGIFY(TTRTS_GAMES); |  | ||||||
| 	if( getenv("TTRTS_GAMES") ) |  | ||||||
| 	{ | 	{ | ||||||
| 		ttrts_games_dir = getenv("TTRTS_GAMES"); | 		return runServer(); | ||||||
| 
 |  | ||||||
| 		// Additional trailing slash
 |  | ||||||
| 		if( ttrts_games_dir.back() != '/' ) |  | ||||||
| 			ttrts_games_dir += "/"; |  | ||||||
| 	} | 	} | ||||||
| 	 | 	else | ||||||
| 	// If file path is not local path and file doesn't exist
 | 		return runFromFilesystem(arg1); | ||||||
| 	if( gameFile.find("/") == std::string::npos |  | ||||||
| 		&& access( gameFile.c_str(), F_OK ) == -1 ) |  | ||||||
| 	{ |  | ||||||
| 		gameFile = ttrts_maps_dir + gameFile; |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	// If still not good
 |  | ||||||
| 	if( access( gameFile.c_str(), F_OK ) == -1 ) |  | ||||||
| 	{ |  | ||||||
| 		std::cerr<<"Error: "<<gameFile<<" file not found"<<std::endl; |  | ||||||
| 		return -1; |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	return runFromFilesystem(ttrts_games_dir, gameFile); |  | ||||||
| 
 | 
 | ||||||
| }; | }; | ||||||
|  |  | ||||||
|  | @ -1 +1,6 @@ | ||||||
| #include "server.h" | #include "server.h" | ||||||
|  | 
 | ||||||
|  | int runServer() | ||||||
|  | { | ||||||
|  | 
 | ||||||
|  | } | ||||||
|  | @ -1,4 +1,6 @@ | ||||||
| #ifndef _TTRTS_SERVER_H_ | #ifndef _TTRTS_SERVER_H_ | ||||||
| #define _TTRTS_SERVER_H_ | #define _TTRTS_SERVER_H_ | ||||||
| 
 | 
 | ||||||
|  | int runServer(); | ||||||
|  | 
 | ||||||
| #endif | #endif | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue