-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol.cc
270 lines (233 loc) · 8.79 KB
/
protocol.cc
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include "protocol.h"
#include <iostream>
#include <sstream>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
bool protocol::is_gameover_msg (const std::string &msg) {
return msg.find ("GAMEOVER") != std::string::npos;
}
bool protocol::is_play_msg (const std::string &msg) {
return msg.find ("PLAY") != std::string::npos;
}
// A GRAPH message has the following format:
// GRAPH: <dimension of board>; <list of node_info separated by
// comma> ; <list of edge_info separated by comma>
// dimension of board is x_d y_d
graph protocol::parse_graph (const std::string &msg) {
int x_d;
int y_d;
std::map< int, std::pair<int, int> > id2coord;
std::vector< std::pair<int, int> > edges;
std::string msg_ = msg.substr (msg.find (":") + 1);
std::vector<std::string> strs;
boost::split(strs, msg_, boost::is_any_of(";"));
std::stringstream ss (std::stringstream::in | std::stringstream::out);
// strs[0] corresponds to dimension
ss << strs[0];
ss >> x_d >> y_d;
// strs[1] corresponds to node_infos
std::vector<std::string> node_strs;
boost::split(node_strs, strs[1], boost::is_any_of(","));
for (int i = 0; i < node_strs.size(); i++) {
// if I reuse previous ss, it will generate wrong result for me.
std::stringstream sss (std::stringstream::in | std::stringstream::out);
sss << node_strs[i];
int node_id, x, y;
sss >> node_id >> x >> y;
id2coord.insert (std::pair< int, std::pair<int, int> > (node_id, std::pair<int, int> (x, y)) );
}
// strs[2] corresponds to edges
std::vector<std::string> edge_strs;
boost::split (edge_strs, strs[2], boost::is_any_of(","));
for (int i = 0; i< edge_strs.size(); i++) {
std::stringstream sss (std::stringstream::in | std::stringstream::out);
sss.str(edge_strs[i]);
std::pair<int, int> edge;
sss >> edge.first >> edge.second;
edges.push_back (edge);
}
graph ret (x_d, y_d);
ret.set_nodes (id2coord);
ret.set_edges (edges);
return ret;
}
std::string protocol::generate_graph_msg ( std::map <int, std::pair<int, int> > &nodes,
std::vector< std::pair<int, int> > &edges ) {
std::stringstream ss (std::stringstream::in | std::stringstream::out);
ss << "20 10;";
std::map<int, std::pair<int,int> >::iterator iter;
for (iter = nodes.begin(); iter != nodes.end(); ) {
ss << iter->first << " " << iter->second.first << " " << iter->second.second;
iter++;
if ( nodes.end() != iter ) { ss << ","; }
}
ss << ";";
for (int i = 0; i < edges.size(); i++) {
ss << edges[i].first << " " << edges[i].second;
if ( (edges.size() - 1) != i ) { ss << ","; }
}
return ss.str();
}
// PLAY: <red_score>; <blue_score>; <red_left>; <red_nanomunchers>;
// <blue_left>; <blue_nanomunchers>; <eaten_nodes>
state protocol::parse_play (const std::string &msg) {
int red_score = 0;
int blue_score = 0;
int red_left = 0;
int blue_left = 0;
std::vector<muncher> red_munchers;
std::vector<muncher> blue_munchers;
std::vector<int> eaten_nodes;
// preprocess
std::string play_msg = msg.substr(msg.find(":") + 1);
boost::algorithm::trim (play_msg);
std::vector<std::string> strs;
boost::split(strs, play_msg, boost::is_any_of(";"));
// std::stringstream ss (std::stringstream::in | std::stringstream::out);
// strs[0] stores red score
red_score = boost::lexical_cast< int > (strs[0]);
// strs[1] stores blue score
// ss.str("");
// ss << strs[1];
// ss >> blue_score;
blue_score = boost::lexical_cast< int > (strs[1]);
// strs[2] stores red left
// ss.str("");
// ss << strs[2];
// ss >> red_left;
red_score = boost::lexical_cast< int > (strs[2]);
// strs[3] stores alive red nanomunchers if any
std::vector<std::string> red_muncher_strs;
boost::split(red_muncher_strs, strs[3], boost::is_any_of(","));
for (int i = 0; i < red_muncher_strs.size(); i++) {
if (red_muncher_strs[i].empty() ) continue;
std::istringstream iss (red_muncher_strs[i]);
int node_id, count;
std::string program;
iss >> node_id >> program >> count;
muncher m (node_id, program, count);
red_munchers.push_back (m);
}
// strs[4] stores blue left
// ss.str("");
// ss << strs[4];
// ss >> blue_left;
blue_left = boost::lexical_cast< int > (strs[4]);
// strs[5] stores allive blue nanomunchers if any
std::vector<std::string> blue_muncher_strs;
boost::split(blue_muncher_strs, strs[5], boost::is_any_of(","));
for (int i = 0; i< blue_muncher_strs.size(); i++) {
if (blue_muncher_strs[i].empty() ) continue;
std::istringstream iss (blue_muncher_strs[i]);
int node_id, count;
std::string program;
iss >> node_id >> program >> count;
muncher m (node_id, program, count);
blue_munchers.push_back (m);
}
// strs[6] stores eaten nodes
std::vector<std::string> eaten_node_strs;
boost::split(eaten_node_strs, strs[6], boost::is_any_of(","));
for (int i = 0; i < eaten_node_strs.size(); i++) {
if (eaten_node_strs[i].empty()) continue;
std::istringstream iss (eaten_node_strs[i]);
int node_id = -1;
iss >> node_id;
eaten_nodes.push_back (node_id);
}
state new_state ( red_score, blue_score, red_left, red_munchers,
blue_left, blue_munchers, eaten_nodes );
return new_state;
}
std::string protocol::generate_play_msg (const state & s) {
std::stringstream ss (std::stringstream::in | std::stringstream::out);
ss << "PLAY: " << s.get_red_score() << ";" << s.get_blue_score() << ";";
ss << s.get_red_left() << ";";
std::vector<muncher> red_munchers = s.get_red_munchers();
for (int i = 0; i < red_munchers.size(); i++) {
ss << red_munchers[i].get_nodeid() << " "
<< red_munchers[i].get_program() << " "
<< red_munchers[i].get_count();
if ( (red_munchers.size() - 1) != i) { ss << ","; }
}
ss << ";";
ss << s.get_blue_left() << ";";
std::vector<muncher> blue_munchers = s.get_blue_munchers();
for (int i = 0; i < blue_munchers.size(); i++) {
ss << blue_munchers[i].get_nodeid() << " "
<< blue_munchers[i].get_program() << " "
<< blue_munchers[i].get_count();
if ( (blue_munchers.size() - 1) != i) { ss << ","; }
}
ss << ";";
std::vector<int> eaten_nodes = s.get_eaten_nodes();
for (int i = 0; i < eaten_nodes.size(); i++) {
ss << eaten_nodes[i];
if ( (eaten_nodes.size() - 1) != i ) { ss << ","; }
}
return ss.str();
}
std::vector<muncher> protocol::parse_add (int player , const std::string &msg) {
std::string add_msg = msg.substr(msg.find(":") + 1);
boost::algorithm::trim (add_msg);
std::vector<muncher> munchers;
std::vector<std::string> muncher_strs;
boost::split(muncher_strs, add_msg, boost::is_any_of(","));
for (int i = 0; i < muncher_strs.size(); i++) {
if ( muncher_strs[i].empty() ) continue;
std::istringstream iss (muncher_strs[i]);
int nodeid, count;
std::string program;
iss >> nodeid >> program >> count;
muncher alive_muncher (nodeid, program, count, player);
munchers.push_back (alive_muncher);
}
return munchers;
}
std::string protocol::generate_add_msg(const std::vector<muncher> & munchers) {
std::stringstream ss (std::stringstream::in | std::stringstream::out);
ss << "ADD: ";
for (int i = 0; i < munchers.size(); i++) {
ss << munchers[i].get_nodeid() << " "
<< munchers[i].get_program() << " "
<< munchers[i].get_count();
if (munchers.size() - 1 != i) { ss << ","; }
}
return ss.str();
}
std::string protocol::generate_gameover_msg (int red_score, int blue_score) {
std::stringstream ss (std::stringstream::in | std::stringstream::out);
std::string result;
if (red_score > blue_score) { result.assign ("RED"); }
if (red_score < blue_score) { result.assign ("BLUE"); }
if (red_score == blue_score) { result.assign ("TIE"); }
ss << "GAMEOVER: " << result << " " << red_score << " " << blue_score;
return ss.str();
}
std::string protocol::parse_role (const std::string &msg) {
std::string role_msg = msg.substr (msg.find(":") + 1);
return role_msg;
}
std::string protocol::generate_role_msg (const std::string &role_str) {
std::stringstream ss (std::stringstream::in | std::stringstream::out);
ss << "ROLE: " << role_str;
return ss.str();
}
std::string protocol::parse_join (const std::string &msg) {
std::string join_msg = msg.substr (msg.find(":") + 1);
return join_msg;
}
std::string protocol::generate_join_msg (const std::string & teamname) {
std::stringstream ss (std::stringstream::in | std::stringstream::out);
ss << "JOIN: " << teamname;
return ss.str();
}
std::string protocol::parse_ack (const std::string &msg) {
std::string ack_msg = msg.substr (msg.find(":") + 1);
return ack_msg;
}
std::string protocol::generate_ack_msg (const std::string &msg) {
std::stringstream ss (std::stringstream::in | std::stringstream::out);
ss << "ACK: " << msg;
return ss.str();
}