From 7f27efd124d309c0276c5fe63ae5ae5992b6e78a Mon Sep 17 00:00:00 2001 From: vesper-arch Date: Sun, 11 Aug 2024 11:52:04 -0400 Subject: [PATCH] Again. --- message_bus.py | 54 ------------------------------- message_bus_tools.py | 75 -------------------------------------------- 2 files changed, 129 deletions(-) delete mode 100644 message_bus.py delete mode 100644 message_bus_tools.py diff --git a/message_bus.py b/message_bus.py deleted file mode 100644 index f393ec4d..00000000 --- a/message_bus.py +++ /dev/null @@ -1,54 +0,0 @@ -from enum import StrEnum - -from ansi_tags import ansiprint - - -class Message(StrEnum): - # These are just some basic events to start off with. I will add more as I go along. - START_OF_COMBAT = 'start_of_combat' - END_OF_COMBAT = 'end_of_combat' - START_OF_TURN = 'start_of_turn' - END_OF_TURN = 'end_of_turn' - -class MessageBus: - ''' - This is a Pub/Sub, or Publish/Subscribe, message bus. It allows components to subscribe to messages, - registering a callback function that will be called when that message is published. - ''' - def __init__(self, debug=True): - self.subscribers = dict(dict()) # noqa: C408 - self.debug = debug - - def subscribe(self, event_type: Message, callback, uid): - if event_type not in self.subscribers: - self.subscribers[event_type] = {} - self.subscribers[event_type][uid] = callback - if self.debug: - ansiprint(f"MESSAGEBUS: {event_type} | Subscribed {callback.__qualname__}") - - def unsubscribe(self, event_type, uid): - if self.debug: - ansiprint(f"MESSAGEBUS: Unsubscribed {self.subscribers[event_type][uid].__qualname__} from {', '.join(event_type).rstrip(', ')}") - del self.subscribers[event_type][uid] - - def publish(self, event_type: Message, data): - if event_type in self.subscribers: - for _uid, callback in self.subscribers[event_type].items(): - if self.debug: - ansiprint(f"MESSAGEBUS: {event_type} | Calling {callback.__qualname__}") - callback(event_type, data) - return data - -class Registerable(): - '''Convenience class so I don't have to repeat the register function in every class that needs it.''' - registers = [] - def register(self): - for message in self.registers: - bus.subscribe(message, self.callback, self.uid) - - def unregister(self, event_types: list[Message]=None): - event_types = event_types if event_types else [] - for message in self.registers: - bus.unsubscribe(message, self.uid) - -bus = MessageBus(debug=True) diff --git a/message_bus_tools.py b/message_bus_tools.py deleted file mode 100644 index 8730d318..00000000 --- a/message_bus_tools.py +++ /dev/null @@ -1,75 +0,0 @@ -from enum import StrEnum -from ansi_tags import ansiprint - -class Message(StrEnum): - '''Messages that can be sent to the message bus.''' - START_OF_COMBAT = 'start_of_combat' - END_OF_COMBAT = 'end_of_combat' - START_OF_TURN = 'start_of_turn' - END_OF_TURN = 'end_of_turn' - BEFORE_ATTACK = 'before_attack' - AFTER_ATTACK = 'after_attack' - ON_PICKUP = 'on_pickup' # For relics - -class MessageBus(): - '''This is a Pub/Sub, or Publish/Subscribe, message bus. It allows components to subscribe to messages, - registering a callback function that will be called when that message is published. - ''' - def __init__(self, debug=True): - self.subscribers = dict(dict()) - self.debug = debug - - def subscribe(self, event_type: Message, callback, uid): - if event_type not in self.subscribers: - self.subscribers[event_type] = dict() - self.subscribers[event_type][uid] = callback - if self.debug: - ansiprint(f"MESSAGEBUS: {event_type} | Subscribed {callback.__qualname__}") - - def publish(self, event_type: Message, data): - if event_type in self.subscribers: - for uid, callback in self.subscribers[event_type].items(): - if self.debug: - ansiprint(f"MESSAGEBUS: {event_type} | Calling {callback.__qualname__}") - callback(event_type, data) - return data - -class Registerable(): - registers = [] - def register(self, bus): - for message in self.registers: - bus.subscribe(message, self.callback, self.uid) - -class Effect(Registerable): - def __init__(self, stack_type, info, amount=0): - self.name = 'default' - self.stack_type = stack_type - self.info = info - self.amount = amount - - def pretty_print(self): - stack_type_colors = {'duration': 'light-blue', 'intensity': 'orange', 'counter': 'magenta', 'no stack': 'white'} - return f"<{stack_type_colors[self.type]}>{self.name}{f' {self.amount}' if self.type != 'no stack' else ''}" - -class Relic(Registerable): - def __init__(self, name, info, flavor_text, rarity, player_class='Any'): - self.name = name - self.info = info - self.flavor_text = flavor_text - self.rarity = rarity - self.player_class = player_class - - def pretty_print(self): - return f"<{self.rarity}>{self.name} | {self.info} | {self.flavor_text}" - -class Card(Registerable): - def __init__(self, name, info, rarity, player_class, card_type, energy_cost=-1): - self.name = name - self.info = info - self.rarity = rarity - self.player_class = player_class - self.type = card_type - self.energy_cost = energy_cost - - def pretty_print(self): - return f"<{self.rarity}>{self.name}{f' | {self.energy_cost} Energy' if self.energy > -1 else ''} | {self.info}"