universe@0: /* universe@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@0: * universe@0: * Copyright 2014 Mike Becker. All rights reserved. universe@0: * universe@0: * Redistribution and use in source and binary forms, with or without universe@0: * modification, are permitted provided that the following conditions are met: universe@0: * universe@0: * 1. Redistributions of source code must retain the above copyright universe@0: * notice, this list of conditions and the following disclaimer. universe@0: * universe@0: * 2. Redistributions in binary form must reproduce the above copyright universe@0: * notice, this list of conditions and the following disclaimer in the universe@0: * documentation and/or other materials provided with the distribution. universe@0: * universe@0: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@0: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@0: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@0: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@0: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@0: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@0: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@0: * POSSIBILITY OF SUCH DAMAGE. universe@0: * universe@0: */ universe@0: universe@1: #include "terminal-chess.h" universe@7: #include "game.h" universe@7: #include "input.h" universe@1: #include universe@2: #include universe@7: #include universe@1: universe@1: int get_settings(int argc, char **argv, Settings *settings) { universe@1: char *valid; universe@2: unsigned long int time, port; universe@2: uint8_t timeunit = 60; universe@2: size_t len; universe@1: universe@2: for (char opt ; (opt = getopt(argc, argv, "a:bhp:rt:")) != -1 ;) { universe@1: switch (opt) { universe@2: case 'b': universe@2: settings->gameinfo.servercolor = BLACK; universe@2: break; universe@2: case 'r': universe@7: settings->gameinfo.servercolor = rand() & 1 ? WHITE : BLACK; universe@2: break; universe@2: case 't': universe@2: case 'a': universe@2: len = strlen(optarg); universe@2: if (optarg[len-1] == 's') { universe@2: optarg[len-1] = '\0'; universe@2: timeunit = 1; universe@2: } universe@2: universe@2: if ((time = strtoul(optarg, &valid, 10)) > TIME_MAX universe@2: || *valid != '\0') { universe@2: fprintf(stderr, "Specified time is invalid (%s)\n", optarg); universe@2: return 1; universe@2: } else { universe@2: if (opt=='t') { universe@2: settings->gameinfo.time = timeunit * time; universe@1: } else { universe@2: settings->gameinfo.addtime = time; universe@1: } universe@2: } universe@2: break; universe@2: case 'p': universe@2: port = strtol(optarg, &valid, 10); universe@2: if (port < 1025 || port > 65535 || *valid != '\0') { universe@2: fprintf(stderr, universe@2: "Invalid port number (%s) - choose a number between " universe@2: "1025 and 65535\n", universe@2: optarg); universe@2: return 1; universe@2: } else { universe@2: settings->port = optarg; universe@2: } universe@2: break; universe@2: case 'h': universe@2: case '?': universe@2: settings->printhelp = 1; universe@2: break; universe@1: } universe@1: } universe@1: universe@1: if (optind == argc - 1) { universe@1: settings->serverhost = argv[optind]; universe@1: } else if (optind < argc - 1) { universe@1: fprintf(stderr, "Too many arguments\n"); universe@1: return 1; universe@1: } universe@1: universe@1: return 0; universe@1: } universe@1: universe@1: Settings default_settings() { universe@1: Settings settings; universe@1: memset(&settings, 0, sizeof(Settings)); universe@2: settings.gameinfo.servercolor = WHITE; universe@1: settings.port = "27015"; universe@1: return settings; universe@1: } universe@1: universe@2: void dump_gameinfo(Gameinfo *gameinfo) { universe@2: int serverwhite = gameinfo->servercolor == WHITE; universe@3: attron(A_UNDERLINE); universe@3: printw("Game details\n"); universe@3: attroff(A_UNDERLINE); universe@3: printw(" Server: %s\n Client: %s\n", universe@2: serverwhite?"white":"black", serverwhite?"black":"White" universe@2: ); universe@2: if (gameinfo->time > 0) { universe@2: if (gameinfo->time % 60) { universe@3: printw(" Time limit: %ds + %ds\n", universe@2: gameinfo->time, gameinfo->addtime); universe@2: } else { universe@3: printw(" Time limit: %dm + %ds\n", universe@2: gameinfo->time/60, gameinfo->addtime); universe@2: } universe@2: } else { universe@3: printw(" No time limit\n"); universe@2: } universe@3: refresh(); universe@2: } universe@2: universe@4: void leavescr() { universe@4: endwin(); universe@4: } universe@4: universe@0: int main(int argc, char **argv) { universe@2: srand(time(NULL)); universe@1: universe@1: Settings settings = default_settings(); universe@2: if (get_settings(argc, argv, &settings)) { universe@2: return 1; universe@2: } universe@1: universe@1: if (settings.printhelp) { universe@1: printf( universe@2: "Usage: terminal-chess [OPTION]... [HOST]\n" universe@2: "Starts/joins a network chess game\n" universe@2: "\nGeneral options\n" universe@1: " -h This help page\n" universe@2: " -p TCP port to use (default: 27015)\n" universe@2: "\nServer options\n" universe@2: " -a