diff options
Diffstat (limited to 'src/client.c')
-rw-r--r-- | src/client.c | 79 |
1 files changed, 72 insertions, 7 deletions
diff --git a/src/client.c b/src/client.c index d15ba8e..08eae9d 100644 --- a/src/client.c +++ b/src/client.c @@ -13,10 +13,17 @@ #include "content.h" typedef struct { + str name; + uint64_t id; +} player; + +typedef struct { peer conn; struct termios oldtio; str name; str pass; + uint64_t self_id; + array(player) players; } client; void gfx_alt_buffer(bool enable) @@ -27,9 +34,29 @@ void gfx_alt_buffer(bool enable) printf("\e[?1049l\e[?25h"); } +void gfx_clear() +{ + printf("\e[2J"); +} + void gfx_render(client *c, uint64_t dtime) { - // TODO: render game + gfx_clear(); + printf("\e[H"); + for (size_t i = 0; i < c->players.len; i++) + printf("%.*s\n", PSTR(c->players.data[i].name)); + fflush(stdout); +} + +void free_players(client *c) +{ + for (size_t i = 0; i < c->players.len; i++) + free(c->players.data[i].name.data); + + free(c->players.data); + + c->players.len = 0; + c->players.data = NULL; } void client_exit(client *c, int ret) @@ -37,6 +64,8 @@ void client_exit(client *c, int ret) free(c->name.data); free(c->pass.data); + free_players(c); + peer_free(&c->conn); tcsetattr(STDIN_FILENO, TCSANOW, &c->oldtio); gfx_alt_buffer(false); @@ -53,20 +82,56 @@ void handle_input(client *c, char ch) } } -bool handle_pkt(client *c, str pkt) +bool handle_players(str *w, client *c) { - // TODO: handle network traffic + free_players(c); + + uint16_t len; + if (!deser_u16(w, &len)) + return false; + + c->players.data = malloc(sizeof *c->players.data * len); + + for (c->players.len = 0; c->players.len < len; c->players.len++) { + player *p = &c->players.data[c->players.len]; + if (!deser_str(w, &p->name)) + return false; + if (!deser_u64(w, &p->id)) + return false; + if (str_cmp(p->name, c->name) == 0) + c->self_id = p->id; + p->name = str_clone(p->name); + } return true; } -int main() +bool handle_pkt(client *c, str pkt) { + pkt_type type; + if (!deser_pkt_type(&pkt, &type)) + return false; + + switch (type) { + case CPKT_PLAYERS: return handle_players(&pkt, c); + default: return false; + } +} + +int main(int argc, char **argv) +{ + if (argc < 4) { + fprintf(stderr, "usage: %s server port name [pass]\n", argv[0]); + return EXIT_FAILURE; + } + signal_setup(); client c = {0}; - c.name = str_clone(S("kitten")); - c.pass = str_clone(S("")); + const char *server = argv[1]; + const char *port = argv[2]; + c.name = str_clone(str_intro(argv[3])); + c.pass = str_clone(argc < 5 ? S("") : str_intro(argv[4])); c.conn.socket = -1; @@ -78,7 +143,7 @@ int main() SET_NONBLOCK(STDIN_FILENO); // TODO: handle error - int socket = socket_create("127.0.0.1", "4560", false); + int socket = socket_create(server, port, false); if (socket < 0) client_exit(&c, EXIT_FAILURE); str server_name = S("server"); |