Skip to content

Commit 1bac8af

Browse files
committed
feat: implement next round notifications and card readiness checks
1 parent 9a4f00f commit 1bac8af

6 files changed

Lines changed: 116 additions & 10 deletions

File tree

include/stage.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ sfBool are_all_players_dead(wolf_t *wolf);
2222
void reset_game_run(wolf_t *wolf);
2323
void reset_game_run_seed(wolf_t *wolf, uint32_t seed);
2424
void notify_game_reset(wolf_t *wolf);
25+
void notify_next_round(wolf_t *wolf);
2526
void notify_game_card(wolf_t *wolf);
27+
void notify_card_ready(wolf_t *wolf);
2628
void notify_player_dead(wolf_t *wolf);
2729
void stage(wolf_t *wolf, player_t *player, sfEvent event);
2830
bsp_t *bsp_new(int x, int y, int w, int h);

include/types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ typedef struct wolf_s {
449449
int stage;
450450
int score;
451451
uint32_t map_seed;
452+
int card_ready[MAX_PLAYERS];
452453
} wolf_t;
453454

454455
#endif

src/game/check_end.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ void check_game_finish(wolf_t *wolf)
3535
fflush(stdout);
3636
wolf->game->state = CARD;
3737
wolf->state = GAME;
38+
memset(wolf->card_ready, 0, sizeof(wolf->card_ready));
3839
init_card(wolf, &wolf->game->inter);
3940
if (network_is_host(wolf))
4041
notify_game_card(wolf);

src/game_network.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,37 @@ static void handle_connect_packet(wolf_t *wolf, network_packet_t *pkt)
131131
wolf->lobby.nb_connected = (int)pkt->player_id + 1;
132132
}
133133

134+
static int get_network_player_count(wolf_t *wolf)
135+
{
136+
int count = wolf->lobby.nb_connected;
137+
138+
if (count < wolf->nb_others)
139+
count = wolf->nb_others;
140+
if (wolf->net.player_id != MAX_PLAYERS &&
141+
count < (int)wolf->net.player_id + 1)
142+
count = (int)wolf->net.player_id + 1;
143+
if (count < 1)
144+
count = 1;
145+
if (count > MAX_PLAYERS)
146+
count = MAX_PLAYERS;
147+
return count;
148+
}
149+
150+
static void start_next_round_when_ready(wolf_t *wolf)
151+
{
152+
int count = get_network_player_count(wolf);
153+
154+
if (!network_is_host(wolf))
155+
return;
156+
for (int i = 0; i < count; i++) {
157+
if (!wolf->card_ready[i])
158+
return;
159+
}
160+
reset_game_run(wolf);
161+
notify_next_round(wolf);
162+
wolf->state = GAME;
163+
}
164+
134165
static void handle_start_packet(wolf_t *wolf, network_packet_t *pkt)
135166
{
136167
window_t *win = wolf->window_data;
@@ -153,13 +184,24 @@ static void handle_state_packet(wolf_t *wolf, network_packet_t *pkt)
153184
wolf->state = GAME;
154185
return;
155186
}
187+
if (pkt->data[0] == 'N') {
188+
reset_game_run_seed(wolf, pkt->timestamp);
189+
wolf->state = GAME;
190+
return;
191+
}
156192
if (pkt->data[0] == 'C') {
157193
wolf->stage++;
158194
wolf->state = GAME;
159195
wolf->game->state = CARD;
196+
memset(wolf->card_ready, 0, sizeof(wolf->card_ready));
160197
init_card(wolf, &wolf->game->inter);
161198
return;
162199
}
200+
if (pkt->data[0] == 'K' && pkt->player_id < MAX_PLAYERS) {
201+
wolf->card_ready[pkt->player_id] = 1;
202+
start_next_round_when_ready(wolf);
203+
return;
204+
}
163205
if (pkt->data[0] != 'D' || pkt->player_id >= MAX_PLAYERS)
164206
return;
165207
wolf->others[pkt->player_id].alive = sfFalse;

src/stage/reset_network.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@ void notify_game_reset(wolf_t *wolf)
2323
client_send_packet(&wolf->net, &pkt);
2424
}
2525

