diff --git a/client/src/render/render.c b/client/src/render/render.c index 8212f74..4cd8e42 100644 --- a/client/src/render/render.c +++ b/client/src/render/render.c @@ -1,11 +1,14 @@ // render/render.c +#include #include #include "render.h" void render_world(struct world_t const *world) { + assert(world != NULL); + printf("%zu %zu\n", world->height, world->width); for (size_t i = 0; i < MAX_ENTITIES; i++) { diff --git a/common/include/request.h b/common/include/request.h index e94e624..9d5460f 100644 --- a/common/include/request.h +++ b/common/include/request.h @@ -2,11 +2,10 @@ #define COMMON_REQUEST_H #include +#include #include "error.h" -#include "world.h" - -// ### REQUEST TYPES ### // +#include "request/get_world_data.h" enum request_type_t { REQUEST_NONE, @@ -14,11 +13,6 @@ enum request_type_t { }; -struct request_body_get_world_data_t { - size_t world_id; -}; - - struct request_t { enum request_type_t type; void *body; @@ -32,13 +26,6 @@ enum error_t request_deserialise_str(struct request_t *, char const *); enum error_t request_serialise_buf(struct request_t const *, char *, size_t); -// ### RESPONSE TYPES ### // - -struct response_body_get_world_data_t { - struct world_t world; -}; - - struct response_t { enum request_type_t type; bool success; diff --git a/common/include/request/get_world_data.h b/common/include/request/get_world_data.h new file mode 100644 index 0000000..4a6dda3 --- /dev/null +++ b/common/include/request/get_world_data.h @@ -0,0 +1,41 @@ +#ifndef COMMON_REQUEST_GET_WORLD_DATA_H +#define COMMON_REQUEST_GET_WORLD_DATA_H + +#include + +#include "../world.h" + +struct request_body_get_world_data_t { + size_t world_id; +}; + + +enum error_t request_body_get_world_data_serialise( + struct request_body_get_world_data_t const *, + struct json_t ** +); + + +enum error_t request_body_get_world_data_deserialise( + struct request_body_get_world_data_t *, + struct json_t * +); + + +struct response_body_get_world_data_t { + struct world_t world; +}; + + +enum error_t response_body_get_world_data_serialise( + struct response_body_get_world_data_t const *, + struct json_t ** +); + + +enum error_t response_body_get_world_data_deserialise( + struct response_body_get_world_data_t *, + struct json_t * +); + +#endif diff --git a/common/src/request.c b/common/src/request.c index e5d1708..b33bb33 100644 --- a/common/src/request.c +++ b/common/src/request.c @@ -4,34 +4,12 @@ #include #include "request.h" +#include "request/get_world_data.h" -// Request Format Strings static char const *REQUEST_JSON_FMT = "{si, so}"; - -static char const *REQUEST_BODY_GET_WORLD_DATA_JSON_FMT = "{sI}"; - -// Response Format Strings static char const *RESPONSE_JSON_FMT = "{sb, si, so}"; -static enum error_t request_serialise_body_get_world_data( - struct request_body_get_world_data_t const *self, - struct json_t **jsonptr -) { - assert(self != NULL); - assert(jsonptr != NULL); - - struct json_t *json = json_pack(REQUEST_BODY_GET_WORLD_DATA_JSON_FMT, - "world-id", self->world_id - ); - - if (json == NULL) return ERR_JSON_SERIALISE; - - *jsonptr = json; - return ERR_OK; -} - - static enum error_t request_serialise_body( struct request_t const *self, struct json_t **jsonptr @@ -41,7 +19,7 @@ static enum error_t request_serialise_body( switch (self->type) { case REQUEST_GET_WORLD_DATA: - return request_serialise_body_get_world_data(self->body, jsonptr); + return request_body_get_world_data_serialise(self->body, jsonptr); default: return ERR_JSON_SERIALISE; } @@ -160,22 +138,6 @@ static enum error_t request_deserialise_parts( } -static enum error_t request_deserialise_body_get_world_data( - struct request_body_get_world_data_t *self, - struct json_t *json -) { - assert(self != NULL); - assert(json != NULL); - - int err = json_unpack(json, REQUEST_BODY_GET_WORLD_DATA_JSON_FMT, - "world-id", &self->world_id - ); - - if (err < 0) return ERR_JSON_DESERIALISE; - return ERR_OK; -} - - static enum error_t request_deserialise_body( struct request_t *self, struct json_t *json @@ -188,7 +150,7 @@ static enum error_t request_deserialise_body( self->body = malloc(sizeof(struct request_body_get_world_data_t)); if (self->body == NULL) return ERR_ALLOC; - return request_deserialise_body_get_world_data(self->body, json); + return request_body_get_world_data_deserialise(self->body, json); default: return ERR_JSON_SERIALISE; } @@ -239,22 +201,6 @@ enum error_t request_deserialise_str(struct request_t *self, char const *str) { } -static enum error_t response_serialise_body_get_world_data( - struct response_body_get_world_data_t const *self, - struct json_t **jsonptr -) { - assert(self != NULL); - assert(jsonptr != NULL); - - struct json_t *json = NULL; - enum error_t err = world_serialise(&self->world, &json); - if (err != ERR_OK) return err; - - *jsonptr = json; - return ERR_OK; -} - - static enum error_t response_serialise_body( struct response_t const *self, struct json_t **jsonptr @@ -264,7 +210,7 @@ static enum error_t response_serialise_body( switch (self->type) { case REQUEST_GET_WORLD_DATA: - return response_serialise_body_get_world_data(self->body, jsonptr); + return response_body_get_world_data_serialise(self->body, jsonptr); default: return ERR_JSON_SERIALISE; } @@ -391,20 +337,6 @@ static enum error_t response_deserialise_parts( } -static enum error_t response_deserialise_body_get_world_data( - struct response_body_get_world_data_t *self, - struct json_t *json -) { - assert(self != NULL); - assert(json != NULL); - - enum error_t err = world_deserialise(&self->world, json); - if (err != ERR_OK) return err; - - return ERR_OK; -} - - static enum error_t response_deserialise_body( struct response_t *self, struct json_t *json @@ -417,7 +349,7 @@ static enum error_t response_deserialise_body( self->body = malloc(sizeof(struct response_body_get_world_data_t)); if (self->body == NULL) return ERR_ALLOC; - return response_deserialise_body_get_world_data(self->body, json); + return response_body_get_world_data_deserialise(self->body, json); default: return ERR_JSON_SERIALISE; } diff --git a/common/src/request/get_world_data.c b/common/src/request/get_world_data.c new file mode 100644 index 0000000..45546ea --- /dev/null +++ b/common/src/request/get_world_data.c @@ -0,0 +1,71 @@ +// request/get_world_data.c + +#include + +#include "request/get_world_data.h" + +static char const *REQUEST_BODY_GET_WORLD_DATA_JSON_FMT = "{sI}"; + + +enum error_t request_body_get_world_data_serialise( + struct request_body_get_world_data_t const *self, + struct json_t **jsonptr +) { + assert(self != NULL); + assert(jsonptr != NULL); + + struct json_t *json = json_pack(REQUEST_BODY_GET_WORLD_DATA_JSON_FMT, + "world-id", self->world_id + ); + + if (json == NULL) return ERR_JSON_SERIALISE; + + *jsonptr = json; + return ERR_OK; +} + + +enum error_t request_body_get_world_data_deserialise( + struct request_body_get_world_data_t *self, + struct json_t *json +) { + assert(self != NULL); + assert(json != NULL); + + int err = json_unpack(json, REQUEST_BODY_GET_WORLD_DATA_JSON_FMT, + "world-id", &self->world_id + ); + + if (err < 0) return ERR_JSON_DESERIALISE; + return ERR_OK; +} + + +enum error_t response_body_get_world_data_serialise( + struct response_body_get_world_data_t const *self, + struct json_t **jsonptr +) { + assert(self != NULL); + assert(jsonptr != NULL); + + struct json_t *json = NULL; + enum error_t err = world_serialise(&self->world, &json); + if (err != ERR_OK) return err; + + *jsonptr = json; + return ERR_OK; +} + + +enum error_t response_body_get_world_data_deserialise( + struct response_body_get_world_data_t *self, + struct json_t *json +) { + assert(self != NULL); + assert(json != NULL); + + enum error_t err = world_deserialise(&self->world, json); + if (err != ERR_OK) return err; + + return ERR_OK; +} diff --git a/doc/TODO.gmi b/doc/TODO.gmi index a6b8d45..ee985b7 100644 --- a/doc/TODO.gmi +++ b/doc/TODO.gmi @@ -20,6 +20,7 @@ A list of things I'd like to accomplish * Write Makefile to automate compilation ## Planned +* Requests/responses for more than just the complete set of world data all at once * Create and load worlds * Display environment and pan camera * Time controls (play/pause/speed up) diff --git a/server/src/socket.c b/server/src/socket.c index 865f2be..b1eb9db 100644 --- a/server/src/socket.c +++ b/server/src/socket.c @@ -43,6 +43,10 @@ void socket_free(int const *sockptr) { } +// TODO: Out of curiosity, what's the consensus on struct declarations outside +// of header files? Its a private struct that the end-user should never need to +// use, so I don't want to include it in socket.h, but it feels off putting it +// in socket.c as well just because I'm used to them all being in .h struct socket_data_t { struct game_data_t *game_data; int socket;