Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# -*- coding: utf-8 -*- 

2from __future__ import annotations 

3 

4# -- stdlib -- 

5from typing import Dict, List, TYPE_CHECKING, Type 

6 

7# -- third party -- 

8# -- own -- 

9from game.base import Action, EventDispatcher, EventHandler, Game, Player 

10from utils.misc import BatchList 

11 

12# -- typing -- 

13if TYPE_CHECKING: 

14 from thb.cards.base import Card, Deck # noqa: F401 

15 from thb.characters.base import Character # noqa: F401 

16 from thb.common import PlayerRole # noqa: F401 

17 

18 

19# -- code -- 

20class THBEventDispatcher(EventDispatcher): 

21 game: 'THBattle' 

22 

23 def populate_handlers(self) -> List[EventHandler]: 

24 from thb.actions import COMMON_EVENT_HANDLERS 

25 g = self.game 

26 ehclasses = list(COMMON_EVENT_HANDLERS) + list(g.game_ehs) 

27 for c in getattr(g, 'players', ()): 

28 ehclasses.extend(c.eventhandlers) 

29 

30 return EventHandler.make_list(g, ehclasses) 

31 

32 

33class THBEventHandler(EventHandler): 

34 game: THBattle 

35 

36 

37class THBAction(Action): 

38 source: Character 

39 target: Character 

40 game: THBattle 

41 associated_card: Card 

42 

43 def __init__(self, source: Character, target: Character): 

44 self.source = source 

45 self.target = target 

46 

47 

48class THBattle(Game[THBAction, THBEventHandler]): 

49 game: THBattle 

50 game_ehs: List[Type[THBEventHandler]] 

51 deck: Deck 

52 players: BatchList[Character] 

53 roles: Dict[Player, PlayerRole] 

54 dispatcher: THBEventDispatcher 

55 

56 dispatcher_cls = THBEventDispatcher 

57 

58 def find_character(g, p: Player) -> Character: 

59 for ch in g.players: 

60 if ch.player is p: 

61 return ch 

62 

63 raise IndexError('Could not find character!')