change session stamps to use redis

This commit is contained in:
nhaar 2024-09-16 21:28:14 -03:00
parent 375948f6ba
commit 3504c48de2
3 changed files with 17 additions and 11 deletions

View File

@ -1465,7 +1465,6 @@ CREATE TABLE penguin_stamp (
penguin_id INT NOT NULL,
stamp_id INT NOT NULL,
recent BOOLEAN NOT NULL DEFAULT TRUE,
in_game_session BOOLEAN NOT NULL DEFAULT TRUE,
PRIMARY KEY (penguin_id, stamp_id),
CONSTRAINT stamp_ibfk_1 FOREIGN KEY (penguin_id) REFERENCES penguin (id) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT stamp_ibfk_2 FOREIGN KEY (stamp_id) REFERENCES stamp (id) ON DELETE CASCADE ON UPDATE CASCADE
@ -1477,7 +1476,6 @@ COMMENT ON COLUMN penguin_stamp.penguin_id IS 'Stamp penguin ID';
COMMENT ON COLUMN penguin_stamp.stamp_id IS 'Stamp ID';
COMMENT ON COLUMN penguin_stamp.recent IS 'Is recently earned?';
COMMENT ON COLUMN penguin_stamp.recent IS 'Is recently earned?';
COMMENT ON COLUMN penguin_stamp.in_game_session IS 'Still in the minigame session it was earned';
DROP TABLE IF EXISTS penguin_membership;
CREATE TABLE penguin_membership (

View File

@ -54,7 +54,6 @@ class PenguinStamp(db.Model):
stamp_id = db.Column(db.ForeignKey('stamp.id', ondelete='CASCADE', onupdate='CASCADE'), primary_key=True,
nullable=False)
recent = db.Column(db.Boolean, nullable=False, server_default=db.text("true"))
in_game_session = db.Column(db.Boolean, nullable=False, server_default=db.text("true"))
class StampCollection(AbstractDataCollection):

View File

@ -244,6 +244,8 @@ class Penguin(Spheniscidae, penguin.Penguin):
if stamp.id in self.stamps:
return False
await self.server.redis.set(self.get_recent_stamp_key(stamp.id), 1)
await self.stamps.insert(stamp_id=stamp.id)
if notify:
@ -254,6 +256,10 @@ class Penguin(Spheniscidae, penguin.Penguin):
return True
def get_recent_stamp_key(self, stamp_id):
"""Get redis key that locates the session recency of a stamp"""
return f'{self.id}.{stamp_id}.recentstamp'
async def add_inbox(self, postcard, sender_name="sys", sender_id=None, details=""):
penguin_postcard = await PenguinPostcard.create(penguin_id=self.id, sender_id=sender_id,
postcard_id=postcard.id, details=details)
@ -417,11 +423,15 @@ class Penguin(Spheniscidae, penguin.Penguin):
game_stamps_ids = [stamp.id for stamp in game_stamps]
recently_collected_game_stamps = [
stamp
for stamp in self.stamps.values()
if (stamp.in_game_session and stamp.stamp_id in game_stamps_ids)
]
recently_collected_game_stamps = []
for stamp in self.stamps.values():
if stamp.stamp_id in game_stamps_ids:
is_recent = await self.server.redis.get(
self.get_recent_stamp_key(stamp.stamp_id)
)
if is_recent:
recently_collected_game_stamps.append(stamp)
collected_game_stamps = [
stamp for stamp in game_stamps if (stamp.id in self.stamps and stamp)
@ -457,9 +467,8 @@ class Penguin(Spheniscidae, penguin.Penguin):
"""
Exits a game session and unmarks all stamps since we are no longer in their session
"""
stamps = [stamp for stamp in self.stamps.values() if stamp.in_game_session]
for stamp in stamps:
await stamp.update(in_game_session=False).apply()
async for key in self.server.redis.scan_iter(self.get_recent_stamp_key("*")):
await self.server.redis.delete(key)
def __repr__(self):
if self.id is not None: