Switch from aiocache to cacheout library

This commit is contained in:
Ben 2020-03-27 03:13:48 +00:00
parent 923c31fbd1
commit 1ccca96cc2
9 changed files with 127 additions and 146 deletions

View File

@ -2,8 +2,6 @@ import datetime
import random import random
import time import time
from aiocache import cached
from houdini import handlers from houdini import handlers
from houdini.constants import ClientType from houdini.constants import ClientType
from houdini.data.item import Item from houdini.data.item import Item
@ -13,7 +11,6 @@ from houdini.handlers import XTPacket
from houdini.handlers.play.mail import handle_start_mail_engine from houdini.handlers.play.mail import handle_start_mail_engine
@cached(alias='default', key='com_messages')
async def get_com_messages(p): async def get_com_messages(p):
async with p.server.db.transaction(): async with p.server.db.transaction():
messages = [] messages = []
@ -123,9 +120,11 @@ async def handle_get_com_messages(p):
unread_com_message = await EpfComMessage.query.where( unread_com_message = await EpfComMessage.query.where(
EpfComMessage.date > p.com_message_read_date).gino.scalar() EpfComMessage.date > p.com_message_read_date).gino.scalar()
if unread_com_message: if unread_com_message:
await p.server.cache.delete('com_messages') p.server.cache.delete('com_messages')
await p.update(com_message_read_date=datetime.datetime.now()).apply() await p.update(com_message_read_date=datetime.datetime.now()).apply()
com_messages = await get_com_messages(p) com_messages = p.server.cache.get('com_messages')
com_messages = await get_com_messages(p) if com_messages is None else com_messages
p.server.cache.set('com_messages', com_messages)
if com_messages: if com_messages:
await p.send_xt('epfgm', int(bool(unread_com_message)), com_messages) await p.send_xt('epfgm', int(bool(unread_com_message)), com_messages)
else: else:

View File

