Commit 219ea049 authored by minus's avatar minus

made statscollector more reliable

parent a4c95fe9
#!/usr/bin/env python3
import redis import redis
from teeworlds import query_masters_serverlist, create_masters, EventSocket, MasterServer, Player from teeworlds import query_masters_serverlist, create_masters, EventSocket, MasterServer, Player, Server, Address
from teeworlds.base import Address
from teeworlds.server import Server
import json import json
from teeworlds.teeworlds import query_masters_serverlist
from json import JSONEncoder from json import JSONEncoder
MASTER_REFRESH_INTERVAL = 60 MASTER_REFRESH_INTERVAL = 120
SERVER_REFRESH_INTERVAL = 120 SERVER_TIMEOUT = 180
SERVER_TIMEOUT = 300
PLAYER_TIMEOUT = SERVER_TIMEOUT PLAYER_TIMEOUT = SERVER_TIMEOUT
# Featues
# - server list → redis set
# - server details → redis key (server:ip:port)
# - online players → server mapping → redis hash
class TeeworldsJSONEncoder(JSONEncoder): class TeeworldsJSONEncoder(JSONEncoder):
def default(self, o): def default(self, o):
...@@ -31,6 +24,13 @@ class TeeworldsJSONEncoder(JSONEncoder): ...@@ -31,6 +24,13 @@ class TeeworldsJSONEncoder(JSONEncoder):
rconn = redis.StrictRedis() rconn = redis.StrictRedis()
class RefreshingServer(Server): class RefreshingServer(Server):
"""Stores server and player info into redis
Server info is stored in a key "server:<server address>"
Player info is stored in a key "player:<name>:<server address>"
Keys time out as configured through SERVER_TIMEOUT and PLAYER_TIMEOUT respectively
"""
def on_info_received(self): def on_info_received(self):
server_info = json.dumps({k: v for k, v in filter(lambda it: not it[0].startswith("_"), self.__dict__.items())}, cls=TeeworldsJSONEncoder) server_info = json.dumps({k: v for k, v in filter(lambda it: not it[0].startswith("_"), self.__dict__.items())}, cls=TeeworldsJSONEncoder)
detail_key_name = "server:{}".format(str(self.address)) detail_key_name = "server:{}".format(str(self.address))
...@@ -39,7 +39,6 @@ class RefreshingServer(Server): ...@@ -39,7 +39,6 @@ class RefreshingServer(Server):
player_key_name = "player:{}:{}".format(player.name, str(self.address)) player_key_name = "player:{}:{}".format(player.name, str(self.address))
player_info = json.dumps({k: v for k, v in filter(lambda it: not it[0].startswith("_"), player.__dict__.items())}, cls=TeeworldsJSONEncoder) player_info = json.dumps({k: v for k, v in filter(lambda it: not it[0].startswith("_"), player.__dict__.items())}, cls=TeeworldsJSONEncoder)
rconn.setex(player_key_name, PLAYER_TIMEOUT, player_info) rconn.setex(player_key_name, PLAYER_TIMEOUT, player_info)
self._socket.call_later(SERVER_REFRESH_INTERVAL, self.request_info)
#self._socket.call_later(SERVER_TIMEOUT, self.on_timeout) #self._socket.call_later(SERVER_TIMEOUT, self.on_timeout)
# def on_timeout(self): # def on_timeout(self):
...@@ -56,10 +55,12 @@ class RefreshingMaster(MasterServer): ...@@ -56,10 +55,12 @@ class RefreshingMaster(MasterServer):
server.request_info() server.request_info()
e = EventSocket(max_packet_rate=50) e = EventSocket(max_packet_rate=100)
m = create_masters(e, master_factory=RefreshingMaster)
query_masters_serverlist(m) def loop():
#for m in t.masterlist: m = create_masters(e, master_factory=RefreshingMaster)
# m.on_server_add = lambda s: s.request_info() query_masters_serverlist(m)
#t.query_masters() e.call_later(MASTER_REFRESH_INTERVAL, loop)
loop()
e.run() e.run()
\ No newline at end of file
from .teeworlds import create_masters, query_masters_serverlist, query_masters_servercount from .teeworlds import create_masters, query_masters_serverlist, query_masters_servercount
from .base import EventSocket from .base import EventSocket, Address
from .master import MasterServer from .master import MasterServer
from .server import Server from .server import Server
from .player import Player from .player import Player
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment