Refactor running game server into own function

This commit is contained in:
mdiluzio 2015-01-03 22:43:39 +00:00
parent 4055d85d99
commit 43d688a728

View file

@ -9,11 +9,12 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include "net.h"
#include "filesystem.h"
int runServer(int argc, char* argv[])
CTTRTSGame &RunServerForGame(CTTRTSGame &game)
{
std::cout<<"Setting up server on port "<<TTRTS_PORT<< std::endl;
@ -64,9 +65,6 @@ int runServer(int argc, char* argv[])
// max is usually set to 5
listen(sockfd,5);
// Set up game
CTTRTSGame game = GetGameFromFile("Tiny2Player.txt");
std::vector<player_t> players = game.GetPlayers();
unsigned int numClients = players.size();
auto player_iterator = players.begin();
@ -160,20 +158,27 @@ int runServer(int argc, char* argv[])
// Send final state to all the clients
SendGameInfoToClients(myClients, game, gameMutex);
return game;
}
int runServer(int argc, char* argv[])
{
// Set up game
CTTRTSGame game = GetGameFromFile(argv[1]);
if(game.GetNumUnits() == 0)
fatal_error("game not valid");
RunServerForGame(game);
// Get the winning player
player_t winningPlayer = game.GetWinningPlayer();
// Print the winner!
if ( winningPlayer != player_t::NUM_INVALID )
{
std::cout<<"Game over! Winner:"<<(int) winningPlayer <<std::endl;
}
else
{
std::cout<<"Game over! It was a draw!"<<std::endl;
}
// Return
return 0;
return (int)winningPlayer;
}