-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.hpp
181 lines (157 loc) · 3.69 KB
/
parser.hpp
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
/**
* @file parser.hpp
* @author Marcus Edel
*
* Miscellaneous parser routines.
*/
#include <mlpack/core.hpp>
#include <iostream>
#include <string>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
namespace parser {
using boost::property_tree::ptree;
class Parser {
public:
/**
* Create the Parser object.
*/
Parser() { /* Nothing to do here */ }
/**
* Create the Parser object using the specified json string and create the
* tree to extract the attributes.
*
* @param data The data encoded as json string.
*/
Parser(const std::string& data)
{
Parse(data);
}
/**
* Parse the specified json string and create a tree to extract the
* attributes.
*
* @param data The data encoded as json string.
*/
void Parse(const std::string& data)
{
std::stringstream ss(data);
boost::property_tree::read_json(ss, pt);
}
/**
* Parse the postion of mario.
*
* @param x The x coordinate of mario.
* @param y The y coordinate of mario.
*/
void MarioPostion(int& x, int& y)
{
x = pt.get_child("mario").get<int>("x");
y = pt.get_child("mario").get<int>("y");
}
/**
* Parse the endpoint data.
*
* @param host The endpoint hostname.
* @param port The endpoint port.
*/
void Endpoint(std::string& host, std::string& port)
{
host = pt.get_child("endpoint").get<std::string>("host");
port = pt.get_child("endpoint").get<std::string>("port");
}
/**
* Parse the tiles data and return in matrix form.
*
* @param tiles The tiles as matrix.
*/
void Tiles(arma::mat& tiles)
{
int radius = 0;
ptree::const_iterator end = pt.get_child("tiles").end();
// Get the number of tiles in one row.
for (ptree::const_iterator it = pt.get_child("tiles").begin();
it != end; ++it, radius++);
// Create the tiles matrix.
tiles = arma::zeros<arma::mat>(radius, radius);
// Get the radius (view field).
radius = std::floor(radius / 2);
// Transform json string to armadillo matrix.
for (ptree::const_iterator it = pt.get_child("tiles").begin();
it != end; ++it)
{
int index = std::stoi(it->first);
// Map the index to the right matrix postion.
if (index > 1)
{
index = radius + index - 1;
}
else if (index < 1)
{
if (index != 0)
{
index = radius + index;
}
else
{
index = tiles.n_rows - 1;
}
}
else
{
index = radius;
}
Vec(it->second, it->second.get_value<std::string>(), tiles, index);
}
}
/**
* Parse the number lives.
*
* @param lives The current lives.
*/
void MarioLives(int& lives)
{
lives = pt.get<int>("lives");
}
/**
* Parse the number coins.
*
* @param coins The current number of coins.
*/
void MarioCoins(int& coins)
{
coins = pt.get<int>("coins");
}
/**
* Parse the player state.
*
* @param state The current player state.
*/
void PlayerState(int& state)
{
state = pt.get<int>("state");
}
/**
* Parse the current game image.
*
* @param image The current game image as string.
*/
void GameImage(const std::string& json, std::string& image)
{
image = json;
}
private:
//! Store results of the given json string in the row'th of the given
// matrix v.
void Vec(const ptree& pt, const ptree::key_type& key, arma::mat& v, int row)
{
int col = 0;
for (auto& item : pt.get_child(key))
{
v(row, col++) = item.second.get_value<int>();
}
}
//! Locally stored property_tree to parse the json string.
ptree pt;
}; // class Parser
} // namespace parser