# Coverage for thb/thbunit.py : 88%

## 80 statements 70 run 10 missing 0 excluded 0 partial

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 -*-

2 from __future__ import annotations

3

4# -- stdlib --

5 from typing import Any, Dict, List, Type

6 import logging

7

8# -- third party --

9# -- own --

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

11 from thb.common import roll

12 from thb.item import GameItem

13 from thb.mode import THBattle

14 from utils.misc import BatchList

15 from thb.mode import THBEventHandler

16

17

18# -- code --

19 log = logging.getLogger('THBattleUnit')

20

21

22 class 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

47 class 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

56 class 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

65 class 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

74 class 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

83 class 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

94 class 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

103 class 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

116 class 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

125 def 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 ]})