-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscoreboard.cpp
85 lines (75 loc) · 2.63 KB
/
scoreboard.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "scoreboard.hpp"
#include "answer_screen.hpp"
#include "game.hpp"
#include "invalid_event.hpp"
#include "jeopardy_exception.hpp"
using namespace std;
using namespace rapidjson;
scoreboard::scoreboard(player *current_player, struct game_state_params *params)
: game_state(params)
{
this->current_player = current_player;
}
scoreboard::scoreboard(struct game_state_params *params)
: scoreboard(nullptr, params)
{
// Nothing to do
}
scoreboard::scoreboard(const rapidjson::GenericValue<rapidjson::UTF8<>> &root, struct game_state_params *params)
: game_state(root, params)
{
string current_player_id = root["current_player"].GetString();
auto it = find_if(players.begin(), players.end(), [current_player_id](const player &player){return player.get_id() == current_player_id;});
if (it == players.end())
throw invalid_json("id of current player is invalid");
this->current_player = &*it;
}
void scoreboard::initialize()
{
if (current_player == nullptr)
current_player = &*players.begin(); // Gets the address of the object the iterator points to
}
bool scoreboard::process_event(const GenericValue<UTF8<>> &event)
{
string event_type = event["event"].GetString();
if (event_type == "select_answer")
{
int category_id = event["category"].GetInt();
int answer_id = event["answer"].GetInt();
try
{
next_state.reset(new answer_screen(&categories.at(category_id).get_mutable_answers().at(answer_id), params));
}
catch (out_of_range &)
{
throw jeopardy_exception("Answer " + to_string(category_id) + "," + to_string(answer_id) + " does not exist");
}
return true;
}
else
{
throw invalid_event();
}
}
bool scoreboard::on_buzz(const buzzer &)
{
return false;
}
void scoreboard::current_state(rapidjson::Document &d)
{
d.SetObject();
d.AddMember("state", "scoreboard", d.GetAllocator());
Value scoreboardValue;
game::make_scoreboard(scoreboardValue, categories, d.GetAllocator());
d.AddMember("scoreboard", scoreboardValue, d.GetAllocator());
Value playersValue;
game::list_players(playersValue, players, d.GetAllocator());
d.AddMember("players", playersValue, d.GetAllocator());
d.AddMember("current_player", Value(current_player->get_id(), d.GetAllocator()), d.GetAllocator());
}
void scoreboard::store_state(rapidjson::Document &root)
{
root.AddMember("state", "scoreboard", root.GetAllocator());
game_state::store_state(root);
root.AddMember("current_player", Value(current_player->get_id(), root.GetAllocator()), root.GetAllocator());
}