-
Notifications
You must be signed in to change notification settings - Fork 10
/
handicap.py
350 lines (310 loc) · 16.1 KB
/
handicap.py
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
"""
This is an extension plugin for minqlx, a Quake Live Server Admin Tool..
Copyright (C) 2016 BarelyMiSSeD (github)
You can redistribute it and/or modify it under the terms of the
GNU General Public License as published by the Free Software Foundation,
either version 3 of the License, or (at your option) any later version.
You should have received a copy of the GNU General Public License
along with minqlx. If not, see <http://www.gnu.org/licenses/>.
Handicap.py is a simple script made to automatically put a handicap on people who are over a certain ELO.
It will calculate the percentage of a handicap to put on a player based on the lower and upper ELO settings.
"""
import minqlx
import requests
import time
"""
The handicap given to players above the LOWER_ELO setting.
The severity of the handicap given can be adjusted by changing the UPPER_ELO setting.
Increase it to reduce the severity of the handicap and lower it to increase the severity.
It should not be lowered further than the highest ELO connected to the server.
****Adjust the LOWER_ELO/LOWER_BDM to the level you want the script to start giving handicaps***
****Adjust the UPPER_ELO/UPPER_BDM to adjust the amount of handicap it gives. The higher the UPPER_ELO/UPPER_BDM***
****the less severe the handicap.***
// Sets the minqlx permission level needed to use admin commands
set qlx_handicapAdminLevel "3"
// Show the player a message if they are handicapped by the server (1=on, 0=off)
set qlx_handicapMsgPlayer "1"
// Use BDM or ELO for handicaping players (1=BDM, 0=ELO)
set qlx_handicapUseBDM "1"
"""
UPPER_ELO = 3500
LOWER_ELO = 1750
UPPER_BDM = 2500
LOWER_BDM = 1550
PING_ADJUSTMENT = 70
MAX_ATTEMPTS = 3
BDM_KEY = "minqlx:players:{}:bdm:{}:{}"
ELO_KEY = "minqlx:players:{}:elo:{}:{}"
VERSION = 2.02
class handicap(minqlx.Plugin):
def __init__(self):
self.set_cvar_once("qlx_handicapAdminLevel", "3")
self.set_cvar_once("qlx_handicapMsgPlayer", "1")
self.set_cvar_once("qlx_handicapUseBDM", "1")
self.add_hook("new_game", self.handle_new_game)
self.add_hook("game_end", self.handle_game_end)
self.add_hook("player_loaded", self.handle_player_loaded)
self.add_hook("player_disconnect", self.handle_player_disconnect)
self.add_hook("userinfo", self.handle_user_info)
self.add_command(("handicap", "handi"), self.cmd_handicap)
self.add_command("hversion", self.cmd_hversion)
self.add_command(("handicapon", "handion"), self.cmd_handicap_on,
self.get_cvar("qlx_handicapAdminLevel", int))
self.add_command(("handicapoff", "handioff"), self.cmd_handicap_off,
self.get_cvar("qlx_handicapAdminLevel", int))
self.add_command(("listhandicaps", "listhandi"), self.cmd_list_handicaps,
self.get_cvar("qlx_handicapAdminLevel", int))
self.handicapped_players = {}
self.handicap_gametype = self.game.type_short
self.handicap_on = True
self.end_screen = False
self.check_players()
def modify_handicapped(self, pid, action, handi_value=0, ping_adjustment=None):
if action == "add":
if ping_adjustment:
self.handicapped_players[str(pid)] = ping_adjustment
else:
self.handicapped_players[str(pid)] = handi_value
elif action == "del" or handi_value == 0:
del self.handicapped_players[str(pid)]
@minqlx.thread
def check_players(self):
if self.handicap_on:
players = self.players()
loaded_scripts = self.plugins
if self.get_cvar("qlx_handicapUseBDM", bool):
if "serverBDM" not in loaded_scripts:
minqlx.console_print("The serverBDM plugin needs to be loaded to use {} with BDMs."
.format(self.__class__.__name__))
return minqlx.RET_STOP_ALL
for player in players:
pid = player.steam_id
rating = loaded_scripts["serverBDM"].get_bdm_field(player, self.handicap_gametype, "rating")
if rating > LOWER_BDM:
percentage = 100 - abs(round(((LOWER_BDM - rating) / (UPPER_BDM - LOWER_BDM)) * 100))
self.modify_handicapped(pid, "add", percentage)
else:
pids = []
elo = self.get_cvar("qlx_balanceApi")
response = False
for player in players:
pids.append(str(player.steam_id))
url = "http://{}/{}/{}".format(self.get_cvar("qlx_balanceUrl"), elo, "+".join(pids))
attempts = 0
while attempts < MAX_ATTEMPTS:
attempts += 1
info = requests.get(url)
if info.status_code != requests.codes.ok:
continue
info_js = info.json()
if "players" in info_js:
attempts = MAX_ATTEMPTS
response = True
if response:
for info in info_js["players"]:
rating = int(info[str(self.handicap_gametype)]["elo"])
self.db[ELO_KEY.format(int(info["steamid"]), elo, self.handicap_gametype)] =\
int(info[str(self.handicap_gametype)]["elo"])
if rating > LOWER_ELO:
percentage = 100 - abs(round(((LOWER_ELO - rating) / (UPPER_ELO - LOWER_ELO)) * 100))
self.modify_handicapped(info["steamid"], "add", percentage)
else:
for player in players:
rating = 0
pid = player.steam_id
try:
rating = int(self.db.get(ELO_KEY.format(pid, elo, self.handicap_gametype)))
except:
pass
if rating > LOWER_ELO:
percentage = 100 - abs(round(((LOWER_ELO - rating) / (UPPER_ELO - LOWER_ELO)) * 100))
self.modify_handicapped(pid, "add", percentage)
for player in players:
pid = player.steam_id
if str(pid) in self.handicapped_players:
percentage = int(self.handicapped_players[str(pid)])
ping = player.ping
if ping > PING_ADJUSTMENT:
percentage = round(percentage + (ping * ping * ping * ping / 15000000))
if percentage > 100:
percentage = 100
self.modify_handicapped(pid, "add", percentage)
player.handicap = percentage
def cmd_handicap_on(self, player, msg, channel):
self.handicap_on = True
player.tell("^3Players will now not be able to change their handicaps")
self.check_players()
return minqlx.RET_STOP_ALL
def cmd_handicap_off(self, player, msg, channel):
self.handicap_on = False
player.tell("^3Players will now be able to change their handicaps")
return minqlx.RET_STOP_ALL
def cmd_handicap(self, player, msg, channel):
if len(msg) < 3:
if len(msg) < 2:
if str(player.steam_id) in self.handicapped_players:
channel.reply("{} ^7has a handicap of ^1{}^7%"
.format(player, self.handicapped_players[str(player.steam_id)]))
else:
channel.reply("{} ^7is not handicapped by the server.".format(player))
else:
try:
pid = int(msg[1])
except ValueError:
player.tell("^3Enter a player ID from ^10 ^3to ^163^3.")
return
if 0 <= pid < 64:
p = self.player(pid)
if not p:
player.tell("^3There is no player associated with that player ID.")
return
else:
if str(p.steam_id) in self.handicapped_players:
channel.reply("{} ^7has a handicap of ^1{}^7%"
.format(p, self.handicapped_players[str(p.steam_id)]))
else:
channel.reply("{} ^7is not handicapped by the server.".format(p))
else:
player.tell("^3Enter a player ID from ^10 ^3to ^163^3.")
elif self.db.has_permission(player, self.get_cvar("qlx_handicapAdminLevel", int)):
try:
pid = int(msg[1])
handi = int(msg[2])
except ValueError:
player.tell("^1Use a valid Player ID and a handicap between 1 and 100.")
return minqlx.RET_STOP_ALL
target_player = self.player(pid)
if handi <= 0 or handi > 100:
player.tell("^3The handicap must be between 1 and 100.")
return minqlx.RET_STOP_ALL
if target_player:
self.modify_handicapped(target_player.steam_id, "add", handi)
target_player.handicap = handi
if int(self.get_cvar("qlx_handicapMsgPlayer")):
self.admin_message_player(target_player, handi)
return minqlx.RET_STOP_ALL
def cmd_list_handicaps(self, player, msg, channel):
if len(self.handicapped_players) > 0:
handi_list = ["^1Handicapped ^7Players:\n"]
for pl, handi in self.handicapped_players.items():
handi_list.append(" ^7{} ^7: ^2{}%\n".format(self.player(int(pl)), handi))
player.tell("".join(handi_list))
else:
player.tell("^3There is no one being hadnicapped on the server by the {} script."
.format(self.__class__.__name__))
return minqlx.RET_STOP_ALL
def handle_player_loaded(self, player):
if self.handicap_on:
self.check_handi(player)
@minqlx.thread
def check_handi(self, player):
time.sleep(6)
if self.get_cvar("qlx_handicapUseBDM", bool):
if "serverBDM" not in minqlx.Plugin._loaded_plugins:
minqlx.console_print("The serverBDM plugin needs to be loaded to use {} with BDMs."
.format(self.__class__.__name__))
return minqlx.RET_STOP_ALL
rating = minqlx.Plugin._loaded_plugins["serverBDM"]\
.get_bdm_field(player, self.handicap_gametype, "rating")
if rating > LOWER_BDM:
ping_adjustment = None
percentage = 100 - abs(round(((LOWER_BDM - rating) / (UPPER_BDM - LOWER_BDM)) * 100))
ping = player.ping
if ping > PING_ADJUSTMENT:
ping_adjustment = round(percentage + (ping * ping * ping * ping / 15000000))
if ping_adjustment:
if ping_adjustment > 100:
ping_adjustment = 100
player.handicap = ping_adjustment
else:
player.handicap = percentage
self.modify_handicapped(player.steam_id, "add", percentage, ping_adjustment)
if int(self.get_cvar("qlx_handicapMsgPlayer")):
self.message_player(player, percentage, ping_adjustment, "bdm")
else:
elo = self.get_cvar("qlx_balanceApi")
response = False
pid = player.steam_id
ping = player.ping
url = "http://{}/{}/{}".format(self.get_cvar("qlx_balanceUrl"), elo, pid)
rating = 0
attempts = 0
while attempts < MAX_ATTEMPTS:
attempts += 1
info = requests.get(url)
if info.status_code != requests.codes.ok:
continue
info_js = info.json()
if "players" in info_js:
attempts = MAX_ATTEMPTS
response = True
if response:
for info in info_js["players"]:
rating = int(info[str(self.handicap_gametype)]["elo"])
self.db[ELO_KEY.format(int(pid), elo, self.handicap_gametype)] =\
int(info[str(self.handicap_gametype)]["elo"])
else:
try:
rating = int(self.db.get(ELO_KEY.format(pid, elo, self.handicap_gametype)))
except:
return
if rating > LOWER_ELO:
ping_adjustment = None
percentage = 100 - abs(round(((LOWER_ELO - rating) / (UPPER_ELO - LOWER_ELO)) * 100))
if ping > PING_ADJUSTMENT:
ping_adjustment = round(percentage + (ping * ping * ping * ping / 15000000))
if ping_adjustment:
if ping_adjustment > 100:
ping_adjustment = 100
player.handicap = ping_adjustment
else:
player.handicap = percentage
self.modify_handicapped(pid, "add", percentage, ping_adjustment)
if int(self.get_cvar("qlx_handicapMsgPlayer")):
self.message_player(player, percentage, ping_adjustment, "elo")
def message_player(self, player, percentage, ping_adjustment, type="bdm"):
if type == "bdm":
lower_limit = LOWER_BDM
else:
lower_limit = LOWER_ELO
if ping_adjustment:
player.tell("^7You, {}^7 would have had a handicap of ^1{}% ^7because your {} is over ^4{}^7, "
"but because of your ping your handicap has been adjusted to ^1{}%^7."
.format(player, percentage, lower_limit, type, ping_adjustment))
else:
player.tell("^7You, {}^7 have been set to an auto handicap of ^1{}% ^7because your {} is over ^4{}^7."
.format(player, percentage, type, lower_limit))
def admin_message_player(self, player, percentage):
player.tell("^7You, {}^7 have been set to a handicap of ^1{}% ^7on this server."
.format(player, percentage, LOWER_ELO))
def cmd_hversion(self, player, msg, channel):
channel.reply('^7This server has installed ^2{} version {} by BarelyMiSSeD'
.format(self.__class__.__name__, VERSION))
def handle_user_info(self, player, info):
if not self.end_screen and 'handicap' in info:
if str(player.steam_id) in self.handicapped_players and self.handicap_on:
if int(info["handicap"]) > self.handicapped_players[str(player.steam_id)] or int(info["handicap"]) == 0:
player.tell("^1Your handicap is being set by the server to ^1{}% ^7and can't be changed."
.format(self.handicapped_players[str(player.steam_id)]))
info["handicap"] = self.handicapped_players[str(player.steam_id)]
return info
return
def handle_new_game(self):
self.process_new_game()
return
@minqlx.delay(5)
def process_new_game(self):
self.end_screen = False
gtype = self.game.type_short
if gtype != self.handicap_gametype and self.handicap_on:
self.handicap_gametype = gtype
self.check_players()
def handle_game_end(self, data):
self.end_screen = True
def handle_player_disconnect(self, player, reason):
self.process_player_disconnect(player)
return
def process_player_disconnect(self, player):
pid = player.steam_id
if str(pid) in self.handicapped_players:
self.modify_handicapped(pid, "del")