2023-05-14 18:03:50 +02:00
# include <sys/types.h>
# include <sys/ipc.h>
2023-05-14 21:44:03 +02:00
# include <sys/shm.h>
2023-05-16 14:55:12 +02:00
# include <sys/stat.h>
2023-05-15 16:50:48 +02:00
# include <signal.h>
# include <stdlib.h>
# include <stdio.h>
2023-05-16 15:55:05 +02:00
# include <sys/msg.h>
2023-05-16 16:32:24 +02:00
# include <sys/sem.h>
2023-05-17 14:24:46 +02:00
# include <string.h>
2023-05-22 09:26:39 +02:00
# include <unistd.h>
2023-05-14 18:03:50 +02:00
2023-05-14 18:10:44 +02:00
# include <server.h>
# include <structures.h>
2023-05-15 10:31:50 +02:00
# include <custom_sem.h>
2023-05-15 10:43:25 +02:00
# include <custom_shm.h>
2023-05-15 16:50:48 +02:00
# include <errExit.h>
2023-05-16 16:53:23 +02:00
# include <forza4.h>
2023-05-19 15:21:15 +02:00
# include <custom_msgq.h>
2023-05-21 11:53:23 +02:00
# include <custom_sig.h>
2023-05-14 18:03:50 +02:00
2023-05-14 17:07:34 +02:00
int main ( int argc , char * argv [ ] ) {
2023-05-25 14:28:45 +02:00
printf ( " Il mio pid per killarmi: %d \n " , getpid ( ) ) ; //FIXME: TMP
2023-05-22 09:26:39 +02:00
2023-05-26 15:17:22 +02:00
setupServer ( argc , argv ) ; //TODO: passare struct con id
2023-05-15 16:50:48 +02:00
2023-05-26 15:16:26 +02:00
//FIXME: fixare usando le var globali prima di mettere in setupServer
2023-05-16 16:10:43 +02:00
// SHM
2023-05-22 10:07:57 +02:00
//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
2023-05-22 11:03:36 +02:00
printf ( PSERVER " Setting up shm \n " ) ;
2023-05-23 12:37:44 +02:00
_SHMID = getShmid ( _INPUT_S . rows , _INPUT_S . collums ) ;
_BOARD = shmServerAt ( _SHMID ) ;
2023-05-16 16:10:43 +02:00
//
2023-05-15 16:50:48 +02:00
2023-05-26 15:16:26 +02:00
// aperta semaforo per connessione dei due client
2023-05-22 11:03:36 +02:00
printf ( PSERVER " Waiting for players... \n " ) ;
2023-05-23 12:37:44 +02:00
semOp ( _SEMID , 0 , 1 ) ;
semOp ( _SEMID , 1 , 1 ) ;
2023-05-16 23:27:27 +02:00
2023-05-14 22:13:42 +02:00
2023-05-16 23:27:27 +02:00
// 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)
2023-05-23 12:37:44 +02:00
semOp ( _SEMID , 2 , - 1 ) ;
2023-05-14 17:07:34 +02:00
2023-05-17 15:57:10 +02:00
player_ds player ;
2023-05-17 14:24:46 +02:00
char name [ 2 ] [ MAX_NAME ] ;
2023-05-16 23:27:27 +02:00
// ricevuta nome primo client
2023-05-19 15:21:15 +02:00
rcvPlayer ( & player ) ;
2023-05-16 23:27:27 +02:00
2023-05-17 15:57:10 +02:00
strcpy ( name [ player . id ] , player . name_player ) ;
2023-05-23 12:37:44 +02:00
_PIDS [ player . id ] = player . pid ;
2023-05-17 10:45:16 +02:00
2023-05-22 17:22:17 +02:00
printf ( PSERVER " Player %s connected \n " , name [ player . id ] ) ;
2023-05-16 23:27:27 +02:00
// aspetto secondo client
2023-05-23 12:37:44 +02:00
semOp ( _SEMID , 2 , - 1 ) ;
2023-05-16 23:27:27 +02:00
// ricevuta nome secondo client
2023-05-19 15:21:15 +02:00
rcvPlayer ( & player ) ;
2023-05-17 15:57:10 +02:00
strcpy ( name [ player . id ] , player . name_player ) ;
2023-05-23 12:37:44 +02:00
_PIDS [ player . id ] = player . pid ;
2023-05-16 23:27:27 +02:00
2023-05-22 17:22:17 +02:00
printf ( PSERVER " Player %s connected \n " , name [ player . id ] ) ;
2023-05-16 23:27:27 +02:00
2023-05-23 12:37:44 +02:00
semOp ( _SEMID , 0 , 1 ) ;
semOp ( _SEMID , 1 , 1 ) ;
2023-05-16 23:27:27 +02:00
// PARTITA
2023-05-22 11:03:36 +02:00
printf ( PSERVER " Starting game \n " ) ;
2023-05-26 10:58:57 +02:00
printf ( " %d %d \n " , _INPUT_S . collums , _INPUT_S . rows ) ;
2023-05-23 12:37:44 +02:00
setCollums ( _INPUT_S . collums ) ;
setRows ( _INPUT_S . rows ) ;
2023-05-15 11:57:30 +02:00
2023-05-26 10:58:57 +02:00
printf ( " %d %d \n " , _ROWS , _COLLUMS ) ;
for ( int i = 0 ; i < _ROWS * _COLLUMS ; i + + ) { //FIXME: tmp
_BOARD [ i ] = EMPTY ;
}
2023-05-23 12:37:44 +02:00
int turns_left = _INPUT_S . rows * _INPUT_S . collums ;
2023-05-16 17:07:35 +02:00
int turn = 0 ;
2023-05-17 15:46:51 +02:00
tile_t result ;
2023-05-16 16:53:23 +02:00
move_t move ;
do {
2023-05-23 12:37:44 +02:00
semOp ( _SEMID , turn , 1 ) ;
2023-05-16 16:53:23 +02:00
2023-05-22 11:03:36 +02:00
printf ( PSERVER " %s's turn \n " , name [ turn ] ) ;
2023-05-16 18:43:56 +02:00
2023-05-19 15:21:15 +02:00
rcvMove ( & move ) ;
2023-05-16 16:53:23 +02:00
2023-05-16 23:27:27 +02:00
//TMP
printf ( " move: %d \n " , move . move ) ;
2023-05-22 17:22:17 +02:00
2023-05-23 12:37:44 +02:00
insertMove ( _BOARD , move . move , turn ) ;
2023-05-22 17:22:17 +02:00
2023-05-16 23:27:27 +02:00
printf ( " turns left: %d \n " , turns_left - 1 ) ;
2023-05-26 10:58:57 +02:00
semOp ( _SEMID , turn , 1 ) ;
2023-05-16 16:53:23 +02:00
2023-05-26 10:58:57 +02:00
turn ^ = 1 ;
2023-05-23 12:37:44 +02:00
} while ( - - turns_left & & ! ( result = checkWin ( _BOARD , move . move ) ) ) ;
2023-05-16 16:53:23 +02:00
2023-05-15 11:57:30 +02:00
2023-05-18 13:57:41 +02:00
if ( ! result ) { //FIXME: change printf
2023-05-22 11:03:36 +02:00
printf ( PSERVER " Game ended in a draw \n " ) ;
2023-05-18 13:57:41 +02:00
2023-05-16 17:07:35 +02:00
} else {
2023-05-22 11:03:36 +02:00
printf ( PSERVER " Game ended \n " ) ;
2023-05-16 17:07:35 +02:00
}
2023-05-22 17:22:17 +02:00
2023-05-19 15:21:15 +02:00
game_end_t game_end = { . mtype = 4 , . winner = result } ;
sndGame_end ( & game_end ) ;
sndGame_end ( & game_end ) ;
2023-05-23 12:37:44 +02:00
kill ( _PIDS [ 0 ] , SIGUSR1 ) ;
kill ( _PIDS [ 1 ] , SIGUSR1 ) ;
2023-05-16 17:07:35 +02:00
2023-05-15 11:57:30 +02:00
2023-05-16 17:07:35 +02:00
//TODO: end of game
2023-05-15 11:57:30 +02:00
2023-05-14 17:07:34 +02:00
return 0 ;
2023-05-16 18:43:56 +02:00
}