mirror of
https://github.com/jorenchik/testing-spring-2024.git
synced 2025-10-21 20:10:36 +00:00
Merge pull request #16 from jorenchik/develop
top-down rule in validation.cc
This commit is contained in:
commit
9c9929565b
@ -9,48 +9,11 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
/**
|
// These functions need to be declared to use the top-down rule and
|
||||||
* Check if a player belongs to a given room.
|
// make them as module private functions.
|
||||||
*
|
bool player_belongs_to_room(const Player *player, const Room *room);
|
||||||
* @param player Pointer to the player object.
|
bool action_belongs_to_role(const Role *role, const Action *action);
|
||||||
* @param room Pointer to the room object.
|
bool is_action_allowed(const Action *action, std::vector<Event> *relevant_events);
|
||||||
* @return `true` if the player belongs to the room, otherwise `false`.
|
|
||||||
*/
|
|
||||||
bool player_belongs_to_room(const Player *player, const Room *room) {
|
|
||||||
return std::find(room->players.begin(), room->players.end(), *player) != room->players.end();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if an action belongs to a given role.
|
|
||||||
*
|
|
||||||
* @param role Pointer to the role object.
|
|
||||||
* @param action Pointer to the action object.
|
|
||||||
* @return `true` if the action belongs to the role, otherwise `false`.
|
|
||||||
*/
|
|
||||||
bool action_belongs_to_role(const Role *role, const Action *action) {
|
|
||||||
return std::find(role->actions.begin(), role->actions.end(), *action) != role->actions.end();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if an action is allowed based on relevant events.
|
|
||||||
*
|
|
||||||
* @param action Pointer to the action object.
|
|
||||||
* @param relevantEvents Pointer to the vector of relevant events.
|
|
||||||
* @return `true` if the action is allowed, otherwise `false`.
|
|
||||||
*/
|
|
||||||
bool is_action_allowed(const Action *action, std::vector<Event> *relevant_events) {
|
|
||||||
bool allowed = false; // Actions are disabled by default
|
|
||||||
std::sort(relevant_events->begin(), relevant_events->end());
|
|
||||||
for (const auto &event : *relevant_events) {
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validate if an action is valid for a player in a room based on related events.
|
* Validate if an action is valid for a player in a room based on related events.
|
||||||
@ -98,6 +61,49 @@ ValidationStatus validate_action(
|
|||||||
return ValidationStatus::ActionValid;
|
return ValidationStatus::ActionValid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if a player belongs to a given room.
|
||||||
|
*
|
||||||
|
* @param player Pointer to the player object.
|
||||||
|
* @param room Pointer to the room object.
|
||||||
|
* @return `true` if the player belongs to the room, otherwise `false`.
|
||||||
|
*/
|
||||||
|
bool player_belongs_to_room(const Player *player, const Room *room) {
|
||||||
|
return std::find(room->players.begin(), room->players.end(), *player) != room->players.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if an action belongs to a given role.
|
||||||
|
*
|
||||||
|
* @param role Pointer to the role object.
|
||||||
|
* @param action Pointer to the action object.
|
||||||
|
* @return `true` if the action belongs to the role, otherwise `false`.
|
||||||
|
*/
|
||||||
|
bool action_belongs_to_role(const Role *role, const Action *action) {
|
||||||
|
return std::find(role->actions.begin(), role->actions.end(), *action) != role->actions.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if an action is allowed based on relevant events.
|
||||||
|
*
|
||||||
|
* @param action Pointer to the action object.
|
||||||
|
* @param relevantEvents Pointer to the vector of relevant events.
|
||||||
|
* @return `true` if the action is allowed, otherwise `false`.
|
||||||
|
*/
|
||||||
|
bool is_action_allowed(const Action *action, std::vector<Event> *relevant_events) {
|
||||||
|
bool allowed = false; // Actions are disabled by default
|
||||||
|
std::sort(relevant_events->begin(), relevant_events->end());
|
||||||
|
for (const auto &event : *relevant_events) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
std::string ValidationStatusUtils::to_string(ValidationStatus status) {
|
std::string ValidationStatusUtils::to_string(ValidationStatus status) {
|
||||||
switch (status) {
|
switch (status) {
|
||||||
case ValidationStatus::PlayerNotInRoom: return "player not in room";
|
case ValidationStatus::PlayerNotInRoom: return "player not in room";
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user