From 7631bf3041a4cf5ccc3a5a61678748a690556a14 Mon Sep 17 00:00:00 2001 From: Bonta-kun <40473493+Bonta0@users.noreply.github.com> Date: Fri, 10 Jan 2020 22:44:07 +0100 Subject: [PATCH] MultiServer: added a !countdown chat command for convenience --- MultiServer.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/MultiServer.py b/MultiServer.py index 87d1fd41..d64524fa 100644 --- a/MultiServer.py +++ b/MultiServer.py @@ -34,6 +34,7 @@ class Context: self.port = port self.password = password self.server = None + self.countdown_timer = 0 self.clients = [] self.received_items = {} @@ -117,6 +118,19 @@ async def on_client_joined(ctx : Context, client : Client): async def on_client_left(ctx : Context, client : Client): notify_all(ctx, "%s (Player %d, %s) has left the game" % (client.name, client.slot, "Default team" if not client.team else "Team %s" % client.team)) +async def countdown(ctx : Context, timer): + notify_all(ctx, f'[Server]: Starting countdown of {timer}s') + if ctx.countdown_timer: + ctx.countdown_timer = timer + return + + ctx.countdown_timer = timer + while ctx.countdown_timer > 0: + notify_all(ctx, f'[Server]: {ctx.countdown_timer}') + ctx.countdown_timer -= 1 + await asyncio.sleep(1) + notify_all(ctx, f'[Server]: GO') + def get_connected_players_string(ctx : Context): auth_clients = [c for c in ctx.clients if c.auth] if not auth_clients: @@ -280,10 +294,16 @@ async def process_client_cmd(ctx : Context, client : Client, cmd, args): notify_all(ctx, client.name + ': ' + args) - if args[:8] == '!players': + if args.startswith('!players'): notify_all(ctx, get_connected_players_string(ctx)) - if args[:8] == '!forfeit': + if args.startswith('!forfeit'): forfeit_player(ctx, client.team, client.slot, client.name) + if args.startswith('!countdown'): + try: + timer = int(args.split()[1]) + except (IndexError, ValueError): + timer = 10 + asyncio.create_task(countdown(ctx, timer)) def set_password(ctx : Context, password): ctx.password = password