X-Git-Url: https://plomlompom.com/repos/foo.html?a=blobdiff_plain;f=src%2Fserver%2Frun.c;h=414549227f963db1d3c21135cdfe51891cfb24de;hb=0438f2fc5df337e4264103a86c1765ace9c6565a;hp=540f025cc7c1eb7792be1c35c3bd0bffe8189f67;hpb=65efdbea2c5978de283aab627ca62f5a2c983353;p=plomrogue diff --git a/src/server/run.c b/src/server/run.c index 540f025..4145492 100644 --- a/src/server/run.c +++ b/src/server/run.c @@ -3,16 +3,17 @@ #include "run.h" #include /* NULL */ #include /* uint8_t, uint16_t, uint32_t */ -#include /* FILE, sprintf() */ +#include /* FILE, sprintf(), fflush() */ #include /* free() */ -#include /* strlen(), strncmp(), atoi() */ +#include /* strlen(), strcmp() strncmp(), atoi() */ #include /* access() */ #include "../common/readwrite.h" /* try_fopen(), try_fcose(), try_fwrite(), * try_fgets(), try_fclose_unlink_rename(), - * textfile_sizes(), try_fputc() + * textfile_width(), try_fputc() */ -#include "../common/rexit.h" /* exit_trouble() */ -#include "ai.h" /* pretty_dumb_ai() */ +#include "../common/rexit.h" /* exit_trouble(), exit_err() */ +#include "ai.h" /* ai() */ +#include "cleanup.h" /* unset_cleanup_flag() */ #include "init.h" /* remake_world() */ #include "io.h" /* io_round() */ #include "map_object_actions.h" /* get_moa_id_by_name() */ @@ -33,6 +34,13 @@ static void turn_over(); */ static uint8_t apply_player_command(char * msg, char * command_name); +/* Compares first line of file at world.path_out to world.server_test, aborts if + * they don't match, but not before unsetting the flags deleting files in the + * server directory, for in that case those must be assumed to belong to another + * server process. + */ +static void server_test(); + static void turn_over() @@ -40,7 +48,6 @@ static void turn_over() struct MapObj * player = get_player(); struct MapObj * map_object = player; uint16_t start_turn = world.turn; - uint8_t first_round = 1; while ( 0 < player->lifepoints || (0 == player->lifepoints && start_turn == world.turn)) { @@ -51,15 +58,14 @@ static void turn_over() } if (0 < map_object->lifepoints) { - if (0 == first_round && 0 == map_object->progress) + if (0 == map_object->command) { if (map_object == player) { break; } - pretty_dumb_ai(map_object); + ai(map_object); } - first_round = 0; map_object->progress++; struct MapObjAct * moa = world.map_obj_acts; while (moa->id != map_object->command) @@ -69,6 +75,7 @@ static void turn_over() if (map_object->progress == moa->effort) { moa->func(map_object); + map_object->command = 0; map_object->progress = 0; } } @@ -93,6 +100,27 @@ static uint8_t apply_player_command(char * msg, char * command_name) +static void server_test() +{ + char * f_name = "server_test()"; + char test[10 + 1 + 10 + 1 + 1]; + FILE * file = try_fopen(world.path_out, "r", f_name); + try_fgets(test, 10 + 10 + 1 + 1, file, f_name); + try_fclose(file, f_name); + if (strcmp(test, world.server_test)) + { + unset_cleanup_flag(CLEANUP_WORLDSTATE); + unset_cleanup_flag(CLEANUP_OUT); + unset_cleanup_flag(CLEANUP_IN); + char * msg = "Server test string in server output file does not match. " + "This indicates that the current server process has been " + "superseded by another one."; + exit_err(1, msg); + } +} + + + extern void obey_msg(char * msg, uint8_t do_record) { char * f_name = "obey_msg()"; @@ -117,7 +145,7 @@ extern void obey_msg(char * msg, uint8_t do_record) if (!access(world.path_record, F_OK)) { FILE * file_read = try_fopen(world.path_record, "r", f_name); - uint32_t linemax = textfile_sizes(file_read, NULL); + uint32_t linemax = textfile_width(file_read); char line[linemax + 1]; while (try_fgets(line, linemax + 1, file_read, f_name)) { @@ -138,6 +166,7 @@ extern uint8_t io_loop() char * f_name = "io_loop()"; while (1) { + server_test(); char * msg = io_round(); if (NULL == msg) { @@ -152,6 +181,14 @@ extern uint8_t io_loop() free(msg); return 1; } + if (!strcmp("PING", msg)) + { + free(msg); + char * pong = "PONG\n"; + try_fwrite(pong, strlen(pong), 1, world.file_out, f_name); + fflush(world.file_out); + continue; + } if (world.replay) { free(msg);