diff --git a/src/lib.cpp b/src/lib.cpp index 53e0857..c4197d2 100644 --- a/src/lib.cpp +++ b/src/lib.cpp @@ -8,6 +8,7 @@ #include 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 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 *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(); } diff --git a/src/lib.hh b/src/lib.hh index 425808d..bf9ec23 100644 --- a/src/lib.hh +++ b/src/lib.hh @@ -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 *relevantEvents); int validateAction(Player *actor, const Action *action, Room *room, std::vector *relatedEvents, Player *target); diff --git a/src/prep/prep.cpp b/src/prep/prep.cpp index 5ec4935..52253dc 100644 --- a/src/prep/prep.cpp +++ b/src/prep/prep.cpp @@ -2,6 +2,7 @@ #include "timeUtils.hh" +#include #include int add(int a, int b) { @@ -29,12 +30,17 @@ Role::Role(std::vector 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 players): id(id), diff --git a/src/prep/prep.hh b/src/prep/prep.hh index 65a70ac..890ab17 100644 --- a/src/prep/prep.hh +++ b/src/prep/prep.hh @@ -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 {