# Coverage for thb/thbunit.py : 88%

## 78 statements 68 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

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

16

17# -- code --

18 log = logging.getLogger('THBattleUnit')

19

20

21 class THBattleUTBootstrap(BootstrapAction):

22

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

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

25 players: BatchList[Player]):

26 self.source = self.target = None

27 self.params = params

28 self.items = items

29 self.players = players

30

31 def apply_action(self) -> bool:

32 pl = self.players

33 g = self.game

34 items = self.items

35 _ = roll(g, pl, items)

36 sync_primitive(1, pl)

37 sync_primitive(2, pl)

38 sync_primitive(3, pl)

39 sync_primitive(4, pl)

40 sync_primitive(5, pl)

41 sync_primitive(6, pl)

42 raise GameEnded(self.players)

43

44

45 class THBattleDummy1(THBattle):

46 n_persons = 1

47 game_ehs = []

48 bootstrap = THBattleUTBootstrap

49

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

51 return False

52

53

54 class THBattleDummy2(THBattle):

55 n_persons = 2

56 game_ehs = []

57 bootstrap = THBattleUTBootstrap

58

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

60 return False

61

62

63 class THBattleDummy3(THBattle):

64 n_persons = 3

65 game_ehs = []

66 bootstrap = THBattleUTBootstrap

67

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

69 return False

70

71

72 class THBattleDummy4(THBattle):

73 n_persons = 4

74 game_ehs = []

75 bootstrap = THBattleUTBootstrap

76

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

78 return False

79

80

81 class THBattleUTCrashBootstrap(BootstrapAction):

82

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

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

85 players: BatchList[Player]):

86 pass

87

88 def apply_action(self):

89 raise Exception("Deliberate crash")

90

91

92 class THBattleCrash(THBattle):

93 n_persons = 1

94 game_ehs = []

95 bootstrap = THBattleUTCrashBootstrap

96

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

98 return False

99

100

101 class THBattleUTHaltBootstrap(BootstrapAction):

102

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

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

105 players: BatchList[Player]):

106 pass

107

108 def apply_action(self):

109 g = self.game

110 g.core.runner.sleep(10000000)

111 return True

112

113

114 class THBattleHalt(THBattle):

115 n_persons = 1

116 game_ehs = []

117 bootstrap = THBattleUTHaltBootstrap

118

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

120 return False

121

122

123 def inject():

124 import thb

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

126 THBattleDummy1,

127 THBattleDummy2,

128 THBattleDummy3,

129 THBattleDummy4,

130 THBattleCrash,

131 THBattleHalt,

132 ]})