do all the world initialisation stuff i previously did plus some temporary serverside rendering so i know it works

This commit is contained in:
snit 2024-10-06 01:11:49 -05:00
parent 0c80ea5f10
commit f64646b40b
3 changed files with 50 additions and 4 deletions

View File

@ -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

View File

@ -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;
};

View File

@ -2,6 +2,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <signal.h>
#include <unistd.h>
#include <sys/syslog.h>
@ -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);