From dfbe9f6603f0cc1651cd5b14690b542f283ef11a Mon Sep 17 00:00:00 2001 From: Dominic Zimmer Date: Sun, 30 Oct 2022 11:25:24 +0100 Subject: [PATCH] Implement timeouts --- backend/backend/arbiter.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/backend/backend/arbiter.py b/backend/backend/arbiter.py index c6f2f26..5168f12 100644 --- a/backend/backend/arbiter.py +++ b/backend/backend/arbiter.py @@ -20,6 +20,8 @@ class GameMode: name: str mode_function: ModeFunction allow_multitouch: bool + min_time: int + max_time: int @dataclass @@ -62,9 +64,9 @@ class Arbiter: self.modeswitch_time = None self.clients: dict[WebSocket, ClientState] = dict() - def mode(self, name: str, allow_multitouch: bool = True): + def mode(self, name: str, allow_multitouch: bool = True, min_time=10, max_time=90): def inner(f: ModeFunction): - self.modes.append(GameMode(name, f, allow_multitouch)) + self.modes.append(GameMode(name, f, allow_multitouch, min_time, max_time)) return inner @@ -118,7 +120,7 @@ class Arbiter: self.state.mode = self.current_mode.name self.update_next_mode() self.modeswitch_time = datetime.now() + timedelta( - seconds=settings.arbiter_mode_switch_cycle + seconds=random.randint(self.current_mode.min_time, self.current_mode.max_time) ) if self.current_mode_task: @@ -151,7 +153,7 @@ class Arbiter: arbiter = Arbiter() -@arbiter.mode("democracy", allow_multitouch=False) +@arbiter.mode("democracy", allow_multitouch=False, min_time=5*60, max_time=10*60) async def _(get_input: InputGetter, set_output: OutputSetter): while True: await asyncio.sleep(settings.democracy_vote_cycle) @@ -188,7 +190,7 @@ async def _(get_input: InputGetter, set_output: OutputSetter): set_output(output) -@arbiter.mode("anarchy", allow_multitouch=False) +@arbiter.mode("anarchy", allow_multitouch=False, min_time=5*60, max_time=10*60) async def _(get_input: InputGetter, set_output: OutputSetter): while True: await asyncio.sleep(settings.democracy_vote_cycle) @@ -206,7 +208,7 @@ async def _(get_input: InputGetter, set_output: OutputSetter): set_output(output) -@arbiter.mode("random", allow_multitouch=False) +@arbiter.mode("random", allow_multitouch=False, min_time=20, max_time=90) async def _(get_input: InputGetter, set_output: OutputSetter): while True: await asyncio.sleep(settings.democracy_vote_cycle)