-
Notifications
You must be signed in to change notification settings - Fork 9
/
railable.py
143 lines (118 loc) · 5.46 KB
/
railable.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
# 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.
#
# It provides an option to receive a message when you become railable
#
import minqlx
import datetime
import time
import threading
import requests
VERSION = "v0.2"
PLAYER_KEY = "minqlx:players:{}"
RAIL_KEY = PLAYER_KEY + ":railable"
RAIL_MSG_KEY = PLAYER_KEY + ":railmsg"
DEFAULT_MESSAGE = "^7Railable! (edit via ^2!railmsg ^7or disable via ^2!railable^7)"
class railable(minqlx.Plugin):
def __init__(self):
super().__init__()
self.shown = []
self.running = False
self.add_hook("round_start", self.handle_round_start)
self.add_hook("round_end", self.handle_round_end)
self.add_hook("death", self.handle_death)
self.add_command("railable", self.cmd_toggle_pref)
self.add_command("railmsg", self.cmd_set_rail_msg, usage="<message>")
self.add_command("railinfo", self.cmd_info)
self.add_command("v_railable", self.cmd_version)
self.add_hook("player_connect", self.handle_player_connect)
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 cmd_info(self, player, msg, channel):
channel.reply("^7Get a customizable ^2!railmsg ^7when you become ^2!railable^7.")
def cmd_toggle_pref(self, player, msg, channel):
self.set_notif_pref(player.steam_id)
if self.get_notif_pref(player.steam_id):
channel.reply("^7{} will now get a hint when ^2!railable^7.".format(player.name))
else:
channel.reply("^7{} will stop seeing ^2!railable ^7notifications.".format(player.name))
def cmd_set_rail_msg(self, player, msg, channel):
if len(msg) < 2:
if not self.get_notif_pref(player.steam_id):
channel.reply("^7{} does not have the ^2!railable ^7notifications activated.".format(player.name))
return
m = self.get_rail_msg(player.steam_id)
channel.reply("^7{}^7's ^2!railmsg ^7is: {}.".format(player.name, m or DEFAULT_MESSAGE))
else:
self.set_rail_msg(player.steam_id, " ".join(msg[1:]))
self.db[RAIL_KEY.format(player.steam_id)] = 1
channel.reply("^7{}^7's ^2!railmsg ^7has been changed and ^2!railable ^7activated!".format(player.name))
def handle_death(self, victim, killer, data):
if victim.id in self.shown:
self.shown.remove()
def handle_round_start(self, n):
self.running = True
self.shown = []
threading.Thread(target=self.looking).start()
def handle_round_end(self, n):
self.running = False
def handle_player_connect(self, player):
if self.db.has_permission(player, 5):
self.check_version(player=player)
def looking(self):
while self.game.state == 'in_progress' and self.running:
teams = self.teams()
for p in teams['red'] + teams['blue']:
pid = p.steam_id
if self.get_notif_pref(pid) and self.railable(p) and not pid in self.shown:
railmsg = self.get_rail_msg(pid)
p.center_print(railmsg or DEFAULT_MESSAGE)
self.shown.append(pid)
time.sleep(0.33)
def railable(self, p):
if p.is_alive:
h = p.health
a = p.armor
return (h <= 26) or (h+a <= 80)
return False
def get_notif_pref(self, sid):
try:
return int(self.db[RAIL_KEY.format(sid)])
except:
return False
def set_notif_pref(self, sid):
self.db[RAIL_KEY.format(sid)] = 0 if self.get_notif_pref(sid) else 1
def get_rail_msg(self, sid):
try:
return self.db[RAIL_MSG_KEY.format(sid)]
except:
return False
def set_rail_msg(self, sid, msg):
self.db[RAIL_MSG_KEY.format(sid)] = msg