diff options
| author | Franklin Wei <git@fwei.tk> | 2015-12-24 19:18:45 -0500 |
|---|---|---|
| committer | Franklin Wei <git@fwei.tk> | 2015-12-24 19:18:45 -0500 |
| commit | 53c15b0461ee39a4c32e61ff484389efb1e91d84 (patch) | |
| tree | c26e64930e1d73960eebc26b02d9d2185d2e1aef /src/client.c | |
| parent | 28f94a54984fa7aa46fcb25e7991c1136329670f (diff) | |
| download | netcosm-53c15b0461ee39a4c32e61ff484389efb1e91d84.zip netcosm-53c15b0461ee39a4c32e61ff484389efb1e91d84.tar.gz netcosm-53c15b0461ee39a4c32e61ff484389efb1e91d84.tar.bz2 netcosm-53c15b0461ee39a4c32e61ff484389efb1e91d84.tar.xz | |
stuff
Diffstat (limited to 'src/client.c')
| -rw-r--r-- | src/client.c | 75 |
1 files changed, 56 insertions, 19 deletions
diff --git a/src/client.c b/src/client.c index 96f8150..ee40e5c 100644 --- a/src/client.c +++ b/src/client.c @@ -181,30 +181,59 @@ static void client_change_room(room_id id) static void client_move(const char *dir) { - struct { + const struct dir_pair { const char *text; enum direction_t val; } dirs[] = { - { "N", DIR_N }, - { "NE", DIR_NE }, - { "E", DIR_E }, - { "SE", DIR_SE }, - { "S", DIR_S }, - { "SW", DIR_SW }, - { "W", DIR_W }, - { "NW", DIR_NW }, - { "UP", DIR_UP }, - { "DOWN", DIR_DOWN } + { "N", DIR_N }, + { "NORTH", DIR_N }, + { "NE", DIR_NE }, + { "NORTHEAST", DIR_N }, + { "E", DIR_E }, + { "EAST", DIR_E }, + { "SE", DIR_SE }, + { "SOUTHEAST", DIR_SE }, + { "S", DIR_S }, + { "SOUTH", DIR_S }, + { "SW", DIR_SW }, + { "SOUTHWEST", DIR_SW }, + { "W", DIR_W }, + { "WEST", DIR_W }, + { "NW", DIR_NW }, + { "NORTHWEST", DIR_NW }, + { "U", DIR_UP }, + { "UP", DIR_UP }, + { "D", DIR_DOWN }, + { "DOWN", DIR_DOWN }, }; - for(unsigned i = 0; i < ARRAYLEN(dirs); ++i) + static void *map = NULL; + if(!map) { - if(!strcmp(dir, dirs[i].text)) + map = hash_init(ARRAYLEN(dirs), hash_djb, strcmp); + for(unsigned i = 0; i < ARRAYLEN(dirs); ++i) { - send_master(REQ_MOVE, &dirs[i].val, sizeof(dirs[i].val)); - return; + hash_insert(map, dirs[i].text, dirs + i); + + void *new = hash_lookup(map, dirs[i].text); + if(new != dirs + i) + error("weird %p %p", new, dirs + i); } } - out("Unknown direction.\n"); + + struct dir_pair *pair = hash_lookup(map, dir); + if(pair) + { + send_master(REQ_MOVE, &pair->val, sizeof(pair->val)); + } + else + out("Unknown direction.\n"); +} + +static void client_look(void) +{ + send_master(REQ_GETROOMNAME, NULL, 0); + out("\n"); + send_master(REQ_GETROOMDESC, NULL, 0); } #define WSPACE " \t\r\n" @@ -292,7 +321,9 @@ auth: /* authenticated */ printf("Authenticated as %s\n", current_user); client_change_user(current_user); + current_room = 0; client_change_room(current_room); + client_look(); while(1) { out(">> "); @@ -451,7 +482,7 @@ auth: } else if(!strcmp(tok, "LOOK")) { - send_master(REQ_LOOK, NULL, 0); + client_look(); } else if(!strcmp(tok, "WAIT")) { @@ -460,8 +491,14 @@ auth: else if(!strcmp(tok, "GO")) { char *dir = strtok_r(NULL, WSPACE, &save); - all_upper(dir); - client_move(dir); + if(dir) + { + all_upper(dir); + client_move(dir); + client_look(); + } + else + out("Expected direction after GO.\n"); } next_cmd: |