-
Notifications
You must be signed in to change notification settings - Fork 1
/
gameprompt.py
234 lines (203 loc) · 7.28 KB
/
gameprompt.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import game
import draw
import hud
import constants as C
import WConio as W
try:
from winsound import MessageBeep
except:
def MessageBeep(*args):
pass
import string
import logging
import terminal
log = logging.getLogger('gameprompt')
commands = []
old_matches = ''
def format_time(t, tv=False):
if tv:
tv = '+'
else:
tv = ''
if t < 60:
return '%im %s' % (t, tv)
if t < 3600:
return '%ih %im %s' % (t / 60, t % 60, tv)
if t < 86400:
return '%id %ih %im %s' % (t / 3600, t / 60 % 60, t % 60, tv)
def display_suggestion(msg):
global old_matches
matches = []
if not msg or not msg.strip():
if old_matches != matches:
W.gotoxy(0, C.height - 2)
W.clreol()
old_matches = matches
return
msg = msg.split()[0]
for command in commands:
if command.command.startswith(msg) and not command.hide:
matches.append(command)
if matches:
#limit our match count
matches = matches[:5]
if old_matches != matches:
old_matches = matches
W.gotoxy(0, C.height - 2)
W.clreol()
targetwidth = C.width / len(matches)
W.textcolor(W.DARKGREY)
for command in matches:
W.cputs(('%s (%s)' % (command.command, format_time(command.time, command.time_variable))).center(targetwidth))
return matches
def get_input():
buf = []
while True:
msg = ''.join(buf)
matches = display_suggestion(msg)
W.gotoxy(0, C.height - 1)
W.clreol()
W.textcolor(W.GREEN)
W.cputs(game.state.current_node.command_prompt + '$ '),
W.textcolor(W.LIGHTGREEN)
W.cputs(msg)
if matches and len(matches) == 1:
#show the argument help text
match = matches[0]
x = W.wherex()
W.textcolor(W.DARKGREY)
splitted = match.arguments.split()
splitted = splitted[max(0, len(msg.split())-2):]
W.cputs(' ' + ' '.join(splitted))
W.textcolor(W.LIGHTGREEN)
W.gotoxy(x, C.height - 1)
else:
match = None
#Read input
W.setcursortype(1)
(chn, chs) = W.getch()
W.setcursortype(0)
#figure out if we're done
if chs == '\r':
#enter, exit
break
if chn == 8:
#backspace
if len(buf):
buf.pop()
else:
MessageBeep()
continue
if chn == 3:
log.debug('took a ctrl-c')
game.fire('specialkey', 'ctrlc')
break
if chn == 0 or chn == 224:
#special keys come in two parts
(chn2, _) = W.getch()
if chn2 in W.__keydict:
game.fire('specialkey', W.__keydict[chn2])
continue
if len(buf) >= C.width:
#way too long now
break
if chs not in string.printable:
#dont care
continue
buf.append(chs)
return buf, match
def confirm_exit():
return show_confirmation(
"Are you sure you want to exit the game?"
)
def death_screen():
left = C.width / 4
width = C.width / 2
height = 12
top = C.height / 2 - height /2
time = game.state.time - (24*60)
survived = "%i days, %i hours, and %i minutes" % ((time / 60) / 24, (time / 60) % 60, time % 60)
death_box = draw.draw_box(left, top, width, height,
border_color=W.LIGHTRED, border_background_color=W.RED,
interior_background_color=W.RED)
death_box.text.extend([
draw.Text(W.WHITE, 1, 3, "UNHANDLED EXCEPTION:".center(width - 2)),
draw.Text(W.LIGHTRED, 1, 4, ("Killed by %s." % game.state.killer).center(width - 2)),
draw.Text(W.LIGHTRED, 1, 7, ("You lasted %s. " % survived).center(width - 2)),
draw.Text(W.WHITE, 1, 10, "Press <ENTER> to quit.".center(width - 2)),
])
draw.draw_box_text(death_box)
while True:
(chn, chs) = W.getch()
#figure out if we're done
if chs == '\r':
#enter, exit
raise game.GameShutdown()
def show_confirmation(line1=' ', line2=' '):
line1 = terminal.Line.parse(line1)[0]
line2 = terminal.Line.parse(line2)[0]
width = max(14, max(line1.width, line2.width) + 2)
height = 8
left = (C.width - width) / 2
top = (C.height - height) / 2
previous_buf = W.gettext(left, top, left + width, top + height)
btn_width = 7
btn_height = 4
btn_offset = 3
try:
container = draw.draw_box(left, top, width, height,
border_color=W.LIGHTBLUE, border_background_color = W.BLUE)
W.puttext(left+1, top + 1, left + line1.width, top + 1, line1.buf)
W.puttext(left+1, top + 2, left + line2.width, top + 2, line2.buf)
yes_button = draw.draw_box(left + btn_offset, top + height - btn_height, btn_width, btn_height,
border_color=W.LIGHTBLUE, border_background_color = W.BLUE,
corners = {'bl': 'teeup', 'br': 'teeup'})
no_button = draw.draw_box(left + width - btn_offset - btn_width, top + height - btn_height, btn_width, btn_height,
border_color=W.LIGHTBLUE, border_background_color = W.BLUE,
corners = {'bl': 'teeup', 'br': 'teeup'})
yes_text = draw.Text(W.DARKGREY, 1, 1, "YES".center(btn_width -2 ))
no_text = draw.Text(W.WHITE, 1, 1, "NO".center(btn_width -2 ))
yes_button.text.append(yes_text)
no_button.text.append(no_text)
selected_button = no_button
while True:
if selected_button == yes_button:
yes_text.color = W.WHITE
no_text.color = W.DARKGREY
else:
no_text.color = W.WHITE
yes_text.color = W.DARKGREY
draw.draw_box_text(yes_button)
draw.draw_box_text(no_button)
(chn, chs) = W.getch()
#figure out if we're done
if chs == '\r':
#enter, exit
break
if chs == 'y':
selected_button = yes_button
break
if chs == 'n':
selected_button = no_button
break
if chn == 0 or chn == 224:
#special keys come in two parts
(chn2, _) = W.getch()
if chn2 in W.__keydict:
name = W.__keydict[chn2]
if 'left' in name or 'right' in name:
selected_button = (selected_button == yes_button and no_button or yes_button)
return (selected_button == yes_button and True or False)
finally:
#Restore what we scribbled on
W.puttext(left, top, left + width, top + height, previous_buf)
@game.on('clear')
def prompt_hbar():
W.textcolor(W.WHITE)
W.gotoxy(0, C.height-3)
#print C.Characters.box_single.horiz * C.width
W.cputs(C.Characters.box_single.horiz * C.width)
@game.on('prompt')
def do_prompt():
command_line, match = get_input()
game.fire('command', command_line, match)