-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.cpp
118 lines (99 loc) · 2.3 KB
/
player.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
#include "player.hpp"
using namespace std;
using namespace std::chrono;
using namespace rapidjson;
player::player(string id, string name, color c, const buzzer &buzzer)
: mybuzzer(buzzer),
buzztime(0)
{
this->id = id;
this->name = name;
this->c = c;
this->score = 0;
this->connected = true;
this->buzzed = false;
}
player::player(const GenericValue<UTF8<>> &root)
{
this->id = root["id"].GetString();
this->name = root["name"].GetString();
this->c = color(root["color"].GetString());
this->score = root["score"].GetInt();
this->buzzed = root["buzzed"].GetBool();
this->buzztime = duration<int, milli>(root["buzztime"].GetInt());
this->connected = false;
}
void player::set_buzztime(const duration<int, milli> &buzztime)
{
this->buzzed = true;
this->buzztime = buzztime;
}
void player::reset_buzztime()
{
this->buzzed = false;
}
void player::add_score(int score)
{
this->score += score;
}
void player::set_score(int score)
{
this->score = score;
}
void player::set_buzzer(const buzzer &mybuzzer)
{
this->mybuzzer = mybuzzer;
this->connected = true;
}
void player::disconnect()
{
this->connected = false;
}
const string & player::get_id() const
{
return id;
}
const string & player::get_name() const
{
return name;
}
const color & player::get_color() const
{
return c;
}
int player::get_score() const
{
return score;
}
bool player::is_connected() const
{
return connected;
}
const buzzer& player::get_buzzer() const
{
return mybuzzer;
}
bool player::has_buzzed() const
{
return buzzed;
}
const std::chrono::duration<int, std::milli>& player::get_buzztime() const
{
return buzztime;
}
GenericValue<UTF8<>> player::buzzed_value() const
{
if (!buzzed)
return Value();
return Value(buzztime.count());
}
void player::store_state(GenericValue<rapidjson::UTF8<>> &root, GenericValue<UTF8<>>::AllocatorType &allocator) const
{
root.SetObject();
root.AddMember("id", Value(id, allocator), allocator);
root.AddMember("name", Value(name, allocator), allocator);
root.AddMember("color", Value(c.string(), allocator), allocator);
root.AddMember("score", score, allocator);
root.AddMember("buzzed", buzzed, allocator);
root.AddMember("buzztime", buzztime.count(), allocator);
}