added top game handling

This commit is contained in:
2018-12-19 15:16:15 +01:00
parent ed3a3c5595
commit 0e70126c7a
3 changed files with 102 additions and 31 deletions

View File

@@ -1,4 +1,3 @@
[autochannel]
; Comma seperated list of servers to operate on, leave empty for all
servers =
@@ -10,10 +9,13 @@ spare_channel = 1
top_games_limit = 10
; Name used for root channel for newly created channels
root_channel_name = Random Channel
root_channel_name = Endless
; Interval in sec between channel checks
check_timer_interval = 5
check_timer_interval = 10
; Additional game channels specified with steam AppIDs, comma seperated
game_channels_permament =
game_channel_permanent =
; Name of top games root channel
game_channel_name = Games

View File

@@ -5,7 +5,7 @@ from threading import Timer
import requests
from mumo_module import (commaSeperatedIntegers, MumoModule)
from tools.Utils import find_channel_with_name, get_empty_channels
from tools.Utils import find_create_channel, get_empty_channels, get_subchannels, get_user_for_channel
class autochannel(MumoModule):
@@ -13,9 +13,10 @@ class autochannel(MumoModule):
('servers', commaSeperatedIntegers, []),
('spare_channel', int, 1),
('top_games_limit', int, 10),
('root_channel_name', str, "Random Channel"),
('check_timer_interval', int, 5),
('game_channels_permament', str, "")
('root_channel_name', str, "Endless"),
('check_timer_interval', int, 10),
('game_channel_permanent', str, ""),
('game_channel_name', str, "Games")
)
}
@@ -26,7 +27,10 @@ class autochannel(MumoModule):
self.top_games_limit = self.cfg().autochannel.top_games_limit
self.root_channel_name = self.cfg().autochannel.root_channel_name
self.timer_interval = self.cfg().autochannel.check_timer_interval
self.root = None
self.game_channel_permanent = self.cfg().autochannel.game_channel_permanent.replace(" ", "").split(',')
self.game_channel_name = self.cfg().autochannel.game_channel_name
self.random_root = None
self.game_root = None
self.servertimer = {}
# Load steam top 100 last two weeks
@@ -47,8 +51,10 @@ class autochannel(MumoModule):
else:
self.wordlist = None
Timer(60 * 60, self.update_timer).start()
def connected(self):
self.log().debug("Register for Server callbacks")
self.log().debug("Register timer for running servers")
servers = self.cfg().autochannel.servers
if not servers:
@@ -57,30 +63,35 @@ class autochannel(MumoModule):
self.manager().subscribeMetaCallbacks(self, servers)
for server in self.manager().getMeta().getBootedServers():
self.init_channels(server)
if server.id in self.servertimer:
self.servertimer[server.id].stop()
self.servertimer[server.id] = Timer(self.timer_interval, self.handle_timer, [server])
self.servertimer[server.id].start()
def handle_timer(self, server):
self.check_channel(server)
self.check_random_channel(server)
self.check_game_channel(server)
self.servertimer[server.id] = Timer(self.timer_interval, self.handle_timer, [server])
self.servertimer[server.id].start()
def update_timer(self):
r = requests.get("http://steamspy.com/api.php?request=top100in2weeks")
if r.status_code == 200:
self.top_list = r.json()
self.log().info("Reloaded steam top100")
Timer(60 * 60, self.update_timer).start()
else:
self.log().warn("Failed to reload top100 - HTTP " + r.status_code)
Timer(10, self.update_timer).start()
def disconnected(self):
pass
def init_channels(self, server):
self.root = find_channel_with_name(self.root_channel_name, server, 0)
if not self.root:
new_root_cid = server.addChannel(self.root_channel_name, 0)
self.root = server.getChannelState(new_root_cid)
self.check_channel(server)
def check_channel(self, server):
empty_channels = get_empty_channels(server, self.root.id)
def check_random_channel(self, server):
self.random_root = find_create_channel(self.root_channel_name, server, 0)
empty_channels = get_empty_channels(server, self.random_root.id)
for cid in empty_channels[1:]:
server.removeChannel(cid)
@@ -94,19 +105,65 @@ class autochannel(MumoModule):
while not word.isalnum():
word = self.wordlist[random.randint(0, len(self.wordlist))]
server.addChannel(word, self.root.id)
self.log().info("Added new channel " + word)
server.addChannel(word, self.random_root.id)
def check_game_channel(self, server):
self.game_root = find_create_channel(self.game_channel_name, server, 0)
games = {}
for idx, app in enumerate(self.top_list.values()):
if idx < self.top_games_limit:
games[app["name"]] = app
else:
break
for pgame in self.game_channel_permanent:
if pgame and str(pgame) in self.top_list:
game = self.top_list[str(pgame)]
if game:
games[game["name"]] = game
channels = get_subchannels(server, self.game_root.id)
games_matched = []
for channel in channels:
if len(get_user_for_channel(channel.id, server)) == 0 and channel.name not in games:
server.removeChannel(channel.id)
elif channel.name in games:
games_matched.append(channel.name)
channel_tbc = {*games} - set(games_matched)
if games_matched:
for game in games_matched:
ch = find_create_channel(game, server, self.game_root.id)
if ch.position is not list(self.top_list).index(str(games[game]["appid"])):
ch.description = 'Top {0} on <a href="http://store.steampowered.com/app/{1}">Steam</a>'.format(
list(self.top_list).index(str(games[game]["appid"])) + 1, games[game]["appid"])
ch.position = list(self.top_list).index(str(games[game]["appid"]))
server.setChannelState(ch)
for game in channel_tbc:
nc = server.getChannelState(server.addChannel(game, self.game_root.id))
if nc:
nc.description = 'Top {0} on <a href="http://store.steampowered.com/app/{1}">Steam</a>'.format(
list(self.top_list).index(str(games[nc.name]["appid"])) + 1, games[nc.name]["appid"])
nc.position = list(self.top_list).index(str(games[nc.name]["appid"]))
server.setChannelState(nc)
#
# --- Meta callback functions
#
def started(self, server, context=None):
self.manager().subscribeServerCallbacks(self, [server.id])
def started(self, server):
self.log().debug("Register timer for new server")
self.servertimer[server.id] = Timer(self.timer_interval, self.handle_timer, [server])
self.servertimer[server.id].start()
self.init_channels(server)
def stopped(self, server, context=None):
def stopped(self, server):
self.log().debug("Stopped timer for server")
if server.id in self.servertimer:
self.servertimer[server.id].stop()

View File

@@ -1,4 +1,4 @@
def find_channel_with_name(name: str, server, parent: int = None):
def find_create_channel(name: str, server, parent: int = None):
channels = server.getChannels()
for cid, channel in channels.items():
@@ -9,7 +9,8 @@ def find_channel_with_name(name: str, server, parent: int = None):
if channel.name == name:
return channel
return None
new_cid = server.addChannel(name, parent if parent else 0)
return server.getChannelState(new_cid)
def get_empty_channels(server, parent: int = None):
@@ -24,6 +25,17 @@ def get_empty_channels(server, parent: int = None):
return empty_channels
def get_subchannels(server, parent: int):
sub_channel = []
channels = server.getChannels()
for cid, channel in channels.items():
if channel.parent == parent:
sub_channel.append(channel)
return sub_channel
def get_user_for_channel(cid, server):
users = server.getUsers()
users_in_channel = {}