Skip to content

Commit

Permalink
ZappyServer: Add and fix tests for cmd_edi
Browse files Browse the repository at this point in the history
  • Loading branch information
romainpanno committed Jun 19, 2023
1 parent 3c12331 commit aa75bb8
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 47 deletions.
2 changes: 1 addition & 1 deletion src/SERVER/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ tests_run: fclean $(T_OBJ)
tests_run: $(LIBS_DIR)/tinylibc/libtinylibc.a
tests_run: $(LIBS_DIR)/circularbuffer/libcircularbuffer.a
$(CC) $(T_OBJ) -o $(TTARGET) $(LDFLAGS) $(CFLAGS)
./$(TTARGET) || exit 1
./$(TTARGET) -j1 || exit 1
gcovr --exclude-directories tests || true
gcovr --exclude-directories tests --branch || true
true
Expand Down
8 changes: 4 additions & 4 deletions src/SERVER/include/broadcast_events.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ bool cmd_pgt(ntw_t *ntw, ntw_client_t *cl, action_t *action);
/**
** @brief broadcast event pdi (player died)
** @param ntw
** @param cl
** @param id
** @return
**/
bool cmd_pdi(ntw_t *ntw, ntw_client_t *cl);
bool cmd_pdi(ntw_t *ntw, int id);

/**
** @brief broadcast event enw (new egg)
Expand All @@ -87,10 +87,10 @@ bool cmd_ebo(ntw_t *ntw, ntw_client_t *cl);
/**
** @brief broadcast event edi (egg died)
** @param ntw
** @param cl
** @param id
** @return
**/
bool cmd_edi(ntw_t *ntw, ntw_client_t *cl);
bool cmd_edi(ntw_t *ntw, int id);

