def chooseFromEvents(self, char: Character, events: list[Event] = None, state: State = None) -> Optional[Event]: if not events: events = self.events.values() possibleEvents: list[Event] = [] totalChance = 0 defaultEvent: Optional[Event] = None for event in events: if event.prepare(char, self.tributes, state): if event.getChance() == 0: defaultEvent = event continue totalChance += event.getChance() possibleEvents.append(event) print(f"Character: {char}") print(f" alive:\t{char.alive}") print(f" age:\t{char.age}") print(f" location:\t{char.location}") print(f" status:\t{char.status}") print(f" tags:") [print(f" {tag}") for tag in char.tags] print(f" inventory:\t{char.items}") print(f" alliance:\t{char.alliance}") print(f"Possible events: {possibleEvents}") print(f"Default event: {defaultEvent}\n") if not possibleEvents: if defaultEvent: return defaultEvent else: raise Exception("No events matched when choosing from events") choice = randint(0, totalChance - 1) count = 0 for event in possibleEvents: if choice >= count and choice < (count + event.getChance()): return event count = count + event.getChance() if not char.isAlive(): return None raise Exception( f"Invalid choice when choosing from events ({choice} out of {totalChance})" )
def check(self, char: Character, state: State) -> bool: if self.aState == AliveCheck.ALIVE: return char.isAlive() else: return not char.isAlive()