-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
73 lines (59 loc) · 1.48 KB
/
server.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "listen.h"
#include "game.h"
#define ROOMSIZE 2
int client[ROOMSIZE];
bool _client_Missing ()
{
for (int i = 0; i < ROOMSIZE; i++)
if (client[i] < 0)
return true;
return false;
}
int main (int argc, char const *argv[])
{
void *game = game_New();
int err = listen_Init();
if (err) exit(err);
printf("Server listening...\n");
for (int i = 0; i < ROOMSIZE; i++) {
client[i] = -1;
printf("Waiting player %d...\n", i + 1);
client[i] = listen_Wait();
int playerId = game_Join(game);
listen_SendMsg(client + i, MSGJOIN);
listen_SendInt(client + i, playerId);
}
for (int i = 0; i < ROOMSIZE; i++)
listen_SendMsg(client + i, MSGSTART);
while (!_client_Missing()) {
int now = game_Turn(game) % ROOMSIZE;
for (int i = 0; i < ROOMSIZE; i++)
if (i != now)
listen_SendMsg(client + i, MSGWAIT);
#if DEBUG
printf("Waiting message from player %d...\n", now + 1);
#endif
listen_SendMsg(client + now, MSGINPUT);
int input = listen_Read(client + now);
int err = game_Update(game, &input);
if (err) {
listen_SendMsg(client + now, MSGERROR);
listen_SendInt(client + now, input);
}
int event = game_Result(game);
for (int i = 0; i < ROOMSIZE; i++) {
listen_SendMsg(client + i, MSGUPDATE);
listen_SendInt(client + i, input);
if (!event)
continue;
listen_SendMsg(client + i, MSGEVENT);
listen_SendInt(client + i, event);
}
}
return 0;
}