-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathredis.py
61 lines (47 loc) · 1.88 KB
/
redis.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
import json
from twisted.internet import protocol
from twisted.internet import reactor
from mk2.plugins import Plugin
from mk2 import events
class RedisProtocol(protocol.Protocol):
def __init__(self, parent):
self.parent = parent
def request(self, *args):
self.transport.write(self.encode_request(args))
def encode_request(self, args):
lines = []
lines.append('*' + str(len(args)))
for a in args:
if isinstance(a, str):
a = a.encode('utf8')
lines.append('$' + str(len(a)))
lines.append(a)
lines.append('')
return '\r\n'.join(lines)
class RedisFactory(protocol.ReconnectingClientFactory):
def __init__(self, parent, channel):
self.parent = parent
self.channel = channel
def buildProtocol(self, addr):
self.protocol = RedisProtocol(self.parent)
return self.protocol
def relay(self, data, channel=None):
channel = channel or self.channel
self.protocol.request("PUBLISH", channel, json.dumps(data))
class Redis(Plugin):
host = Plugin.Property(default="localhost")
port = Plugin.Property(default=6379)
channel = Plugin.Property(default="mark2-{server}")
relay_events = Plugin.Property(default="StatPlayers,PlayerJoin,PlayerQuit,PlayerChat,PlayerDeath")
def setup(self):
channel = self.channel.format(server=self.parent.server_name)
self.factory = RedisFactory(self, channel)
reactor.connectTCP(self.host, self.port, self.factory)
for ev in self.relay_events.split(','):
ty = events.get_by_name(ev.strip())
if ty:
self.register(self.on_event, ty)
else:
self.console("redis: couldn't bind to event: {}".format(ev))
def on_event(self, event):
self.factory.relay(event.serialize())