Skip to content

Commit

Permalink
Save/load auto pairings on server restart
Browse files Browse the repository at this point in the history
  • Loading branch information
gbtami committed Dec 21, 2024
1 parent 4d9124d commit 62c62a0
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions server/pychess_global_app_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down Expand Up @@ -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"}')
Expand Down

0 comments on commit 62c62a0

Please sign in to comment.