feat(lobby): implement bounded WebSocket sessions and countdown
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "maze32_lobby.h"
|
||||
#include "maze32_protocol.h"
|
||||
|
||||
static const char* TOKEN_1 = "000102030405060708090a0b0c0d0e0f";
|
||||
static const char* TOKEN_2 = "101112131415161718191a1b1c1d1e1f";
|
||||
static const char* OBSERVER_TOKENS[] = {
|
||||
"202122232425262728292a2b2c2d2e2f", "303132333435363738393a3b3c3d3e3f",
|
||||
"404142434445464748494a4b4c4d4e4f", "505152535455565758595a5b5c5d5e5f",
|
||||
"606162636465666768696a6b6c6d6e6f",
|
||||
};
|
||||
|
||||
static void test_protocol_schema(void) {
|
||||
maze32_client_message_t message;
|
||||
const char* join = "{\"v\":1,\"type\":\"join\",\"seq\":1,\"requestId\":\"join-1\","
|
||||
"\"payload\":{\"role\":\"player\",\"name\":\"Лорд Лабиринт\"}}";
|
||||
assert(maze32_protocol_parse(join, strlen(join), &message) == MAZE32_PROTOCOL_OK);
|
||||
assert(message.type == MAZE32_MESSAGE_JOIN);
|
||||
assert(message.sequence == 1U);
|
||||
assert(message.role == MAZE32_ROLE_PLAYER);
|
||||
assert(strcmp(message.name, "Лорд Лабиринт") == 0);
|
||||
|
||||
const char* restore = "{\"v\":1,\"type\":\"restore\",\"seq\":2,\"payload\":{\"token\":"
|
||||
"\"000102030405060708090a0b0c0d0e0f\"}}";
|
||||
assert(maze32_protocol_parse(restore, strlen(restore), &message) == MAZE32_PROTOCOL_OK);
|
||||
assert(message.type == MAZE32_MESSAGE_RESTORE);
|
||||
assert(strcmp(message.token, TOKEN_1) == 0);
|
||||
|
||||
const char* bad_version = "{\"v\":2,\"type\":\"start\",\"seq\":1,\"payload\":{}}";
|
||||
assert(maze32_protocol_parse(bad_version, strlen(bad_version), &message) ==
|
||||
MAZE32_PROTOCOL_UNSUPPORTED_VERSION);
|
||||
const char* unknown = "{\"v\":1,\"type\":\"teleport\",\"seq\":1,\"payload\":{}}";
|
||||
assert(maze32_protocol_parse(unknown, strlen(unknown), &message) ==
|
||||
MAZE32_PROTOCOL_UNKNOWN_TYPE);
|
||||
const char* extra = "{\"v\":1,\"type\":\"start\",\"seq\":1,\"payload\":{\"power\":\"yes\"}}";
|
||||
assert(maze32_protocol_parse(extra, strlen(extra), &message) ==
|
||||
MAZE32_PROTOCOL_INVALID_PAYLOAD);
|
||||
const char* duplicate = "{\"v\":1,\"v\":1,\"type\":\"start\",\"seq\":1,\"payload\":{}}";
|
||||
assert(maze32_protocol_parse(duplicate, strlen(duplicate), &message) ==
|
||||
MAZE32_PROTOCOL_MALFORMED);
|
||||
char oversized[MAZE32_PROTOCOL_MESSAGE_LIMIT + 2U];
|
||||
memset(oversized, 'x', sizeof(oversized));
|
||||
assert(maze32_protocol_parse(oversized, sizeof(oversized), &message) ==
|
||||
MAZE32_PROTOCOL_TOO_LARGE);
|
||||
}
|
||||
|
||||
static void test_lobby_limits_restoration_and_authority(void) {
|
||||
maze32_lobby_t lobby;
|
||||
maze32_lobby_init(&lobby);
|
||||
maze32_session_t* player_1 = NULL;
|
||||
maze32_session_t* player_2 = NULL;
|
||||
assert(maze32_lobby_join_player(&lobby, 10, "Капитан Клавиша", TOKEN_1, &player_1) ==
|
||||
MAZE32_LOBBY_OK);
|
||||
assert(player_1->player_slot == 1U);
|
||||
uint32_t revision = lobby.revision;
|
||||
assert(maze32_lobby_join_player(&lobby, 11, "Капитан Клавиша", TOKEN_2, &player_2) ==
|
||||
MAZE32_LOBBY_DUPLICATE_NAME);
|
||||
assert(lobby.revision == revision);
|
||||
assert(maze32_lobby_join_player(&lobby, 11, "Мышь в доспехах", TOKEN_2, &player_2) ==
|
||||
MAZE32_LOBBY_OK);
|
||||
assert(player_2->player_slot == 2U);
|
||||
assert(maze32_lobby_ready(&lobby));
|
||||
|
||||
for (unsigned int index = 0U; index < MAZE32_OBSERVER_LIMIT; ++index) {
|
||||
maze32_session_t* observer = NULL;
|
||||
assert(maze32_lobby_join_observer(&lobby, 20 + (int)index, OBSERVER_TOKENS[index],
|
||||
&observer) == MAZE32_LOBBY_OK);
|
||||
assert(observer->role == MAZE32_ROLE_OBSERVER);
|
||||
}
|
||||
assert(maze32_lobby_connected_observers(&lobby) == MAZE32_OBSERVER_LIMIT);
|
||||
maze32_session_t* extra = NULL;
|
||||
revision = lobby.revision;
|
||||
assert(maze32_lobby_join_observer(&lobby, 30, OBSERVER_TOKENS[4], &extra) ==
|
||||
MAZE32_LOBBY_ROLE_FULL);
|
||||
assert(lobby.revision == revision);
|
||||
|
||||
assert(maze32_lobby_start(&lobby, 11, 1000U) == MAZE32_LOBBY_NOT_OWNER);
|
||||
assert(lobby.phase == MAZE32_LOBBY_WAITING);
|
||||
maze32_lobby_disconnect(&lobby, 11);
|
||||
assert(!maze32_lobby_ready(&lobby));
|
||||
assert(maze32_lobby_start(&lobby, 10, 1000U) == MAZE32_LOBBY_NOT_READY);
|
||||
assert(maze32_lobby_restore(&lobby, 31, TOKEN_2, &player_2) == MAZE32_LOBBY_OK);
|
||||
assert(player_2->player_slot == 2U && player_2->connection_id == 31);
|
||||
|
||||
assert(maze32_lobby_start(&lobby, 10, 2000U) == MAZE32_LOBBY_OK);
|
||||
assert(lobby.phase == MAZE32_LOBBY_COUNTDOWN);
|
||||
assert(lobby.activation_ms == 5000U);
|
||||
assert(!maze32_lobby_can_reconfigure(&lobby));
|
||||
assert(!maze32_lobby_activate_if_due(&lobby, 4999U));
|
||||
assert(maze32_lobby_activate_if_due(&lobby, 5000U));
|
||||
assert(lobby.phase == MAZE32_LOBBY_ACTIVE);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
test_protocol_schema();
|
||||
test_lobby_limits_restoration_and_authority();
|
||||
puts("lobby and protocol tests: PASS");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user