2015-01-02 16:09:24 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
2015-01-02 15:13:05 +00:00
|
|
|
#include "net.h"
|
2015-01-02 19:31:14 +00:00
|
|
|
#include "game.h"
|
2015-01-10 16:55:30 +00:00
|
|
|
#include "error.h"
|
2015-01-02 19:46:09 +00:00
|
|
|
#include "filesystem.h"
|
2015-01-02 15:03:29 +00:00
|
|
|
|
2015-01-10 16:55:30 +00:00
|
|
|
int main(int argc, char* argv[])
|
2015-01-02 15:03:29 +00:00
|
|
|
{
|
2015-01-02 15:13:05 +00:00
|
|
|
// must provide information
|
|
|
|
if (argc < 2)
|
2015-01-10 16:55:30 +00:00
|
|
|
fatal_error("Usage: ttrts-client HOST");
|
2015-01-02 15:13:05 +00:00
|
|
|
|
2015-01-04 13:09:37 +00:00
|
|
|
std::string hostname = argv[1];
|
2015-01-02 15:13:05 +00:00
|
|
|
|
2015-01-04 13:09:37 +00:00
|
|
|
sockaddr_in serv_addr; // Server address
|
2015-01-02 15:13:05 +00:00
|
|
|
memset(&serv_addr,0, sizeof(serv_addr));
|
|
|
|
|
|
|
|
// Set the server to AF_INET
|
|
|
|
serv_addr.sin_family = AF_INET;
|
|
|
|
// Set our server address port to the port number provided
|
2015-01-04 13:09:37 +00:00
|
|
|
serv_addr.sin_port = htons(TTRTS_PORT);
|
2015-01-02 15:13:05 +00:00
|
|
|
|
2015-01-04 13:09:37 +00:00
|
|
|
std::cout<<"TTRTS: Connecting to "<<hostname<<std::endl;
|
|
|
|
int sockfd = ConnectToHostServer(hostname, serv_addr);
|
2015-01-02 15:13:05 +00:00
|
|
|
|
2015-01-03 20:04:31 +00:00
|
|
|
unsigned int player;
|
2015-01-04 11:08:35 +00:00
|
|
|
std::string gameNameString;
|
|
|
|
|
2015-01-04 13:09:37 +00:00
|
|
|
// Handshake with server to fetch player and gamestring
|
2015-01-04 11:08:35 +00:00
|
|
|
PerformClientHandshake(sockfd, player, gameNameString);
|
2015-01-02 19:31:14 +00:00
|
|
|
|
2015-01-04 13:09:37 +00:00
|
|
|
// output our information
|
|
|
|
player_t myPlayer = (player_t)player;
|
2015-01-04 11:59:58 +00:00
|
|
|
std::cout<<"TTRTS: I am player "<<std::to_string((int)myPlayer)<<std::endl;
|
|
|
|
std::cout<<"TTRTS: Game is "<<gameNameString<<std::endl;
|
2015-01-03 20:04:31 +00:00
|
|
|
|
|
|
|
// Clean out the games dir
|
2015-01-04 11:08:35 +00:00
|
|
|
CreateAndCleanGameDir(gameNameString);
|
2015-01-02 19:31:14 +00:00
|
|
|
|
2015-01-04 13:09:37 +00:00
|
|
|
// Buffer for messages
|
|
|
|
char buffer[1028];
|
|
|
|
memset(buffer,0,sizeof(buffer));
|
|
|
|
|
|
|
|
int n = 0; // return value for read and write calls
|
2015-01-02 15:13:05 +00:00
|
|
|
while ( n >= 0 )
|
|
|
|
{
|
2015-01-04 11:59:58 +00:00
|
|
|
std::cout<<"TTRTS: Waiting for gamestate"<<std::endl;
|
2015-01-04 13:09:37 +00:00
|
|
|
std::string gamestate = WaitForGamestateMessage(sockfd);
|
2015-01-03 18:55:44 +00:00
|
|
|
|
2015-01-03 19:27:08 +00:00
|
|
|
// Output the gamestate file for this game
|
|
|
|
CTTRTSGame thisGame = GetGameFromString(gamestate);
|
2015-01-03 19:31:42 +00:00
|
|
|
OutputGameStateFile(thisGame);
|
2015-01-02 17:14:16 +00:00
|
|
|
|
2015-01-04 13:09:37 +00:00
|
|
|
// If game over, exit with out winning player and message
|
|
|
|
if(thisGame.GameOver())
|
|
|
|
exit( OutputGameEnd(thisGame) );
|
|
|
|
|
|
|
|
// Get the order file for this turn`
|
2015-01-03 19:27:08 +00:00
|
|
|
std::string orders = GetOrdersFromPlayerFile(thisGame,myPlayer);
|
2015-01-02 15:13:05 +00:00
|
|
|
|
2015-01-04 11:59:58 +00:00
|
|
|
std::cout<<"TTRTS: Sending orders"<<std::endl;
|
2015-01-02 18:57:47 +00:00
|
|
|
std::cout<<orders<<std::endl;
|
2015-01-02 15:13:05 +00:00
|
|
|
// Write to the socket with the buffer
|
2015-01-04 13:09:37 +00:00
|
|
|
n = SendOrdersToServer(sockfd, orders);
|
2015-01-02 15:13:05 +00:00
|
|
|
}
|
|
|
|
|
2015-01-02 15:03:29 +00:00
|
|
|
return 0;
|
|
|
|
}
|