More refactoring of functionality into seperate files, with stubs for server and client

This commit is contained in:
mdiluzio 2015-01-02 15:03:29 +00:00
parent 8835bfb82a
commit 2281bcb6cd
7 changed files with 101 additions and 45 deletions

View file

@ -1 +1,7 @@
#include "client.h"
int runClient( const std::string& host_address )
{
return 0;
}

View file

@ -1,4 +1,8 @@
#ifndef _TTRTS_CLIENT_H_
#define _TTRTS_CLIENT_H_
#include <string>
int runClient( const std::string& host_address );
#endif

View file

@ -13,18 +13,19 @@
#include <unistd.h>
#include <stdlib.h>
// =====================================================================================================================
// 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 )
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 )
void WaitForFile( const std::string& name, const std::chrono::milliseconds& time )
{
while( !FileExists(name) ) std::this_thread::sleep_for(time);
}
@ -49,8 +50,59 @@ bool OutputGameStateFile(CTTRTSGame &game, const std::string &gameDir)
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);
@ -79,7 +131,7 @@ int runFromFilesystem( const std::string& directory, const std::string gamefile
auto players = game.GetPlayers();
// Current game directory
std::string gameDir = directory + game.GetName();
std::string gameDir = ttrts_games_dir + game.GetName();
// Empty the current game directory
struct stat info;

View file

@ -2,7 +2,22 @@
#define _TTRTS_FILESYSTEM_H_
#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

View file

@ -1,11 +1,9 @@
#include "game.h"
#include "filesystem.h"
#include "server.h"
#include "client.h"
#include <iostream>
#include <unistd.h>
#define STRINGIFY(x) _STRINGIFY(x)
#define _STRINGIFY(x) #x
static const char* sk_usage =
#include "usage.h"
@ -22,44 +20,18 @@ int main(int argc, char* argv[])
}
// Attempt to open the game file
std::string gameFile = argv[1];
std::string arg1 = argv[1];
// Default for maps
std::string ttrts_maps_dir = STRINGIFY(TTRTS_MAPS);
if( getenv("TTRTS_MAPS") )
if( arg1 == "client" )
{
ttrts_maps_dir = getenv("TTRTS_MAPS");
// Additional trailing slash
if( ttrts_maps_dir.back() != '/' )
ttrts_maps_dir += "/";
if( argc == 3 )
return runClient(argv[2]);
}
// Default for games
std::string ttrts_games_dir = STRINGIFY(TTRTS_GAMES);
if( getenv("TTRTS_GAMES") )
else if ( arg1 == "server" )
{
ttrts_games_dir = getenv("TTRTS_GAMES");
// Additional trailing slash
if( ttrts_games_dir.back() != '/' )
ttrts_games_dir += "/";
return runServer();
}
// 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;
}
return runFromFilesystem(ttrts_games_dir, gameFile);
else
return runFromFilesystem(arg1);
};

View file

@ -1 +1,6 @@
#include "server.h"
int runServer()
{
}

View file

@ -1,4 +1,6 @@
#ifndef _TTRTS_SERVER_H_
#define _TTRTS_SERVER_H_
int runServer();
#endif