/
tester.py
149 lines (122 loc) · 4.5 KB
/
tester.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import arkham
import modules
import maps
import fun
class EndTest (Exception):
def __init__ (self, success):
self.m_success = success
def success (self):
return self.m_success
class TestUI (arkham.UI):
def __init__ (self, controller):
self.m_controller = controller
def setup_players (self, game):
self.m_controller.setup_players (game)
def select_action (self, game, investigator, actions):
print "SELECT_ACTION\n" + "\n".join (" " + action.name () for action in actions)
selector = self.m_controller.next ()
if not callable (selector):
self.m_controller.throw (AssertionError ("need callable selector"))
actions = [action for action in actions if selector (action)]
if len (actions) != 1:
self.m_controller.throw (AssertionError ("need one action, got %d"
% len (actions)))
print "=> " + actions[0].name ()
return actions[0]
class FixedSkills:
def __init__ (self, skills):
self.m_skills = dict (skills)
def skill (self, skill):
return self.m_skills[skill]
class TestInvestigator (arkham.CommonInvestigator):
def __init__ (self, name, home, skills={},
sanity=1, stamina=1,
money=0, clues=0):
arkham.CommonInvestigator.__init__ \
(self, name,
{arkham.health_sanity: sanity,
arkham.health_stamina: stamina},
money, clues,
FixedSkills (skills), home)
def initial_movement_points (self):
return 0
class ModuleProto (arkham.ModuleProto):
def __init__ (self, constructor, request):
arkham.ModuleProto.__init__ (self, "test", "Test Board")
self.m_constructor = constructor
self.m_locations = {}
self.m_request = list (request)
def consistent (self, mod_index):
for req in self.m_request:
if not mod_index.request (req):
return False
return True
def construct (self, game):
self.m_constructor.construct (game, self)
def add_locations (self, *names):
ret = []
for name in names:
if isinstance (name, tuple):
name, attributes = name
else:
attributes = {}
loc = maps.location ("Loc" + name, **attributes)
self.m_locations[name] = loc
ret.append (loc)
return ret
def add_investigator (self, game, name, location, skills):
ret = TestInvestigator ("Inv" + name,
self.m_locations[location], skills)
game.add_investigator (ret)
return ret
def add_monster (self, game, location, proto):
mon = arkham.Monster (proto)
game.add_monster (mon, self.m_locations[location])
return [mon]
def match_investigator (name):
return lambda arg: arg.name () == "Inv" + name
def match_combat (klass):
return lambda arg: isinstance (arg.game, klass)
class TestGame (arkham.Game):
def __init__ (self, controller, request=[]):
idx = arkham.ModuleIndex ()
modules.discover_modules (idx)
controller.add_modules (idx)
idx.add_module (ModuleProto (controller, request))
idx.request ("test")
arkham.Game.__init__ (self, idx.requested_modules (),
TestUI (controller))
self.m_controller = controller
@self.normal_one_dice_roll_hook.match \
(fun.matchclass (TestGame), fun.any, fun.any, fun.any)
def do (game, investigator, subject, skill_name):
return game.roll ()
def roll (self):
ret = self.m_controller.next ()
assert isinstance (ret, int), ret
assert ret > 0
return ret
class Controller:
def __init__ (self):
self.m_generator = self.actions ()
def next (self):
return self.m_generator.next ()
def throw (self, obj):
return self.m_generator.throw (obj)
def use_investigators (self, game, investigators):
ret = []
for investigator in game.all_investigators ():
if investigator.name () in investigators:
game.use_investigator (investigator)
ret.append (investigator)
return ret
def construct (self, game, module):
pass
def add_modules (self, idx):
pass
def run_test (game):
try:
game.run ()
except EndTest, e:
if not e.success ():
print "FAILURE"