-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBZList.cpp
167 lines (137 loc) · 5.08 KB
/
BZList.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
/*
* MIT License
*
* Copyright (c) 2019-2020 The Noah
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
#include "bzfsAPI.h"
#include "plugin_utils.h"
#include "easywsclient.hpp"
using easywsclient::WebSocket;
class BZList : public bz_Plugin{
public:
virtual const char* Name();
virtual void Init(const char* config);
virtual void Cleanup();
virtual void Event(bz_EventData* eventData);
private:
void send(const char* message);
void connect();
const char* KEY;
WebSocket::pointer ws;
bool connected = false;
};
BZ_PLUGIN(BZList)
const char* BZList::Name(){
return "BZList v0.0.1";
}
void BZList::Init(const char* config){
Register(bz_ePlayerScoreChanged);
Register(bz_ePlayerJoinEvent);
Register(bz_ePlayerPartEvent);
Register(bz_eTeamScoreChanged);
KEY = config;
bz_debugMessagef(2, "BZList: server address => %s", bz_getPublicAddr().c_str());
bz_debugMessagef(2, "BZList: config => %s", config);
connect();
}
void BZList::Cleanup(){
ws->close();
Flush();
}
void BZList::Event(bz_EventData* eventData){
switch (eventData->eventType){
case bz_ePlayerScoreChanged: {
bz_PlayerScoreChangeEventData_V1* data = (bz_PlayerScoreChangeEventData_V1*)eventData;
bz_BasePlayerRecord *player = bz_getPlayerByIndex(data->playerID);
std::string json = "{";
json += "\"event\": \"player_score_changed\",";
json += "\"player\": {";
json += bz_format("\"callsign\": \"%s\",", player->callsign.c_str());
json += bz_format("\"wins\": %d,", player->wins);
json += bz_format("\"losses\": %d,", player->losses);
json += bz_format("\"tks\": %d", player->teamKills);
json += "}";
json += "}";
send(json.c_str());
bz_freePlayerRecord(player);
} break;
case bz_ePlayerJoinEvent: {
// This event is called each time a player joins the game
bz_PlayerJoinPartEventData_V1* data = (bz_PlayerJoinPartEventData_V1*)eventData;
std::string json = "{";
json += "\"event\": \"player_join\",";
json += "\"player\": {";
json += bz_format("\"callsign\": \"%s\",", data->record->callsign.c_str());
json += bz_format("\"motto\": \"%s\",", bz_getPlayerMotto(data->record->playerID));
json += bz_format("\"team\": \"%s\",", bzu_GetTeamName(data->record->team));
json += bz_format("\"wins\": %d,", data->record->wins);
json += bz_format("\"losses\": %d,", data->record->losses);
json += bz_format("\"tks\": %d", data->record->teamKills);
json += "}";
json += "}";
send(json.c_str());
} break;
case bz_ePlayerPartEvent: {
// This event is called each time a player leaves a game
bz_PlayerJoinPartEventData_V1* data = (bz_PlayerJoinPartEventData_V1*)eventData;
std::string json = "{";
json += "\"event\": \"player_part\",";
json += bz_format("\"callsign\": \"%s\"", data->record->callsign.c_str());
json += "}";
send(json.c_str());
} break;
case bz_eTeamScoreChanged: {
// This event is called when a team's score changes
bz_TeamScoreChangeEventData_V1* data = (bz_TeamScoreChangeEventData_V1*)eventData;
std::string json = "{";
json += "\"event\": \"team_score_changed\",";
json += bz_format("\"team\": \"%s\",", bzu_GetTeamName(data->team));
json += bz_format("\"wins\": \"%d\",", bz_getTeamWins(data->team));
json += bz_format("\"losses\": \"%d\"", bz_getTeamLosses(data->team));
json += "}";
send(json.c_str());
} break;
default:
break;
}
}
void BZList::send(const char* message){
if(ws->getReadyState() <= 2){
connect();
}
ws->send(message);
ws->poll();
}
void BZList::connect(){
if(connected){
ws->poll();
}
ws = WebSocket::from_url("ws://api.bzlist.net:8080");
connected = ws->getReadyState() == WebSocket::OPEN;
bz_debugMessagef(2, "BZList: connected => %s", connected ? "yes" : "no");
std::string json = "{";
json += "\"event\": \"auth\",";
json += "\"server\": \"" + std::string(bz_getPublicAddr().c_str()) + "\",";
json += bz_format("\"key\": \"%s\"", KEY);
json += "}";
send(json.c_str());
}