1.1 --- a/src/main.c Sun Feb 23 21:03:35 2014 +0100 1.2 +++ b/src/main.c Thu Mar 06 15:03:06 2014 +0100 1.3 @@ -30,6 +30,8 @@ 1.4 #include "terminal-chess.h" 1.5 #include <string.h> 1.6 #include <time.h> 1.7 +#include <ncurses.h> 1.8 +#include "input.h" 1.9 1.10 int get_settings(int argc, char **argv, Settings *settings) { 1.11 char *valid; 1.12 @@ -104,25 +106,28 @@ 1.13 1.14 void dump_gameinfo(Gameinfo *gameinfo) { 1.15 int serverwhite = gameinfo->servercolor == WHITE; 1.16 - printf( 1.17 - "Game details:\n" 1.18 - " Server plays %s - Client plays %s\n", 1.19 + attron(A_UNDERLINE); 1.20 + printw("Game details\n"); 1.21 + attroff(A_UNDERLINE); 1.22 + printw(" Server: %s\n Client: %s\n", 1.23 serverwhite?"white":"black", serverwhite?"black":"White" 1.24 ); 1.25 if (gameinfo->time > 0) { 1.26 if (gameinfo->time % 60) { 1.27 - printf(" Time limit: %ds + %ds\n", 1.28 + printw(" Time limit: %ds + %ds\n", 1.29 gameinfo->time, gameinfo->addtime); 1.30 } else { 1.31 - printf(" Time limit: %dm + %ds\n", 1.32 + printw(" Time limit: %dm + %ds\n", 1.33 gameinfo->time/60, gameinfo->addtime); 1.34 } 1.35 } else { 1.36 - printf(" No time limit\n"); 1.37 + printw(" No time limit\n"); 1.38 } 1.39 + refresh(); 1.40 } 1.41 1.42 int cleanup(Settings *settings, int exitcode) { 1.43 + 1.44 if (settings->server) { 1.45 if (net_destroy(settings->server)) { 1.46 perror("Server shutdown failed"); 1.47 @@ -162,12 +167,17 @@ 1.48 return EXIT_SUCCESS; 1.49 } 1.50 1.51 + initscr(); 1.52 + cbreak(); 1.53 + atexit((void(*)(void)) endwin); 1.54 + 1.55 Server server; 1.56 settings.server = &server; 1.57 1.58 if (is_server(&settings)) { 1.59 dump_gameinfo(&(settings.gameinfo)); 1.60 - printf("\nListening for client...\n"); 1.61 + printw("\nListening for client...\n"); 1.62 + refresh(); 1.63 if (net_create(&server, settings.port)) { 1.64 perror("Server creation failed"); 1.65 return cleanup(&settings, EXIT_FAILURE); 1.66 @@ -178,12 +188,36 @@ 1.67 return cleanup(&settings, EXIT_FAILURE); 1.68 } 1.69 1.70 - printf("Client connected - transmitting gameinfo...\n"); 1.71 - net_send(server.client->fd, NETCODE_GAMEINFO, 1.72 - &(settings.gameinfo), sizeof(settings.gameinfo)); 1.73 + /* net version handshake */ 1.74 + int fd = server.client->fd; 1.75 + net_send_code(fd, NETCODE_VERSION); 1.76 + if (net_recieve_code(fd) != NETCODE_VERSION) { 1.77 + fprintf(stderr, "Client uses an incompatible software version.\n"); 1.78 + return cleanup(&settings, EXIT_FAILURE); 1.79 + } 1.80 + 1.81 + printw("Client connected - transmitting gameinfo..."); 1.82 + refresh(); 1.83 + 1.84 + 1.85 + net_send_code(fd, NETCODE_GAMEINFO); 1.86 + net_send_data(fd, &(settings.gameinfo), sizeof(settings.gameinfo)); 1.87 + printw("\rClient connected - awaiting challenge acceptance..."); 1.88 + refresh(); 1.89 + int code = net_recieve_code(fd); 1.90 + if (code == NETCODE_ACCEPT) { 1.91 + printw("\rClient connected - challenge accepted."); 1.92 + clrtoeol(); 1.93 + } else if (code == NETCODE_DECLINE) { 1.94 + printw("\rClient connected - challenge declined."); 1.95 + clrtoeol(); 1.96 + } else { 1.97 + fprintf(stderr, "Invalid client response\n"); 1.98 + return cleanup(&settings, EXIT_FAILURE); 1.99 + } 1.100 } else { 1.101 if (net_find(&server, settings.serverhost, settings.port)) { 1.102 - perror("Can't find server"); 1.103 + fprintf(stderr, "Can't find server\n"); 1.104 return cleanup(&settings, EXIT_FAILURE); 1.105 } 1.106 1.107 @@ -191,17 +225,35 @@ 1.108 perror("Can't connect to server"); 1.109 return cleanup(&settings, EXIT_FAILURE); 1.110 } 1.111 + 1.112 + int fd = server.fd; 1.113 + if (net_recieve_code(fd) != NETCODE_VERSION) { 1.114 + fprintf(stderr, "Server uses an incompatible software version.\n"); 1.115 + return cleanup(&settings, EXIT_FAILURE); 1.116 + } else { 1.117 + net_send_code(fd, NETCODE_VERSION); 1.118 + } 1.119 1.120 - printf("Connection established!\n\n"); 1.121 - if (net_recieve_code(server.fd) == NETCODE_GAMEINFO) { 1.122 - net_recieve_data(server.fd, &(settings.gameinfo), 1.123 + printw("Connection established!\n\n"); 1.124 + refresh(); 1.125 + 1.126 + if (net_recieve_code(fd) == NETCODE_GAMEINFO) { 1.127 + net_recieve_data(fd, &(settings.gameinfo), 1.128 sizeof(settings.gameinfo)); 1.129 dump_gameinfo(&(settings.gameinfo)); 1.130 + printw("Accept challenge (y/n)? "); 1.131 + if (prompt_yesno()) { 1.132 + net_send_code(fd, NETCODE_ACCEPT); 1.133 + // TODO: start game 1.134 + } else { 1.135 + net_send_code(fd, NETCODE_DECLINE); 1.136 + } 1.137 } else { 1.138 fprintf(stderr, "Server sent invalid gameinfo.\n"); 1.139 } 1.140 } 1.141 1.142 + getch(); /* TODO: remove */ 1.143 return cleanup(&settings, EXIT_SUCCESS); 1.144 } 1.145