-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanswer.cpp
More file actions
84 lines (71 loc) · 1.7 KB
/
answer.cpp
File metadata and controls
84 lines (71 loc) · 1.7 KB
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
#include "answer.hpp"
#include <vector>
#include <boost/filesystem/fstream.hpp>
#include "base64.hpp"
using namespace std;
using namespace boost::filesystem;
using namespace rapidjson;
answer::answer(const GenericValue<UTF8<>> &answer, path category_path, unsigned int points, unsigned int col, unsigned int row)
{
this->col = col;
this->row = row;
this->type = answer["type"].GetString();
this->data = answer["data"].GetString();
this->points = points;
this->data_loaded = false;
this->category_path = category_path;
this->has_winner = false;
this->winner = nullptr;
}
bool answer::is_won() const
{
return has_winner;
}
void answer::set_winner(player *winner)
{
this->has_winner = true;
this->winner = winner;
}
const string & answer::get_type() const
{
return type;
}
const string & answer::get_data() const
{
return data;
}
unsigned int answer::get_points() const
{
return points;
}
unsigned int answer::get_col() const
{
return col;
}
unsigned int answer::get_row() const
{
return row;
}
rapidjson::GenericValue<rapidjson::UTF8<>> answer::winner_value(GenericValue<UTF8<>>::AllocatorType &allocator) const
{
if (!has_winner)
return Value();
if (winner == nullptr)
return Value(false);
return Value(winner->get_id(), allocator);
}
void answer::load_data()
{
if (data_loaded)
return;
if (type != "text") {
boost::filesystem::ifstream file(category_path / data, ios::ate | ios::binary);
auto size = file.tellg();
file.seekg(0);
vector<char> buffer(size);
file.read(buffer.data(), size);
file.close();
data = base64encode(buffer);
}
data_loaded = true;
}