#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include int main(int argc, char *argv[]){ printf("Il mio pid per killarmi: %d\n", getpid()); //FIXME: TMP setupServer(argc, argv); //TODO: passare struct con id //FIXME: fixare usando le var globali prima di mettere in setupServer // SHM //TODO: forse bisogna fare il reset della shm, perche in teoria mette a 0 quando crea //ma nel caso in cui il campo non si fosse chiuso per qualche motivo forse quando lo riapre non lo azzera printf(PSERVER "Setting up shm\n"); _SHMID = getShmid(_INPUT_S.rows, _INPUT_S.collums); _BOARD = shmServerAt(_SHMID); // // aperta semaforo per connessione dei due client printf(PSERVER "Waiting for players...\n"); semOp(_SEMID, 0, 1); semOp(_SEMID, 1, 1); // aspetto che si connettano i due client //FIXME: forse non serve perche tanto deve aspettare che i player gli mandono un messaggio con il loro nome (a cose servono i semafori se ci sono i messaggi che fanno l'attesa non in polling aaaaaaaaa) semOp(_SEMID, 2, -1); player_ds player; char name[2][MAX_NAME]; // ricevuta nome primo client rcvPlayer(&player); strcpy(name[player.id], player.name_player); _PIDS[player.id] = player.pid; printf(PSERVER "Player %s connected\n", name[player.id]); // aspetto secondo client semOp(_SEMID, 2, -1); // ricevuta nome secondo client rcvPlayer(&player); strcpy(name[player.id], player.name_player); _PIDS[player.id] = player.pid; printf(PSERVER "Player %s connected\n", name[player.id]); semOp(_SEMID, 0, 1); semOp(_SEMID, 1, 1); // PARTITA printf(PSERVER "Starting game\n"); printf("%d %d\n", _INPUT_S.collums, _INPUT_S.rows); setCollums(_INPUT_S.collums); setRows(_INPUT_S.rows); printf("%d %d\n", _ROWS, _COLLUMS); for (int i=0; i < _ROWS * _COLLUMS; i++) { //FIXME: tmp _BOARD[i] = EMPTY; } int turns_left = _INPUT_S.rows * _INPUT_S.collums; int turn = 0; tile_t result; move_t move; do { semOp(_SEMID, turn, 1); printf(PSERVER "%s's turn\n", name[turn]); rcvMove(&move); //TMP printf("move: %d\n", move.move); insertMove(_BOARD, move.move, turn); printf("turns left: %d\n", turns_left - 1); semOp(_SEMID, turn, 1); turn ^= 1; } while (--turns_left && !(result = checkWin(_BOARD, move.move))); if (!result) { //FIXME: change printf printf(PSERVER "Game ended in a draw\n"); } else { printf(PSERVER "Game ended\n"); } game_end_t game_end = {.mtype = 4, .winner = result}; sndGame_end(&game_end); sndGame_end(&game_end); kill(_PIDS[0], SIGUSR1); kill(_PIDS[1], SIGUSR1); //TODO: end of game return 0; }