Mon, 17 Mar 2014 14:35:53 +0100
exit hook
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 static WINDOW* window;
142 void leavescr() {
143 mvprintw(getmaxy(window)-1, 0, "Leaving terminal-chess. Press any key...");
144 getch();
145 endwin();
146 }
148 int main(int argc, char **argv) {
149 srand(time(NULL));
151 Settings settings = default_settings();
152 if (get_settings(argc, argv, &settings)) {
153 return 1;
154 }
156 if (settings.printhelp) {
157 printf(
158 "Usage: terminal-chess [OPTION]... [HOST]\n"
159 "Starts/joins a network chess game\n"
160 "\nGeneral options\n"
161 " -h This help page\n"
162 " -p TCP port to use (default: 27015)\n"
163 "\nServer options\n"
164 " -a <time> Specifies the time to add after each move\n"
165 " -b Server plays black pieces (default: white)\n"
166 " -r Distribute color randomly\n"
167 " -t <time> Specifies time limit (default: no limit)\n"
168 "\nNotes\n"
169 "White pieces are displayed as uppercase and black pieces as "
170 "lowercase letters.\n"
171 "The time unit for -a is seconds and for -t minutes by default. To "
172 "specify\nseconds for the -t option, use the s suffix.\n"
173 "Example: -t 150s\n"
174 );
175 return EXIT_SUCCESS;
176 }
178 window = initscr();
179 cbreak();
180 atexit(leavescr);
182 Server server;
183 settings.server = &server;
185 if (is_server(&settings)) {
186 dump_gameinfo(&(settings.gameinfo));
187 printw("\nListening for client...\n");
188 refresh();
189 if (net_create(&server, settings.port)) {
190 perror("Server creation failed");
191 return cleanup(&settings, EXIT_FAILURE);
192 }
194 if (net_listen(&server)) {
195 perror("Listening for client failed");
196 return cleanup(&settings, EXIT_FAILURE);
197 }
199 /* net version handshake */
200 int fd = server.client->fd;
201 net_send_code(fd, NETCODE_VERSION);
202 if (net_recieve_code(fd) != NETCODE_VERSION) {
203 fprintf(stderr, "Client uses an incompatible software version.\n");
204 return cleanup(&settings, EXIT_FAILURE);
205 }
207 printw("Client connected - transmitting gameinfo...");
208 refresh();
211 net_send_code(fd, NETCODE_GAMEINFO);
212 net_send_data(fd, &(settings.gameinfo), sizeof(settings.gameinfo));
213 printw("\rClient connected - awaiting challenge acceptance...");
214 refresh();
215 int code = net_recieve_code(fd);
216 if (code == NETCODE_ACCEPT) {
217 printw("\rClient connected - challenge accepted.");
218 clrtoeol();
219 } else if (code == NETCODE_DECLINE) {
220 printw("\rClient connected - challenge declined.");
221 clrtoeol();
222 } else {
223 fprintf(stderr, "Invalid client response\n");
224 return cleanup(&settings, EXIT_FAILURE);
225 }
226 } else {
227 if (net_find(&server, settings.serverhost, settings.port)) {
228 fprintf(stderr, "Can't find server\n");
229 return cleanup(&settings, EXIT_FAILURE);
230 }
232 if (net_connect(&server)) {
233 perror("Can't connect to server");
234 return cleanup(&settings, EXIT_FAILURE);
235 }
237 int fd = server.fd;
238 if (net_recieve_code(fd) != NETCODE_VERSION) {
239 fprintf(stderr, "Server uses an incompatible software version.\n");
240 return cleanup(&settings, EXIT_FAILURE);
241 } else {
242 net_send_code(fd, NETCODE_VERSION);
243 }
245 printw("Connection established!\n\n");
246 refresh();
248 if (net_recieve_code(fd) == NETCODE_GAMEINFO) {
249 net_recieve_data(fd, &(settings.gameinfo),
250 sizeof(settings.gameinfo));
251 dump_gameinfo(&(settings.gameinfo));
252 printw("Accept challenge (y/n)? ");
253 if (prompt_yesno()) {
254 net_send_code(fd, NETCODE_ACCEPT);
255 // TODO: start game
256 } else {
257 net_send_code(fd, NETCODE_DECLINE);
258 }
259 } else {
260 fprintf(stderr, "Server sent invalid gameinfo.\n");
261 }
262 }
264 return cleanup(&settings, EXIT_SUCCESS);
265 }