Repository navigation
Expand file tree
/
Copy pathUserData.cpp
More file actions
124 lines (103 loc) · 2.46 KB
/
Copy pathUserData.cpp
File metadata and controls
124 lines (103 loc) · 2.46 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
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
using namespace std;
#include "../include/frames/Frame.h"
#include "../include/event.h"
#include "UserData.h"
#include "ConnectionHandler.h"
#include "Summary.h"
#include <ios>
UserData *UserData::instance;
UserData::UserData()
: shouldTerminateFlag(false), connected(false), nextReceiptNumber(0), nextSubscriptionNumber(0),
userName(), m(), cv(), handler(nullptr), gameNameToSubId(), subIdToGameName(), gameSummaries() {}
UserData &UserData::getInstance()
{
if (instance == nullptr)
{
instance = new UserData();
}
return *instance;
}
void UserData::setUserName(string userName)
{
this->userName = userName;
}
string &UserData::getUserName()
{
return userName;
}
bool UserData::shouldTerminate()
{
return shouldTerminateFlag;
}
void UserData::terminate()
{
shouldTerminateFlag = true;
}
bool UserData::isConnected()
{
return connected;
}
void UserData::setConnected(bool connected)
{
this->connected = connected;
}
void UserData::setHandler(ConnectionHandler &handler)
{
this->handler = &handler;
}
ConnectionHandler &UserData::getHandler()
{
return *handler;
}
void UserData::deleteInstance(bool b1, bool b2, bool b3)
{
if (b1 & b2 & b3)
delete instance;
instance = nullptr;
}
UserData::~UserData()
{
delete handler;
for (auto &pair : gameSummaries)
{
delete pair.second;
}
}
int UserData::getReceiptId()
{
return nextReceiptNumber++;
}
int UserData::generateSubId(string topic)
{
return nextSubscriptionNumber++;
}
int UserData::getSubId(string topic)
{
return gameNameToSubId[topic];
}
void UserData::addGameEvent(Event &gameEvent) {
string reporter = gameEvent.get_reporter();
string gameName = gameEvent.get_game_name();
GameReport gameReport(reporter, gameName);
auto it = gameSummaries.find(gameReport);
Summary* summary;
if (it != gameSummaries.end()) {
summary = it->second;
} else {
summary = new Summary(reporter, gameName);
gameSummaries[gameReport] = summary;
}
summary->addEvent(gameEvent);
}
string UserData::getSummary(string reporter, string gameName) const
{
GameReport gameReport(reporter, gameName);
Summary *summary = nullptr;
try {
summary = gameSummaries.at(gameReport);
} catch (const std::out_of_range& ignored) {
throw std::ios_base::failure("No such game, or no such reporter");
}
string summaryString = summary->printSummary();
return summaryString;
}