Exemplo n.º 1
0
async def setup_membership(p):
    if not p.server.config.expire_membership or p.moderator or p.character:
        p.is_member = True
        p.membership_days_total = FullBadgeAge if p.moderator or p.character else p.age
        return

    membership_history = PenguinMembership.query.where(PenguinMembership.penguin_id == p.id)
    current_timestamp = datetime.now()
    postcards = []

    warning_postcards = MemberWarningPostcardsVanilla if p.is_vanilla_client else MemberWarningPostcardsLegacy
    start_postcard = MemberStartPostcardVanilla if p.is_vanilla_client else MemberStartPostcardLegacy

    async with db.transaction():
        async for membership_record in membership_history.gino.iterate():
            membership_recurring = membership_record.expires is None
            membership_active = membership_recurring or membership_record.expires >= current_timestamp

            if membership_record.start < current_timestamp:
                if membership_active:
                    p.is_member = True

                    if not membership_recurring:
                        days_to_expiry = (membership_record.expires.date() - datetime.now().date()).days
                        p.membership_days_remain = days_to_expiry

                        if days_to_expiry <= MemberWarningDaysToExpiry and not membership_record.expires_aware:
                            postcards.append(dict(
                                penguin_id=p.id,
                                postcard_id=random.choice(warning_postcards),
                                send_date=membership_record.expires - timedelta(days=MemberWarningDaysToExpiry)
                            ))
                            await membership_record.update(expires_aware=True).apply()
                else:
                    if p.membership_days_remain < 0:
                        days_since_expiry = (membership_record.expires.date() - datetime.now().date()).days
                        p.membership_days_remain = min(p.membership_days_remain, days_since_expiry)

                    if not membership_record.expired_aware:
                        if p.is_vanilla_client:
                            postcards.append(dict(
                                penguin_id=p.id,
                                postcard_id=MemberExpiredPostcard,
                                send_date=membership_record.expires
                            ))
                        await membership_record.update(expired_aware=True).apply()

                if not membership_record.start_aware:
                    postcards.append(dict(
                        penguin_id=p.id,
                        postcard_id=start_postcard,
                        send_date=membership_record.start
                    ))
                    await membership_record.update(start_aware=True).apply()

            membership_end_date = current_timestamp if membership_active else membership_record.expires
            p.membership_days_total += (membership_end_date - membership_record.start).days

    if postcards:
        await PenguinPostcard.insert().values(postcards).gino.status()
Exemplo n.º 2
0
async def setup_waddles(room_collection):
    async with db.transaction():
        async for waddle in RoomWaddle.query.gino.iterate():
            room_collection[waddle.room_id].waddles[waddle.id] = waddle
            waddle.room = room_collection[waddle.room_id]
            waddle.logic = WaddleLogicMapping[waddle.game]
            waddle.penguins = [None] * waddle.seats
Exemplo n.º 3
0
async def handle_refresh_my_track_likes(p):
    likes = db.func.count(TrackLike.track_id)
    track_likes_query = db.select([PenguinTrack.id, likes])\
        .select_from(PenguinTrack.outerjoin(TrackLike))\
        .where(PenguinTrack.owner_id == p.id)\
        .group_by(PenguinTrack.id).gino.load(PenguinTrack.load(likes=ColumnLoader(likes)))
    async with db.transaction():
        async for track in track_likes_query.iterate():
            await p.send_xt('getlikecountfortrack', p.id, f'10000{track.id}', track.likes)
Exemplo n.º 4
0
async def get_player_tracks(p):
    player_tracks = []
    likes = db.func.count(TrackLike.track_id)
    tracks_query = db.select([PenguinTrack, likes])\
        .select_from(PenguinTrack.outerjoin(TrackLike))\
        .where(PenguinTrack.owner_id == p.id)\
        .group_by(PenguinTrack.id).gino.load(PenguinTrack.load(likes=ColumnLoader(likes)))
    async with db.transaction():
        async for track in tracks_query.iterate():
            player_tracks.append(f'10000{track.id}|{track.name}|{int(track.sharing)}|{track.likes}')
    return player_tracks
