feat: player belongs to a room with a check

This commit is contained in:
Jorens Shtekels 2024-03-12 22:02:22 +02:00
parent 23345ba89f
commit 23c1c23858
4 changed files with 22 additions and 5 deletions

View File

@ -8,6 +8,7 @@
#include <vector>
enum VALIDATION_STATUS {
PLAYER_NOT_IN_ROOM,
NO_PLAYER,
ROOM_NOT_IN_PROGRESS,
ACTION_PROHIBITED,
@ -25,9 +26,9 @@ void run() {
Event event2 = Event("Event 2", 1710087363, 1, true, {kill}, {});
Event event3 = Event("Event 3", 1710087369, 1, true, {}, {kill});
std::vector<Event> relatedEvents({event2, event3});
Player player1 = Player("player1", role1, PlayerStatus::ALIVE);
Player player2 = Player("player2", role1, PlayerStatus::ALIVE);
Room room1(1, "Room 1", 1710087364, RoomStatus::IN_PROGRESS, {player1, player2});
Player player1 = Player(69, "player1", role1, PlayerStatus::ALIVE);
Player player2 = Player(420, "player2", role1, PlayerStatus::ALIVE);
Room room1(1, "Room 1", 1710087364, RoomStatus::IN_PROGRESS, {});
Room room2(2, "Room 2", 1710087384, RoomStatus::ENDED, {player1, player2});
int actionValidated = validateAction(&player1, &kill, &room1, &relatedEvents, &player2);
std::cout << actionValidated << std::endl;
@ -35,6 +36,9 @@ void run() {
int validateAction(
Player *actor, const Action *action, Room *room, std::vector<Event> *relatedEvents, Player *target = nullptr) {
if (!playerBelongsToRoom(actor, room)) {
return PLAYER_NOT_IN_ROOM;
}
if (!actor) {
return NO_PLAYER;
}
@ -57,6 +61,10 @@ int validateAction(
return ACTION_VALID;
}
bool playerBelongsToRoom(Player *player, Room *room) {
return std::find(room->players.begin(), room->players.end(), *player) != room->players.end();
}
bool actionBelongsToRole(Role *role, const Action *action) {
return std::find(role->actions.begin(), role->actions.end(), *action) != role->actions.end();
}

View File

@ -2,6 +2,7 @@
void run();
bool playerBelongsToRoom(Player *player, Room *room);
bool actionBelongsToRole(Role *role, const Action *action);
bool isActionAllowed(const Action *action, std::vector<Event> *relevantEvents);
int validateAction(Player *actor, const Action *action, Room *room, std::vector<Event> *relatedEvents, Player *target);

View File

@ -2,6 +2,7 @@
#include "timeUtils.hh"
#include <cstdint>
#include <initializer_list>
int add(int a, int b) {
@ -29,12 +30,17 @@ Role::Role(std::vector<Action> actions) {
}
}
Player::Player(std::string username, Role role, PlayerStatus playerStatus):
Player::Player(uint32_t id, std::string username, Role role, PlayerStatus playerStatus):
id(id),
username(username),
role(role),
playerStatus(playerStatus) {
}
bool Player::operator==(const Player &other) const {
return this->id == other.id;
}
Room::Room(
uint32_t id, std::string title, uint32_t utcTimestampCreatedAt, RoomStatus status, std::vector<Player> players):
id(id),

View File

@ -53,11 +53,13 @@ struct Role {
};
struct Player {
uint32_t id;
std::string username;
Role role;
PlayerStatus playerStatus;
Player(std::string username, Role role, PlayerStatus playerStatus);
Player(uint32_t id, std::string username, Role role, PlayerStatus playerStatus);
bool operator==(const Player &other) const;
};
struct Room {