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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "snapshot.h"
int
dp_snapshot_init(struct snapshot *s, int32_t players_len, int32_t balls_len)
{
if (!s)
return -1;
if (!(s->players = calloc(players_len, sizeof(*s->players))))
return -1;
if (!(s->balls = calloc(balls_len, sizeof(*s->balls))))
{
free(s->players);
return -1;
}
s->players_len = players_len;
s->balls_len = balls_len;
return 0;
}
void
dp_snapshot_deinit(struct snapshot *s)
{
if (!s)
return;
if (s->players)
{
free(s->players);
s->players = NULL;
}
if (s->balls)
{
free(s->balls);
s->balls = NULL;
}
}
int
dp_snapshot_put_player(struct snapshot *s, int32_t id, const struct player *p)
{
if (!s || !p)
return -1;
if (id >= s->players_len)
return -1;
s->players[id] = *p;
s->players[id].state = STATE_ACTIVE;
return 0;
}
struct player *
dp_snapshot_get_player(struct snapshot *s, int32_t id)
{
struct player *p;
if (!s)
return NULL;
if (id >= s->players_len)
return NULL;
p = &s->players[id];
if (p->state == STATE_INACTIVE)
return NULL;
return p;
}
int
dp_snapshot_remove_player(struct snapshot *s, int32_t id)
{
struct player *p;
if (!s)
return -1;
if (id >= s->players_len)
return -1;
p = dp_snapshot_get_player(s, id);
memset(p, 0, sizeof(*p));
p->state = STATE_REMOVED;
return 0;
}
int
dp_snapshot_set_player_input(struct snapshot *s, int32_t id, uint8_t input)
{
struct player *p;
if (!(p = dp_snapshot_get_player(s, id)))
return -1;
p->input = input;
return 0;
}
int
dp_snapshot_put_ball(struct snapshot *s, int32_t id, const struct ball *b)
{
if (!s || !b)
return -1;
if (id >= s->balls_len)
return -1;
s->balls[id] = *b;
s->balls[id].state = STATE_ACTIVE;
return 0;
}
struct ball *
dp_snapshot_get_ball(struct snapshot *s, int32_t id)
{
struct ball *b;
if (!s)
return NULL;
if (id >= s->balls_len)
return NULL;
b = &s->balls[id];
if (b->state == STATE_INACTIVE)
return NULL;
return b;
}
int
dp_snapshot_remove_ball(struct snapshot *s, int32_t id)
{
struct ball *b;
if (!s)
return -1;
if (id >= s->balls_len)
return -1;
b = dp_snapshot_get_ball(s, id);
memset(b, 0, sizeof(*b));
b->state = STATE_REMOVED;
return 0;
}
|