fixed scores menu
[rlserver.git] / menu.c
blobe4d67f84c7fab5b8f64f36d7b61af888b96aa5b4
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include <string.h>
5 #include <sys/socket.h>
6 #include <sys/stat.h>
7 #include <pthread.h>
8 #include <time.h>
9 #include "telnet.h"
10 #include "users.h"
11 #include "games.h"
12 #include "server.h"
13 #include "run.h"
14 #include "log.h"
17 extern server_config config;
19 extern session_info *sessionlist;
20 extern pthread_mutex_t sessionlist_mutex;
22 #define BUF_SIZE 1 * 1024
25 // return 1 if enter is pressed
26 static int input (char c, char *str, int *pos, int len, char *out, int *out_pos)
28 // enter
29 if (c == '\n')
31 str[*pos] = 0;
32 out[*out_pos] = 0;
33 return 1;
36 // backspace
37 if (c == 8 || c == 127)
39 if (*pos > 0) *pos -= 1;
41 out[*out_pos] = 8;
42 if (*out_pos < len) *out_pos += 1;
44 else if (*pos < len - 1)
46 str[*pos] = c;
47 if (*pos < len) *pos += 1;
49 out[*out_pos] = c;
50 if (*out_pos < len) *out_pos += 1;
53 return 0;
58 static int recv_input (const session_info *sess, char *inp, int len)
60 char buf[BUF_SIZE], out[BUF_SIZE];
61 int pos = 0;
64 while (1)
66 int opos = 0;
68 int n = recv(sess->socket, buf, BUF_SIZE, 0);
69 if (n == -1) return -1;
71 n = translate_telnet_input(buf, n, NULL);
72 if (n > 0)
74 int i;
75 for (i = 0; i < n; i++)
77 if (input(buf[i], inp, &pos, len, out, &opos))
79 return pos;
84 // echo
85 if (send(sess->socket, out, opos, 0) == -1) return -1;
91 static int print_and_wait (const session_info *sess, const char *msg)
93 char buf[1];
96 if (send(sess->socket, msg, strlen(msg) + 1, 0) == -1) return -1;
98 // wait for a keypress
99 int n = recv(sess->socket, buf, 1, 0);
100 if (n == -1) return -1;
102 return 0;
106 #define REG_INPUTS 3
108 static int register_menu (session_info *sess)
110 char data[REG_INPUTS][BUF_SIZE];
111 int id, r = 0;
112 const char *const prompts[REG_INPUTS] =
114 "\r\nUser name (3-16 chars, alphabet and digits only, start with a letter): ",
115 "\r\nPassword (4-16 chars, it is INSECURE, so do not use the same as email): ",
116 "\r\nEmail (optional): "
118 int lengths[REG_INPUTS][2] = { {MIN_USER_NAME_LEN, USER_NAME_LEN}, {MIN_USER_PASS_LEN, USER_PASS_LEN}, {0, USER_EMAIL_LEN} };
119 char buf[BUF_SIZE];
120 user usr;
123 // ask user for data
124 while (r < REG_INPUTS)
126 int len;
128 if (send(sess->socket, prompts[r], strlen(prompts[r]), 0) == -1) return -1;
130 len = recv_input(sess, data[r], lengths[r][1]);
131 if (len < 0) return -1;
133 if (len < lengths[r][0])
135 int l = sprintf(buf, "\r\nminimum %d symbols, try again: ", lengths[r][0]);
136 if (send(sess->socket, buf, l, 0) == -1) return -1;
137 continue;
140 r++;
144 // try to register using the data
145 id = add_user(&usr, data[0], data[1], data[2]);
146 if (id > 0)
148 int l;
150 sess->user_id = usr.id;
151 strcpy (sess->user_name, usr.name);
153 write_log (LOG_DEBUG, "registered user %s", usr.name);
155 l = sprintf(buf, "\r\nRegistration succeded\r\n");
157 if (send(sess->socket, buf, l, 0) == -1) return -1;
159 else
161 int l;
162 if (id == -1)
164 l = sprintf(buf, "\r\nInvalid name\r\n");
166 else if (id == -4)
168 l = sprintf(buf, "\r\nUser with such name already exists\r\n");
170 else
172 write_log (LOG_ERROR, "user registration failed, error %d", id);
174 l = sprintf(buf, "\r\nRegistration failed\r\n");
177 if (send(sess->socket, buf, l, 0) == -1) return -1;
180 // wait for a keypress
181 int n = recv(sess->socket, buf, BUF_SIZE, 0);
182 if (n == -1) return -1;
184 return 0;
189 static int login_menu (session_info *sess)
191 const char login_prompt[] = "\r\nUser name: ";
192 const char pass_prompt[] = "\r\nPassword: ";
193 char name[USER_NAME_LEN], pass[USER_PASS_LEN];
194 int id = 0;
195 user usr;
198 if (send(sess->socket, login_prompt, sizeof(login_prompt) - 1, 0) == -1) return -1;
199 if (recv_input(sess, name, USER_NAME_LEN) < 0) return -1;
201 if (send(sess->socket, pass_prompt, sizeof(pass_prompt) - 1, 0) == -1) return -1;
202 if (recv_input(sess, pass, USER_PASS_LEN) < 0) return -1;
204 // check user name and password
205 if (check_user_login(&usr, name, pass) < 0)
207 if (print_and_wait(sess, "\r\nLogin failed\r\n") == -1) return -1;
208 return -2;
211 // disallow simultaneous connections
212 if (user_is_connected(usr.id))
214 if (print_and_wait(sess, "\r\nYou are already logged in\r\n") == -1) return -1;
215 return -3;
218 sess->user_id = usr.id;
219 strcpy (sess->user_name, usr.name);
220 write_log (LOG_DEBUG, "user %s logs in", usr.name);
222 return id;
227 static int passwd_menu (const session_info *sess)
229 const char old_prompt[] = "\r\nOld Password: ";
230 const char new_prompt[] = "\r\nNew Password: ";
231 char pass[USER_PASS_LEN];
232 user usr;
233 int len;
236 // ask for old password first
237 if (send(sess->socket, old_prompt, sizeof(old_prompt) - 1, 0) == -1) return -1;
238 if (recv_input(sess, pass, USER_PASS_LEN) < 0) return -1;
240 // check old password
241 if (check_user_login(&usr, sess->user_name, pass) < 0)
243 if (print_and_wait(sess, "\r\nWrong password\r\n") == -1) return -1;
244 return -2;
247 // ask for new password
248 if (send(sess->socket, new_prompt, sizeof(new_prompt) - 1, 0) == -1) return -1;
249 len = recv_input(sess, pass, USER_PASS_LEN);
250 if (len < 0) return -1;
252 if (len < MIN_USER_PASS_LEN)
254 if (print_and_wait(sess, "\r\nNew password is too short\r\n") == -1) return -1;
255 return -3;
258 if (change_password(sess->user_id, pass) == 0)
260 if (print_and_wait(sess, "\r\nPassword has been changed\r\n") == -1) return -1;
263 return 0;
268 static int observe (session_info *sess)
270 uint8_t buf[BUF_SIZE];
273 while (1)
275 int n = recv(sess->socket, buf, BUF_SIZE, 0);
276 if (n == -1) return -1;
278 if (buf[0] == 'q') break;
281 return 0;
286 static int observer_menu (session_info *sess)
288 char header[] = TELNET_CLS "\tGame\t\t\t\tUser\t\t Idle\t\tTerminal\r\n";
289 char buf[BUF_SIZE];
290 session_info *s;
291 int n, l, gn, count = 0;
292 time_t now;
294 char inp[20];
297 if (send(sess->socket, header, sizeof(header) - 1, 0) == -1) return -1;
299 pthread_mutex_lock (&sessionlist_mutex);
300 now = time(NULL);
301 for (s = sessionlist; s != NULL; s = s->next)
303 int idle;
305 if (s->game[0] == 0 || s->observer_count == -1) continue;
307 idle = now - s->last_activity;
309 l = sprintf(buf, "%3d\t%s", count + 1, s->game);
310 for (; l < 4 * 8; l++) buf[l] = ' ';
312 l += sprintf(&buf[l], "\t%s", s->user_name);
313 for (; l < 6 * 8; l++) buf[l] = ' ';
315 l += sprintf(&buf[l], "\t%3dm %02ds\t%dx%d\r\n", idle / 60, idle % 60, s->term_wid, s->term_hgt);
316 if (send(sess->socket, buf, l, 0) == -1) return -1;
317 count++;
319 pthread_mutex_unlock (&sessionlist_mutex);
320 l = sprintf(buf, "\r\n%d games total\r\n\r\n"
321 "Input number to observe or anything else to return to the main menu\r\n"
322 "You can press 'q' anytime to stop observing\r\n", count);
323 if (send(sess->socket, buf, l, 0) == -1) return -1;
326 // get number
327 n = recv_input(sess, inp, 20);
328 if (n == -1) return -1;
330 gn = atoi(inp);
331 if (gn > 0 && gn <= count)
333 // try to observe the specified game
334 if (add_observer(gn - 1, sess->socket))
336 int r = observe(sess);
337 stop_observing (sess);
338 if (r == -1) return -1;
342 return 0;
347 // if score == 1, display highscore list and exit rather than playing
348 static int play_menu (session_info *sess, int score)
350 const char msg[] = TELNET_CLS "Select a game by entering its id\r\n";
351 game_info *gamelist, *g;
352 char buf[BUF_SIZE];
353 int n;
354 char inp[20];
357 gamelist = load_gamelist();
359 if (send(sess->socket, msg, sizeof(msg), 0) == -1)
361 free_gamelist (gamelist);
362 return -1;
365 for (g = gamelist; g != NULL; g = g->next)
367 if (score && !g->cmd_score[0]) continue;
369 int l = sprintf(buf, "%8s: %s\r\n", g->id, g->name);
371 if (send(sess->socket, buf, l, 0) == -1)
373 free_gamelist (gamelist);
374 return -1;
379 // get id
380 n = recv_input(sess, inp, 20);
381 if (n == -1) return -1;
383 // find and run game with such id
384 for (g = gamelist; g != NULL; g = g->next)
386 if (score && !g->cmd_score[0]) continue;
388 if (strcmp(inp, g->id) == 0)
390 // hack for displaying score
391 if (score)
393 sess->observer_count = -1;
394 strcpy (g->cmd, g->cmd_score);
397 run_game (sess, g);
398 free_gamelist (gamelist);
400 if (print_and_wait(sess, "\r\nYou have left the game\r\n") == -1) return -1;
402 if (sess->observer_count == -1) sess->observer_count = 0;
404 return 1;
408 free_gamelist (gamelist);
410 return 0;
415 static void run_editor (session_info *sess, const game_info *g, int i)
417 game_info ed;
418 char name[BUF_SIZE];
419 int dirln;
422 // prefix with game directory name
423 if (g->dir[0]) dirln = sprintf(name, "%s/", g->dir);
424 else dirln = 0;
426 // copy file name to bufer
427 strcat (&name[dirln], g->files[i].copy);
429 // set up a a game struct for editor
430 memset (&ed, 0, sizeof(game_info));
431 strcpy (ed.id, "editor");
432 strcpy (ed.name, "editor");
433 ed.enter = '\n';
434 snprintf (ed.cmd, GAME_COMMAND_LEN, "%s %s", config.editor, name);
436 // disable observing
437 sess->observer_count = -1;
439 run_game (sess, &ed);
441 // enable observing again
442 sess->observer_count = 0;
447 static int edit_menu (session_info *sess)
449 const char msg[] = TELNET_CLS "Select a file to edit by entering its number\r\n";
450 game_info *gamelist, *g;
451 char buf[BUF_SIZE], name[BUF_SIZE];
452 int n, idx = 1;
453 char inp[20];
456 gamelist = load_gamelist();
458 if (send(sess->socket, msg, sizeof(msg), 0) == -1)
460 free_gamelist (gamelist);
461 return -1;
464 g = gamelist;
465 while (g != NULL)
467 struct stat st;
468 int l, i, dirln = 0;
470 for (i = 0; i < GAME_FILES; i++)
472 if (!g->files[i].file[0]) break;
473 if (!g->files[i].allow_edit) continue;
475 // prefix with game directory name, expanding user name
476 if (g->dir[0])
478 str_replace (name, g->dir, "$USER$", sess->user_name);
479 strcat (name, "/");
480 dirln = strlen(name);
483 // copy file name to bufer, expanding user name
484 str_replace (&name[dirln], g->files[i].copy, "$USER$", sess->user_name);
485 // file does not exist
486 if (stat(name, &st) != 0) continue;
488 l = sprintf(buf, "%3d: [%s] %s\r\n", idx, g->name, name);
490 if (send(sess->socket, buf, l, 0) == -1)
492 free_gamelist (gamelist);
493 return -1;
496 // we (ab)use the allow_edit to store index
497 // to find the file when the user enters a number
498 g->files[i].allow_edit = 0x80000000 | idx;
500 idx++;
503 g = g->next;
507 // get id
508 n = recv_input(sess, inp, 20);
509 if (n == -1) return -1;
511 n = 0x80000000 | atoi(inp);
513 // find and run game with such id
514 g = gamelist;
515 while (g != NULL)
517 int i;
519 for (i = 0; i < GAME_FILES; i++)
521 if (!g->files[i].file[0]) break;
522 if (g->files[i].allow_edit != n) continue;
524 run_editor (sess, g, i);
526 free_gamelist (gamelist);
528 return 0;
531 g = g->next;
534 free_gamelist (gamelist);
536 return 0;
541 static int print_main_menu (session_info *sess)
543 char msg[BUF_SIZE];
544 const char *reg, *name, *play;
547 // XXX this is ugly, clean it up someday
549 if (sess->user_id < 0)
551 reg = "r) Register as a user (needed in order to play)";
552 name = "player of roguelikes";
553 play = "l) Log in to play";
555 else
557 reg = "c) Change password";
558 name = sess->user_name;
559 play = "p) Play\r\n\te) Edit game options files";
562 int l = snprintf(msg, BUF_SIZE, TELNET_CLS "Welcome to the roguelike server, %s!\r\n"
563 "\t%s\r\n"
564 "\to) Observe running games\r\n"
565 "\ts) View high-score lists\r\n"
566 "\t%s\r\n"
567 "\tq) Leave\r\n", name, play, reg);
569 if (send(sess->socket, msg, l, 0) == -1) return -1;
570 return 0;
575 int main_menu (session_info *sess)
577 if (print_main_menu(sess) == -1) return -1;
579 while (1)
581 char buf[BUF_SIZE];
582 int n = recv(sess->socket, buf, BUF_SIZE, 0);
583 if (n == -1) return -1;
585 n = translate_telnet_input(buf, n, sess);
586 if (n > 0)
588 if (buf[0] == 'p' && sess->user_id >= 0)
590 if (play_menu(sess, 0) == -1) return -1;
591 if (print_main_menu(sess) == -1) return -1;
594 if (buf[0] == 's')
596 if (play_menu(sess, 1) == -1) return -1;
597 if (print_main_menu(sess) == -1) return -1;
600 if (buf[0] == 'e' && sess->user_id >= 0)
602 if (edit_menu(sess) == -1) return -1;
603 if (print_main_menu(sess) == -1) return -1;
606 if (buf[0] == 'l' && sess->user_id < 0)
608 if (login_menu(sess) == -1) return -1;
609 if (print_main_menu(sess) == -1) return -1;
612 if (buf[0] == 'r' && sess->user_id < 0)
614 if (register_menu(sess) == -1) return -1;
615 if (print_main_menu(sess) == -1) return -1;
618 if (buf[0] == 'c' && sess->user_id >= 0)
620 if (passwd_menu(sess) == -1) return -1;
621 if (print_main_menu(sess) == -1) return -1;
624 if (buf[0] == 'o')
626 if (observer_menu(sess) == -1) return -1;
627 if (print_main_menu(sess) == -1) return -1;
630 if (buf[0] == 'q') return 0;