int doRPCCmd(Client *C, char c) { int rc=-1; switch (c) { case 'h': { rc = proto_client_hello(C->ph); printf("hello: rc=%x\n", rc); if (rc > 0) game_process_reply(C); } break; case 'm': scanf("%c", &c); rc = proto_client_move(C->ph, c); break; case 'g': rc = proto_client_goodbye(C->ph); break; default: printf("%s: unknown command %c\n", __func__, c); } // NULL MT OVERRIDE ;-) printf("%s: rc=0x%x\n", __func__, rc); if (rc == 0xdeadbeef) rc=1; return rc; }
int doRPCCmd(Client *C, char c) { int rc=-1; switch (c) { case 'h': { if (PROTO_PRINT_DUMPS==1) printf("hello: rc=%x\n", rc); rc = proto_client_hello(C->ph); if (rc > 0) game_process_reply(C); } break; case 'm': gettimeofday(&rpc_start, NULL); if (PROTO_PRINT_DUMPS==1) printf("move: rc=%x\n", rc); rc = proto_client_move(C->ph, c); break; case 'f': gettimeofday(&rpc_pickup_start, NULL); if (PROTO_PRINT_DUMPS==1) printf("pickup: rc=%x\n", rc); rc = proto_client_pickup(C->ph); break; case 'g': if (PROTO_PRINT_DUMPS==1) printf("goodbye: rc=%x\n", rc); rc = proto_client_goodbye(C->ph); // We are done, exit the client exit(1); rc = -1; break; case 's': if (PROTO_PRINT_DUMPS==1) printf("start: rc=%x\n", rc); rc = proto_client_startgame(C->ph); break; case 'q': if (PROTO_PRINT_DUMPS==1) printf("query map: rc=%x\n", rc); rc = proto_client_querymap(C->ph); break; default: printf("%s: unknown command %c\n", __func__, c); } // NULL MT OVERRIDE ;-) if (PROTO_PRINT_DUMPS==1) printf("%s: rc=0x%x\n", __func__, rc); if (rc == 0xdeadbeef) rc=1; return rc; }
int main(int argc, char **argv) { Client c; if (!FASTINPUTMODE) fprintf(stderr, "Type 'connect <host:port>' to connect to a game.\n"); if (clientInit(&c) < 0) { fprintf(stderr, "ERROR: clientInit failed\n"); return -1; } initGlobals(argc, argv); if (FASTINPUTMODE) { startConnection(&c, globals.host, globals.port, update_event_handler); doRPCCmd(&c, 'q'); //query for the map if (STRESS_TEST==1) proto_client_hello(c.ph); } shell(&c); // Cannot put shell on a separate thread because fget() function doesn't work for some reason // pthread_t tid; // pthread_create(&tid, NULL, shell, &c); Proto_Client *proto_client = c.ph; Player *me = getPlayer(&proto_client->game, proto_client->playerID); if (DISPLAYUI==1) { // If I am not the host and we are stress testing, I should be wandering if (me->isHost==0 && STRESS_TEST==1) { // Wander(&c, 0); docmd(&c, "test\n"); } // The host will be the only ones that has UI showing, other players just wonder else { //window will be consistently 20x20 // pthread_t tid; // pthread_create(&tid, NULL, shell, NULL); // Init for UI stuff tty_init(STDIN_FILENO); ui_init(&(ui)); // WITH OSX ITS IS EASIEST TO KEEP UI ON MAIN THREAD // SO JUMP THROW HOOPS :-( Proto_Client *client = (Proto_Client *) c.ph; Player *me = getPlayer(&client->game, client->playerID); doRPCCmd(&c, 'q'); //query for the map ui_main_loop(ui, (32 * WINDOW_SIZE), (32 * WINDOW_SIZE), &client->game, me, &c); } } // launchUI(&c); return 0; }
int docmd(Client *C, char *cmd) { int rc = 1; Proto_Client *client = (Proto_Client *) C->ph; // If this is a connect attempt char input[50]; strcpy(input, cmd); int connectAttempt = check_if_connect(input); if (connectAttempt==1) { // Ok startup our connection to the server if (startConnection(C, globals.host, globals.port, update_event_handler)<0) { fprintf(stderr, "ERROR: startConnection failed\n"); return -1; } else { fprintf(stderr, "Successfully connected to <%s:%d>\n", globals.host, globals.port); proto_client_hello(C->ph); doRPCCmd(C, 'q'); //query for the map return 1; } return 1; } strcpy(input,cmd); if (strcmp(cmd, "disconnect\n")==0) { doRPCCmd(C, 'g'); rc=-1; } else if (strcmp(cmd, "where\n")==0) where(); else if (strcmp(cmd, "numhome 1\n")==0) { doRPCCmd(C, 'q'); // query map if (client->game.map.numHome1!=0) fprintf(stderr, "%d\n", client->game.map.numHome1); } else if (strcmp(cmd, "numhome 2\n")==0) { doRPCCmd(C, 'q'); // query map if (client->game.map.numHome2!=0) fprintf(stderr, "%d\n", client->game.map.numHome2); } else if (strcmp(cmd, "numjail 1\n")==0) { doRPCCmd(C, 'q'); // query map if (client->game.map.numJail1!=0) fprintf(stderr, "%d\n", client->game.map.numJail1); } else if (strcmp(cmd, "numjail 2\n")==0) { doRPCCmd(C, 'q'); // query map if (client->game.map.numHome2!=0) fprintf(stderr, "%d\n", client->game.map.numJail2); } else if (strcmp(cmd, "numwall\n")==0) { doRPCCmd(C, 'q'); // query map if (client->game.map.numFixedWall!=0 && client->game.map.numNonfixedWall!=0) fprintf(stderr, "%d\n", client->game.map.numFixedWall + client->game.map.numNonfixedWall); } else if (strcmp(cmd, "numfloor\n")==0) { doRPCCmd(C, 'q'); // query map if (client->game.map.numFloor1!=0 && client->game.map.numFloor2!=0) fprintf(stderr, "%d\n", client->game.map.numFloor1+client->game.map.numFloor2); } else if (strcmp(cmd, "dim\n")==0) { doRPCCmd(C, 'q'); // query map if (client->game.map.dimension.x!=0 && client->game.map.dimension.y!=0) fprintf(stderr, "%dx%d\n", client->game.map.dimension.x, client->game.map.dimension.y); } else if (strcmp(cmd, "dump\n")==0) { doRPCCmd(C, 'q'); // query map printMap(&client->game.map); if (DISPLAYUI==1) ui_update(ui); } else if (containsString(input, "cinfo")>0) { doRPCCmd(C, 'q'); // query map cinfo(cmd, C); } else if (strcmp(cmd, "start\n")==0) { doRPCCmd(C, 's'); // query map } else if (strcmp(cmd, "test\n")==0) { if (STRESS_TEST == 1) { pid_t myChild; myChild = fork(); if (myChild == 0) { Wander(C, 0); } else{} } } // MOVEMENT else if (strcmp(cmd, "w\n")==0) { Proto_Client *client = C->ph; client->rpc_session.shdr.returnCode = UP; doRPCCmd(C, 'm'); // query map } else if (strcmp(cmd, "a\n")==0) { Proto_Client *client = C->ph; client->rpc_session.shdr.returnCode = LEFT; doRPCCmd(C, 'm'); // query map } else if (strcmp(cmd, "d\n")==0) { Proto_Client *client = C->ph; client->rpc_session.shdr.returnCode = RIGHT; doRPCCmd(C, 'm'); // query map } else if (strcmp(cmd, "s\n")==0) { Proto_Client *client = C->ph; client->rpc_session.shdr.returnCode = DOWN; doRPCCmd(C, 'm'); // query map } // END OF MOVEMENT //PICKUP else if (strcmp(cmd, "f\n")==0){ Proto_Client *client = C->ph; doRPCCmd(C, 'f'); } //END PICKUP else if (strcmp(cmd, "O\n")==0) proto_debug_on(); else if (strcmp(cmd, "o\n")==0) proto_debug_off(); else if (strcmp(cmd, "rh\n")==0) rc = proto_client_hello(C->ph); else if (strcmp(cmd, "q\n")==0) { fprintf(stderr, "Game Over: You Quit\n"); doRPCCmd(C, 'g'); rc=-1; } else if (strcmp(cmd, "quit\n")==0) { doRPCCmd(C, 'g'); fprintf(stderr, "Game Over: You Quit\n"); rc=-1; } else if (strcmp(cmd, "\n")==0) { rc = proto_client_update(C->ph); rc=1; } else { fprintf(stderr, "Unknown command\n"); } return rc; }
int doCMDS(Client *C, char *cmdInput) { int rc = 1; char **tokens; int k = 0; tokens = str_split(cmdInput, ' '); if (tokens) { for (k = 0; * (tokens + k); k++) { if (proto_debug()) printf("%d - %s\n", k, *(tokens + k)); } } if (proto_debug()) printf("Total commands - %d\n", k); if (connectFLAG == FALSE) { if (strcmp(*(tokens + 0), "connect") != 0) { printf("%s\n", "Please do a connect first - connect <IP> <PORT>"); return rc; } } if (tokens) { if ( strcmp(*(tokens + 0), "pickup") == 0 ) { if (k == 2) { if (strcmp(*(tokens + 1), "shovel") == 0) rc = proto_item_action(C->ph, 'S', 'p'); else if (strcmp(*(tokens + 1), "flag") == 0) rc = proto_item_action(C->ph, 'F', 'p'); else printf("%s\n", "Error - usage of pickup: pickup <shovel|flag>"); } else printf("%s\n", "Error - usage of pickup: pickup <shovel|flag>"); return rc; } if ( strcmp(*(tokens + 0), "drop") == 0 ) { if (k == 2) { if (strcmp(*(tokens + 1), "shovel") == 0) rc = proto_item_action(C->ph, 'S', 'd'); else if (strcmp(*(tokens + 1), "flag") == 0) rc = proto_item_action(C->ph, 'F', 'd'); else printf("%s\n", "Error - usage of drop: drop <shovel|flag>"); } else printf("%s\n", "Error - usage of drop: drop <shovel|flag>"); return rc; } if ( strcmp(*(tokens + 0), "move") == 0 ) { if (k == 2) { if (strcmp(*(tokens + 1), "U") == 0) rc = proto_client_move(C->ph, 'U'); else if (strcmp(*(tokens + 1), "D") == 0) rc = proto_client_move(C->ph, 'D'); else if (strcmp(*(tokens + 1), "L") == 0) rc = proto_client_move(C->ph, 'L'); else if (strcmp(*(tokens + 1), "R") == 0) rc = proto_client_move(C->ph, 'R'); else printf("%s\n", "Error - usage of move: move <U|D|L|R>"); } else printf("%s\n", "Error - usage of move: move <U|D|L|R>"); return rc; } if ( strcmp(*(tokens + 0), "join") == 0 ) { rc = proto_client_hello(C->ph); return rc; } if ( strcmp(*(tokens + 0), "printdata") == 0 ) { rc = print_client_data(); return rc; } if ( strcmp(*(tokens + 0), "connect") == 0 ) { //rc = doRPCCmd(C, 'h'); //return rc; //rc = proto_client_hello(C->ph); //printf("hello: rc=%x\n", rc); //if (rc > 0) game_process_reply(C); if (connectFLAG == TRUE) { //fprintf(stderr, "Are are already connected."); } return rc; } else if (strcmp(*(tokens + 0), "numhome") == 0) { //rc = docmd(C, 'q'); //return rc; if (k == 2) { rc = proto_client_numhome(C->ph, atoi(*(tokens + 1))); if ((strcmp(*(tokens + 1), "1") == 0) || (strcmp(*(tokens + 1), "2") == 0 )) printf("The number of home cells that team %s has - %d\n", *(tokens + 1), rc); else printf("%s\n", "Error - usage of numhome: numhome <1|2>"); } else printf("%s\n", "Error - usage of numhome: numhome <1|2>"); rc = 1; return rc; } else if (strcmp(*(tokens + 0), "numjail") == 0) { //rc = docmd(C, 'q'); //return rc if (k == 2) { rc = proto_client_numjail(C->ph, atoi(*(tokens + 1))); if (strcmp(*(tokens + 1), "1") == 0 || strcmp(*(tokens + 1), "2") == 0 ) printf("The number of jail cells that team %s has - %d\n", *(tokens + 1), rc); else printf("%s\n", "Error - usage of numjail: numjail <1|2>"); } else printf("%s\n", "Error - usage of numjail: numjail <1|2>"); rc = 1; return rc; } else if (strcmp(*(tokens + 0), "numwall") == 0) { //rc = docmd(C, 'q'); //return rc; rc = proto_client_numwall(C->ph); printf("The number of wall cells - %d\n", rc); rc = 1; return rc; } else if (strcmp(*(tokens + 0), "numfloor") == 0) { //rc = docmd(C, 'q'); //return rc; //rc = proto_client_numfloor(C->ph); rc = proto_client_numfloor(C->ph); printf("The number of floor cells - %d\n", rc); rc = 1; return rc; } else if (strcmp(*(tokens + 0), "dim") == 0) { //rc = docmd(C, 'q'); //return rc; rc = proto_client_dim(C->ph); short x = getA(rc); short y = getB(rc); printf("The dimensions of the maze - %hd by %hd\n", x, y); rc = 1; return rc; } else if (strcmp(*(tokens + 0), "cinfo") == 0) { //rc = docmd(C, 'q'); //return rc; if (k == 3) { rc = proto_client_cinfo(C->ph, atoi(*(tokens + 1)), atoi(*(tokens + 2))); if (atoi(*(tokens + 1)) > 0 && atoi(*(tokens + 2)) > 0 ) { if (proto_debug()) printf("%s\n", "Good"); int cell = (rc >> (8 * 1)) & 0xff; printf("The cell is a - %c\n", (char) cell); int team = (rc >> (8 * 2)) & 0xff; printf("The team is - %c\n", (char) team); int occupied = (rc >> (8 * 3)) & 0xff; printf("Is the cell occupied - %c\n", (char) occupied); } else printf("%s\n", "Error - usage of cinfo: cinfo <X> <Y>"); } else