From 657ee1d0632defa838f3b24dc1fff26bc3523124 Mon Sep 17 00:00:00 2001 From: Ben Date: Thu, 26 Mar 2020 22:13:43 +0000 Subject: [PATCH] New `card` handler module for dealing with CJ cards & starter decks --- houdini/data/ninja.py | 15 ++++++++++ houdini/handlers/play/card.py | 52 ++++++++++++++++++++++++++++++++++ houdini/handlers/play/ninja.py | 14 +-------- 3 files changed, 68 insertions(+), 13 deletions(-) create mode 100644 houdini/handlers/play/card.py diff --git a/houdini/data/ninja.py b/houdini/data/ninja.py index 5925b82..bfc43db 100644 --- a/houdini/data/ninja.py +++ b/houdini/data/ninja.py @@ -40,6 +40,21 @@ class CardCollection(AbstractDataCollection): __indexby__ = 'id' __filterby__ = 'id' + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + self.starter_decks = {} + + def set_starter_decks(self, starter_deck_cards): + for card in starter_deck_cards: + starter_deck = self.starter_decks.get(card.item_id, []) + starter_deck.append((self.get(card.card_id), card.quantity)) + self.starter_decks[card.item_id] = starter_deck + + @property + def power_cards(self): + return [card for card in self.values() if card.power_id > 0] + class PenguinCardCollection(AbstractDataCollection): __model__ = PenguinCard diff --git a/houdini/handlers/play/card.py b/houdini/handlers/play/card.py new file mode 100644 index 0000000..23d1f7b --- /dev/null +++ b/houdini/handlers/play/card.py @@ -0,0 +1,52 @@ +import random + +from houdini import handlers +from houdini.data.ninja import CardCollection, CardStarterDeck, PenguinCardCollection +from houdini.handlers import Priority, XMLPacket, XTPacket + + +@handlers.boot +async def cards_load(server): + server.cards = await CardCollection.get_collection() + server.logger.info(f'Loaded {len(server.cards)} ninja cards') + + starter_deck_cards = await CardStarterDeck.query.gino.all() + server.cards.set_starter_decks(starter_deck_cards) + server.logger.info(f'Loaded {len(server.cards.starter_decks)} starter decks') + + +@handlers.handler(XMLPacket('login'), priority=Priority.Low) +@handlers.allow_once +async def load_card_inventory(p): + p.cards = await PenguinCardCollection.get_collection(p.id) + + +@handlers.handler(XTPacket('i', 'ai')) +async def handle_buy_starter_deck(p, deck_id: int): + if deck_id in p.server.cards.starter_decks: + starter_deck = p.server.cards.starter_decks[deck_id] + power_cards = [card for card, qty in starter_deck if card.power_id > 0] + for card, qty in starter_deck: + if card.power_id == 0: + await p.add_card(card, quantity=qty) + power_card = random.choice(power_cards) + await p.add_card(power_card, quantity=1) + + +@handlers.handler(XTPacket('cd', 'gcd')) +async def handle_get_card_data(p): + await p.send_xt('gcd', '|'.join(f'{card.card_id},{card.quantity},{card.member_quantity}' + for card in p.cards.values())) + + +@handlers.handler(XTPacket('cd', 'bpc')) +async def handle_buy_power_cards(p): + if p.coins >= 1500: + power_cards = random.sample(p.server.cards.power_cards, 3) + for card in power_cards: + await p.add_card(card, member_quantity=1) + + await p.update(coins=p.coins - 1500).apply() + await p.send_xt('bpc', ','.join([str(card.id) for card in power_cards]), p.coins) + else: + await p.send_xt('bpc', 401) diff --git a/houdini/handlers/play/ninja.py b/houdini/handlers/play/ninja.py index 162c6dc..bfddd68 100644 --- a/houdini/handlers/play/ninja.py +++ b/houdini/handlers/play/ninja.py @@ -2,18 +2,6 @@ from houdini import handlers from houdini.handlers import XTPacket -@handlers.boot -async def cards_load(server): - server.cards = await CardCollection.get_collection() - server.logger.info(f'Loaded {len(server.cards)} ninja cards') - - -@handlers.handler(XMLPacket('login'), priority=Priority.Low) -@handlers.allow_once -async def load_ninja_inventory(p): - p.cards = await PenguinCardCollection.get_collection(p.id) - - @handlers.handler(XTPacket('ni', 'gnr')) async def handle_get_ninja_ranks(p): await p.send_xt('gnr', p.id, p.ninja_rank, p.fire_ninja_rank, p.water_ninja_rank, 0) @@ -39,7 +27,7 @@ async def handle_get_snow_level(p): await p.send_xt('gsl', 0, 0) -@handlers.handler(XTPacket('cd', 'gcd')) +@handlers.handler(XTPacket('ni', 'gcd')) async def handle_get_card_data(p): await p.send_xt('gcd', '|'.join(f'{card.card_id},{card.quantity},{card.member_quantity}' for card in p.cards.values()))