From 62c62a0e1526187f0ececa516ba4e0e6272a7d86 Mon Sep 17 00:00:00 2001 From: gbtami Date: Sat, 21 Dec 2024 21:39:53 +0100 Subject: [PATCH] Save/load auto pairings on server restart --- server/pychess_global_app_state.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/server/pychess_global_app_state.py b/server/pychess_global_app_state.py index f621f2f14..d61b407bd 100644 --- a/server/pychess_global_app_state.py +++ b/server/pychess_global_app_state.py @@ -237,6 +237,18 @@ async def init_from_db(self): await self.db.create_collection("seek") await self.db.seek.create_index("expireAt", expireAfterSeconds=0) + # Load auto pairings from database + async for doc in self.db.autopairing.find(): + variant_tc = tuple(doc["variant_tc"]) + print(variant_tc) + if variant_tc not in self.auto_pairings: + self.auto_pairings[variant_tc] = set() + + for username in doc["users"]: + user = await self.users.get(username) + self.auto_pairing_users.add(user) + self.auto_pairings[variant_tc].add(user) + # Load seeks from database async for doc in self.db.seek.find(): user = await self.users.get(doc["user"]) @@ -451,6 +463,18 @@ async def server_shutdown(self): log.debug("saving regular seek to database: %s" % seek) await self.db.seek.insert_many(reg_seeks) + # save auto pairings + await self.db.autopairing.delete_many({}) + auto_pairings = [ + { + "variant_tc": variant_tc, + "users": [user.username for user in self.auto_pairings[variant_tc]], + } + for variant_tc in self.auto_pairings + ] + if len(auto_pairings) > 0: + await self.db.autopairing.insert_many(auto_pairings) + # terminate BOT users for user in [user for user in self.users.values() if user.bot]: await user.event_queue.put('{"type": "terminated"}')