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, Sequence, 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 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) -> Sequence['THBEventHandler']: 

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 THBattle(Game): 

38 game: THBattle 

39 game_ehs: List[Type[THBEventHandler]] 

40 deck: Deck 

41 players: BatchList[Character] 

42 roles: Dict[Player, PlayerRole] 

43 dispatcher: THBEventDispatcher 

44 

45 dispatcher_cls = THBEventDispatcher 

46 

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

48 for ch in g.players: 48 ↛ 52line 48 didn't jump to line 52, because the loop on line 48 didn't complete

49 if ch.player is p: 

50 return ch 

51 

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

53 

54 

55class THBAction(Action): 

56 source: 'Character' 

57 target: 'Character' 

58 game: THBattle 

59 

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

61 self.source = source 

62 self.target = target 

63 

64 

65class THBPlayerAction(Action): 

66 source: Player 

67 target: Player 

68 game: THBattle 

69 

70 def __init__(self, source: Player, target: Player): 

71 self.source = source 

72 self.target = target