src/main.c

Mon, 17 Mar 2014 14:35:53 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 17 Mar 2014 14:35:53 +0100
changeset 4
560e07f7a6a1
parent 3
3693fd2eb0e9
child 5
f7dfef88947d
permissions
-rw-r--r--

exit hook

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

mercurial