Thu, 06 Mar 2014 15:03:06 +0100
changed UI to ncurses session + added network handshake
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2014 Mike Becker. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
30 #include "terminal-chess.h"
31 #include <string.h>
32 #include <time.h>
33 #include <ncurses.h>
34 #include "input.h"
36 int get_settings(int argc, char **argv, Settings *settings) {
37 char *valid;
38 unsigned long int time, port;
39 uint8_t timeunit = 60;
40 size_t len;
42 for (char opt ; (opt = getopt(argc, argv, "a:bhp:rt:")) != -1 ;) {
43 switch (opt) {
44 case 'b':
45 settings->gameinfo.servercolor = BLACK;
46 break;
47 case 'r':
48 settings->gameinfo.servercolor = (rand()>>5) & 1 ? WHITE : BLACK;
49 break;
50 case 't':
51 case 'a':
52 len = strlen(optarg);
53 if (optarg[len-1] == 's') {
54 optarg[len-1] = '\0';
55 timeunit = 1;
56 }
58 if ((time = strtoul(optarg, &valid, 10)) > TIME_MAX
59 || *valid != '\0') {
60 fprintf(stderr, "Specified time is invalid (%s)\n", optarg);
61 return 1;
62 } else {
63 if (opt=='t') {
64 settings->gameinfo.time = timeunit * time;
65 } else {
66 settings->gameinfo.addtime = time;
67 }
68 }
69 break;
70 case 'p':
71 port = strtol(optarg, &valid, 10);
72 if (port < 1025 || port > 65535 || *valid != '\0') {
73 fprintf(stderr,
74 "Invalid port number (%s) - choose a number between "
75 "1025 and 65535\n",
76 optarg);
77 return 1;
78 } else {
79 settings->port = optarg;
80 }
81 break;
82 case 'h':
83 case '?':
84 settings->printhelp = 1;
85 break;
86 }
87 }
89 if (optind == argc - 1) {
90 settings->serverhost = argv[optind];
91 } else if (optind < argc - 1) {
92 fprintf(stderr, "Too many arguments\n");
93 return 1;
94 }
96 return 0;
97 }
99 Settings default_settings() {
100 Settings settings;
101 memset(&settings, 0, sizeof(Settings));
102 settings.gameinfo.servercolor = WHITE;
103 settings.port = "27015";
104 return settings;
105 }
107 void dump_gameinfo(Gameinfo *gameinfo) {
108 int serverwhite = gameinfo->servercolor == WHITE;
109 attron(A_UNDERLINE);
110 printw("Game details\n");
111 attroff(A_UNDERLINE);
112 printw(" Server: %s\n Client: %s\n",
113 serverwhite?"white":"black", serverwhite?"black":"White"
114 );
115 if (gameinfo->time > 0) {
116 if (gameinfo->time % 60) {
117 printw(" Time limit: %ds + %ds\n",
118 gameinfo->time, gameinfo->addtime);
119 } else {
120 printw(" Time limit: %dm + %ds\n",
121 gameinfo->time/60, gameinfo->addtime);
122 }
123 } else {
124 printw(" No time limit\n");
125 }
126 refresh();
127 }
129 int cleanup(Settings *settings, int exitcode) {
131 if (settings->server) {
132 if (net_destroy(settings->server)) {
133 perror("Server shutdown failed");
134 }
135 }
137 return exitcode;
138 }
140 int main(int argc, char **argv) {
141 srand(time(NULL));
143 Settings settings = default_settings();
144 if (get_settings(argc, argv, &settings)) {
145 return 1;
146 }
148 if (settings.printhelp) {
149 printf(
150 "Usage: terminal-chess [OPTION]... [HOST]\n"
151 "Starts/joins a network chess game\n"
152 "\nGeneral options\n"
153 " -h This help page\n"
154 " -p TCP port to use (default: 27015)\n"
155 "\nServer options\n"
156 " -a <time> Specifies the time to add after each move\n"
157 " -b Server plays black pieces (default: white)\n"
158 " -r Distribute color randomly\n"
159 " -t <time> Specifies time limit (default: no limit)\n"
160 "\nNotes\n"
161 "White pieces are displayed as uppercase and black pieces as "
162 "lowercase letters.\n"
163 "The time unit for -a is seconds and for -t minutes by default. To "
164 "specify\nseconds for the -t option, use the s suffix.\n"
165 "Example: -t 150s\n"
166 );
167 return EXIT_SUCCESS;
168 }
170 initscr();
171 cbreak();
172 atexit((void(*)(void)) endwin);
174 Server server;
175 settings.server = &server;
177 if (is_server(&settings)) {
178 dump_gameinfo(&(settings.gameinfo));
179 printw("\nListening for client...\n");
180 refresh();
181 if (net_create(&server, settings.port)) {
182 perror("Server creation failed");
183 return cleanup(&settings, EXIT_FAILURE);
184 }
186 if (net_listen(&server)) {
187 perror("Listening for client failed");
188 return cleanup(&settings, EXIT_FAILURE);
189 }
191 /* net version handshake */
192 int fd = server.client->fd;
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 }
199 printw("Client connected - transmitting gameinfo...");
200 refresh();
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 }
218 } else {
219 if (net_find(&server, settings.serverhost, settings.port)) {
220 fprintf(stderr, "Can't find server\n");
221 return cleanup(&settings, EXIT_FAILURE);
222 }
224 if (net_connect(&server)) {
225 perror("Can't connect to server");
226 return cleanup(&settings, EXIT_FAILURE);
227 }
229 int fd = server.fd;
230 if (net_recieve_code(fd) != NETCODE_VERSION) {
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 }
237 printw("Connection established!\n\n");
238 refresh();
240 if (net_recieve_code(fd) == NETCODE_GAMEINFO) {
241 net_recieve_data(fd, &(settings.gameinfo),
242 sizeof(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 }
251 } else {
252 fprintf(stderr, "Server sent invalid gameinfo.\n");
253 }
254 }
256 getch(); /* TODO: remove */
257 return cleanup(&settings, EXIT_SUCCESS);
258 }