refactor(validation): move validation to separate file

This commit is contained in:
Kristofers Solo 2024-03-14 01:31:32 +02:00
parent 09d17e2d0a
commit 9f0b32ed8c
7 changed files with 98 additions and 97 deletions

View File

@ -1,80 +0,0 @@
#include "lib.hh"
#include "prep/prep.hh"
#include <algorithm>
#include <cstdio>
#include <vector>
enum VALIDATION_STATUS {
PLAYER_NOT_IN_ROOM,
NO_TARGET_PLAYER_SPECIFIED,
ROOM_NOT_IN_PROGRESS,
ACTION_DOES_NOT_BELONG_TO_ROLE,
ACTION_PROHIBITED,
NO_ACTOR,
NO_ACTION,
NO_ROLE,
NO_ROOM,
NO_RELATED_EVENTS,
ACTION_VALID,
};
int validateAction(
Player *actor, const Action *action, Room *room, std::vector<Event> *relatedEvents, Player *target = nullptr) {
if (!actor) {
return NO_ACTOR;
}
if (!action) {
return NO_ACTION;
}
if (!room) {
return NO_ROOM;
}
if (!relatedEvents) {
return NO_RELATED_EVENTS;
}
if (!playerBelongsToRoom(actor, room)) {
return PLAYER_NOT_IN_ROOM;
}
if (action->hasTarget && !target) {
return NO_TARGET_PLAYER_SPECIFIED;
}
if (room->status != RoomStatus::IN_PROGRESS) {
return ROOM_NOT_IN_PROGRESS;
}
Role *role = &actor->role;
if (!role) {
return NO_ROLE;
}
if (!actionBelongsToRole(role, action)) {
return ACTION_DOES_NOT_BELONG_TO_ROLE;
}
if (!isActionAllowed(action, relatedEvents)) {
return ACTION_PROHIBITED;
}
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();
}
bool isActionAllowed(const Action *action, std::vector<Event> *relevantEvents) {
// actions are disabled by default
bool allowed = false;
std::sort(relevantEvents->begin(), relevantEvents->end());
for (auto &event : *relevantEvents) {
if (std::find(event.prohibits.begin(), event.prohibits.end(), *action) != event.prohibits.end()) {
allowed = false;
}
if (std::find(event.allows.begin(), event.allows.end(), *action) != event.allows.end()) {
allowed = true;
}
}
return allowed;
}

View File

@ -1,10 +1 @@
#include "action.hh"
#include "player.hh"
#include "prep/prep.hh"
#include "role.hh"
#include "room.hh"
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);
#include "validation.hh"

View File

@ -12,10 +12,10 @@ int main(int argc, char *argv[]) {
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(69, "player1", role1, PlayerStatus::ALIVE);
Player player2 = Player(420, "player2", role1, PlayerStatus::ALIVE);
Room room1(1, "Room 1", 1710087364, RoomStatus::IN_PROGRESS, {player1, player2});
Room room2(2, "Room 2", 1710087384, RoomStatus::ENDED, {});
Player player1 = Player(69, "player1", role1, player::Alive);
Player player2 = Player(420, "player2", role1, player::Alive);
Room room1(1, "Room 1", 1710087364, room::InProgress, {player1, player2});
Room room2(2, "Room 2", 1710087384, room::Ended, {});
int actionValidated = validateAction(&player1, &kill, &room1, &relatedEvents, &player2);
printf("The action validation result is %u\n", actionValidated);
return EXIT_SUCCESS;

View File

@ -4,7 +4,7 @@
#include <string>
Player::Player(uint32_t id, std::string username, Role role, PlayerStatus status):
Player::Player(uint32_t id, std::string username, Role role, player::Status status):
id(id),
username(username),
role(role),

View File

@ -7,7 +7,7 @@
#include <string>
#include <vector>
Room::Room(uint32_t id, std::string title, uint32_t created_at, RoomStatus status, std::vector<Player> players):
Room::Room(uint32_t id, std::string title, uint32_t created_at, room::Status status, std::vector<Player> players):
id(id),
title(title),
created_at(create_utc_timestamp(created_at)),
@ -16,7 +16,7 @@ Room::Room(uint32_t id, std::string title, uint32_t created_at, RoomStatus statu
}
Room::Room(
uint32_t id, std::string title, uint32_t created_at, RoomStatus status, std::initializer_list<Player> players):
uint32_t id, std::string title, uint32_t created_at, room::Status status, std::initializer_list<Player> players):
id(id),
title(title),
created_at(create_utc_timestamp(created_at)),

64
src/validation.cc Normal file
View File

@ -0,0 +1,64 @@
#include "validation.hh"
#include "room.hh"
#include <algorithm>
bool player_belongs_to_room(Player *player, Room *room) {
return std::find(room->players.begin(), room->players.end(), *player) != room->players.end();
}
bool action_belongs_to_role(Role *role, const Action *action) {
return std::find(role->actions.begin(), role->actions.end(), *action) != role->actions.end();
}
bool is_action_allowed(const Action *action, std::vector<Event> *relevantEvents) {
// actions are disabled by default
bool allowed = false;
std::sort(relevantEvents->begin(), relevantEvents->end());
for (auto &event : *relevantEvents) {
if (std::find(event.prohibits.begin(), event.prohibits.end(), *action) != event.prohibits.end()) {
allowed = false;
}
if (std::find(event.allows.begin(), event.allows.end(), *action) != event.allows.end()) {
allowed = true;
}
}
return allowed;
}
int validateAction(
Player *actor, const Action *action, Room *room, std::vector<Event> *relatedEvents, Player *target = nullptr) {
if (!actor) {
return validation::NoActor;
}
if (!action) {
return validation::NoAction;
}
if (!room) {
return validation::NoRoom;
}
if (!relatedEvents) {
return validation::NoRelatedEvents;
}
if (!player_belongs_to_room(actor, room)) {
return validation::PlayerNotInRoom;
}
if (action->has_target && !target) {
return validation::NoTargetPlayerSpecified;
}
if (room->status != room::Status::InProgress) {
return validation::RoomNotInProgress;
}
Role *role = &actor->role;
if (!role) {
return validation::NoRole;
}
if (!action_belongs_to_role(role, action)) {
return validation::ActionDoesNotBelongToRole;
}
if (!is_action_allowed(action, relatedEvents)) {
return validation::ActionProhibited;
}
return validation::ActionProhibited;
}

26
src/validation.hh Normal file
View File

@ -0,0 +1,26 @@
#ifndef VALIDATION_HH
#define VALIDATION_HH
#include "event.hh"
#include "player.hh"
#include "room.hh"
namespace validation {
enum Status {
PlayerNotInRoom,
NoTargetPlayerSpecified,
RoomNotInProgress,
ActionDoesNotBelongToRole,
ActionProhibited,
NoActor,
NoAction,
NoRole,
NoRoom,
NoRelatedEvents,
ActionValid,
};
}
int validateAction(Player *actor, const Action *action, Room *room, std::vector<Event> *relatedEvents, Player *target);
#endif // !VALIDATION_HH