forked from dsverdlo/minqlx-plugins
/
afk.py
180 lines (145 loc) · 6.75 KB
/
afk.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
# This is a plugin created by iouonegirl(@gmail.com)
# Copyright (c) 2016 iouonegirl
# https://github.com/dsverdlo/minqlx-plugins
#
# You are free to modify this plugin to your custom,
# except for the version command related code.
#
# Its purpose is to detect afk players and provide actions
# to be taken on them.
#
# Uses:
# - qlx_afk_warning_seconds "10"
# - qlx_afk_detection_seconds "20"
# - qlx_afk_put_to_spec "1"
import minqlx
import threading
import time
import requests
VERSION = "v0.5"
VAR_WARNING = "qlx_afk_warning_seconds"
VAR_DETECTION = "qlx_afk_detection_seconds"
VAR_PUT_SPEC = "qlx_afk_put_to_spec"
# Interval for the thread to update positions. Default = 0.33
interval = 0.33
class afk(minqlx.Plugin):
def __init__(self):
# Set required cvars once. DONT EDIT THEM HERE BUT IN SERVER.CFG
self.set_cvar_once(VAR_WARNING, "10")
self.set_cvar_once(VAR_DETECTION, "20")
self.set_cvar_once(VAR_PUT_SPEC, "1")
# Get required cvars
self.warning = int(self.get_cvar(VAR_WARNING))
self.detection = int(self.get_cvar(VAR_DETECTION))
self.put_to_spec = int(self.get_cvar(VAR_PUT_SPEC))
# steamid : [position, seconds]
self.positions = {}
# keep looking for AFK players
self.running = False
# punished players
self.punished = []
self.add_command("v_afk", self.cmd_version)
self.add_hook("round_start", self.handle_round_start)
self.add_hook("round_end", self.handle_round_end)
self.add_hook("team_switch", self.handle_player_switch)
self.add_hook("unload", self.handle_unload)
self.add_hook("player_connect", self.handle_player_connect)
def handle_player_connect(self, player):
if self.db.has_permission(player, 5):
self.check_version(player=player)
def handle_unload(self, plugin):
if plugin == self.__class__.__name__:
self.running = False
self.punished = []
def handle_round_start(self, round_number):
teams = self.teams()
for p in teams['red'] + teams['blue']:
self.positions[p.steam_id] = [self.help_get_pos(p), 0]
self.punished = []
# start checking thread
self.running = True
threading.Thread(target=self.help_create_thread).start()
def handle_round_end(self, round_number):
self.running = False
self.punished = []
def handle_player_switch(self, player, old, new):
if new == 'spectator':
if player.steam_id in self.positions:
del self.positions[player.steam_id]
if player in self.punished:
self.punished.remove(player)
if new in ['red', 'blue']:
self.positions[player.steam_id] = [self.help_get_pos(player), 0]
def cmd_version(self, player, msg, channel):
self.check_version(channel=channel)
@minqlx.thread
def check_version(self, player=None, channel=None):
url = "https://raw.githubusercontent.com/dsverdlo/minqlx-plugins/master/{}.py".format(self.__class__.__name__)
res = requests.get(url)
last_status = res.status_code
if res.status_code != requests.codes.ok: return
for line in res.iter_lines():
if line.startswith(b'VERSION'):
line = line.replace(b'VERSION = ', b'')
line = line.replace(b'"', b'')
# If called manually and outdated
if channel and VERSION.encode() != line:
channel.reply("^7Currently using ^3iou^7one^4girl^7's ^6{}^7 plugin ^1outdated^7 version ^6{}^7.".format(self.__class__.__name__, VERSION))
# If called manually and alright
elif channel and VERSION.encode() == line:
channel.reply("^7Currently using ^3iou^7one^4girl^7's latest ^6{}^7 plugin version ^6{}^7.".format(self.__class__.__name__, VERSION))
# If routine check and it's not alright.
elif player and VERSION.encode() != line:
time.sleep(15)
try:
player.tell("^3Plugin update alert^7:^6 {}^7's latest version is ^6{}^7 and you're using ^6{}^7!".format(self.__class__.__name__, line.decode(), VERSION))
except Exception as e: minqlx.console_command("echo {}".format(e))
return
def help_create_thread(self):
while self.running and self.game.state == 'in_progress':
teams = self.teams()
for p in teams['red'] + teams['blue']:
pid = p.steam_id
if pid not in self.positions:
self.positions[pid] = [self.help_get_pos(p), 0]
prev_pos, secs = self.positions[pid]
curr_pos = self.help_get_pos(p)
# If position stayed the same, add the time difference and check for thresholds
if prev_pos == curr_pos:
self.positions[pid] = [curr_pos, secs+interval]
if secs+interval >= self.warning and secs < self.warning:
self.help_warn(p)
elif secs+interval >= self.detection and secs < self.detection:
self.help_detected_print(p)
else:
self.positions[pid] = [curr_pos, 0]
if p in self.punished:
# if the player started moving, remove him from punished players
self.punished.remove(p)
time.sleep(interval)
def help_warn(self, player):
message = "You have been inactive for {} seconds...".format(self.warning)
minqlx.send_server_command(player.id, "cp \"\n\n\n{}\"".format(message))
def help_detected_print(self, player):
self.msg("^1{} ^1has been inactive for {} seconds! Commencing punishment!".format(player.name, int(self.positions[player.steam_id][1])))
self.punished.append(player)
self.punish(player)
@minqlx.thread
def punish(self, player, pain=10, wait=0.5):
while self.game.state == 'in_progress' and player in self.punished:
if player.health >= pain:
player.health -= pain
if player.steam_id in self.positions:
s = int((self.positions[player.steam_id])[1])
else:
s = self.detection
message = "^1Inactive for {} seconds! \n\n^7Move or keep getting damage!".format(s)
minqlx.send_server_command(player.id, "cp \"\n\n\n{}\"".format(message))
time.sleep(wait)
else:
break
self.punished.remove(player)
if self.put_to_spec: player.put('spectator')
return
def help_get_pos(self, player):
return player.position()