From 57c74713d71f5ef306f2142fa7033d8e73f1ac9a Mon Sep 17 00:00:00 2001 From: Ilya Belyy Date: Mon, 16 Jun 2008 21:31:57 +0900 Subject: [PATCH] Added option to game_info for ENTER key character translation --- README | 8 ++++++++ games.c | 16 ++++++++++++++-- games.h | 1 + menu.c | 1 + run.c | 15 +++++++++++---- 5 files changed, 35 insertions(+), 6 deletions(-) diff --git a/README b/README index 3dd4ada..75fd0df 100644 --- a/README +++ b/README @@ -70,6 +70,7 @@ chdir: myrl-0.1.2 copy: data/newtemplate.save $USER$.save copy-edit: data/keys keys-$USER$ run: ./myrl -user $USER$ +enter: lf The first line in brackets denotes section start. The string inside brackets is the game id. It will be used when players are selecting a game @@ -94,6 +95,10 @@ will be used for both file names. Optional copy-edit: is very similar to copy:. The only difference is that the copied file will be allowed to be edited by player. + Optional enter: specifies how to translate the Enter key character +prior to passing the input to player. Possible values are LF (default) for +'\n' and CR for '\r'. + Game requirements @@ -115,3 +120,6 @@ the server is shutting down. it should employ some file locking mechanism to protect these file from corruption due to simultaneous writes initiated by different players. + A game should have an option to display a high-score list and then +exit. + diff --git a/games.c b/games.c index 3c851ef..800d2ad 100644 --- a/games.c +++ b/games.c @@ -36,6 +36,7 @@ static game_info* parse_game_begin (const char *buf, int ln) memset (game, 0, sizeof(game_info)); memcpy (game->id, buf + 1, n - 1); game->id[n - 1] = 0; + game->enter = '\n'; return game; } @@ -45,8 +46,8 @@ static game_info* parse_game_begin (const char *buf, int ln) static int parse_game_line (game_info *game, const char *buf, int ln) { // if expand/reorder, change the switch below - static const char* const tags[] = {"name:", "chdir:", "run:", "copy:", "copy-edit:", NULL}; - static const int lengths[] = {GAME_NAME_LEN, GAME_DIR_LEN, GAME_COMMAND_LEN, GAME_FILE_LEN, GAME_FILE_LEN}; + static const char* const tags[] = {"name:", "chdir:", "run:", "copy:", "copy-edit:", "enter:", NULL}; + static const int lengths[] = {GAME_NAME_LEN, GAME_DIR_LEN, GAME_COMMAND_LEN, GAME_FILE_LEN, GAME_FILE_LEN, 3}; int taglen, len, i, j; @@ -81,19 +82,24 @@ static int parse_game_line (game_info *game, const char *buf, int ln) switch (i) { + // name: case 0: memcpy (game->name, &buf[taglen], len); game->name[len] = 0; break; + // chdir: case 1: memcpy (game->dir, &buf[taglen], len); game->dir[len] = 0; break; + // run: case 2: memcpy (game->command, &buf[taglen], len); game->command[len] = 0; break; + // copy: case 3: + // copy-edit: case 4: for (j = 0; game->files[j].file[0] != 0; j++) { @@ -117,6 +123,12 @@ static int parse_game_line (game_info *game, const char *buf, int ln) game->files[j].allow_edit = (i == 4); break; + // enter: + case 5: + if (strncasecmp(&buf[taglen], "cr", len) == 0) game->enter = '\r'; + else if (strncasecmp(&buf[taglen], "lf", len) == 0) game->enter = '\n'; + else write_log (LOG_ERROR, GAME_FILE ":%d: unknown value for enter key", ln); + break; default: // should never happen return -1; diff --git a/games.h b/games.h index 1c8a125..ee0bb0a 100644 --- a/games.h +++ b/games.h @@ -16,6 +16,7 @@ typedef struct game_info char name[GAME_NAME_LEN]; char dir[GAME_DIR_LEN]; char command[GAME_COMMAND_LEN]; + char enter; // what symbol to use for Enter key (\n or \r) struct { int allow_edit; diff --git a/menu.c b/menu.c index d01cecd..2b6dc28 100644 --- a/menu.c +++ b/menu.c @@ -422,6 +422,7 @@ static void run_editor (session_info *sess, const game_info *g, int i) memset (&ed, sizeof(game_info), 0); strcpy (ed.id, "editor"); strcpy (ed.name, "editor"); + ed.enter = '\n'; snprintf (ed.command, GAME_COMMAND_LEN, "%s %s", config.editor, name); // disable observing diff --git a/run.c b/run.c index 53884da..0d6f4d1 100644 --- a/run.c +++ b/run.c @@ -68,11 +68,12 @@ static void remove_observers (session_info *sess) #define BUF_SIZE 16*1024 -static void play_game (session_info *sess) +static void play_game (session_info *sess, char enter_char) { char buf[BUF_SIZE]; fd_set rfds; - int sn = (sess->socket > sess->pty_master) ? sess->socket : sess->pty_master; + int i, sn = (sess->socket > sess->pty_master) ? sess->socket : sess->pty_master; + sess->term = init_term(sess->term_wid, sess->term_hgt); @@ -113,7 +114,13 @@ static void play_game (session_info *sess) if (nr <= 0) break; nr = translate_telnet_input(buf, nr, sess); - if (nr > 0) write (sess->pty_master, buf, nr); + if (nr > 0) + { + // replace ENTER key + for (i = 0; i < nr; i++) if (buf[i] == '\n') buf[i] = enter_char; + + write (sess->pty_master, buf, nr); + } // update activity timestamp sess->last_activity = time(NULL); @@ -334,7 +341,7 @@ void run_game (session_info *sess, const game_info *game) sess->child_pid = pid; strcpy (sess->game, game->name); - play_game (sess); + play_game (sess, game->enter); remove_observers (sess); -- 2.11.4.GIT