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 Any, Dict, List, Type 

6import logging 

7 

8# -- third party -- 

9# -- own -- 

10from game.base import BootstrapAction, GameEnded, Player, sync_primitive 

11from thb.common import roll 

12from thb.item import GameItem 

13from thb.mode import THBattle 

14from utils.misc import BatchList 

15from thb.mode import THBEventHandler 

16 

17 

18# -- code -- 

19log = logging.getLogger('THBattleUnit') 

20 

21 

22class THBattleUTBootstrap(BootstrapAction): 

23 game: THBattle 

24 

25 def __init__(self, params: Dict[str, Any], 

26 items: Dict[Player, List[GameItem]], 

27 players: BatchList[Player]): 

28 self.source = self.target = None 

29 self.params = params 

30 self.items = items 

31 self.players = players 

32 

33 def apply_action(self) -> bool: 

34 pl = self.players 

35 g = self.game 

36 items = self.items 

37 _ = roll(g, pl, items) 

38 sync_primitive(1, pl) 

39 sync_primitive(2, pl) 

40 sync_primitive(3, pl) 

41 sync_primitive(4, pl) 

42 sync_primitive(5, pl) 

43 sync_primitive(6, pl) 

44 raise GameEnded(self.players) 

45 

46 

47class THBattleDummy1(THBattle): 

48 n_persons = 1 

49 game_ehs: List[Type[THBEventHandler]] = [] 

50 bootstrap = THBattleUTBootstrap 

51 

52 def can_leave(g: THBattle, p: Player) -> bool: 

53 return False 

54 

55 

56class THBattleDummy2(THBattle): 

57 n_persons = 2 

58 game_ehs: List[Type[THBEventHandler]] = [] 

59 bootstrap = THBattleUTBootstrap 

60 

61 def can_leave(g: THBattle, p: Player) -> bool: 

62 return False 

63 

64 

65class THBattleDummy3(THBattle): 

66 n_persons = 3 

67 game_ehs: List[Type[THBEventHandler]] = [] 

68 bootstrap = THBattleUTBootstrap 

69 

70 def can_leave(g: THBattle, p: Player) -> bool: 

71 return False 

72 

73 

74class THBattleDummy4(THBattle): 

75 n_persons = 4 

76 game_ehs: List[Type[THBEventHandler]] = [] 

77 bootstrap = THBattleUTBootstrap 

78 

79 def can_leave(g: THBattle, p: Player) -> bool: 

80 return False 

81 

82 

83class THBattleUTCrashBootstrap(BootstrapAction): 

84 

85 def __init__(self, params: Dict[str, Any], 

86 items: Dict[Player, List[GameItem]], 

87 players: BatchList[Player]): 

88 pass 

89 

90 def apply_action(self): 

91 raise Exception("Deliberate crash") 

92 

93 

94class THBattleCrash(THBattle): 

95 n_persons = 1 

96 game_ehs: List[Type[THBEventHandler]] = [] 

97 bootstrap = THBattleUTCrashBootstrap 

98 

99 def can_leave(g: THBattle, p: Player) -> bool: 

100 return False 

101 

102 

103class THBattleUTHaltBootstrap(BootstrapAction): 

104 

105 def __init__(self, params: Dict[str, Any], 

106 items: Dict[Player, List[GameItem]], 

107 players: BatchList[Player]): 

108 pass 

109 

110 def apply_action(self): 

111 g = self.game 

112 g.core.runner.sleep(10000000) # type: ignore 

113 return True 

114 

115 

116class THBattleHalt(THBattle): 

117 n_persons = 1 

118 game_ehs: List[Type[THBEventHandler]] = [] 

119 bootstrap = THBattleUTHaltBootstrap 

120 

121 def can_leave(g: THBattle, p: Player) -> bool: 

122 return False 

123 

124 

125def inject(): 

126 import thb 

127 thb.modes.update({cls.__name__: cls for cls in [ 

128 THBattleDummy1, 

129 THBattleDummy2, 

130 THBattleDummy3, 

131 THBattleDummy4, 

132 THBattleCrash, 

133 THBattleHalt, 

134 ]})