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