Began implementation of networking
[progcomp2012.git] / judge / manager / main.cpp
index 0fbd6ae..83b3c63 100644 (file)
@@ -8,6 +8,7 @@
 
 #include "game.h"
 
+
 using namespace std;
 
 Piece::Colour SetupGame(int argc, char ** argv);
@@ -61,8 +62,9 @@ int main(int argc, char ** argv)
 
 Piece::Colour SetupGame(int argc, char ** argv)
 {
-       char * red = NULL; char * blue = NULL; double timeout = 0.00001; bool graphics = false; bool allowIllegal = false; FILE * log = NULL;
-       Piece::Colour reveal = Piece::BOTH; char * inputFile = NULL; int maxTurns = 5000; bool printBoard = false;
+       char * red = NULL; char * blue = NULL; double stallTime = 0.0; bool graphics = false; bool allowIllegal = false; FILE * log = NULL;
+       Piece::Colour reveal = Piece::BOTH; char * inputFile = NULL; int maxTurns = 5000; bool printBoard = false; double timeoutTime = 2.0;
+       bool server = false; bool client = false;
        for (int ii=1; ii < argc; ++ii)
        {
                if (argv[ii][0] == '-')
@@ -72,17 +74,37 @@ Piece::Colour SetupGame(int argc, char ** argv)
                                case 't':
                                        if (argc - ii <= 1)
                                        {
-                                               fprintf(stderr, "ARGUMENT_ERROR - Expected timeout value after -t switch!\n");
+                                               fprintf(stderr, "ARGUMENT_ERROR - Expected stall time value after -t switch!\n");
+                                               exit(EXIT_FAILURE);
+                                       }
+                                       if (strcmp(argv[ii+1], "inf") == 0)
+                                               stallTime = -1;
+                                       else
+                                               stallTime = atof(argv[ii+1]);
+                                       ++ii;
+                                       break;
+
+                               case 'T':
+                                       if (argc - ii <= 1)
+                                       {
+                                               fprintf(stderr, "ARGUMENT_ERROR - Expected timeout value after -T switch!\n");
                                                exit(EXIT_FAILURE);
                                        }
                                        if (strcmp(argv[ii+1], "inf") == 0)
-                                               timeout = -1;
+                                               timeoutTime = -1;
                                        else
-                                               timeout = atof(argv[ii+1]);
+                                               timeoutTime = atof(argv[ii+1]);
                                        ++ii;
                                        break;
+
                                case 'g':
+                                       #ifdef BUILD_GRAPHICS
                                        graphics = !graphics;
+                                       #else
+                                       fprintf(stderr, "ERROR: -g switch supplied, but the program was not built with graphics.\n Please do not use the -g switch.");
+                                       exit(EXIT_FAILURE);
+                                       #endif //BUILD_GRAPHICS
+
                                        break;
                                case 'p':
                                        printBoard = !printBoard;
@@ -163,6 +185,26 @@ Piece::Colour SetupGame(int argc, char ** argv)
                                                system("less manual.txt");
                                                exit(EXIT_SUCCESS);
                                        }
+                                       else if (strcmp(argv[ii]+2, "server") == 0)
+                                       {
+                                               if (client == true)
+                                               {
+                                                       fprintf(stderr, "ARGUMENT_ERROR - Can't be both a server and a client!\n");
+                                                       exit(EXIT_FAILURE);
+                                               }
+                                               server = true;
+                                               
+                                       }
+                                       else if (strcmp(argv[ii]+2, "client") == 0)
+                                       {
+                                               if (server == true)
+                                               {
+                                                       fprintf(stderr, "ARGUMENT_ERROR - Can't be both a server and a client!\n");
+                                                       exit(EXIT_FAILURE);
+                                               }
+                                               client = true;  
+                                               
+                                       }
                                        else
                                        {
                                                fprintf(stderr, "ARGUMENT_ERROR - Unrecognised switch \"%s\"...\n", argv[ii]);
@@ -185,20 +227,40 @@ Piece::Colour SetupGame(int argc, char ** argv)
                }
        }
 
-
+       if (graphics && stallTime == 0.0)
+               stallTime = 0.00001; //Hack so that SDL events (ie SDL_QUIT) will have time to be captured when graphics are enabled
 
        if (inputFile == NULL)
        {
-               if (red == NULL || blue == NULL) //Not enough arguments
+               if (server)  
+               {
+                       if (red != NULL && blue != NULL)
+                       {
+                               fprintf(stderr, "ARGUMENT_ERROR - When using the --server switch, only supply ONE (1) player.\n");
+                               exit(EXIT_FAILURE);
+                       }
+               }
+               else if (red == NULL || blue == NULL) //Not enough players
                {
                        fprintf(stderr, "ARGUMENT_ERROR - Did not recieve enough players (did you mean to use the -f switch?)\n");      
                        exit(EXIT_FAILURE);     
                }
-               Game::theGame = new Game(red,blue, graphics, timeout, allowIllegal,log, reveal,maxTurns, printBoard);
+               
+               if (client)
+               {
+                       blue = red; red = NULL;
+               }
+
+               Game::theGame = new Game(red,blue, graphics, stallTime, allowIllegal,log, reveal,maxTurns, printBoard, timeoutTime, server, client);
        }
        else
        {
-               Game::theGame = new Game(inputFile, graphics, timeout, allowIllegal,log, reveal,maxTurns, printBoard);
+               if (server || client)
+               {
+                       fprintf(stderr, "ARGUMENT_ERROR - The -f switch is incompatable with the --server or --client switches!\n");
+                       exit(EXIT_FAILURE);
+               }
+               Game::theGame = new Game(inputFile, graphics, stallTime, allowIllegal,log, reveal,maxTurns, printBoard, timeoutTime);
        }
 
        if (Game::theGame == NULL)
@@ -240,7 +302,8 @@ void PrintResults(const MovementResult & result, string & buffer)
        {
                switch (result.type)
                {
-                       case MovementResult::VICTORY:
+                       case MovementResult::VICTORY_FLAG:
+                       case MovementResult::VICTORY_ATTRITION: //It does not matter how you win, it just matters that you won!
                                s <<  "VICTORY ";
                                break;
                        case MovementResult::SURRENDER:

UCC git Repository :: git.ucc.asn.au