-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataserver.cpp
206 lines (173 loc) · 5.93 KB
/
dataserver.cpp
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
#include "dataServer.h"
const int PORT = 3010;
const char* SERVER_IP = "150.136.109.101";
class ShotClass {
public:
std::string name;
std::string courseName;
std::string map;
int holeNum;
double x;
double y;
double angleStart;
double angleEnd;
float startDistance;
float endDistance;
float magnitude;
double aimTime;
std::string shotTime;
std::string path = "shot";
void clear() {
name = "";
x = 0;
y = 0;
angleStart = 0;
angleEnd = 0;
startDistance = 0;
endDistance = 0;
magnitude = 0;
aimTime = 0;
shotTime = "";
};
};
ShotClass shot;
void updateDoubleData(const double& value, const std::string& dataTypeInfo, const std::string& path) {
if (path == "shot") {
if (dataTypeInfo == "x") {
shot.x = value;
} else if (dataTypeInfo == "y") {
shot.y = value;
} else if (dataTypeInfo == "angleStart") {
shot.angleStart = value;
} else if (dataTypeInfo == "angleEnd") {
shot.angleEnd = value;
} else if (dataTypeInfo == "startDistance") {
shot.startDistance = value;
} else if (dataTypeInfo == "endDistance") {
shot.endDistance = value;
} else if (dataTypeInfo == "magnitude") {
shot.magnitude = value;
} else if (dataTypeInfo == "aimTime") {
shot.aimTime = value;
}
}
}
void updateData(const std::string& value, const std::string& dataTypeInfo, const std::string& path) {
if (path == "shot") {
if (dataTypeInfo == "name") {
shot.name = value;
} else if (dataTypeInfo == "map") {
shot.map = value;
} else if (dataTypeInfo == "courseName") {
shot.courseName = value;
} else if (dataTypeInfo == "startDistance") {
shot.startDistance = std::stof(value);
} else if (dataTypeInfo == "endDistance") {
shot.endDistance = std::stof(value);
} else if (dataTypeInfo == "shotTime") {
shot.shotTime = std::stol(value);
}
}
}
void updateIntData(const int& value, const std::string& dataTypeInfo, const std::string& path) {
if (path == "shot") {
if (dataTypeInfo == "holeNum") {
shot.holeNum = value;
}
}
}
std::string escapeJsonString(const std::string& s) {
std::ostringstream o;
for (auto c : s) {
switch (c) {
case '"': o << "\\\""; break;
// Add other cases as necessary
default: o << c;
}
}
return o.str();
}
void sendShotDataToServer() {
nlohmann::json j;
j["/turn"]["name"] = shot.name;
j["/shot"]["name"] = shot.name;
j["/shot"]["course"] = shot.courseName;
j["/shot"]["hole"] = shot.holeNum;
j["/shot"]["map"] = shot.map;
j["/shot"]["x"] = shot.x;
j["/shot"]["y"] = shot.y;
j["/shot"]["aim-start-angle"] = shot.angleStart;
j["/shot"]["aim-end-angle"] = shot.angleEnd;
j["/shot"]["aim-start-distance"] = shot.startDistance;
j["/shot"]["aim-end-distance"] = shot.endDistance;
j["/shot"]["force"] = shot.magnitude;
j["/shot"]["aim-time"] = shot.aimTime;
j["/shot"]["timestamp"] = shot.shotTime;
std::string jsonStr = j.dump();
sendJsonToServer(jsonStr.c_str(), "shot");
}
void sendTurnDataToServer() {
nlohmann::json j;
j["/turn"]["name"] = shot.name;
std::string jsonStr = j.dump();
sendJsonToServer(jsonStr.c_str(), "turn");
}
void sendHoleDataToServer(HoleClass& hole) {
nlohmann::json j;
j["/hole"]["name"] = hole.name;
j["/hole"]["shots"] = hole.shots;
j["/hole"]["score"] = hole.score;
j["/hole"]["par"] = hole.par;
j["/hole"]["time"] = hole.aimTime;
j["/hole"]["place"] = hole.ranking;
std::string jsonStr = j.dump();
sendJsonToServer(jsonStr.c_str(), "hole");
}
void resetData() {
shot.clear();
}
void sendJsonToServer(const char* json_data, const char* path) {
// if windows
#ifdef _WIN32
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
std::cerr << "WSAStartup failed\n";
return;
}
#endif
SOCKET client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket == INVALID_SOCKET) {
std::cerr << "Error creating socket\n";
#ifdef _WIN32
std::cerr << "Error code: " << WSAGetLastError() << std::endl;
WSACleanup();
#endif
return;
}
sockaddr_in server_address{};
server_address.sin_family = AF_INET;
server_address.sin_port = htons(PORT);
inet_pton(AF_INET, SERVER_IP, &server_address.sin_addr);
if (connect(client_socket, (struct sockaddr*)&server_address, sizeof(server_address)) == -1) {
std::cerr << "Error connecting to the server\n";
#ifdef _WIN32
std::cerr << "Error code: " << WSAGetLastError() << std::endl;
closesocket(client_socket);
WSACleanup();
#else
std::cerr << "Error code: " << errno << std::endl;
closesocket(client_socket);
#endif
return;
}
std::string http_request = "POST /" + std::string(path) + " HTTP/1.1\r\n"
"Host: " + std::string(SERVER_IP) + "\r\n"
"Content-Type: application/json\r\n"
"Content-Length: " + std::to_string(strlen(json_data)) + "\r\n"
"\r\n" + std::string(json_data);
send(client_socket, http_request.c_str(), http_request.size(), 0);
closesocket(client_socket);
#ifdef _WIN32
WSACleanup();
#endif
}