home · contact · privacy
Server: Some refactoring.
[plomrogue] / src / server / run.c
1 /* src/server/run.c */
2
3 #define _POSIX_C_SOURCE 200809L
4 #include "run.h"
5 #include <stddef.h> /* NULL */
6 #include <stdint.h> /* uint8_t, uint16_t, uint32_t */
7 #include <stdio.h> /* FILE, printf(), fflush() */
8 #include <stdlib.h> /* free() */
9 #include <string.h> /* strlen(), strcmp(), strncmp(), strdup() */
10 #include <time.h> /* time_t, time() */
11 #include <unistd.h> /* access() */
12 #include "../common/parse_file.h" /* set_err_line_options(), token_from_line(),
13                                    * err_line(), err_line_inc(), parse_val()
14                                    */
15 #include "../common/readwrite.h" /* try_fopen(), try_fcose(), try_fwrite(),
16                                   * try_fgets(), textfile_width(), try_fputc(),
17                                   * atomic_write_finish(), build_temp_path()
18                                   */
19 #include "../common/rexit.h" /* exit_trouble(), exit_err() */
20 #include "../common/try_malloc.h" /* try_malloc() */
21 #include "ai.h" /* ai() */
22 #include "cleanup.h" /* unset_cleanup_flag() */
23 #include "god_commands.h" /* parse_god_command_(1|2|3)arg() */
24 #include "hardcoded_strings.h" /* s */
25 #include "io.h" /* io_round(), save_world() */
26 #include "things.h" /* Thing, get_thing_action_id_by_name(), get_player() */
27 #include "world.h" /* world */
28
29
30
31 /* If "string" and "comparand" match in string, set "c_to_set" to value."  */
32 static uint8_t set_char_by_string_comparison(char * string, char * comparand,
33                                              char * c_to_set, char value);
34
35 /* Return 1 on world.exists, else 0 and err_line() appropriate error message. */
36 static uint8_t player_commands_allowed();
37
38 /* Parse player command "tok0" with no argument to player action. */
39 static uint8_t parse_player_command_0arg(char * tok0);
40
41 /* Parse player command "tok0" with one argument "tok1" to player action. */
42 static uint8_t parse_player_command_1arg(char * tok0, char * tok1);
43
44 /* Parse/apply command "tok0" (read further tokens as necessary). */
45 static uint8_t parse_command(char * tok0);
46
47 /* Compares first line of server out file to world.server_test, aborts if they
48  * don't match, but not before unsetting the flags deleting files in the server
49  * directory, for in that case those must be assumed to belong to another server
50  * process.
51  */
52 static void server_test();
53
54 /* Run the game world and its inhabitants (and their actions) until the player
55  * avatar is free to receive new commands (or is dead).
56  */
57 static void turn_over();
58
59
60
61 static uint8_t set_char_by_string_comparison(char * string, char * comparand,
62                                              char * c_to_set, char value)
63 {
64     if (!strcmp(string, comparand))
65     {
66         * c_to_set = value;
67         return 1;
68     }
69     return 0;
70 }
71
72
73
74 static uint8_t player_commands_allowed()
75 {
76     if (!world.exists)
77     {
78         return !err_line(1, "No world exists in which to run player commands.");
79     }
80     return 1;
81 }
82
83
84
85 static uint8_t parse_player_command_0arg(char * tok0)
86 {
87     struct Thing * player = get_player();
88     if (   !strcmp(tok0, s[S_CMD_WAIT]) || !strcmp(tok0, s[S_CMD_PICKUP])
89         || !strcmp(tok0, s[S_CMD_AI]))
90     {
91         if (player_commands_allowed())
92         {
93             if (!strcmp(tok0, s[S_CMD_AI]))
94             {
95                 ai(player);
96             }
97             else
98             {
99                 player->command = get_thing_action_id_by_name(tok0);
100                 player->arg = 0;
101             }
102             turn_over();
103         }
104         return 1;
105     }
106     return 0;
107 }
108
109
110
111 static uint8_t parse_player_command_1arg(char * tok0, char * tok1)
112 {
113     struct Thing * player = get_player();
114     if (   (   parse_val(tok0, tok1, s[S_CMD_DROP], '8', (char *) &player->arg)
115             || parse_val(tok0, tok1, s[S_CMD_USE], '8', (char *) &player->arg))
116         && player_commands_allowed())
117     {
118         player->command = get_thing_action_id_by_name(tok0);
119         turn_over();
120     }
121     else if (!strcmp(tok0, s[S_CMD_MOVE]) && player_commands_allowed())
122     {
123         char dir = '\0';
124         if (!(   set_char_by_string_comparison(tok1, "east",       &dir, 'd')
125               || set_char_by_string_comparison(tok1, "south-east", &dir, 'c')
126               || set_char_by_string_comparison(tok1, "south-west", &dir, 'x')
127               || set_char_by_string_comparison(tok1, "west",       &dir, 's')
128               || set_char_by_string_comparison(tok1, "north-west", &dir, 'w')
129               || set_char_by_string_comparison(tok1, "north-east", &dir, 'e')))
130         {
131             return 0;
132         }
133         player->arg = dir;
134         player->command = get_thing_action_id_by_name(tok0);
135         turn_over();
136     }
137     else
138     {
139         return 0;
140     }
141     return 1;
142 }
143
144
145
146 static uint8_t parse_command(char * tok0)
147 {
148     if (parse_player_command_0arg(tok0))
149     {
150         return 1;
151     }
152     else
153     {
154         char * tok1 = token_from_line(NULL);
155         if (tok1 && (   parse_player_command_1arg(tok0, tok1)
156                      || parse_god_command_1arg(tok0, tok1)))
157         {
158             return 1;
159         }
160         else
161         {
162             char * tok2 = token_from_line(NULL);
163             if (tok2 && parse_god_command_2arg(tok0, tok1, tok2))
164             {
165                 return 1;
166             }
167             else
168             {
169                 char * tok3 = token_from_line(NULL);
170                 if (tok2 && parse_god_command_3arg(tok0, tok1, tok2, tok3))
171                 {
172                     return 1;
173                 }
174             }
175         }
176     }
177     return 0;
178 }
179
180
181
182 static void server_test()
183 {
184     char test[10 + 1 + 10 + 1 + 1];
185     FILE * file = try_fopen(s[S_PATH_OUT], "r", __func__);
186     try_fgets(test, 10 + 10 + 1 + 1, file, __func__);
187     try_fclose(file, __func__);
188     if (strcmp(test, world.server_test))
189     {
190         unset_cleanup_flag(CLEANUP_WORLDSTATE);
191         unset_cleanup_flag(CLEANUP_OUT);
192         unset_cleanup_flag(CLEANUP_IN);
193         char * msg = "Server test string in server output file does not match. "
194                      "This indicates that the current server process has been "
195                      "superseded by another one.";
196         exit_err(1, msg);
197     }
198 }
199
200
201
202 static void turn_over()
203 {
204     struct Thing * player = get_player();
205     struct Thing * thing = player;
206     uint16_t start_turn = world.turn;
207     while (    0 < player->lifepoints
208            || (0 == player->lifepoints && start_turn == world.turn))
209     {
210         if (!thing)
211         {
212             world.turn++;
213             thing = world.things;
214         }
215         if (0 < thing->lifepoints)
216         {
217             if (0 == thing->command)
218             {
219                 if (thing == player)
220                 {
221                     break;
222                 }
223                 ai(thing);
224             }
225             thing->progress++;
226             struct ThingAction * ta = get_thing_action(thing->command);
227             if (thing->progress == ta->effort)
228             {
229                 ta->func(thing);
230                 thing->command = 0;
231                 thing->progress = 0;
232             }
233         }
234         thing = thing->next;
235     }
236 }
237
238
239
240 extern void record(char * msg, uint8_t force)
241 {
242     static FILE * file_tmp = NULL;
243     static time_t save_wait = 0;
244     static char * path_tmp;
245     if (!file_tmp)
246     {
247         path_tmp = build_temp_path(s[S_PATH_RECORD]);
248         file_tmp = try_fopen(path_tmp, "w", __func__);
249         if (!access(s[S_PATH_RECORD], F_OK))
250         {
251             FILE * file_read = try_fopen(s[S_PATH_RECORD], "r", __func__);
252             uint32_t linemax = textfile_width(file_read);
253             char * line = try_malloc(linemax + 1, __func__);
254             while (try_fgets(line, linemax + 1, file_read, __func__))
255             {
256                 try_fwrite(line, strlen(line), 1, file_tmp, __func__);
257             }
258             free(line);
259             try_fclose(file_read, __func__);
260         }
261     }
262     if (msg)
263     {
264         try_fwrite(msg, strlen(msg), 1, file_tmp, __func__);
265         try_fputc('\n', file_tmp, __func__);
266     }
267     if (force || time(NULL) > save_wait + 15)
268     {
269         save_wait = time(NULL);
270         save_world();
271         atomic_write_finish(file_tmp, s[S_PATH_RECORD], path_tmp);
272         file_tmp = NULL;
273     }
274 }
275
276
277
278 extern void obey_msg(char * msg, uint8_t do_record, uint8_t do_verbose)
279 {
280     if (world.is_verbose && do_verbose)
281     {
282         exit_trouble(-1 == printf("Input: %s\n", msg), __func__, "printf");
283     }
284     set_err_line_options("Trouble with message: ", msg, 0);
285     char * msg_copy = strdup(msg);
286     char * tok0 = token_from_line(msg_copy);
287     if (tok0)
288     {
289
290         if (parse_command(tok0))
291         {
292             if (world.exists)
293             {
294                 world.do_update = 1;
295             }
296             if (do_record)
297             {
298                 record(msg, 0);
299             }
300             char * tokplus = token_from_line(NULL);
301             err_line(!(!tokplus), "Too many arguments, ignoring overflow.");
302             free(msg_copy);
303             return;
304         }
305     }
306     err_line(1, "Unknown command/argument or bad number of tokens.");
307     free(msg_copy);
308 }
309
310
311
312 extern uint8_t io_loop()
313 {
314     while (1)
315     {
316         char * msg = io_round();
317         server_test();
318         if (!msg)
319         {
320             continue;
321         }
322         if (world.is_verbose)
323         {
324             exit_trouble(-1 == printf("Input: %s\n", msg), __func__, "printf");
325         }
326         if (!strcmp("QUIT", msg))
327         {
328             free(msg);
329             return 1;
330         }
331         if (!strcmp("PING", msg))
332         {
333             free(msg);
334             char * pong = "PONG\n";
335             try_fwrite(pong, strlen(pong), 1, world.file_out, __func__);
336             fflush(world.file_out);
337             continue;
338         }
339         if (world.replay)
340         {
341             free(msg);
342             return 0;
343         }
344         obey_msg(msg, 1, 0);
345         free(msg);
346     }
347 }