From f64646b40bf3dacb3fad6219d8feb7b6e60b333a Mon Sep 17 00:00:00 2001 From: snit Date: Sun, 6 Oct 2024 01:11:49 -0500 Subject: [PATCH] do all the world initialisation stuff i previously did plus some temporary serverside rendering so i know it works --- TODO.gmi | 3 ++- server/src/data.h | 4 ++++ server/src/main.c | 47 ++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/TODO.gmi b/TODO.gmi index cb57a5f..6b6f997 100644 --- a/TODO.gmi +++ b/TODO.gmi @@ -2,11 +2,12 @@ A list of things I'd like to accomplish ## In Progress -* Initialise a world again +* Serialise data to JSON for socket data transmission * Send world data to client * Have client render world data (return to pre-daemonised equivalent state) ## Completed +* Initialise a world again * Expose a socket or pipe on the daemon for the client * Create client to connect to daemon * Daemonise the serverside diff --git a/server/src/data.h b/server/src/data.h index 642700b..c5c8103 100644 --- a/server/src/data.h +++ b/server/src/data.h @@ -1,7 +1,11 @@ #ifndef DAEMON_DATA_H #define DAEMON_DATA_H +#include "world.h" + struct data_t { + struct world_t world; + int socket; int socket_accept; }; diff --git a/server/src/main.c b/server/src/main.c index a26d167..39dbaa8 100644 --- a/server/src/main.c +++ b/server/src/main.c @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -20,7 +21,7 @@ static void handle_signal(int _) { enum error_t game_loop(struct data_t *data) { - char buffer[1024] = { 0 }; + uint8_t buffer[1024] = { 0 }; read(data->socket_accept, buffer, 1024); write(data->socket_accept, buffer, 1024); @@ -30,12 +31,15 @@ enum error_t game_loop(struct data_t *data) { int main(int argc, char **argv) { + // Set up variables enum error_t err = ERR_OK; - struct data_t daemon_data = { 0 }; + struct data_t data = { 0 }; + // Signal handling; TODO: should probably improve this signal(SIGINT, handle_signal); signal(SIGTERM, handle_signal); + // Handle commandline options struct options_t options = { 0 }; opts_default(&options); @@ -45,12 +49,49 @@ int main(int argc, char **argv) { err = opts_init(&options); if (err) goto handle_error; - err = sock_loop(&daemon_data, game_loop); + // World initialisation + err = world_init(&data.world, 10, 10); if (err) goto handle_error; + err = world_register_entity(&data.world, "john", 'j'); + if (err) goto handle_error_world; + + err = world_place_entity(&data.world, 1, 3, 7); + if (err) goto handle_error_world; + + // Temporary code because I just realised I need to properly serialise + // data because don't work cross-socket + // TODO: Serialise to JSON + const char *fmt = "%zu (%s) (%zu, %zu) '%c'\n"; + struct entity_t const *const entity = &data.world.entities[0]; + struct entity_registrant_t const *const registrant = + &data.world.registered_entities.entities[entity->id]; + + if (options.daemonise) + syslog(LOG_NOTICE, fmt, + entity->id, registrant->name, + entity->x, entity->y, + registrant->tile + ); + else + printf(fmt, + entity->id, registrant->name, + entity->x, entity->y, + registrant->tile + ); + + // Socket handling and run gameloop + err = sock_loop(&data, game_loop); + if (err) goto handle_error; + + // Deinitialisation + world_free(&data.world); opts_free(&options); return ERR_OK; +handle_error_world: + world_free(&data.world); + handle_error: if (options.daemonise) syslog(LOG_ERR, "%s", ERROR_STRS[err]); opts_free(&options);