-
Notifications
You must be signed in to change notification settings - Fork 1
/
UrlHandler.cpp
157 lines (130 loc) · 5.24 KB
/
UrlHandler.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
#include "UrlHandler.h"
bool replace_v2(std::string &s, const char *orig, const char *rep)
{
bool retval = false;
while (s.find(orig) != std::string::npos)
{
s.replace(s.find(orig), std::string(orig).size(), rep);
retval = true;
}
return retval;
}
void BaseUrlHandler::URLDone(const char * /*URL*/, const void *data, unsigned int size, bool complete)
{
if (complete)
{
int _playerId = _playerIds[0];
_playerIds.erase(_playerIds.begin());
if (size > 1 && size < _max_data_size)
{
std::string _data;
_data.append((const char *)data, size);
if (is_valid_status(_data))
{
replace_v2(_data, "\r\n", "\n");
replace_v2(_data, "\r", "\n");
replace_v2(_data, "\n\n", "\n \n");
// dataList->tokenize(_data.c_str(), "\r\n");
dataList->tokenize(_data.c_str(), "\n");
showData(_playerId);
}
else
{
bz_sendTextMessage(BZ_SERVER, _playerId, "No valid data was received !");
bz_sendTextMessage(BZ_SERVER, _playerId, "This points to a bug or misuse. Please contact the server admin.");
}
}
else
{
bz_sendTextMessagef(BZ_SERVER, _playerId, "The received data size (%d) exceede the limit (%d)", size, _max_data_size);
bz_sendTextMessage(BZ_SERVER, _playerId, "This points to a bug or misuse. Please contact the server admin.");
}
}
}
void BaseUrlHandler::URLError(const char * /*URL*/, int errorCode, const char *errorString)
{
int _playerId = _playerIds[0];
_playerIds.erase(_playerIds.begin());
bz_sendTextMessagef(BZ_SERVER, _playerId, "Action failed with errorcode = %d - %s !!", errorCode, errorString);
}
void BaseUrlHandler::showDataOK(int playerId)
{
for (unsigned int i = 1; i < dataList->size(); i++)
bz_sendTextMessagef(BZ_SERVER, playerId, "%s", dataList->get(i).c_str());
}
void BaseUrlHandler::showDataNOK(int playerId)
{
for (unsigned int i = 1; i < dataList->size(); i++)
bz_sendTextMessagef(BZ_SERVER, playerId, "%s", dataList->get(1).c_str());
}
void BaseUrlHandler::showData(int playerId)
{
if (dataList->get(0) == "OK")
showDataOK(playerId);
if (dataList->get(0) == "NOK" && (!noNOKNotify))
showDataNOK(playerId);
}
void BaseUrlHandler::setPlayerId(int playerId)
{
_playerIds.push_back(playerId);
}
bool BaseUrlHandler::is_valid_status(const std::string &data)
{
if (data.size() < 2)
return false;
if (data.compare(0, 2, "OK") == 0 || data.compare(0, 3, "NOK") == 0)
return true;
return false;
}
void BaseUrlHandler::setNoNoKNotify(bool notify)
{
noNOKNotify = notify;
}
void PlayerInfo::showDataOK(int playerId)
{
for (unsigned int i = 1; i < dataList->size(); i++)
{
bz_APIStringList *playerList = bz_newStringList();
playerList->tokenize(dataList->get(i).c_str(), "\t", 6, false);
bz_sendTextMessagef(BZ_SERVER, playerId, "Player info for %s", playerList->get(0).c_str());
bz_sendTextMessage(BZ_SERVER, playerId, "-------------------------------------------");
bz_sendTextMessagef(BZ_SERVER, playerId, "zelo : %s", playerList->get(1).c_str());
bz_sendTextMessagef(BZ_SERVER, playerId, "score : %s", playerList->get(5).c_str());
bz_sendTextMessagef(BZ_SERVER, playerId, "matches won : %s", playerList->get(2).c_str());
bz_sendTextMessagef(BZ_SERVER, playerId, "matches lost : %s", playerList->get(3).c_str());
bz_sendTextMessagef(BZ_SERVER, playerId, "status : %s", playerList->get(4).c_str());
bz_sendTextMessage(BZ_SERVER, playerId, "-------------------------------------------");
bz_deleteStringList(playerList);
}
}
void TopScore::showDataOK(int playerId)
{
bz_sendTextMessage(BZ_SERVER, playerId, "Monthly points ranking");
bz_sendTextMessage(BZ_SERVER, playerId, "-------------------------------------------");
bz_sendTextMessagef(BZ_SERVER, playerId, "%-4s %-32s %5s", "Pos", "Player", "Score");
bz_sendTextMessagef(BZ_SERVER, playerId, "%-4s %-32s %5s", "---", "------", "-----");
for (unsigned int i = 1; i < dataList->size(); i++)
{
bz_APIStringList *topScoreList = bz_newStringList();
topScoreList->tokenize(dataList->get(i).c_str(), "\t", 3, false);
bz_sendTextMessagef(BZ_SERVER, playerId, "%-4s %-32s %5s", topScoreList->get(0).c_str(),
topScoreList->get(1).c_str(), topScoreList->get(2).c_str());
bz_deleteStringList(topScoreList);
}
bz_sendTextMessage(BZ_SERVER, playerId, "-------------------------------------------");
}
void TopZelo::showDataOK(int playerId)
{
bz_sendTextMessage(BZ_SERVER, playerId, "Zelo score ranking");
bz_sendTextMessage(BZ_SERVER, playerId, "-------------------------------------------");
bz_sendTextMessagef(BZ_SERVER, playerId, "%-4s %-32s %4s", "Pos", "Player", "Zelo");
bz_sendTextMessagef(BZ_SERVER, playerId, "%-4s %-32s %4s", "---", "------", "----");
for (unsigned int i = 1; i < dataList->size(); i++)
{
bz_APIStringList *topZeloList = bz_newStringList();
topZeloList->tokenize(dataList->get(i).c_str(), "\t", 3, false);
bz_sendTextMessagef(BZ_SERVER, playerId, "%-4s %-32s %-4s", topZeloList->get(0).c_str(), topZeloList->get(1).c_str(), topZeloList->get(2).c_str());
bz_deleteStringList(topZeloList);
}
bz_sendTextMessage(BZ_SERVER, playerId, "-------------------------------------------");
}