fix: invalid validation result

This commit is contained in:
Kristofers Solo 2024-03-14 03:28:22 +02:00
parent e316beedbc
commit 72b89de4ce
3 changed files with 6 additions and 5 deletions

View File

@ -19,6 +19,6 @@ int main(int argc, char *argv[]) {
Room room2(2, "Room 2", 1710087384, RoomStatus::Ended, {}); Room room2(2, "Room 2", 1710087384, RoomStatus::Ended, {});
ValidationStatus validated_action = validate_action(&player1, &kill, &room1, &relatedEvents, &player2); ValidationStatus validated_action = validate_action(&player1, &kill, &room1, &relatedEvents, &player2);
std::string validated_action_str = ValidationStatusUtils::to_string(validated_action); std::string validated_action_str = ValidationStatusUtils::to_string(validated_action);
printf("The action validation result is \"%s\"\n", validated_action_str.c_str()); printf("The validation result is \"%s\"\n", validated_action_str.c_str());
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -8,7 +8,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
enum EventType { enum class EventType {
PhaseChange, PhaseChange,
EventAction, EventAction,
RoomStateChange, RoomStateChange,

View File

@ -38,11 +38,12 @@ bool action_belongs_to_role(const Role *role, const Action *action) {
* @param relevantEvents Pointer to the vector of relevant events. * @param relevantEvents Pointer to the vector of relevant events.
* @return `true` if the action is allowed, otherwise `false`. * @return `true` if the action is allowed, otherwise `false`.
*/ */
bool is_action_allowed(const Action *action, const std::vector<Event> *relevant_events) { bool is_action_allowed(const Action *action, std::vector<Event> *relevant_events) {
bool allowed = false; // Actions are disabled by default bool allowed = false; // Actions are disabled by default
std::sort(relevant_events->begin(), relevant_events->end());
for (const auto &event : *relevant_events) { for (const auto &event : *relevant_events) {
if (std::find(event.prohibits.begin(), event.prohibits.end(), *action) != event.prohibits.end()) { if (std::find(event.prohibits.begin(), event.prohibits.end(), *action) != event.prohibits.end()) {
return false; // If action is prohibited, return false immediately allowed = false;
} }
if (std::find(event.allows.begin(), event.allows.end(), *action) != event.allows.end()) { if (std::find(event.allows.begin(), event.allows.end(), *action) != event.allows.end()) {
allowed = true; allowed = true;
@ -94,7 +95,7 @@ ValidationStatus validate_action(
if (!is_action_allowed(action, related_events)) { if (!is_action_allowed(action, related_events)) {
return ValidationStatus::ActionProhibited; return ValidationStatus::ActionProhibited;
} }
return ValidationStatus::ActionProhibited; return ValidationStatus::ActionValid;
} }
std::string ValidationStatusUtils::to_string(ValidationStatus status) { std::string ValidationStatusUtils::to_string(ValidationStatus status) {