Elaborato_SO/src/F4Server.c

110 lines
2.5 KiB
C
Raw Normal View History

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>
#include <unistd.h>
#include <time.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-06-17 11:10:30 +02:00
int bot = 0;
void waitPlayer(char name[2][MAX_NAME]) {
player_ds player;
semOp(_SEMID, SERVER, -1);
// ricevo nome primo client
rcvPlayer(&player);
strcpy(name[player.id], player.name_player);
_PIDS[player.id] = player.pid;
2023-06-17 11:10:30 +02:00
if (player.bot) {
bot = 1;
}
printf(PSERVER "Player %s connected\n", name[player.id]);
}
int main(int argc, char *argv[]) {
2023-05-29 11:37:43 +02:00
setupServer(argc, argv);
2023-05-16 16:10:43 +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-29 12:16:36 +02:00
semOp(_SEMID, CLIENT0, 1);
semOp(_SEMID, CLIENT1, 1);
2023-05-17 14:24:46 +02:00
char name[2][MAX_NAME];
2023-05-16 23:27:27 +02:00
2023-06-17 11:10:30 +02:00
// aspetto PRIMO client
waitPlayer(name);
2023-06-17 11:10:30 +02:00
if (bot) {
int child = fork();
if (child == 0) {
2023-06-17 11:39:54 +02:00
printf(PSERVER "Creating bot\n");
execl("./bin/F4Client", "./bin/F4Client", "Bot", "auto", (char *)NULL);
2023-06-17 11:10:30 +02:00
}
}
2023-05-16 23:27:27 +02:00
2023-05-29 12:16:36 +02:00
// aspetto SECONDO client
waitPlayer(name);
2023-05-16 23:27:27 +02:00
// avviso inizio partita
2023-05-29 12:16:36 +02:00
semOp(_SEMID, CLIENT0, 1);
semOp(_SEMID, CLIENT1, 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-15 11:57:30 +02:00
int turns_left = _INPUT_S.rows * _INPUT_S.collums;
2023-06-01 14:39:04 +02:00
tile_t result = -1;
2023-05-16 16:53:23 +02:00
move_t move;
do {
2023-05-29 12:16:36 +02:00
// apro semaforo al player di turno
semOp(_SEMID, turn, 1);
2023-06-17 10:45:51 +02:00
alarm(TIME_TO_MOVE);
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-29 11:49:35 +02:00
// aspetto che mi mandi una mossa
2023-05-19 15:21:15 +02:00
rcvMove(&move);
2023-06-17 10:45:51 +02:00
alarm(0);
2023-05-31 11:00:59 +02:00
insertMove(move.move, turn);
2023-05-29 11:49:35 +02:00
// info
printf("move: %d\n", move.move);
2023-05-16 23:27:27 +02:00
printf("turns left: %d\n", turns_left - 1);
2023-05-29 11:49:35 +02:00
// dico al player che la mossa è stata effettuata per fargli stampare il campo
2023-05-26 10:58:57 +02:00
semOp(_SEMID, turn, 1);
2023-05-29 12:16:36 +02:00
// next turn
2023-05-26 10:58:57 +02:00
turn ^= 1;
2023-05-29 11:49:35 +02:00
2023-06-01 14:39:04 +02:00
} while (--turns_left && (result = checkWin(move.move)) == -1);
2023-05-16 16:53:23 +02:00
2023-06-01 12:05:00 +02:00
printf("result: %d\n", result);
2023-06-01 14:39:04 +02:00
if (result == -1) {
2023-05-22 11:03:36 +02:00
printf(PSERVER "Game ended in a draw\n");
2023-05-16 17:07:35 +02:00
} else {
2023-06-01 12:05:00 +02:00
printf(PSERVER "%s won\n", name[result]);
2023-05-16 17:07:35 +02:00
}
2023-06-17 14:31:24 +02:00
printf("winner %d\n", result);
2023-06-05 16:17:20 +02:00
2023-06-17 10:45:51 +02:00
end_game(result);
2023-05-16 18:43:56 +02:00
}