/**
** @brief broadcast event smg (server message)
Expand Down
8 changes: 3 additions & 5 deletions src/SERVER/src/loop/loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ static void update_trantoriens_available_food(ntw_t *ntw,
if (trantorien == NULL) {
continue;
}
if (new_freq == true) {
update_food(ntw, trantorien, NULL, true);
}
update_food(ntw, trantorien, NULL, true);
}
}

Expand All @@ -102,9 +100,9 @@ bool loop(zappy_t *zappy, bool new_freq)
}
status = update_client(zappy, cl, new_freq) & status;
update_clients_connections(zappy->ntw);
if (L_DATAT(client_t *, client)->type == AI && new_freq == true) {
if (L_DATAT(client_t *, cl)->type == AI && new_freq == true) {
update_food(zappy->ntw,
L_DATAT(client_t *, client)->cl.ai.trantorien, cl, false);
L_DATAT(client_t *, cl)->cl.ai.trantorien, cl, false);
}
}
update_trantoriens_available_food(zappy->ntw,
Expand Down
13 changes: 4 additions & 9 deletions src/SERVER/src/loop/state_connected/graphic_cmd/cmd_edi.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,14 @@
#include "internal.h"
#include "client.h"

bool cmd_edi(ntw_t *ntw, ntw_client_t *cl)
bool cmd_edi(ntw_t *ntw, int id)
{
client_t *client = NULL;
char buff[512] = {0};

if (ntw == NULL || cl == NULL) {
if (ntw == NULL) {
return false;
}
client = L_DATA(cl);
if (client == NULL) {
return false;
}
snprintf(buff, 511, "edi %d\n", client->id);
circular_buffer_write(cl->write_to_outside, buff);
snprintf(buff, 511, "edi %d\n", id);
broadcast_graphic(ntw, buff);
return true;
}
8 changes: 3 additions & 5 deletions src/SERVER/src/loop/state_connected/graphic_cmd/cmd_pdi.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@
#include "internal.h"
#include "client.h"

bool cmd_pdi(ntw_t *ntw, ntw_client_t *cl)
bool cmd_pdi(ntw_t *ntw, int id)
{
char buff[512] = {0};
client_t *client = NULL;

if (ntw == NULL || cl == NULL) {
if (ntw == NULL) {
return false;
}
client = L_DATA(cl);
snprintf(buff, 511, "pdi %d\n", client->id);
snprintf(buff, 511, "pdi %d\n", id);
broadcast_graphic(ntw, buff);
return true;
}
11 changes: 4 additions & 7 deletions src/SERVER/src/loop/update_food.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,33 @@
#include "client.h"
#include "broadcast_events.h"

static void send_event_death(ntw_t *ntw, ntw_client_t *cl, bool is_an_egg)
static void send_event_death(ntw_t *ntw, int id, bool is_an_egg)
{
if (is_an_egg) {
cmd_edi(ntw, cl);
cmd_edi(ntw, id);
} else {
cmd_pdi(ntw, cl);
cmd_pdi(ntw, id);
}
}

void update_food(ntw_t *ntw, trantorien_t *trantorien,
ntw_client_t *cl, bool is_an_egg)
{
puts("update food");j
if (trantorien == NULL || trantorien->alive == false) {
return;
}
trantorien->food_stack_freq += 1;
printf("food stack freq: %d\n", trantorien->food_stack_freq);
if (trantorien->food_stack_freq < MAX_FOOD_FREQ) {
return;
}
printf("ca update la food\n");
trantorien->food_stack_freq = 0;
if (trantorien->ressources[FOOD] > 0) {
trantorien->ressources[FOOD]--;
} else {
trantorien->alive = false;
if (cl) {
circular_buffer_write(cl->write_to_outside, "dead\n");
send_event_death(ntw, cl, is_an_egg);
}
send_event_death(ntw, trantorien->id, is_an_egg);
}
}
31 changes: 15 additions & 16 deletions tests/SERVER/loop/events/event_edi_pdi.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ static void set_up_tests(zappy_t **zappy, int nb_client, int port,
ntw_client_t **graphic)
{
static args_t args = {
.clients_per_teams = 0,
.teams_name = NULL,
.freq = 1000,
.height = 10,
.width = 10,
.is_ok = true,
.port = 0,
.clients_per_teams = 0,
.teams_name = NULL,
.freq = 1000,
.height = 10,
.width = 10,
.is_ok = true,
.port = 0,
};
args.port = port;
args.teams_name = list_create();
Expand Down Expand Up @@ -92,13 +92,13 @@ Test(loop_event_pdi, basic_pdi)
while (circular_buffer_is_read_ready(client_e->write_to_outside) == false) {
cr_assert_eq(loop(zappy, true), false);
}
printf("result incantation = '%s'\n", circular_buffer_read(client_e->write_to_outside));
cr_assert_str_eq(circular_buffer_read(client_e->write_to_outside), "Elevation underway\n");
snprintf(res, 511, "pic %d %d %d %d\n", c_e->cl.ai.trantorien->x, c_e->cl.ai.trantorien->y, c_e->cl.ai.trantorien->level, c_e->cl.ai.trantorien->id);
cr_assert_str_eq(circular_buffer_read(graph->write_to_outside), res);
while (circular_buffer_is_read_ready(client_e->write_to_outside) == false) {
cr_assert_eq(loop(zappy, true), false);
}
printf("result incantation = '%s'\n", circular_buffer_read(client_e->write_to_outside));
cr_assert_str_eq(circular_buffer_read(client_e->write_to_outside), "dead\n");
memset(res, 0, 512);
snprintf(res, 511, "pdi %d\n", c_e->cl.ai.trantorien->id);
cr_assert_str_eq(circular_buffer_read(graph->write_to_outside), res);
Expand All @@ -115,18 +115,17 @@ Test(loop_event_edi_pdi, basic_edi)
cr_assert_not_null(client_e);
client_t *c_e = L_DATA(client_e);
cr_assert_not_null(c_e);
c_e->cl.ai.trantorien->ressources[FOOD] = 1;
c_e->cl.ai.trantorien->ressources[FOOD] = 10000;
zappy->map->tiles[0].ressources[LINEMATE] = 1;
c_e->cl.ai.trantorien->x = 0;
c_e->cl.ai.trantorien->y = 0;
circular_buffer_write(client_e->read_from_outside, "Fork\n");
while (circular_buffer_is_read_ready(client_e->write_to_outside) == false) {
cr_assert_eq(loop(zappy, true), false);
}
printf("result fork = '%s'\n", circular_buffer_read(client_e->write_to_outside));
cr_assert_str_eq(circular_buffer_read(client_e->write_to_outside), "ok\n");
snprintf(res, 511, "pfk %d\n", c_e->cl.ai.trantorien->id);
cr_assert_str_eq(circular_buffer_read(graph->write_to_outside), res);
printf("len trantoriens available = %d\n", zappy->trantoriens_available->len);
trantorien_t *trantorien = L_DATA(zappy->trantoriens_available->end);
cr_assert_not_null(trantorien);
memset(res, 0, 512);
Expand All @@ -137,18 +136,18 @@ Test(loop_event_edi_pdi, basic_edi)
while (circular_buffer_is_read_ready(client_e->write_to_outside) == false) {
cr_assert_eq(loop(zappy, true), false);
}
printf("result incantation = '%s'\n", circular_buffer_read(client_e->write_to_outside));
cr_assert_str_eq(circular_buffer_read(client_e->write_to_outside), "Elevation underway\n");
memset(res, 0, 512);
snprintf(res, 511, "pic %d %d %d %d\n", c_e->cl.ai.trantorien->x, c_e->cl.ai.trantorien->y, c_e->cl.ai.trantorien->level, c_e->cl.ai.trantorien->id);
cr_assert_str_eq(circular_buffer_read(graph->write_to_outside), res);
while (circular_buffer_is_read_ready(client_e->write_to_outside) == false) {
cr_assert_eq(loop(zappy, true), false);
}
printf("result incantation = '%s'\n", circular_buffer_read(client_e->write_to_outside));
cr_assert_str_eq(circular_buffer_read(client_e->write_to_outside), "Current level: 2\n");
memset(res, 0, 512);
snprintf(res, 511, "pdi %d\n", c_e->cl.ai.trantorien->id);
snprintf(res, 511, "edi %d\n", trantorien->id);
cr_assert_str_eq(circular_buffer_read(graph->write_to_outside), res);
memset(res, 0, 512);
snprintf(res, 511, "edi %d\n", trantorien->id);
snprintf(res, 511, "pie %d %d %d\n", c_e->cl.ai.trantorien->x, c_e->cl.ai.trantorien->y, c_e->cl.ai.trantorien->level);
cr_assert_str_eq(circular_buffer_read(graph->write_to_outside), res);
}

0 comments on commit aa75bb8

Please sign in to comment.