Merge pull request #12 from jorenchik/develop

finish function for updated reqs
This commit is contained in:
Jorens Shtekels 2024-03-12 22:47:09 +02:00 committed by GitHub
commit 8e06b54380
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 75 additions and 71 deletions

View File

@ -3,43 +3,60 @@
#include "prep/prep.hh" #include "prep/prep.hh"
#include <algorithm> #include <algorithm>
#include <iostream> #include <cstdio>
#include <ostream>
#include <vector> #include <vector>
enum VALIDATION_STATUS { enum VALIDATION_STATUS {
NO_PLAYER, PLAYER_NOT_IN_ROOM,
NO_TARGET_PLAYER_SPECIFIED,
ROOM_NOT_IN_PROGRESS, ROOM_NOT_IN_PROGRESS,
ACTION_DOES_NOT_BELONG_TO_ROLE,
ACTION_PROHIBITED, ACTION_PROHIBITED,
NO_ACTOR,
NO_ACTION,
NO_ROLE, NO_ROLE,
NO_ROOM,
NO_RELATED_EVENTS,
ACTION_VALID, ACTION_VALID,
}; };
void run() { void run() {
Room room1(1, "Room 1", 1710087364, RoomStatus::IN_PROGRESS);
Room room2(2, "Room 2", 1710087384, RoomStatus::ENDED);
const Action kill = Action("kill", true); const Action kill = Action("kill", true);
const Action heal = Action("heal", true); const Action heal = Action("heal", true);
const Action vote = Action("vote", true); const Action vote = Action("vote", true);
Role role1({vote, kill, heal}); Role role1({vote, kill, heal});
Role role2({heal}); Role role2({heal});
Event event1 = Event("Event 1", 1710087355, 1, true, {vote}, {}, {}); Event event1 = Event("Event 1", 1710087355, 1, true, {}, {});
Event event2 = Event("Event 2", 1710087363, 1, true, {}, {kill}, {}); Event event2 = Event("Event 2", 1710087363, 1, true, {kill}, {});
Event event3 = Event("Event 3", 1710087369, 1, true, {}, {}, {kill}); Event event3 = Event("Event 3", 1710087369, 1, true, {}, {kill});
std::vector<Event> relatedEvents({event2, event3}); std::vector<Event> relatedEvents({event2, event3});
Player player1 = Player("player1", role1, PlayerStatus::ALIVE); Player player1 = Player(69, "player1", role1, PlayerStatus::ALIVE);
Player player2 = Player("player2", 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, {});
int actionValidated = validateAction(&player1, &kill, &room1, &relatedEvents, &player2); int actionValidated = validateAction(&player1, &kill, &room1, &relatedEvents, &player2);
std::cout << actionValidated << std::endl; printf("The action validation result is %u\n", actionValidated);
} }
int validateAction( int validateAction(
Player *actor, const Action *action, Room *room, std::vector<Event> *relatedEvents, Player *target = nullptr) { Player *actor, const Action *action, Room *room, std::vector<Event> *relatedEvents, Player *target = nullptr) {
if (!actor) { if (!actor) {
return NO_PLAYER; 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) { if (action->hasTarget && !target) {
return NO_PLAYER; return NO_TARGET_PLAYER_SPECIFIED;
} }
if (room->status != RoomStatus::IN_PROGRESS) { if (room->status != RoomStatus::IN_PROGRESS) {
return ROOM_NOT_IN_PROGRESS; return ROOM_NOT_IN_PROGRESS;
@ -49,7 +66,7 @@ int validateAction(
return NO_ROLE; return NO_ROLE;
} }
if (!actionBelongsToRole(role, action)) { if (!actionBelongsToRole(role, action)) {
return ACTION_PROHIBITED; return ACTION_DOES_NOT_BELONG_TO_ROLE;
} }
if (!isActionAllowed(action, relatedEvents)) { if (!isActionAllowed(action, relatedEvents)) {
return ACTION_PROHIBITED; return ACTION_PROHIBITED;
@ -57,6 +74,10 @@ int validateAction(
return ACTION_VALID; 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) { bool actionBelongsToRole(Role *role, const Action *action) {
return std::find(role->actions.begin(), role->actions.end(), *action) != role->actions.end(); return std::find(role->actions.begin(), role->actions.end(), *action) != role->actions.end();
} }
@ -75,7 +96,3 @@ bool isActionAllowed(const Action *action, std::vector<Event> *relevantEvents) {
} }
return allowed; return allowed;
} }
int functionToTest(int a) {
return a * 2;
}

View File

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

View File

@ -2,12 +2,9 @@
#include "timeUtils.hh" #include "timeUtils.hh"
#include <cstdint>
#include <initializer_list> #include <initializer_list>
int add(int a, int b) {
return a + b;
}
Action::Action(std::string name, bool hasTarget) { Action::Action(std::string name, bool hasTarget) {
this->name = name; this->name = name;
this->hasTarget = hasTarget; this->hasTarget = hasTarget;
@ -17,28 +14,38 @@ bool Action::operator==(const Action &other) const {
return this->name == other.name; return this->name == other.name;
} }
Role::Role(std::initializer_list<Action> actions) { Role::Role(std::initializer_list<Action> actions): Role(std::vector<Action>(actions)) {
for (auto &a : actions) {
this->actions.push_back(a);
}
} }
Role::Role(std::vector<Action> actions) { Role::Role(std::vector<Action> actions): actions(actions) {
for (auto &a : actions) {
this->actions.push_back(a);
}
} }
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), username(username),
role(role), role(role),
playerStatus(playerStatus) { playerStatus(playerStatus) {
} }
Room::Room(uint32_t id, std::string title, uint32_t utcTimestampCreatedAt, RoomStatus status): 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), id(id),
title(title), title(title),
status(status) { status(status),
players(players) {
this->utcTimestampCreatedAt = createUTCTimestamp(utcTimestampCreatedAt);
}
Room::Room(uint32_t id,
std::string title,
uint32_t utcTimestampCreatedAt,
RoomStatus status,
std::initializer_list<Player> players):
Room(id, title, utcTimestampCreatedAt, status, std::vector<Player>(players)) {
this->utcTimestampCreatedAt = createUTCTimestamp(utcTimestampCreatedAt); this->utcTimestampCreatedAt = createUTCTimestamp(utcTimestampCreatedAt);
} }
@ -46,37 +53,27 @@ Event::Event(std::string title,
uint32_t utcTimestampCreatedAt, uint32_t utcTimestampCreatedAt,
uint32_t numberNight, uint32_t numberNight,
bool isVisible, bool isVisible,
std::vector<Action> causedBy,
std::vector<Action> prohibits, std::vector<Action> prohibits,
std::vector<Action> allows): std::vector<Action> allows):
title(title), title(title),
utcTimestampCreatedAt(createUTCTimestamp(utcTimestampCreatedAt)), utcTimestampCreatedAt(createUTCTimestamp(utcTimestampCreatedAt)),
numberNight(numberNight), numberNight(numberNight),
isVisible(isVisible) { isVisible(isVisible),
prohibits(prohibits),
allows(allows) {
this->utcTimestampCreatedAt = createUTCTimestamp(utcTimestampCreatedAt); this->utcTimestampCreatedAt = createUTCTimestamp(utcTimestampCreatedAt);
for (auto &a : causedBy) {
this->causedBy.push_back(a);
}
for (auto &a : prohibits) {
this->prohibits.push_back(a);
}
for (auto &a : allows) {
this->allows.push_back(a);
}
} }
Event::Event(std::string title, Event::Event(std::string title,
uint32_t utcTimestampCreatedAt, uint32_t utcTimestampCreatedAt,
uint32_t numberNight, uint32_t numberNight,
bool isVisible, bool isVisible,
std::initializer_list<Action> causedBy,
std::initializer_list<Action> prohibits, std::initializer_list<Action> prohibits,
std::initializer_list<Action> allows): std::initializer_list<Action> allows):
Event(title, Event(title,
utcTimestampCreatedAt, utcTimestampCreatedAt,
numberNight, numberNight,
isVisible, isVisible,
std::vector<Action>(causedBy),
std::vector<Action>(prohibits), std::vector<Action>(prohibits),
std::vector<Action>(allows)) { std::vector<Action>(allows)) {
this->utcTimestampCreatedAt = createUTCTimestamp(utcTimestampCreatedAt); this->utcTimestampCreatedAt = createUTCTimestamp(utcTimestampCreatedAt);

View File

@ -1,15 +1,11 @@
#ifndef PREP_H #ifndef PREP_H
#define PREP_H #define PREP_H
#include <chrono>
#include <cstdint> #include <cstdint>
#include <ctime> #include <ctime>
#include <string> #include <string>
#include <vector> #include <vector>
// For test example
int add(int a, int b);
// All IDs are uint32_t // All IDs are uint32_t
enum EventType { enum EventType {
PHASE_CHANGE, PHASE_CHANGE,
@ -53,11 +49,13 @@ struct Role {
}; };
struct Player { struct Player {
uint32_t id;
std::string username; std::string username;
Role role; Role role;
PlayerStatus playerStatus; 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 { struct Room {
@ -65,8 +63,15 @@ struct Room {
std::string title; std::string title;
std::tm *utcTimestampCreatedAt; std::tm *utcTimestampCreatedAt;
RoomStatus status; RoomStatus status;
std::vector<Player> players;
Room(uint32_t id, std::string title, uint32_t utcTimestampCreatedAt, RoomStatus status); Room(
uint32_t id, std::string title, uint32_t utcTimestampCreatedAt, RoomStatus status, std::vector<Player> players);
Room(uint32_t id,
std::string title,
uint32_t utcTimestampCreatedAt,
RoomStatus status,
std::initializer_list<Player> players);
}; };
struct Event { struct Event {
@ -74,7 +79,6 @@ struct Event {
std::tm *utcTimestampCreatedAt; std::tm *utcTimestampCreatedAt;
uint32_t numberNight; uint32_t numberNight;
bool isVisible; bool isVisible;
std::vector<Action> causedBy;
std::vector<Action> prohibits; std::vector<Action> prohibits;
std::vector<Action> allows; std::vector<Action> allows;
@ -86,14 +90,12 @@ struct Event {
uint32_t utcTimestampCreatedAt, uint32_t utcTimestampCreatedAt,
uint32_t numberNight, uint32_t numberNight,
bool isVisible, bool isVisible,
std::vector<Action> causedBy,
std::vector<Action> prohibits, std::vector<Action> prohibits,
std::vector<Action> allows); std::vector<Action> allows);
Event(std::string title, Event(std::string title,
uint32_t utcTimestampCreatedAt, uint32_t utcTimestampCreatedAt,
uint32_t numberNight, uint32_t numberNight,
bool isVisible, bool isVisible,
std::initializer_list<Action> causedBy,
std::initializer_list<Action> prohibits, std::initializer_list<Action> prohibits,
std::initializer_list<Action> allows); std::initializer_list<Action> allows);
}; };

View File

@ -1,17 +1,5 @@
#include "lib.hh"
#include "prep/prep.hh"
#include <gtest/gtest.h> #include <gtest/gtest.h>
// TEST(TestSuiteName, testName) {...} TEST(ProgramTest, testFunction) {
/* TEST(ProgramTest, testFunction) { EXPECT_EQ(8, 8);
EXPECT_EQ(functionToTest(4), 8); }
} */
/* TEST(ProgramTest, testFunctionShouldFail) {
EXPECT_EQ(functionToTest(4), 12);
} */
/* TEST(PrepTest, testAddFunc) {
EXPECT_EQ(add(4, 2), 6);
} */