-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_game.cpp
126 lines (117 loc) · 3.81 KB
/
setup_game.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "setup_game.hpp"
#include "game.hpp"
#include "invalid_event.hpp"
#include "jeopardy_exception.hpp"
#include "scoreboard.hpp"
using namespace std;
using namespace rapidjson;
setup_game::setup_game(struct game_state_params *params)
: game_state(params)
{
this->next_player_id = 0;
this->edit_player_active = false;
}
setup_game::setup_game(const rapidjson::GenericValue<rapidjson::UTF8<>> &root, struct game_state_params *params)
: game_state(root, params)
{
this->next_player_id = root["next_player_id"].GetInt();
this->edit_player_active = false;
}
void setup_game::initialize()
{
// Nothing to do
}
bool setup_game::process_event(const GenericValue<UTF8<>> &event)
{
string event_type = event["event"].GetString();
if (event_type == "add_player")
{
if (edit_player_active)
throw invalid_event();
edit_player_active = true;
current_playername = "";
current_player_connected = false;
playercolor = color(event["color"].GetString());
Document d;
current_state(d);
server.broadcast(d);
return false;
}
else if (event_type == "update_player_name")
{
if (!edit_player_active)
throw invalid_event();
current_playername = event["name"].GetString();
Document d;
current_state(d);
server.broadcast(d);
return false;
}
else if (event_type == "confirm_player")
{
if (!edit_player_active)
throw invalid_event();
if (!current_player_connected)
throw jeopardy_exception("The player doesn't have a buzzer assigned");
edit_player_active = false;
players.emplace_back(to_string(next_player_id++), current_playername, color(playercolor), current_player_buzzer);
Document d;
current_state(d);
server.broadcast(d);
return true;
}
else if (event_type == "start")
{
if (edit_player_active)
throw invalid_event();
if (players.size() < 1)
throw jeopardy_exception("At least one player is required to start the game");
next_state.reset(new scoreboard(params));
return true;
}
else
{
throw invalid_event();
}
}
bool setup_game::on_buzz(const buzzer &buzzer)
{
if (!edit_player_active)
return false;
if (current_player_connected)
return false;
if (find_if(players.begin(), players.end(), [buzzer](const player &player){return player.is_connected() && buzzer == player.get_buzzer();}) != players.end())
return false;
current_player_connected = true;
current_player_buzzer = buzzer;
Document d;
current_state(d);
server.broadcast(d);
return false;
}
void setup_game::current_state(rapidjson::Document &d)
{
d.SetObject();
d.AddMember("state", "setup", d.GetAllocator());
Value scoreboard;
game::make_scoreboard(scoreboard, categories, d.GetAllocator());
d.AddMember("scoreboard", scoreboard, d.GetAllocator());
Value playersValue;
game::list_players(playersValue, players, d.GetAllocator());
d.AddMember("players", playersValue, d.GetAllocator());
Value new_player;
if (edit_player_active)
{
new_player.SetObject();
new_player.AddMember("name", Value(current_playername, d.GetAllocator()), d.GetAllocator());
new_player.AddMember("color", Value(playercolor.string(), d.GetAllocator()), d.GetAllocator());
new_player.AddMember("connected", current_player_connected, d.GetAllocator());
}
d.AddMember("new_player", new_player, d.GetAllocator());
}
void setup_game::store_state(rapidjson::Document &root)
{
root.AddMember("state", "setup_game", root.GetAllocator());
game_state::store_state(root);
root.AddMember("next_player_id", next_player_id, root.GetAllocator());
}