Skip to content

Commit

Permalink
Implemented lobby and slot assignation server -> client
Browse files Browse the repository at this point in the history
  • Loading branch information
iWas-Coder committed Apr 1, 2024
1 parent 011e566 commit c5a38dc
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 37 deletions.
2 changes: 1 addition & 1 deletion include/sk_player.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ static const char * const sk_player_kinds[] = {
[SK_PLAYER_KIND_JETT] = "jett"
};

sk_player sk_player_create(sk_player_kind kind);
sk_player sk_player_create(i8 lobby_id, i8 lobby_slot_idx, sk_player_kind kind);

void sk_player_destroy(sk_player *p);

Expand Down
2 changes: 1 addition & 1 deletion include/sk_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#define SK_SERVER_TICK_RATE 128
#define SK_SERVER_MSG_MAX_SIZE 1024
#define SK_SERVER_MSG_HELLO SK_SERVER_NAME "::hello"
#define SK_SERVER_MSG_HOWDY SK_SERVER_NAME "::howdy::%u/%u"
#define SK_SERVER_MSG_HOWDY SK_SERVER_NAME "::howdy::%hhd/%hhd"

i32 sk_server_socket_create(void);

Expand Down
2 changes: 2 additions & 0 deletions include/sk_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ typedef struct {

sk_state_global sk_state_global_create(void);

void sk_state_global_assign_lobby(sk_state_global *sg, i8 *lobby_id, i8 *lobby_slot_idx);

sk_state sk_state_create_offline(void);

sk_state sk_state_create_online(u8 lobby_id);
16 changes: 11 additions & 5 deletions src/sk_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
*/


#include <stdio.h>
#include <errno.h>
#include <assert.h>
#include <string.h>
#include <unistd.h>
#include <sk_log.h>
Expand Down Expand Up @@ -66,18 +68,22 @@ static i8 online_mode(const char *ip) {
i32 howdy_msg_n = recv(sock_fd, howdy_msg, sizeof(howdy_msg), MSG_WAITALL);
if (howdy_msg_n == -1) {
SK_LOG_ERROR("recv(2) :: %s", strerror(errno));
SK_LOG_ERROR("Unable to communicate with `%s`. Exiting...", ip);
close(sock_fd);
return -1;
}
howdy_msg[howdy_msg_n] = 0;
// TODO: hardcoded now to 0/0 ... change this to save the received IDs
if (strcmp(howdy_msg, TextFormat(SK_SERVER_MSG_HOWDY, 0, 0))) {
SK_LOG_ERROR("Unable to communicate with `%s`. Exiting...", ip);
i8 assigned_lobby_id = -1;
i8 assigned_lobby_slot_idx = -1;
u8 matched_data = sscanf(howdy_msg, SK_SERVER_MSG_HOWDY, &assigned_lobby_id, &assigned_lobby_slot_idx);
assert(matched_data == 2);
if (assigned_lobby_id == -1 || assigned_lobby_slot_idx == -1) {
SK_LOG_ERROR("Connection rejected from `%s`. Exiting...", ip);
close(sock_fd);
return -1;
}
SK_LOG_INFO("Connected successfully to `%s`", ip);
SK_LOG_WARN("Exit due to not being implemented yet");
SK_LOG_INFO("Connected successfully to `%s` (lobby %i, slot %i)", ip, assigned_lobby_id, assigned_lobby_slot_idx);
SK_LOG_WARN("Exiting due to not being fully implemented yet");
sk_server_socket_destroy(sock_fd);
return 0;
}
Expand Down
14 changes: 7 additions & 7 deletions src/sk_player.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,22 @@


#include <stdio.h>
#include <assert.h>
#include <sk_player.h>
#include <sk_config.h>

#define FILEPATH_BUFFER_MAX_SIZE 64
#define PLAYER_HEIGHT 2
#define GRAVITY 19
#define JUMP_INIT_VELOCITY 9
#define PEEK_ACCELERATION 1.5
#define WALK_VELOCITY 0.1

sk_player sk_player_create(sk_player_kind kind) {
sk_player sk_player_create(i8 lobby_id, i8 lobby_slot_idx, sk_player_kind kind) {
assert(lobby_id != -1);
assert(lobby_slot_idx != -1);
return (sk_player) {
.lobby_id = -1,
.lobby_slot_idx = -1,
.lobby_id = lobby_id,
.lobby_slot_idx = lobby_slot_idx,
.kind = kind,
.camera = (Camera3D) {
.position = (Vector3) { 0, PLAYER_HEIGHT, 4 },
Expand All @@ -52,9 +54,7 @@ void sk_player_destroy(sk_player *p) {
}

void sk_player_load(sk_player *p, sk_weapon_kind initial_weapon_kind) {
char filepath[FILEPATH_BUFFER_MAX_SIZE] = {0};
sprintf(filepath, "assets/models/%s.glb", sk_player_kinds[p->kind]);
p->model = LoadModel(filepath);
p->model = LoadModel(TextFormat("assets/models/%s.glb", sk_player_kinds[p->kind]));
p->weapon = sk_weapon_create(initial_weapon_kind);
}

Expand Down
26 changes: 16 additions & 10 deletions src/sk_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <signal.h>
#include <unistd.h>
#include <sk_log.h>
#include <sk_state.h>
#include <sk_server.h>
#include <arpa/inet.h>
#include <sys/socket.h>
Expand Down Expand Up @@ -83,12 +84,13 @@ i32 sk_server_socket_bind(i32 sock_fd) {
SK_LOG_ERROR("sk_server_socket_bind :: %s", strerror(errno));
sk_server_socket_destroy(sock_fd);
}
else SK_LOG_INFO("UDP socket binded to 127.0.0.1:%d", SK_SERVER_PORT);
else SK_LOG_INFO("UDP socket binded to 127.0.0.1:%u", SK_SERVER_PORT);
return result;
}

u8 sk_server_run(void) {
SK_LOG_INFO("Initializing %s", SK_SERVER_NAME);
sk_state_global state_global = sk_state_global_create();
i32 sock_fd = sk_server_socket_create();
if (sock_fd == -1) return 1;
if (sk_server_socket_bind(sock_fd) == -1) return 1;
Expand All @@ -115,22 +117,26 @@ u8 sk_server_run(void) {
}
msg[msg_n] = 0;
if (!strcmp(msg, SK_SERVER_MSG_HELLO)) {
SK_LOG_INFO("Connection from client (%s:%d) requested", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
u8 assigned_lobby_id = 0;
u8 assigned_lobby_slot_id = 0;
// TODO: check if there is a free slot in a lobby
// TODO: assign him a lobby and a player slot inside it
const char * const response = TextFormat(SK_SERVER_MSG_HOWDY, assigned_lobby_id, assigned_lobby_slot_id);
SK_LOG_INFO("Connection from client (%s:%u) requested", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
i8 assigned_lobby_id = -1;
i8 assigned_lobby_slot_idx = -1;
sk_state_global_assign_lobby(&state_global, &assigned_lobby_id, &assigned_lobby_slot_idx);
if (sendto(sock_fd,
response,
strlen(response),
TextFormat(SK_SERVER_MSG_HOWDY, assigned_lobby_id, assigned_lobby_slot_idx),
strlen(TextFormat(SK_SERVER_MSG_HOWDY, assigned_lobby_id, assigned_lobby_slot_idx)),
0,
(struct sockaddr *) &client_addr,
client_addr_len) == -1) {
SK_LOG_ERROR("sendto(2) :: %s", strerror(errno));
SK_LOG_WARN("Connection from client (%s:%u) dropped", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
continue;
}
SK_LOG_INFO("Connection from client (%s:%d) accepted", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
if (assigned_lobby_id == -1 || assigned_lobby_slot_idx == -1) {
SK_LOG_INFO("Connection from client (%s:%u) rejected", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
continue;
}
SK_LOG_INFO("Connection from client (%s:%u) accepted", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
state_global.lobbies[assigned_lobby_id].players[assigned_lobby_slot_idx] = sk_player_create(assigned_lobby_id, assigned_lobby_slot_idx, SK_PLAYER_KIND_JETT);
}
wait_next_tick(dt);
}
Expand Down
29 changes: 25 additions & 4 deletions src/sk_state.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,35 @@
*/


#include <sk_log.h>
#include <sk_state.h>

sk_state_global sk_state_global_create(void) {
sk_state_global s = { .lobbies_count = 0 };
sk_state_global sg = { .lobbies_count = 0 };
for (u16 i = 0; i < SK_STATE_MAX_LOBBIES; ++i) {
s.lobbies[i] = sk_lobby_create(i);
sg.lobbies[i] = sk_lobby_create(i);
}
return s;
return sg;
}

void sk_state_global_assign_lobby(sk_state_global *sg, i8 *lobby_id, i8 *lobby_slot_idx) {
for (u16 i = 0; i < SK_STATE_MAX_LOBBIES; ++i) {
if (sg->lobbies[i].players_count == SK_LOBBY_MAX_PLAYERS) continue;
for (u8 j = 0; j < SK_LOBBY_MAX_PLAYERS; ++j) {
if (sg->lobbies[i].players[j].lobby_id == -1 &&
sg->lobbies[i].players[j].lobby_slot_idx == -1) {
*lobby_id = i;
*lobby_slot_idx = j;
SK_LOG_INFO("sk_state_global_assign_lobby :: assigned lobby %i, slot %i",
*lobby_id,
*lobby_slot_idx);
return;
}
}
}
*lobby_id = -1;
*lobby_slot_idx = -1;
SK_LOG_WARN("sk_state_global_assign_lobby :: all lobbies are full");
}

sk_state sk_state_create_online(u8 lobby_id) {
Expand All @@ -41,6 +62,6 @@ sk_state sk_state_create_offline(void) {
return (sk_state) {
.is_online = 0,
.curr_scene = (sk_scene) { .kind = SK_SCENE_KIND_MAIN_MENU },
.player = sk_player_create(SK_PLAYER_KIND_JETT)
.player = sk_player_create(0, 0, SK_PLAYER_KIND_JETT)
};
}
13 changes: 4 additions & 9 deletions src/sk_weapon.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,15 @@
#include <assert.h>
#include <sk_weapon.h>

#define FILEPATH_BUFFER_MAX_SIZE 64

sk_weapon sk_weapon_create(sk_weapon_kind kind) {
sk_weapon w = {0};
char filepath[FILEPATH_BUFFER_MAX_SIZE] = {0};
sprintf(filepath, "assets/models/%s.glb", sk_weapon_kinds[kind]);
w.model = LoadModel(filepath);
w.model_anims = LoadModelAnimations(filepath, (int *) &w.model_anims_count);
w.model = LoadModel(TextFormat("assets/models/%s.glb", sk_weapon_kinds[kind]));
w.model_anims = LoadModelAnimations(TextFormat("assets/models/%s.glb", sk_weapon_kinds[kind]),
(int *) &w.model_anims_count);
assert(w.model_anims_count == 1);
assert(IsModelAnimationValid(w.model, w.model_anims[0]));
w.model_anim_frame_count = 0;
memset(filepath, 0, sizeof(filepath));
sprintf(filepath, "assets/sounds/%s/shoot.wav", sk_weapon_kinds[kind]);
w.sound_shoot = LoadSound(filepath);
w.sound_shoot = LoadSound(TextFormat("assets/sounds/%s/shoot.wav", sk_weapon_kinds[kind]));
w.kind = kind;
return w;
}
Expand Down

0 comments on commit c5a38dc

Please sign in to comment.