Repository navigation
Expand file tree
/
Copy pathevent.cpp
More file actions
244 lines (211 loc) · 7.02 KB
/
Copy pathevent.cpp
File metadata and controls
244 lines (211 loc) · 7.02 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include "../include/event.h"
#include "../include/json.hpp"
#include "UserData.h"
#include <fstream>
#include <string>
#include <map>
#include <vector>
using json = nlohmann::json;
Event::Event(string reporter, std::string team_a_name, std::string team_b_name, std::string name, int time,
std::map<std::string, std::string> game_updates, std::map<std::string, std::string> team_a_updates,
std::map<std::string, std::string> team_b_updates, std::string description)
: reporter(reporter), team_a_name(team_a_name), team_b_name(team_b_name), name(name),
time(time), game_updates(game_updates), team_a_updates(team_a_updates),
team_b_updates(team_b_updates), description(description){}
const string &Event::get_reporter() const {
return reporter;
}
const std::string &Event::get_team_a_name() const
{
return this->team_a_name;
}
const std::string &Event::get_team_b_name() const
{
return this->team_b_name;
}
const std::string &Event::get_name() const
{
return this->name;
}
int Event::get_time() const
{
return this->time;
}
const std::map<std::string, std::string> &Event::get_game_updates() const
{
return this->game_updates;
}
const std::map<std::string, std::string> &Event::get_team_a_updates() const
{
return this->team_a_updates;
}
const std::map<std::string, std::string> &Event::get_team_b_updates() const
{
return this->team_b_updates;
}
const std::string &Event::get_description() const
{
return this->description;
}
Event::Event(const std::string &frame_body)
: reporter(""), team_a_name(""), team_b_name(""), name(""),
time(0), game_updates(), team_a_updates(),
team_b_updates(), description(""){
// login 127.0.0.1:7777 porn hub
istringstream iss(frame_body);
string line;
//reporter
getline(iss, line);
if (line.find("user:") != string::npos)
{
reporter = line.substr(line.find(":") + 2);
getline(iss, line);
}
//team_a_name
if (line.find("team_a_name:") != string::npos)
{
team_a_name = line.substr(line.find(":") + 2);
getline(iss, line);
}
//team_b_name
if (line.find("team_b_name:") != string::npos)
{
team_b_name = line.substr(line.find(":") + 2);
getline(iss, line);
}
//name
if (line.find("event name:") != string::npos)
{
name = line.substr(line.find(":") + 2);
getline(iss, line);
}
//time
if (line.find("time:") != string::npos)
{
time = stoi(line.substr(line.find(":") + 2));
getline(iss, line);
}
//general updates
if (line.find("general game updates:") != string::npos)
{
while (getline(iss, line) && line.find("team a updates:") == string::npos)
{
line = line.substr(0, line.length()-1);
int delimiter = line.find(":");
string key = line.substr(0, delimiter);
string value = line.substr(delimiter + 2);
game_updates[key] = value;
}
}
//team_a_updates
if (line.find("team a updates:") != string::npos)
{
while (getline(iss, line) && line.find("team b updates:") == string::npos)
{
line = line.substr(0, line.length()-1);
int delimiter = line.find(":");
string key = line.substr(0, delimiter);
string value = line.substr(delimiter + 2);
team_a_updates[key] = value;
}
}
//team_b_updates
if (line.find("team b updates:") != string::npos)
{
while (getline(iss, line) && line.find("description:") == string::npos)
{
line = line.substr(0, line.length()-1);
int delimiter = line.find(":");
string key = line.substr(0, delimiter);
string value = line.substr(delimiter + 2);
team_b_updates[key] = value;
}
}
//description
if (line.find("description:") != string::npos)
{
getline(iss, line);
description = line;
}
}
string Event::toJson() {
json j;
j["user"] = reporter;
j["team_a_name"] = team_a_name;
j["team_b_name"] = team_b_name;
j["name"] = name;
j["time"] = time;
j["game_updates"] = game_updates;
j["team_a_updates"] = team_a_updates;
j["team_b_updates"] = team_b_updates;
j["description"] = description;
return j.dump();
}
string Event::extractFrameBody() {
string output = "";
output += "user: " + reporter+"\n";
output += "team_a_name: " + team_a_name+"\n";
output += "team_b_name: " + team_b_name+"\n";
output += "event name: " + name+"\n";
output += "time: " + to_string(time)+"\n";
output += "general game updates: \n";
for (auto & gameUpdate : game_updates) {
output += gameUpdate.first + ": " + gameUpdate.second + ",\n";
}
output += "team a updates: \n";
for (auto & teamAUpdate : team_a_updates) {
output += teamAUpdate.first + ": " + teamAUpdate.second + ",\n";
}
output += "team b updates: \n";
for (auto & teamBUpdate : team_b_updates) {
output += teamBUpdate.first + ": " + teamBUpdate.second + ",\n";
}
output += "description: \n" + description;
return output;
}
const string Event::get_game_name() const {
return team_a_name + "_" + team_b_name;
}
names_and_events parseEventsFile(std::string json_path)
{
std::ifstream f(json_path);
json data = json::parse(f);
std::string team_a_name = data["team a"];
std::string team_b_name = data["team b"];
// run over all the events and convert them to Event objects
std::vector<Event> events;
for (auto &event : data["events"])
{
std::string name = event["event name"];
int time = event["time"];
std::string description = event["description"];
std::map<std::string, std::string> game_updates;
std::map<std::string, std::string> team_a_updates;
std::map<std::string, std::string> team_b_updates;
for (auto &update : event["general game updates"].items())
{
if (update.value().is_string())
game_updates[update.key()] = update.value();
else
game_updates[update.key()] = update.value().dump();
}
for (auto &update : event["team a updates"].items())
{
if (update.value().is_string())
team_a_updates[update.key()] = update.value();
else
team_a_updates[update.key()] = update.value().dump();
}
for (auto &update : event["team b updates"].items())
{
if (update.value().is_string())
team_b_updates[update.key()] = update.value();
else
team_b_updates[update.key()] = update.value().dump();
}
events.push_back(Event(UserData::getInstance().getUserName(), team_a_name, team_b_name, name, time, game_updates, team_a_updates,
team_b_updates, description));
}
names_and_events events_and_names{team_a_name, team_b_name, events};
return events_and_names;
}