@ -1,5 +1,4 @@
import ujson import ujson
from aiocache import cached
from houdini import handlers from houdini import handlers
from houdini.data import db from houdini.data import db
@ -9,15 +8,6 @@ from houdini.handlers import XTPacket
from houdini.handlers.play.navigation import handle_join_player_room, handle_join_room, handle_join_server from houdini.handlers.play.navigation import handle_join_player_room, handle_join_room, handle_join_server
def get_status_key(_, p):
return f'quest.status.{p.id}'
def get_settings_key(_, p):
return f'quest.settings.{p.room.id}'
@cached(alias='default', key_builder=get_status_key)
async def get_player_quest_status(p): async def get_player_quest_status(p):
query = Quest.load(tasks=QuestTask, query = Quest.load(tasks=QuestTask,
items=QuestAwardItem, items=QuestAwardItem,
@ -63,7 +53,6 @@ AwardTypes = {
} }
@cached(alias='default', key_builder=get_settings_key)
async def get_quest_settings(p): async def get_quest_settings(p):
query = Quest.load(items=QuestAwardItem, query = Quest.load(items=QuestAwardItem,
furniture=QuestAwardFurniture, furniture=QuestAwardFurniture,
@ -127,15 +116,22 @@ async def load_active_quests(p):
@handlers.handler(XTPacket('j', 'js'), after=handle_join_server) @handlers.handler(XTPacket('j', 'js'), after=handle_join_server)
@handlers.allow_once @handlers.allow_once
async def handle_quest_join_server(p): async def handle_quest_join_server(p):
await p.server.cache.delete(f'quest.status.{p.id}') p.server.cache.delete(f'quest.status.{p.id}')
await load_active_quests(p) await load_active_quests(p)
await p.send_xt('nxquestsettings', await get_quest_settings(p)) quest_settings = p.server.cache.get(f'quest.settings.{p.room.id}')
await p.send_xt('nxquestdata', await get_player_quest_status(p)) quest_status = p.server.cache.get(f'quest.status.{p.id}')
quest_settings = await get_quest_settings(p) if quest_settings is None else quest_settings
quest_status = await get_player_quest_status(p) if quest_status is None else quest_status
p.server.cache.set(f'quest.settings.{p.room.id}', quest_settings)
p.server.cache.set(f'quest.status.{p.id}', quest_status)
await p.send_xt('nxquestsettings', quest_settings)
await p.send_xt('nxquestdata', quest_status)
async def set_task_cleared(p, task_id): async def set_task_cleared(p, task_id):
await p.server.cache.delete(f'quest.status.{p.id}') p.server.cache.delete(f'quest.status.{p.id}')
await PenguinQuestTask.update.values(complete=True) \ await PenguinQuestTask.update.values(complete=True) \
.where((PenguinQuestTask.task_id == task_id) & .where((PenguinQuestTask.task_id == task_id) &
@ -166,7 +162,7 @@ async def handle_quest_join_player_room(p):
@handlers.handler(XTPacket('nx', 'nxquestaward')) @handlers.handler(XTPacket('nx', 'nxquestaward'))
async def handle_quest_award(p, quest_id: int): async def handle_quest_award(p, quest_id: int):
await p.server.cache.delete(f'quest.status.{p.id}') p.server.cache.delete(f'quest.status.{p.id}')
quest = await Quest.load(items=QuestAwardItem, quest = await Quest.load(items=QuestAwardItem,
furniture=QuestAwardFurniture, furniture=QuestAwardFurniture,
@ -183,7 +179,7 @@ async def handle_quest_award(p, quest_id: int):
@handlers.handler(XTPacket('nx', 'nxquestactivate')) @handlers.handler(XTPacket('nx', 'nxquestactivate'))
@handlers.allow_once @handlers.allow_once
async def handle_quest_activate(p): async def handle_quest_activate(p):
await p.server.cache.delete(f'quest.status.{p.id}') p.server.cache.delete(f'quest.status.{p.id}')
await init_all_quests(p) await init_all_quests(p)
await p.send_xt('nxquestdata', await get_player_quest_status(p)) await p.send_xt('nxquestdata', await get_player_quest_status(p))

View File

@ -3,7 +3,6 @@ import time
from datetime import datetime, timedelta from datetime import datetime, timedelta
import ujson import ujson
from aiocache import cached
from sqlalchemy.dialects.postgresql import insert from sqlalchemy.dialects.postgresql import insert
from houdini import handlers from houdini import handlers
@ -20,27 +19,6 @@ from houdini.handlers import Priority, XMLPacket, XTPacket
from houdini.handlers.play.navigation import handle_join_server from houdini.handlers.play.navigation import handle_join_server
def get_layout_furniture_key(_, p, igloo_id):
return f'layout_furniture.{igloo_id}'
def get_active_igloo_string_key(_, p, penguin_id):
return f'active_igloo.{penguin_id}'
def get_legacy_igloo_string_key(_, p, penguin_id):
return f'legacy_igloo.{penguin_id}'
def get_igloo_layouts_key(_, p):
return f'igloo_layouts.{p.id}'
def get_layout_like_count_key(_, igloo_id):
return f'layout_like_count.{igloo_id}'
@cached(alias='default', key_builder=get_layout_furniture_key)
async def get_layout_furniture(p, igloo_id): async def get_layout_furniture(p, igloo_id):
igloo_furniture = IglooFurniture.query.where(IglooFurniture.igloo_id == igloo_id).gino igloo_furniture = IglooFurniture.query.where(IglooFurniture.igloo_id == igloo_id).gino
async with p.server.db.transaction(): async with p.server.db.transaction():
@ -50,41 +28,48 @@ async def get_layout_furniture(p, igloo_id):
return furniture_string return furniture_string
@cached(alias='default', key_builder=get_active_igloo_string_key) async def get_igloo(penguin_id):
async def get_active_igloo_string(p, penguin_id): return await PenguinIglooRoom.load(parent=Penguin.on(Penguin.igloo == PenguinIglooRoom.id))\
igloo = await PenguinIglooRoom.load(parent=Penguin.on(Penguin.igloo == PenguinIglooRoom.id))\
.where(PenguinIglooRoom.penguin_id == penguin_id).gino.first() .where(PenguinIglooRoom.penguin_id == penguin_id).gino.first()
furniture_string = await get_layout_furniture(p, igloo.id)
like_count = await get_layout_like_count(igloo.id) async def get_active_igloo_string(p, penguin_id):
igloo = await get_igloo(penguin_id)
furniture_string = p.server.cache.get(f'layout_furniture.{igloo.id}')
furniture_string = await get_layout_furniture(p, igloo.id) if furniture_string is None else furniture_string
like_count = p.server.cache.get(f'layout_like_count.{igloo.id}')
like_count = await get_layout_like_count(igloo.id) if like_count is None else like_count
p.server.cache.set(f'layout_furniture.{igloo.id}', furniture_string)
p.server.cache.set(f'layout_like_count.{igloo.id}', like_count)
return f'{igloo.id}:1:0:{int(igloo.locked)}:{igloo.music}:{igloo.flooring}:' \ return f'{igloo.id}:1:0:{int(igloo.locked)}:{igloo.music}:{igloo.flooring}:' \
f'{igloo.location}:{igloo.type}:{like_count}:{furniture_string}' f'{igloo.location}:{igloo.type}:{like_count}:{furniture_string}'
@cached(alias='default', key_builder=get_legacy_igloo_string_key)
async def get_legacy_igloo_string(p, penguin_id): async def get_legacy_igloo_string(p, penguin_id):
igloo = await PenguinIglooRoom.load(parent=Penguin.on(Penguin.igloo == PenguinIglooRoom.id))\ igloo = await get_igloo(penguin_id)
.where(PenguinIglooRoom.penguin_id == penguin_id).gino.first() furniture_string = p.server.cache.get(f'layout_furniture.{igloo.id}')
furniture_string = await get_layout_furniture(p, igloo.id) if furniture_string is None else furniture_string
furniture_string = await get_layout_furniture(p, igloo.id) p.server.cache.set(f'layout_furniture.{igloo.id}', furniture_string)
return f'{igloo.type}%{igloo.music}%{igloo.flooring}%{furniture_string}' return f'{igloo.type}%{igloo.music}%{igloo.flooring}%{furniture_string}'
@cached(alias='default', key_builder=get_igloo_layouts_key)
async def get_all_igloo_layouts(p): async def get_all_igloo_layouts(p):
layout_details = [] layout_details = []
slot = 0 for slot, igloo in enumerate(p.igloo_rooms.values()):
for igloo in p.igloo_rooms.values(): furniture_string = p.server.cache.get(f'layout_furniture.{igloo.id}')
slot += 1 furniture_string = await get_layout_furniture(p, igloo.id) if furniture_string is None else furniture_string
furniture_string = await get_layout_furniture(p, igloo.id) like_count = p.server.cache.get(f'layout_like_count.{igloo.id}')
like_count = await get_layout_like_count(igloo.id) like_count = await get_layout_like_count(igloo.id) if like_count is None else like_count
p.server.cache.set(f'layout_furniture.{igloo.id}', furniture_string)
p.server.cache.set(f'layout_like_count.{igloo.id}', like_count)
igloo_details = f'{igloo.id}:{slot}:0:{int(igloo.locked)}:{igloo.music}:{igloo.flooring}' \ igloo_details = f'{igloo.id}:{slot}:0:{int(igloo.locked)}:{igloo.music}:{igloo.flooring}' \
f':{igloo.location}:{igloo.type}:{like_count}:{furniture_string}' f':{igloo.location}:{igloo.type}:{like_count}:{furniture_string}'
layout_details.append(igloo_details) layout_details.append(igloo_details)
return '%'.join(layout_details) return '%'.join(layout_details)
@cached(alias='default', key_builder=get_layout_like_count_key)
async def get_layout_like_count(igloo_id): async def get_layout_like_count(igloo_id):
layout_like_count = await db.select([db.func.sum(IglooLike.count)])\ layout_like_count = await db.select([db.func.sum(IglooLike.count)])\
.where(IglooLike.igloo_id == igloo_id).gino.scalar() .where(IglooLike.igloo_id == igloo_id).gino.scalar()
@ -180,14 +165,21 @@ async def load_igloo_inventory(p):
@handlers.cooldown(1) @handlers.cooldown(1)
async def handle_get_igloo_details(p, penguin_id: int): async def handle_get_igloo_details(p, penguin_id: int):
await create_first_igloo(p, penguin_id) await create_first_igloo(p, penguin_id)
cache_key = f'active_igloo.{penguin_id}' if p.is_vanilla_client else f'legacy_igloo.{penguin_id}'
igloo_string_method = get_active_igloo_string if p.is_vanilla_client else get_legacy_igloo_string igloo_string_method = get_active_igloo_string if p.is_vanilla_client else get_legacy_igloo_string
await p.send_xt('gm', penguin_id, await igloo_string_method(p, penguin_id)) igloo_string = p.server.cache.get(cache_key)
igloo_string = await igloo_string_method(p, penguin_id) if igloo_string is None else igloo_string
p.server.cache.set(cache_key, igloo_string)
await p.send_xt('gm', penguin_id, igloo_string)
@handlers.handler(XTPacket('g', 'gail'), client=ClientType.Vanilla) @handlers.handler(XTPacket('g', 'gail'), client=ClientType.Vanilla)
async def handle_get_all_igloo_layouts(p): async def handle_get_all_igloo_layouts(p):
await p.status_field_set(StatusField.OpenedIglooViewer) await p.status_field_set(StatusField.OpenedIglooViewer)
await p.send_xt('gail', p.id, 0, await get_all_igloo_layouts(p)) layouts_string = p.server.cache.get(f'igloo_layouts.{p.id}')
layouts_string = await get_all_igloo_layouts(p) if layouts_string is None else layouts_string
p.server.cache.set(f'igloo_layouts.{p.id}', layouts_string)
await p.send_xt('gail', p.id, 0, layouts_string)
@handlers.handler(XTPacket('g', 'gail'), client=ClientType.Vanilla, after=handle_get_all_igloo_layouts) @handlers.handler(XTPacket('g', 'gail'), client=ClientType.Vanilla, after=handle_get_all_igloo_layouts)
@ -195,7 +187,9 @@ async def handle_get_all_igloo_likes(p):
total = 0 total = 0
like_strings = [] like_strings = []
for igloo in p.igloo_rooms.values(): for igloo in p.igloo_rooms.values():
like_count = await get_layout_like_count(igloo.id) like_count = p.server.cache.get(f'layout_like_count.{igloo.id}')
like_count = await get_layout_like_count(igloo.id) if like_count is None else like_count
p.server.cache.set(f'layout_like_count.{igloo.id}', like_count)
total += like_count total += like_count
like_strings.append(f'{igloo.id}|{like_count}') like_strings.append(f'{igloo.id}|{like_count}')
await p.send_xt('gaili', total, ','.join(like_strings)) await p.send_xt('gaili', total, ','.join(like_strings))
@ -222,9 +216,9 @@ async def handle_buy_flooring(p, flooring: Flooring):
await p.send_xt('ag', flooring.id, p.coins) await p.send_xt('ag', flooring.id, p.coins)
await p.server.cache.delete(f'active_igloo.{p.id}') p.server.cache.delete(f'active_igloo.{p.id}')
await p.server.cache.delete(f'legacy_igloo.{p.id}') p.server.cache.delete(f'legacy_igloo.{p.id}')
await p.server.cache.delete(f'igloo_layouts.{p.id}') p.server.cache.delete(f'igloo_layouts.{p.id}')
@handlers.handler(XTPacket('g', 'aloc'), client=ClientType.Vanilla) @handlers.handler(XTPacket('g', 'aloc'), client=ClientType.Vanilla)
@ -292,15 +286,17 @@ async def handle_update_igloo_configuration(p, igloo_id: int, igloo_type_id: int
music=music_id music=music_id
).apply() ).apply()
like_count = await get_layout_like_count(igloo.id) like_count = p.server.cache.get(f'layout_like_count.{igloo.id}')
like_count = await get_layout_like_count(igloo.id) if like_count is None else like_count
p.server.cache.set(f'layout_like_count.{igloo.id}', like_count)
active_igloo_string = f'{igloo.id}:1:0:{int(igloo.locked)}:{igloo.music}:{igloo.flooring}:' \ active_igloo_string = f'{igloo.id}:1:0:{int(igloo.locked)}:{igloo.music}:{igloo.flooring}:' \
f'{igloo.location}:{igloo.type}:{like_count}:{furniture_data}' f'{igloo.location}:{igloo.type}:{like_count}:{furniture_data}'
await p.room.send_xt('uvi', p.id, active_igloo_string) await p.room.send_xt('uvi', p.id, active_igloo_string)
await p.server.cache.set(f'layout_furniture.{igloo.id}', furniture_data) p.server.cache.set(f'layout_furniture.{igloo.id}', furniture_data)
await p.server.cache.set(f'active_igloo.{p.id}', active_igloo_string) p.server.cache.set(f'active_igloo.{p.id}', active_igloo_string)
await p.server.cache.delete(f'legacy_igloo.{p.id}') p.server.cache.delete(f'legacy_igloo.{p.id}')
await p.server.cache.delete(f'igloo_layouts.{p.id}') p.server.cache.delete(f'igloo_layouts.{p.id}')
@handlers.handler(XTPacket('g', 'ur'), client=ClientType.Legacy) @handlers.handler(XTPacket('g', 'ur'), client=ClientType.Legacy)
@ -308,8 +304,8 @@ async def handle_update_igloo_configuration(p, igloo_id: int, igloo_type_id: int
async def handle_save_igloo_furniture(p, *furniture_data): async def handle_save_igloo_furniture(p, *furniture_data):
await save_igloo_furniture(p, furniture_data) await save_igloo_furniture(p, furniture_data)
await p.server.cache.set(f'layout_furniture.{p.igloo}', ','.join(furniture_data)) p.server.cache.set(f'layout_furniture.{p.igloo}', ','.join(furniture_data))
await p.server.cache.delete(f'legacy_igloo.{p.id}') p.server.cache.delete(f'legacy_igloo.{p.id}')
_slot_converter = SeparatorConverter(separator=',', mapper=str) _slot_converter = SeparatorConverter(separator=',', mapper=str)
@ -350,9 +346,9 @@ async def handle_update_igloo_slot_summary(p, igloo_id: int, slot_summary: _slot
if igloo.locked != bool(locked): if igloo.locked != bool(locked):
await igloo.update(locked=bool(locked)).apply() await igloo.update(locked=bool(locked)).apply()
await p.server.cache.delete(f'active_igloo.{p.id}') p.server.cache.delete(f'active_igloo.{p.id}')
await p.server.cache.delete(f'legacy_igloo.{p.id}') p.server.cache.delete(f'legacy_igloo.{p.id}')
await p.server.cache.delete(f'igloo_layouts.{p.id}') p.server.cache.delete(f'igloo_layouts.{p.id}')
active_igloo_string = await get_active_igloo_string(p, p.id) active_igloo_string = await get_active_igloo_string(p, p.id)
await p.room.send_xt('uvi', p.id, active_igloo_string) await p.room.send_xt('uvi', p.id, active_igloo_string)
@ -393,7 +389,9 @@ async def handle_add_igloo_layout(p):
@handlers.cooldown(1) @handlers.cooldown(1)
async def handle_get_igloo_like_by(p, pagination_start: int, pagination_end: int): async def handle_get_igloo_like_by(p, pagination_start: int, pagination_end: int):
if p.room.igloo: if p.room.igloo:
like_count = await get_layout_like_count(p.room.id) like_count = p.server.cache.get(f'layout_like_count.{p.room.id}')
like_count = await get_layout_like_count(p.room.id) if like_count is None else like_count
p.server.cache.set(f'layout_like_count.{p.room.id}', like_count)
liked_by = IglooLike.query.where(IglooLike.igloo_id == p.room.id). \ liked_by = IglooLike.query.where(IglooLike.igloo_id == p.room.id). \
limit(pagination_end - pagination_start).offset(pagination_start).gino limit(pagination_end - pagination_start).offset(pagination_start).gino
@ -443,12 +441,20 @@ async def handle_can_like_igloo(p):
async def handle_get_open_igloo_list(p): async def handle_get_open_igloo_list(p):
async def get_igloo_string(igloo): async def get_igloo_string(igloo):
owner_name = p.server.penguins_by_id[igloo.penguin_id].safe_name owner_name = p.server.penguins_by_id[igloo.penguin_id].safe_name
like_count = await get_layout_like_count(igloo.id) like_count = p.server.cache.get(f'layout_like_count.{igloo.id}')
like_count = await get_layout_like_count(igloo.id) if like_count is None else like_count
p.server.cache.set(f'layout_like_count.{igloo.id}', like_count)
igloo_population = len(igloo.penguins_by_id) igloo_population = len(igloo.penguins_by_id)
return f'{igloo.penguin_id}|{owner_name}|{like_count}|{igloo_population}|{int(igloo.locked)}' return f'{igloo.penguin_id}|{owner_name}|{like_count}|{igloo_population}|{int(igloo.locked)}'
open_igloos = [await get_igloo_string(igloo) for igloo in p.server.open_igloos_by_penguin_id.values()] open_igloos = [await get_igloo_string(igloo) for igloo in p.server.open_igloos_by_penguin_id.values()]
local_room_population = 0 local_room_population = 0
own_layout_like_count = p.server.cache.get(f'layout_like_count.{p.igloo}')
own_layout_like_count = 0 if p.igloo is None else await get_layout_like_count(p.igloo) \
if own_layout_like_count is None else own_layout_like_count
p.server.cache.set(f'layout_like_count.{p.igloo}', own_layout_like_count)
own_layout_like_count = 0 if p.igloo is None else await get_layout_like_count(p.igloo) own_layout_like_count = 0 if p.igloo is None else await get_layout_like_count(p.igloo)
await p.send_xt('gr', own_layout_like_count, local_room_population, *open_igloos) await p.send_xt('gr', own_layout_like_count, local_room_population, *open_igloos)
@ -494,9 +500,9 @@ async def handle_update_igloo_music(p, music_id: int):
if p.room.igloo and p.room.penguin_id == p.id and p.room.music != music_id: if p.room.igloo and p.room.penguin_id == p.id and p.room.music != music_id:
await p.room.update(music=music_id).apply() await p.room.update(music=music_id).apply()
await p.server.cache.delete(f'active_igloo.{p.id}') p.server.cache.delete(f'active_igloo.{p.id}')
await p.server.cache.delete(f'legacy_igloo.{p.id}') p.server.cache.delete(f'legacy_igloo.{p.id}')
await p.server.cache.delete(f'igloo_layouts.{p.id}') p.server.cache.delete(f'igloo_layouts.{p.id}')
@handlers.handler(XTPacket('g', 'ao'), client=ClientType.Legacy) @handlers.handler(XTPacket('g', 'ao'), client=ClientType.Legacy)
@ -505,15 +511,18 @@ async def handle_activate_igloo_type(p, igloo_type_id: int):
and igloo_type_id in p.igloos: and igloo_type_id in p.igloos:
await p.room.update(type=igloo_type_id, flooring=0).apply() await p.room.update(type=igloo_type_id, flooring=0).apply()
await p.server.cache.delete(f'active_igloo.{p.id}') p.server.cache.delete(f'active_igloo.{p.id}')
await p.server.cache.delete(f'legacy_igloo.{p.id}') p.server.cache.delete(f'legacy_igloo.{p.id}')
await p.server.cache.delete(f'igloo_layouts.{p.id}') p.server.cache.delete(f'igloo_layouts.{p.id}')
@handlers.handler(XTPacket('g', 'grf'), client=ClientType.Vanilla) @handlers.handler(XTPacket('g', 'grf'), client=ClientType.Vanilla)
async def handle_get_friends_igloo_list(p): async def handle_get_friends_igloo_list(p):
async def get_friend_igloo_string(penguin): async def get_friend_igloo_string(penguin):
like_count = 0 if penguin.igloo is None else await get_layout_like_count(penguin.igloo) like_count = p.server.cache.get(f'layout_like_count.{penguin.igloo}')
like_count = 0 if penguin.igloo is None else await get_layout_like_count(penguin.igloo) \
if like_count is None else like_count
p.server.cache.set(f'layout_like_count.{penguin.igloo}', like_count)
return f'{penguin.id}|{like_count}' return f'{penguin.id}|{like_count}'
friend_igloos = [await get_friend_igloo_string(penguin) for penguin in p.server.penguins_by_id.values() friend_igloos = [await get_friend_igloo_string(penguin) for penguin in p.server.penguins_by_id.values()
@ -535,7 +544,7 @@ async def handle_like_igloo(p):
like_count = await like_insert.gino.scalar() like_count = await like_insert.gino.scalar()
await p.room.send_xt('lue', p.id, like_count, f=lambda penguin: penguin.id != p.id) await p.room.send_xt('lue', p.id, like_count, f=lambda penguin: penguin.id != p.id)
await p.server.cache.delete(f'layout_like_count.{p.room.id}') p.server.cache.delete(f'layout_like_count.{p.room.id}')
@handlers.handler(XTPacket('g', 'gii'), client=ClientType.Vanilla) @handlers.handler(XTPacket('g', 'gii'), client=ClientType.Vanilla)

View File

@ -1,40 +1,29 @@
import operator import operator
import time import time
from aiocache import cached
from houdini import handlers from houdini import handlers
from houdini.data.item import Item, ItemCollection, PenguinItemCollection from houdini.data.item import Item, ItemCollection, PenguinItemCollection
from houdini.data.permission import PenguinPermissionCollection from houdini.data.permission import PenguinPermissionCollection
from houdini.handlers import Priority, XMLPacket, XTPacket from houdini.handlers import Priority, XMLPacket, XTPacket
def get_pin_string_key(_, p, player_id):
return f'pins.{player_id}'
def get_awards_string_key(_, p, player_id):
return f'awards.{player_id}'
@cached(alias='default', key_builder=get_pin_string_key)
async def get_pin_string(p, player_id): async def get_pin_string(p, player_id):
if player_id in p.server.penguins_by_id: if player_id in p.server.penguins_by_id:
inventory = p.server.penguins_by_id[player_id].inventory inventory = p.server.penguins_by_id[player_id].inventory
else: else:
inventory = await PenguinItemCollection.get_collection(player_id) inventory = await PenguinItemCollection.get_collection(player_id)
def is_free_pin(pin):
p.server.items[pin].is_flag() and p.server.items[pin].cost == 0
free_pins = (p.server.items[pin] for pin in inventory.keys() if is_free_pin(pin))
pins = sorted(free_pins, key=operator.attrgetter('release_date'))
def get_string(pin): def get_string(pin):
unix = int(time.mktime(pin.release_date.timetuple())) unix = int(time.mktime(pin.release_date.timetuple()))
return f'{pin.id}|{unix}|{int(pin.member)}' return f'{pin.id}|{unix}|{int(pin.member)}'
pins = sorted((p.server.items[pin] for pin in inventory.keys()
if (p.server.items[pin].is_flag() and p.server.items[pin].cost == 0)),
key=operator.attrgetter('release_date'))
return '%'.join(get_string(pin) for pin in pins) return '%'.join(get_string(pin) for pin in pins)
@cached(alias='default', key_builder=get_awards_string_key)
async def get_awards_string(p, player_id): async def get_awards_string(p, player_id):
if player_id in p.server.penguins_by_id: if player_id in p.server.penguins_by_id:
inventory = p.server.penguins_by_id[player_id].inventory inventory = p.server.penguins_by_id[player_id].inventory
@ -96,11 +85,17 @@ async def handle_buy_inventory(p, item: Item):
@handlers.depends_on_packet(XTPacket('i', 'gi')) @handlers.depends_on_packet(XTPacket('i', 'gi'))
@handlers.cooldown(1) @handlers.cooldown(1)
async def handle_query_player_pins(p, player_id: int): async def handle_query_player_pins(p, player_id: int):
await p.send_xt('qpp', await get_pin_string(p, player_id)) string = p.server.cache.get(f'pins.{player_id}')
string = await get_pin_string(p, player_id) if string is None else string
p.server.cache.set(f'pins.{player_id}', string)
await p.send_xt('qpp', string)
@handlers.handler(XTPacket('i', 'qpa')) @handlers.handler(XTPacket('i', 'qpa'))
@handlers.depends_on_packet(XTPacket('i', 'gi')) @handlers.depends_on_packet(XTPacket('i', 'gi'))
@handlers.cooldown(1) @handlers.cooldown(1)
async def handle_query_player_awards(p, player_id: int): async def handle_query_player_awards(p, player_id: int):
await p.send_xt('qpa', await get_awards_string(p, player_id)) string = p.server.cache.get(f'awards.{player_id}')
string = await get_awards_string(p, player_id) if string is None else string
p.server.cache.set(f'awards.{player_id}', string)
await p.send_xt('qpa', string)

View File

@ -3,8 +3,6 @@ import random
import time import time
from datetime import datetime, timedelta from datetime import datetime, timedelta
from aiocache import cached
from houdini import handlers from houdini import handlers
from houdini.constants import ClientType from houdini.constants import ClientType
from houdini.converters import SeparatorConverter from houdini.converters import SeparatorConverter
@ -14,15 +12,6 @@ from houdini.data.penguin import Penguin, PenguinMembership
from houdini.handlers import Priority, XMLPacket, XTPacket from houdini.handlers import Priority, XMLPacket, XTPacket
def get_player_string_key(_, p, player_id):
return f'player.{player_id}'
def get_mascot_string_key(_, p, mascot_id):
return f'mascot.{mascot_id}'
@cached(alias='default', key_builder=get_player_string_key)
async def get_player_string(p, penguin_id: int): async def get_player_string(p, penguin_id: int):
if penguin_id in p.server.penguins_by_id: if penguin_id in p.server.penguins_by_id:
return await p.server.penguins_by_id[penguin_id].string return await p.server.penguins_by_id[penguin_id].string
@ -32,7 +21,6 @@ async def get_player_string(p, penguin_id: int):
return string return string
@cached(alias='default', key_builder=get_mascot_string_key)
async def get_mascot_string(p, mascot_id: int): async def get_mascot_string(p, mascot_id: int):
if mascot_id in p.server.penguins_by_character_id: if mascot_id in p.server.penguins_by_character_id:
return await p.server.penguins_by_character_id[mascot_id].string return await p.server.penguins_by_character_id[mascot_id].string
@ -163,13 +151,18 @@ async def handle_heartbeat(p):
@handlers.handler(XTPacket('u', 'gp')) @handlers.handler(XTPacket('u', 'gp'))
@handlers.cooldown(1) @handlers.cooldown(1)
async def handle_get_player(p, penguin_id: int): async def handle_get_player(p, penguin_id: int):
await p.send_xt('gp', await get_player_string(p, penguin_id)) player_string = p.server.cache.get(f'player.{penguin_id}')
player_string = await get_player_string(p, penguin_id) if player_string is None else player_string
await p.send_xt('gp', player_string)
@handlers.handler(XTPacket('u', 'gmo'), client=ClientType.Vanilla) @handlers.handler(XTPacket('u', 'gmo'), client=ClientType.Vanilla)
@handlers.cooldown(1) @handlers.cooldown(1)
async def handle_get_mascot(p, mascot_id: int): async def handle_get_mascot(p, mascot_id: int):
await p.send_xt('gmo', await get_mascot_string(p, mascot_id)) mascot_string = p.server.cache.get(f'mascot.{mascot_id}')
mascot_string = await get_mascot_string(p, mascot_id) if mascot_string is None else mascot_string
p.server.cache.set(f'mascot.{mascot_id}', mascot_string)
await p.send_xt('gmo', mascot_string)
@handlers.handler(XTPacket('u', 'pbi'), client=ClientType.Vanilla) @handlers.handler(XTPacket('u', 'pbi'), client=ClientType.Vanilla)

View File

@ -1,5 +1,3 @@
from aiocache import cached
from houdini import handlers from houdini import handlers
from houdini.data.penguin import Penguin from houdini.data.penguin import Penguin
from houdini.data.stamp import CoverItem, CoverStamp, PenguinStampCollection, Stamp, StampCollection from houdini.data.stamp import CoverItem, CoverStamp, PenguinStampCollection, Stamp, StampCollection
@ -7,15 +5,6 @@ from houdini.handlers import Priority, XMLPacket, XTPacket
from houdini.handlers.play.navigation import handle_join_room, handle_join_server from houdini.handlers.play.navigation import handle_join_room, handle_join_server
def get_book_cover_key(_, p, player_id):
return f'book.{player_id}'
def get_player_stamps_key(_, p, player_id):
return f'stamps.{player_id}'
@cached(alias='default', key_builder=get_book_cover_key)
async def get_book_cover_string(p, player_id): async def get_book_cover_string(p, player_id):
if player_id in p.server.penguins_by_id: if player_id in p.server.penguins_by_id:
player = p.server.penguins_by_id[player_id] player = p.server.penguins_by_id[player_id]
@ -38,7 +27,6 @@ async def get_book_cover_string(p, player_id):
return '%'.join(map(str, cover_details)) return '%'.join(map(str, cover_details))
@cached(alias='default', key_builder=get_player_stamps_key)
async def get_player_stamps_string(p, player_id): async def get_player_stamps_string(p, player_id):
if player_id in p.server.penguins_by_id: if player_id in p.server.penguins_by_id:
stamp_inventory = p.server.penguins_by_id[player_id].stamps stamp_inventory = p.server.penguins_by_id[player_id].stamps
@ -83,7 +71,10 @@ async def handle_add_mascot_stamp(p):
@handlers.handler(XTPacket('st', 'gps')) @handlers.handler(XTPacket('st', 'gps'))
@handlers.cooldown(1) @handlers.cooldown(1)
async def handle_get_player_stamps(p, player_id: int): async def handle_get_player_stamps(p, player_id: int):
await p.send_xt('gps', p.id, await get_player_stamps_string(p, player_id)) stamps_string = p.server.cache.get(f'stamps.{player_id}')
stamps_string = await get_player_stamps_string(p, player_id) if stamps_string is None else stamps_string
p.server.cache.set(f'stamps.{player_id}', stamps_string)
await p.send_xt('gps', p.id, stamps_string)
@handlers.handler(XTPacket('st', 'gmres')) @handlers.handler(XTPacket('st', 'gmres'))
@ -105,7 +96,10 @@ async def handle_stamp_add(p, stamp: Stamp):
@handlers.handler(XTPacket('st', 'gsbcd')) @handlers.handler(XTPacket('st', 'gsbcd'))
@handlers.cooldown() @handlers.cooldown()
async def handle_get_book_cover(p, player_id: int): async def handle_get_book_cover(p, player_id: int):
await p.send_xt('gsbcd', await get_book_cover_string(p, player_id)) book_string = p.server.cache.get(f'book.{player_id}')
book_string = await get_book_cover_string(p, player_id) if book_string is None else book_string
p.server.cache.set(f'book.{player_id}', book_string)
await p.send_xt('gsbcd', book_string)
@handlers.handler(XTPacket('st', 'ssbcd')) @handlers.handler(XTPacket('st', 'ssbcd'))
@ -160,4 +154,4 @@ async def handle_update_book_cover(p, color: int, highlight: int, pattern: int,
book_modified=1).apply() book_modified=1).apply()
stringified_cover = '%'.join(cover) stringified_cover = '%'.join(cover)
await p.server.cache.set(f'book.{p.id}', f'{color}%{highlight}%{pattern}%{icon}%{stringified_cover}') p.server.cache.set(f'book.{p.id}', f'{color}%{highlight}%{pattern}%{icon}%{stringified_cover}')

View File

@ -5,7 +5,7 @@ import sys
from logging.handlers import RotatingFileHandler from logging.handlers import RotatingFileHandler
import aioredis import aioredis
from aiocache import SimpleMemoryCache, caches from cacheout import Cache
from houdini import PenguinStringCompiler from houdini import PenguinStringCompiler
from houdini.data import db from houdini.data import db
@ -147,12 +147,7 @@ class Houdini:
await self.redis.delete(f'houdini.players.{self.config.id}') await self.redis.delete(f'houdini.players.{self.config.id}')
await self.redis.hset(f'houdini.population', self.config.id, 0) await self.redis.hset(f'houdini.population', self.config.id, 0)
caches.set_config(dict(default=dict( self.cache = Cache(maxsize=None, ttl=self.config.cache_expiry)
cache=SimpleMemoryCache,
namespace='houdini',
ttl=self.config.cache_expiry
)))
self.cache = caches.get('default')
self.client_class = Penguin self.client_class = Penguin
self.penguin_string_compiler = PenguinStringCompiler() self.penguin_string_compiler = PenguinStringCompiler()

View File

@ -103,8 +103,8 @@ class Penguin(Spheniscidae, penguin.Penguin):
self.logger.info(f'{self.username} added \'{item.name}\' to their clothing inventory') self.logger.info(f'{self.username} added \'{item.name}\' to their clothing inventory')
await self.server.cache.delete(f'pins.{self.id}') self.server.cache.delete(f'pins.{self.id}')
await self.server.cache.delete(f'awards.{self.id}') self.server.cache.delete(f'awards.{self.id}')
return True return True
@ -249,7 +249,7 @@ class Penguin(Spheniscidae, penguin.Penguin):
await self.send_xt('aabs', stamp.id) await self.send_xt('aabs', stamp.id)
self.logger.info(f'{self.username} earned stamp \'{stamp.name}\'') self.logger.info(f'{self.username} earned stamp \'{stamp.name}\'')
await self.server.cache.delete(f'stamps.{self.id}') self.server.cache.delete(f'stamps.{self.id}')
return True return True

View File

@ -1,7 +1,7 @@
asyncio asyncio
aioredis aioredis
cacheout
gino gino
aiocache
ujson ujson
defusedxml defusedxml
bcrypt bcrypt