feat: complete human-vs-ESP32 gameplay and cumulative statistics

This commit is contained in:
2026-08-29 23:33:27 +03:00
parent 650ade2726
commit c8e0c9168b
14 changed files with 269 additions and 19 deletions
+31 -2
View File
@@ -44,6 +44,8 @@ static void record_cumulative(game_lifecycle_t *lifecycle) {
++total->games;
total->shots += match->shots;
total->hits += match->hits;
total->misses += match->misses;
total->ships_sunk += match->ships_sunk;
if (lifecycle->game.state.winner == player) ++total->wins;
else ++total->losses;
}
@@ -56,8 +58,14 @@ static lifecycle_result_t start_current_game(game_lifecycle_t *lifecycle) {
game_engine_init(&lifecycle->game);
lifecycle->game.state.game_id = game_id;
lifecycle->game.state.version = version;
return game_result(game_engine_start(&lifecycle->game, game_id, mode,
&(fleet_generator_t){.random = lifecycle->random}));
const lifecycle_result_t result = game_result(game_engine_start(&lifecycle->game, game_id, mode,
&(fleet_generator_t){.random = lifecycle->random}));
if (result != LIFECYCLE_RESULT_OK || mode != MODE_BOT) return result;
bot_player_init(&lifecycle->bot, lifecycle->random, lifecycle->bot_scheduler);
if (lifecycle->game.state.current_player == 1U && lifecycle->bot_scheduler.schedule_after_ms != NULL) {
(void)bot_player_schedule_turn(&lifecycle->bot);
}
return LIFECYCLE_RESULT_OK;
}
void game_lifecycle_init(game_lifecycle_t *lifecycle, random_source_t random) {
@@ -70,6 +78,11 @@ void game_lifecycle_init(game_lifecycle_t *lifecycle, random_source_t random) {
session_manager_init(&lifecycle->sessions);
}
void game_lifecycle_set_bot_scheduler(game_lifecycle_t *lifecycle, scheduler_t scheduler) {
if (lifecycle == NULL) return;
lifecycle->bot_scheduler = scheduler;
}
lifecycle_result_t game_lifecycle_join(game_lifecycle_t *lifecycle, role_t requested_role, const char *name,
uint8_t *session_index) {
if (lifecycle == NULL) return LIFECYCLE_RESULT_UNAUTHORIZED;
@@ -134,6 +147,8 @@ lifecycle_result_t game_lifecycle_shot(game_lifecycle_t *lifecycle, uint8_t sess
if (!game_id_matches(lifecycle, game_id)) return LIFECYCLE_RESULT_STALE_GAME;
const lifecycle_result_t outcome = game_result(game_engine_shot(&lifecycle->game, player, coordinate, result));
if (outcome == LIFECYCLE_RESULT_OK && lifecycle->game.state.phase == PHASE_FINISHED) record_cumulative(lifecycle);
else if (outcome == LIFECYCLE_RESULT_OK && lifecycle->game.state.mode == MODE_BOT && lifecycle->game.state.current_player == 1U &&
lifecycle->bot_scheduler.schedule_after_ms != NULL) (void)bot_player_schedule_turn(&lifecycle->bot);
return outcome;
}
@@ -170,5 +185,19 @@ lifecycle_result_t game_lifecycle_abort(game_lifecycle_t *lifecycle, uint8_t ses
lifecycle->game.state.game_id = next_game_id;
lifecycle->game.state.version = version;
lifecycle->bot_reserved = false;
bot_player_cancel_turn(&lifecycle->bot);
return LIFECYCLE_RESULT_OK;
}
bool game_lifecycle_bot_take_turn(game_lifecycle_t *lifecycle) {
if (lifecycle == NULL || lifecycle->game.state.phase != PHASE_IN_PROGRESS || lifecycle->game.state.mode != MODE_BOT ||
lifecycle->game.state.current_player != 1U) return false;
coordinate_t coordinate = {0};
shot_result_t result = {0};
if (!bot_player_next_shot(&lifecycle->bot, &coordinate) ||
game_engine_shot(&lifecycle->game, 1U, coordinate, &result) != GAME_RESULT_OK) return false;
bot_player_record_result(&lifecycle->bot, coordinate, &(bot_shot_result_t){.hit = result.hit, .sunk = result.sunk});
if (lifecycle->game.state.phase == PHASE_FINISHED) record_cumulative(lifecycle);
else if (lifecycle->game.state.current_player == 1U) (void)bot_player_schedule_turn(&lifecycle->bot);
return true;
}