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, Type
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
15from thb.mode import THBEventHandler
18# -- code --
19log = logging.getLogger('THBattleUnit')
22class THBattleUTBootstrap(BootstrapAction):
23 game: THBattle
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
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)
47class THBattleDummy1(THBattle):
48 n_persons = 1
49 game_ehs: List[Type[THBEventHandler]] = []
50 bootstrap = THBattleUTBootstrap
52 def can_leave(g: THBattle, p: Player) -> bool:
53 return False
56class THBattleDummy2(THBattle):
57 n_persons = 2
58 game_ehs: List[Type[THBEventHandler]] = []
59 bootstrap = THBattleUTBootstrap
61 def can_leave(g: THBattle, p: Player) -> bool:
62 return False
65class THBattleDummy3(THBattle):
66 n_persons = 3
67 game_ehs: List[Type[THBEventHandler]] = []
68 bootstrap = THBattleUTBootstrap
70 def can_leave(g: THBattle, p: Player) -> bool:
71 return False
74class THBattleDummy4(THBattle):
75 n_persons = 4
76 game_ehs: List[Type[THBEventHandler]] = []
77 bootstrap = THBattleUTBootstrap
79 def can_leave(g: THBattle, p: Player) -> bool:
80 return False
83class THBattleUTCrashBootstrap(BootstrapAction):
85 def __init__(self, params: Dict[str, Any],
86 items: Dict[Player, List[GameItem]],
87 players: BatchList[Player]):
88 pass
90 def apply_action(self):
91 raise Exception("Deliberate crash")
94class THBattleCrash(THBattle):
95 n_persons = 1
96 game_ehs: List[Type[THBEventHandler]] = []
97 bootstrap = THBattleUTCrashBootstrap
99 def can_leave(g: THBattle, p: Player) -> bool:
100 return False
103class THBattleUTHaltBootstrap(BootstrapAction):
105 def __init__(self, params: Dict[str, Any],
106 items: Dict[Player, List[GameItem]],
107 players: BatchList[Player]):
108 pass
110 def apply_action(self):
111 g = self.game
112 g.core.runner.sleep(10000000) # type: ignore
113 return True
116class THBattleHalt(THBattle):
117 n_persons = 1
118 game_ehs: List[Type[THBEventHandler]] = []
119 bootstrap = THBattleUTHaltBootstrap
121 def can_leave(g: THBattle, p: Player) -> bool:
122 return False
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 ]})