Commit 219ea049 authored by minus's avatar minus

made statscollector more reliable

parent a4c95fe9
#!/usr/bin/env python3
import redis
from teeworlds import query_masters_serverlist, create_masters, EventSocket, MasterServer, Player
from teeworlds.base import Address
from teeworlds.server import Server
from teeworlds import query_masters_serverlist, create_masters, EventSocket, MasterServer, Player, Server, Address
import json
from teeworlds.teeworlds import query_masters_serverlist
from json import JSONEncoder
MASTER_REFRESH_INTERVAL = 60
SERVER_REFRESH_INTERVAL = 120
SERVER_TIMEOUT = 300
MASTER_REFRESH_INTERVAL = 120
SERVER_TIMEOUT = 180
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):
def default(self, o):
......@@ -31,6 +24,13 @@ class TeeworldsJSONEncoder(JSONEncoder):
rconn = redis.StrictRedis()
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):
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))
......@@ -39,7 +39,6 @@ class RefreshingServer(Server):
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)
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)
# def on_timeout(self):
......@@ -56,10 +55,12 @@ class RefreshingMaster(MasterServer):
server.request_info()
e = EventSocket(max_packet_rate=50)
m = create_masters(e, master_factory=RefreshingMaster)
query_masters_serverlist(m)
#for m in t.masterlist:
# m.on_server_add = lambda s: s.request_info()
#t.query_masters()
e = EventSocket(max_packet_rate=100)
def loop():
m = create_masters(e, master_factory=RefreshingMaster)
query_masters_serverlist(m)
e.call_later(MASTER_REFRESH_INTERVAL, loop)
loop()
e.run()
\ No newline at end of file
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 .server import Server
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