-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
97 lines (83 loc) · 1.79 KB
/
client.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "connect.h"
#include "game.h"
#include "renderer.h"
#define HOST "127.0.0.1"
#define PORT 8080
#define GAMELENGTH 9
#define INPUT_QUIT -1
#define INPUT_INVALID -2
#define USAGE ("./client.out [HOST] [PORT]\n")
int parseOpts (int argc, char const *argv[], char *hostPtr, int *portPtr)
{
if (argc > 1)
sscanf(argv[1], "%s", hostPtr);
if (argc > 2)
sscanf(argv[2], "%d", portPtr);
return 0;
}
int main (int argc, char const *argv[])
{
struct connect conn;
char buffer[BUFFERSIZE];
int port = PORT;
char host[BUFFERSIZE] = HOST;
int playerId = -1;
void *g = game_New();
parseOpts(argc, argv, host, &port);
connect_Init(&conn, host, port);
while (1) {
if (!connect_RecvMsg(&conn, buffer)) {
fprintf(stderr, "\tnothing received. \n");
break;
}
if (!strcmp(buffer, MSGINPUT)) {
int input = renderer_Input();
connect_Send(&conn, input);
continue;
}
if (!strcmp(buffer, MSGJOIN)) {
renderer_OnMsg(MSGJOIN);
connect_RecvInt(&conn, &playerId);
if (playerId < 0) {
renderer_OnMsg(MSGJOINERROR);
break;
}
#if DEBUG
fprintf(stderr, "\tjoined with id=%d.\n", playerId);
#endif
continue;
}
if (!strcmp(buffer, MSGERROR)) {
int n = 0;
connect_RecvInt(&conn, &n);
renderer_OnError(g, n);
}
if (!strcmp(buffer, MSGEVENT)) {
int n = 0;
connect_RecvInt(&conn, &n);
renderer_OnResult(g, playerId, n);
break;
}
if (!strcmp(buffer, MSGUPDATE)) {
int n = 0;
connect_RecvInt(&conn, &n);
game_Update(g, &n);
renderer_Render(g);
continue;
}
if (!strcmp(buffer, MSGSTART)) {
renderer_OnMsg(MSGSTART);
renderer_Render(g);
continue;
}
if (!strcmp(buffer, MSGWAIT)) {
renderer_OnMsg(MSGWAIT);
continue;
}
}
game_Destroy(g);
return 0;
}