diff --git a/README.md b/README.md index 67ef02a..743c3c2 100644 --- a/README.md +++ b/README.md @@ -3,11 +3,11 @@ This is a project I have created to teach myself C and protobuf. It will eventually be a MUD that may or may not link into my other software projects. ## TODO -### Road to v0.1 +### Road to v0.0.1 - [x] Create initial communication process between clients and server. - [ ] Handle multiple connections. - [ ] Use openssl so passwords arn't going over the internet via plaintext. -- [ ] Create world data structure. +- [x] Create world data structure. - [ ] Create an initial set of actions to allow some kind of interaction. - [ ] Move - [ ] Look diff --git a/src/server/command.c b/src/server/command.c index e2053e7..ef53ac0 100644 --- a/src/server/command.c +++ b/src/server/command.c @@ -1,15 +1,36 @@ #include "command.h" -Room *cmd__move(move_direction direction, Room *current_room) { +Room *cmd__check_move(Room *current_room, Room *to_room) { + if (current_room == NULL || to_room == NULL) + return current_room; + + return to_room; +}; + +// Update room based on move direction +void cmd__move_direction(move_direction direction, Room *current_room) { + if (current_room == NULL) { + err__warn("Cannot move from current_room as room is NULL"); + } + switch (direction) { case NORTH: - return current_room->north; + current_room = cmd__check_move(current_room, current_room->north); + break; case EAST: - return current_room->east; + current_room = cmd__check_move(current_room, current_room->east); + break; case SOUTH: - return current_room->south; + current_room = cmd__check_move(current_room, current_room->south); + break; case WEST: - return current_room->west; + current_room = cmd__check_move(current_room, current_room->west); + } +} + +void cmd__recv_proc_cmd(Player *player, Command *command) { + if (command->has_move_direction) { + move_direction dir = {command->move_direction}; + cmd__move_direction(dir, player->current_location); } - return NULL; } diff --git a/src/server/command.h b/src/server/command.h index b9becd1..8ef9e4d 100644 --- a/src/server/command.h +++ b/src/server/command.h @@ -1,9 +1,13 @@ #pragma once +#include "command_internal.h" #include "world.h" #include +#include #include -// Returns pointer to new room based on current room and direction If room -// doesn't exist returns NULL -Room *cmd__move(move_direction direction, Room *current_room); +// Will process a command given a player to execute it as +void cmd__recv_proc_cmd(Player *player, Command *command); + +// Update room pointer based on move direction +void cmd__move_direction(move_direction direction, Room *current_room); diff --git a/src/server/command_internal.h b/src/server/command_internal.h new file mode 100644 index 0000000..277496f --- /dev/null +++ b/src/server/command_internal.h @@ -0,0 +1,6 @@ +#pragma once + +#include + +// Check that room is valid before moving +Room *cmd__check_move(Room *current_room, Room *to_room); diff --git a/urchin-util/include/message.h b/urchin-util/include/message.h index eafe1a3..1543d44 100644 --- a/urchin-util/include/message.h +++ b/urchin-util/include/message.h @@ -1,5 +1,8 @@ #pragma once +#include "../src/protobuf_gen/amessage.pb-c.h" +#include "../src/protobuf_gen/commands.pb-c.h" +#include "../src/protobuf_gen/player.pb-c.h" #include "error.h" #include "message_internal.h" #include //sockaddr_in @@ -26,5 +29,5 @@ void msg__recv_login(int *con_sockfd); void msg__req_login(int *con_sockfd, char *usernm, char *passwd); // send and recive commands -void msg__recv_command(int *con_sockfd); +Command *msg__recv_command(int *con_sockfd); void msg__req_command(int *con_sockfd, char *usernm, char *passwd); diff --git a/urchin-util/include/world_structures.h b/urchin-util/include/world_structures.h index 48268e5..57727db 100644 --- a/urchin-util/include/world_structures.h +++ b/urchin-util/include/world_structures.h @@ -2,14 +2,21 @@ // Make world structures accessible to both server and client // Define moveable directions +// TODO eventually move over to a hash table so players can move through +// exits defined in the room description like hatch, crack, door, etc. typedef enum { NORTH, EAST, SOUTH, WEST } move_direction; // Define what a room is. -typedef struct room { - char* name; - char* description; - struct room* north; - struct room* east; - struct room* south; - struct room* west; -} Room; +typedef struct Room Room; +struct Room { + char *name; + char *description; + Room *north; + Room *east; + Room *south; + Room *west; +}; + +typedef struct player { + Room *current_location; +} Player; diff --git a/urchin-util/src/message.c b/urchin-util/src/message.c index f900547..30771fb 100644 --- a/urchin-util/src/message.c +++ b/urchin-util/src/message.c @@ -1,7 +1,5 @@ #include "message.h" #include "error.h" -#include "protobuf_gen/amessage.pb-c.h" -#include "protobuf_gen/player.pb-c.h" #include "world_structures.h" #include @@ -60,7 +58,7 @@ size_t get_msg_size_in_buffer(unsigned max_length, uint8_t *out) { } void read_from_sock(uint8_t *recvbuf, unsigned bufsize, int *sockfd) { - bzero(recvbuf, bufsize); + bzero(recvbuf, bufsize); if (read(*sockfd, recvbuf, bufsize) < 0) { err__warn("Connection did not send data"); @@ -148,8 +146,22 @@ int msg__create_serv_connection(int portno) { return sockfd; } +Command *msg__recv_command(int *con_sockfd) { + uint8_t msgbuf[MAX_MSG_SIZE]; + read_from_sock(msgbuf, sizeof msgbuf, con_sockfd); + size_t msg_len = get_msg_size_in_buffer(MAX_MSG_SIZE, msgbuf); -void msg__destroy_connection(int sockfd) { close(sockfd); } + Command *cmd = command__unpack(NULL, msg_len, msgbuf); + if (cmd == NULL) { + err__warn("Error unpacking login message"); + + command__free_unpacked(cmd, NULL); + return NULL; + } + + return cmd; +} +void msg__req_command(int *con_sockfd, char *usernm, char *passwd) {} int msg__accept_cli_connection(int server_sockfd) { struct sockaddr_in cli_addr; @@ -164,5 +176,4 @@ int msg__accept_cli_connection(int server_sockfd) { return cli_sockfd; } -void msg__recv_command(int *con_sockfd) {} -void msg__req_command(int *con_sockfd, char *usernm, char *passwd) {} +void msg__destroy_connection(int sockfd) { close(sockfd); }