-
Notifications
You must be signed in to change notification settings - Fork 2
/
Flee.py
executable file
·208 lines (168 loc) · 6.76 KB
/
Flee.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/env python3
from __future__ import annotations
import logging
from typing import Any, Tuple
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import GObject, Gtk
from twisted.internet import gtk3reactor
try:
gtk3reactor.install() # type: ignore
except AssertionError:
pass
from twisted.internet import defer, gtk3reactor, reactor
from twisted.python import log
from slugathon.game import Legion
from slugathon.gui import Chit, ConfirmDialog, Marker, icon
__copyright__ = "Copyright (c) 2006-2021 David Ripton"
__license__ = "GNU GPL v2"
DO_NOT_FLEE = 0
FLEE = 1
def new(
playername: str,
attacker_legion: Legion.Legion,
defender_legion: Legion.Legion,
parent: Gtk.Window,
) -> Tuple[Flee, defer.Deferred]:
"""Create a Flee dialog and return it and a Deferred."""
def1 = defer.Deferred() # type: defer.Deferred
flee = Flee(playername, attacker_legion, defender_legion, def1, parent)
return flee, def1
class Flee(Gtk.Dialog):
"""Dialog to choose whether to flee."""
def __init__(
self,
playername: str,
attacker_legion: Legion.Legion,
defender_legion: Legion.Legion,
def1: defer.Deferred,
parent: Gtk.Window,
):
GObject.GObject.__init__(
self, title=f"Flee - {playername}", parent=parent
)
self.attacker_legion = attacker_legion
self.defender_legion = defender_legion
self.deferred = def1
self.set_icon(icon.pixbuf)
self.set_transient_for(parent)
self.set_destroy_with_parent(True)
self.vbox.set_spacing(9)
hexlabel = defender_legion.hexlabel
masterhex = defender_legion.player.game.board.hexes[hexlabel]
self.legion_name = Gtk.Label(
label=f"Flee with legion {defender_legion.markerid} "
f"({defender_legion.picname}) in {masterhex.terrain} hex "
f"{hexlabel}?"
)
self.vbox.pack_start(self.legion_name, True, True, 0)
self.attacker_hbox = Gtk.HBox(homogeneous=False, spacing=15)
self.vbox.pack_start(self.attacker_hbox, True, True, 0)
self.attacker_marker = Marker.Marker(attacker_legion, True, scale=20)
self.attacker_hbox.pack_start(
self.attacker_marker.event_box, False, False, 0
)
self.attacker_score_label = Gtk.Label(
label=f"{attacker_legion.score}\npoints"
)
self.attacker_hbox.pack_start(
self.attacker_score_label, False, True, 0
)
self.attacker_chits_hbox = Gtk.HBox(homogeneous=False, spacing=3)
self.attacker_hbox.pack_start(self.attacker_chits_hbox, True, True, 0)
assert attacker_legion is not None
assert attacker_legion.player is not None
assert attacker_legion.player.color is not None
for creature in attacker_legion.sorted_creatures:
chit = Chit.Chit(creature, attacker_legion.player.color, scale=20)
chit.show()
self.attacker_chits_hbox.pack_start(chit.event_box, False, True, 0)
self.defender_hbox = Gtk.HBox(homogeneous=False, spacing=15)
self.vbox.pack_start(self.defender_hbox, True, True, 0)
self.defender_marker = Marker.Marker(defender_legion, True, scale=20)
self.defender_hbox.pack_start(
self.defender_marker.event_box, False, False, 0
)
self.defender_score_label = Gtk.Label(
label=f"{defender_legion.score}\npoints"
)
self.defender_hbox.pack_start(
self.defender_score_label, False, True, 0
)
assert defender_legion.player is not None
assert defender_legion.player.color is not None
self.defender_chits_hbox = Gtk.HBox(homogeneous=False, spacing=3)
self.defender_hbox.pack_start(self.defender_chits_hbox, True, True, 0)
for creature in defender_legion.sorted_creatures:
chit = Chit.Chit(creature, defender_legion.player.color, scale=20)
chit.show()
self.defender_chits_hbox.pack_start(chit.event_box, False, True, 0)
self.add_button("Do Not Flee", DO_NOT_FLEE)
self.add_button("Flee", FLEE)
self.connect("response", self.cb_response)
self.show_all()
def cb_response(self, widget: Gtk.Widget, response_id: int) -> None:
"""Fire the deferred, with the attacker, the defender, and
a boolean which is True iff the user chose to flee."""
if response_id == FLEE and (
self.defender_legion.combat_value
>= self.attacker_legion.combat_value
or self.defender_legion.score >= self.attacker_legion.score
):
confirm_dialog, def1 = ConfirmDialog.new(
self,
"Confirm",
"Are you sure you want to flee with a superior legion?",
)
def1.addCallback(self.cb_response2)
def1.addErrback(self.failure)
return
self.destroy()
self.deferred.callback(
(self.attacker_legion, self.defender_legion, response_id)
)
def cb_response2(self, confirmed: bool) -> None:
"""Fire the deferred, with the attacker, the defender, and
a boolean which is True iff the user chose to flee."""
self.destroy()
self.deferred.callback(
(self.attacker_legion, self.defender_legion, confirmed)
)
def failure(self, arg: Any) -> None:
log.err(arg) # type: ignore
if __name__ == "__main__":
import time
from slugathon.game import Creature, Player, Game
now = time.time()
attacker_playername = "Roar!"
game = Game.Game("g1", attacker_playername, now, now, 2, 6)
attacker_player = Player.Player(attacker_playername, game, 0)
attacker_player.color = "Black"
attacker_creature_names = [
"Titan",
"Colossus",
"Serpent",
"Hydra",
"Archangel",
"Angel",
"Unicorn",
]
attacker_creatures = Creature.n2c(attacker_creature_names)
attacker_legion = Legion.Legion(
attacker_player, "Bk01", attacker_creatures, 1
)
defender_playername = "Eek!"
defender_player = Player.Player(defender_playername, game, 0)
defender_player.color = "Gold"
defender_creature_names = ["Ogre", "Centaur", "Gargoyle"]
defender_creatures = Creature.n2c(defender_creature_names)
defender_legion = Legion.Legion(
defender_player, "Rd01", defender_creatures, 1
)
def my_callback(tup: Tuple[Legion.Legion, Legion.Legion, bool]) -> None:
(attacker, defender, fled) = tup
logging.info(f"{fled=}")
reactor.stop() # type: ignore
_, def1 = new(defender_playername, attacker_legion, defender_legion, None)
def1.addCallback(my_callback)
reactor.run() # type: ignore[attr-defined]