26+
void notify_next_round(wolf_t *wolf)
27+
{
28+
network_packet_t pkt;
29+
30+
if (!wolf->connected || wolf->net.player_id == MAX_PLAYERS)
31+
return;
32+
if (wolf->map_seed == 0)
33+
wolf->map_seed = (uint32_t)time(NULL);
34+
memset(&pkt, 0, sizeof(pkt));
35+
pkt.type = PKT_GAME_STATE;
36+
pkt.timestamp = wolf->map_seed;
37+
pkt.data[0] = 'N';
38+
client_send_packet(&wolf->net, &pkt);
39+
}
40+
2641
void notify_game_card(wolf_t *wolf)
2742
{
2843
network_packet_t pkt;
@@ -35,6 +50,18 @@ void notify_game_card(wolf_t *wolf)
3550
client_send_packet(&wolf->net, &pkt);
3651
}
3752

53+
void notify_card_ready(wolf_t *wolf)
54+
{
55+
network_packet_t pkt;
56+
57+
if (!wolf->connected || wolf->net.player_id == MAX_PLAYERS)
58+
return;
59+
memset(&pkt, 0, sizeof(pkt));
60+
pkt.type = PKT_GAME_STATE;
61+
pkt.data[0] = 'K';
62+
client_send_packet(&wolf->net, &pkt);
63+
}
64+
3865
void notify_player_dead(wolf_t *wolf)
3966
{
4067
network_packet_t pkt;

src/stage/stage.c

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,56 @@ static void hoover_card(window_t *win, intermission_t *inter)
7171
}
7272
}
7373

74-
static void restart_game_as_new_game(wolf_t *wolf)
74+
static int get_card_player_count(wolf_t *wolf)
75+
{
76+
int count = wolf->lobby.nb_connected;
77+
78+
if (count < wolf->nb_others)
79+
count = wolf->nb_others;
80+
if (wolf->net.player_id != MAX_PLAYERS &&
81+
count < (int)wolf->net.player_id + 1)
82+
count = (int)wolf->net.player_id + 1;
83+
if (count < 1)
84+
count = 1;
85+
if (count > MAX_PLAYERS)
86+
count = MAX_PLAYERS;
87+
return count;
88+
}
89+
90+
static sfBool are_cards_ready(wolf_t *wolf)
91+
{
92+
int count = get_card_player_count(wolf);
93+
94+
for (int i = 0; i < count; i++) {
95+
if (!wolf->card_ready[i])
96+
return sfFalse;
97+
}
98+
return sfTrue;
99+
}
100+
101+
static void start_next_round(wolf_t *wolf)
75102
{
76-
wolf->score = 0;
77-
wolf->stage = 0;
78103
reset_game_run(wolf);
79104
if (network_is_host(wolf))
80-
notify_game_reset(wolf);
105+
notify_next_round(wolf);
81106
wolf->game->state = STAGE;
82107
sfMouse_setPositionRenderWindow((sfVector2i){wolf->window_data->width / 2,
83108
wolf->window_data->height / 2}, wolf->window_data->window);
84109
sfRenderWindow_setMouseCursorVisible(wolf->window_data->window, sfFalse);
85110
}
86111

112+
static void mark_cards_done(wolf_t *wolf)
113+
{
114+
if (!wolf->connected || wolf->net.player_id == MAX_PLAYERS) {
115+
start_next_round(wolf);
116+
return;
117+
}
118+
wolf->card_ready[wolf->net.player_id] = 1;
119+
notify_card_ready(wolf);
120+
if (network_is_host(wolf) && are_cards_ready(wolf))
121+
start_next_round(wolf);
122+
}
123+
87124
static void handle_card_round_end(wolf_t *wolf, window_t *win,
88125
intermission_t *inter)
89126
{
@@ -94,9 +131,7 @@ static void handle_card_round_end(wolf_t *wolf, window_t *win,
94131
sfClock_restart(inter->clock);
95132
return;
96133
}
97-
if (wolf->connected && !network_is_host(wolf))
98-
return;
99-
restart_game_as_new_game(wolf);
134+
mark_cards_done(wolf);
100135
}
101136

102137
static void click_card(wolf_t *wolf, window_t *win, intermission_t *inter)
@@ -138,9 +173,7 @@ static void select_card_by_key(wolf_t *wolf, intermission_t *inter,
138173
sfClock_restart(inter->clock);
139174
return;
140175
}
141-
if (wolf->connected && !network_is_host(wolf))
142-
return;
143-
restart_game_as_new_game(wolf);
176+
mark_cards_done(wolf);
144177
}
145178

146179
void card_action(wolf_t *wolf, player_t *player, sfEvent event)

0 commit comments

Comments
 (0)