src/main.c

changeset 3
3693fd2eb0e9
parent 2
0a08f79c320d
child 4
560e07f7a6a1
equal deleted inserted replaced
2:0a08f79c320d 3:3693fd2eb0e9
28 */ 28 */
29 29
30 #include "terminal-chess.h" 30 #include "terminal-chess.h"
31 #include <string.h> 31 #include <string.h>
32 #include <time.h> 32 #include <time.h>
33 #include <ncurses.h>
34 #include "input.h"
33 35
34 int get_settings(int argc, char **argv, Settings *settings) { 36 int get_settings(int argc, char **argv, Settings *settings) {
35 char *valid; 37 char *valid;
36 unsigned long int time, port; 38 unsigned long int time, port;
37 uint8_t timeunit = 60; 39 uint8_t timeunit = 60;
102 return settings; 104 return settings;
103 } 105 }
104 106
105 void dump_gameinfo(Gameinfo *gameinfo) { 107 void dump_gameinfo(Gameinfo *gameinfo) {
106 int serverwhite = gameinfo->servercolor == WHITE; 108 int serverwhite = gameinfo->servercolor == WHITE;
107 printf( 109 attron(A_UNDERLINE);
108 "Game details:\n" 110 printw("Game details\n");
109 " Server plays %s - Client plays %s\n", 111 attroff(A_UNDERLINE);
112 printw(" Server: %s\n Client: %s\n",
110 serverwhite?"white":"black", serverwhite?"black":"White" 113 serverwhite?"white":"black", serverwhite?"black":"White"
111 ); 114 );
112 if (gameinfo->time > 0) { 115 if (gameinfo->time > 0) {
113 if (gameinfo->time % 60) { 116 if (gameinfo->time % 60) {
114 printf(" Time limit: %ds + %ds\n", 117 printw(" Time limit: %ds + %ds\n",
115 gameinfo->time, gameinfo->addtime); 118 gameinfo->time, gameinfo->addtime);
116 } else { 119 } else {
117 printf(" Time limit: %dm + %ds\n", 120 printw(" Time limit: %dm + %ds\n",
118 gameinfo->time/60, gameinfo->addtime); 121 gameinfo->time/60, gameinfo->addtime);
119 } 122 }
120 } else { 123 } else {
121 printf(" No time limit\n"); 124 printw(" No time limit\n");
122 } 125 }
126 refresh();
123 } 127 }
124 128
125 int cleanup(Settings *settings, int exitcode) { 129 int cleanup(Settings *settings, int exitcode) {
130
126 if (settings->server) { 131 if (settings->server) {
127 if (net_destroy(settings->server)) { 132 if (net_destroy(settings->server)) {
128 perror("Server shutdown failed"); 133 perror("Server shutdown failed");
129 } 134 }
130 } 135 }
160 "Example: -t 150s\n" 165 "Example: -t 150s\n"
161 ); 166 );
162 return EXIT_SUCCESS; 167 return EXIT_SUCCESS;
163 } 168 }
164 169
170 initscr();
171 cbreak();
172 atexit((void(*)(void)) endwin);
173
165 Server server; 174 Server server;
166 settings.server = &server; 175 settings.server = &server;
167 176
168 if (is_server(&settings)) { 177 if (is_server(&settings)) {
169 dump_gameinfo(&(settings.gameinfo)); 178 dump_gameinfo(&(settings.gameinfo));
170 printf("\nListening for client...\n"); 179 printw("\nListening for client...\n");
180 refresh();
171 if (net_create(&server, settings.port)) { 181 if (net_create(&server, settings.port)) {
172 perror("Server creation failed"); 182 perror("Server creation failed");
173 return cleanup(&settings, EXIT_FAILURE); 183 return cleanup(&settings, EXIT_FAILURE);
174 } 184 }
175 185
176 if (net_listen(&server)) { 186 if (net_listen(&server)) {
177 perror("Listening for client failed"); 187 perror("Listening for client failed");
178 return cleanup(&settings, EXIT_FAILURE); 188 return cleanup(&settings, EXIT_FAILURE);
179 } 189 }
180 190
181 printf("Client connected - transmitting gameinfo...\n"); 191 /* net version handshake */
182 net_send(server.client->fd, NETCODE_GAMEINFO, 192 int fd = server.client->fd;
183 &(settings.gameinfo), sizeof(settings.gameinfo)); 193 net_send_code(fd, NETCODE_VERSION);
194 if (net_recieve_code(fd) != NETCODE_VERSION) {
195 fprintf(stderr, "Client uses an incompatible software version.\n");
196 return cleanup(&settings, EXIT_FAILURE);
197 }
198
199 printw("Client connected - transmitting gameinfo...");
200 refresh();
201
202
203 net_send_code(fd, NETCODE_GAMEINFO);
204 net_send_data(fd, &(settings.gameinfo), sizeof(settings.gameinfo));
205 printw("\rClient connected - awaiting challenge acceptance...");
206 refresh();
207 int code = net_recieve_code(fd);
208 if (code == NETCODE_ACCEPT) {
209 printw("\rClient connected - challenge accepted.");
210 clrtoeol();
211 } else if (code == NETCODE_DECLINE) {
212 printw("\rClient connected - challenge declined.");
213 clrtoeol();
214 } else {
215 fprintf(stderr, "Invalid client response\n");
216 return cleanup(&settings, EXIT_FAILURE);
217 }
184 } else { 218 } else {
185 if (net_find(&server, settings.serverhost, settings.port)) { 219 if (net_find(&server, settings.serverhost, settings.port)) {
186 perror("Can't find server"); 220 fprintf(stderr, "Can't find server\n");
187 return cleanup(&settings, EXIT_FAILURE); 221 return cleanup(&settings, EXIT_FAILURE);
188 } 222 }
189 223
190 if (net_connect(&server)) { 224 if (net_connect(&server)) {
191 perror("Can't connect to server"); 225 perror("Can't connect to server");
192 return cleanup(&settings, EXIT_FAILURE); 226 return cleanup(&settings, EXIT_FAILURE);
193 } 227 }
194 228
195 printf("Connection established!\n\n"); 229 int fd = server.fd;
196 if (net_recieve_code(server.fd) == NETCODE_GAMEINFO) { 230 if (net_recieve_code(fd) != NETCODE_VERSION) {
197 net_recieve_data(server.fd, &(settings.gameinfo), 231 fprintf(stderr, "Server uses an incompatible software version.\n");
232 return cleanup(&settings, EXIT_FAILURE);
233 } else {
234 net_send_code(fd, NETCODE_VERSION);
235 }
236
237 printw("Connection established!\n\n");
238 refresh();
239
240 if (net_recieve_code(fd) == NETCODE_GAMEINFO) {
241 net_recieve_data(fd, &(settings.gameinfo),
198 sizeof(settings.gameinfo)); 242 sizeof(settings.gameinfo));
199 dump_gameinfo(&(settings.gameinfo)); 243 dump_gameinfo(&(settings.gameinfo));
244 printw("Accept challenge (y/n)? ");
245 if (prompt_yesno()) {
246 net_send_code(fd, NETCODE_ACCEPT);
247 // TODO: start game
248 } else {
249 net_send_code(fd, NETCODE_DECLINE);
250 }
200 } else { 251 } else {
201 fprintf(stderr, "Server sent invalid gameinfo.\n"); 252 fprintf(stderr, "Server sent invalid gameinfo.\n");
202 } 253 }
203 } 254 }
204 255
256 getch(); /* TODO: remove */
205 return cleanup(&settings, EXIT_SUCCESS); 257 return cleanup(&settings, EXIT_SUCCESS);
206 } 258 }
207 259

mercurial