Exemplo n.º 5
0
async def get_shared_tracks(p):
    shared_tracks = []
    likes = db.func.count(TrackLike.track_id)
    tracks_query = db.select([PenguinTrack, likes])\
        .select_from(PenguinTrack.outerjoin(TrackLike))\
        .where((PenguinTrack.owner_id.in_(tuple(p.server.penguins_by_id.keys())) & (PenguinTrack.sharing == True)))\
        .group_by(PenguinTrack.id).gino.load(PenguinTrack.load(likes=ColumnLoader(likes)))
    async with db.transaction():
        async for track in tracks_query.iterate():
            penguin = p.server.penguins_by_id[track.owner_id]
            shared_tracks.append(f'{penguin.id}|{penguin.safe_name}|10000{track.id}|{track.likes}')
    return shared_tracks
Exemplo n.º 6
0
async def init_all_quests(p):
    query = Quest.load(tasks=QuestTask,
                       complete=PenguinQuestTask.on(
                           (PenguinQuestTask.penguin_id == p.id)
                           & (QuestTask.id == PenguinQuestTask.task_id))).gino

    async with db.transaction():
        async for quest in query.iterate():
            for task in quest.tasks:
                if task.id not in quest.in_progress.union(quest.complete):
                    await PenguinQuestTask.create(task_id=task.id,
                                                  penguin_id=p.id)

    await load_active_quests(p)
Exemplo n.º 7
0
 async def get_tracks(self):
     self.playlist = []
     self.penguins_by_track_id = {}
     room = self.server.rooms[SoundStudio.StudioRoomId]
     room_penguins = room.penguins_by_id.copy()
     likes = db.func.count(TrackLike.track_id)
     tracks_query = db.select([PenguinTrack, likes]) \
         .select_from(PenguinTrack.outerjoin(TrackLike)) \
         .where((PenguinTrack.owner_id.in_(tuple(room_penguins.keys())))
                & (PenguinTrack.sharing == True)) \
         .group_by(PenguinTrack.id).gino.load(PenguinTrack.load(likes=ColumnLoader(likes)))
     async with db.transaction():
         async for track in tracks_query.iterate():
             self.playlist.append(track)
             self.penguins_by_track_id[track.id] = room_penguins[track.owner_id]
Exemplo n.º 8
0
async def get_quest_settings(p):
    query = Quest.load(items=QuestAwardItem,
                       furniture=QuestAwardFurniture,
                       pet=QuestAwardPuffleItem,
                       tasks=QuestTask).gino

    def encode_award(award):
        award_id, award_type = AwardTypes[type(award)]
        return {
            'id': getattr(award, award_id),
            'type': award_type,
            'n': award.quantity if hasattr(award, 'quantity') else 1
        }

    def encode_task(task):
        return {
            'type': 'room' if task.room_id is not None else '',
            'description': task.description,
            'data': task.room_id if task.room_id is not None else task.data
        }

    async with db.transaction():
        quest_settings = {
            'ver':
            1,
            'spawnRoomId':
            p.room.id,
            'quests': [{
                'id':
                quest.id,
                'name':
                quest.name,
                'awards': [encode_award(award) for award in quest.awards],
                'tasks': [encode_task(task) for task in quest.tasks]
            } async for quest in query.iterate()]
        }

    return ujson.dumps(quest_settings)
Exemplo n.º 9
0
async def get_player_quest_status(p):
    query = Quest.load(tasks=QuestTask,
                       items=QuestAwardItem,
                       furniture=QuestAwardFurniture,
                       pet=QuestAwardPuffleItem,
                       complete=PenguinQuestTask.on(
                           (PenguinQuestTask.penguin_id == p.id)
                           & (QuestTask.id == PenguinQuestTask.task_id))).gino

    def has_award(quest):
        for award in quest.items:
            if award.item_id not in p.inventory:
                return False
        for award in quest.furniture:
            if award.furniture_id not in p.furniture:
                return False
        for award in quest.pet:
            if award.puffle_item_id not in p.puffle_items:
                return False
        return True

    def encode_quest(quest):
        tasks_complete = [task.id in quest.complete for task in quest.tasks]
        quest_status = 'prize claimed' if has_award(quest) else 'complete' if all(tasks_complete) else 'available' \
            if quest.in_progress else 'not available'
        return {
            'id': quest.id,
            'status': quest_status,
            'tasks': tasks_complete
        }

    async with db.transaction():
        player_quest_status = {
            'quests': [encode_quest(quest) async for quest in query.iterate()]
        }

    return ujson.dumps(player_quest_status)
Exemplo n.º 10
0
async def setup_tables(room_collection):
    async with db.transaction():
        async for table in RoomTable.query.gino.iterate():
            room_collection[table.room_id].tables[table.id] = table
            table.room = room_collection[table.room_id]
            table.logic = TableLogicMapping[table.game]()