Coverage for thb/thbunit.py : 88%
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
4# -- stdlib --
5from typing import Any, Dict, List
6import logging
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
17# -- code --
18log = logging.getLogger('THBattleUnit')
21class THBattleUTBootstrap(BootstrapAction):
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
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)
45class THBattleDummy1(THBattle):
46 n_persons = 1
47 game_ehs = []
48 bootstrap = THBattleUTBootstrap
50 def can_leave(g: THBattle, p: Player) -> bool:
51 return False
54class THBattleDummy2(THBattle):
55 n_persons = 2
56 game_ehs = []
57 bootstrap = THBattleUTBootstrap
59 def can_leave(g: THBattle, p: Player) -> bool:
60 return False
63class THBattleDummy3(THBattle):
64 n_persons = 3
65 game_ehs = []
66 bootstrap = THBattleUTBootstrap
68 def can_leave(g: THBattle, p: Player) -> bool:
69 return False
72class THBattleDummy4(THBattle):
73 n_persons = 4
74 game_ehs = []
75 bootstrap = THBattleUTBootstrap
77 def can_leave(g: THBattle, p: Player) -> bool:
78 return False
81class THBattleUTCrashBootstrap(BootstrapAction):
83 def __init__(self, params: Dict[str, Any],
84 items: Dict[Player, List[GameItem]],
85 players: BatchList[Player]):
86 pass
88 def apply_action(self):
89 raise Exception("Deliberate crash")
92class THBattleCrash(THBattle):
93 n_persons = 1
94 game_ehs = []
95 bootstrap = THBattleUTCrashBootstrap
97 def can_leave(g: THBattle, p: Player) -> bool:
98 return False
101class THBattleUTHaltBootstrap(BootstrapAction):
103 def __init__(self, params: Dict[str, Any],
104 items: Dict[Player, List[GameItem]],
105 players: BatchList[Player]):
106 pass
108 def apply_action(self):
109 g = self.game
110 g.core.runner.sleep(10000000)
111 return True
114class THBattleHalt(THBattle):
115 n_persons = 1
116 game_ehs = []
117 bootstrap = THBattleUTHaltBootstrap
119 def can_leave(g: THBattle, p: Player) -> bool:
120 return False
123def 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 ]})