-
Notifications
You must be signed in to change notification settings - Fork 0
/
dumb_frotz.py
69 lines (62 loc) · 1.81 KB
/
dumb_frotz.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
import os
import subprocess
DFROTZ = '/usr/local/bin/dfrotz'
def format_output(output, load, command, save):
"""
Yeah, it's ugly.
Big whoop.
Wanna fight about it?
"""
lines = filter(lambda l: l and l != '>', output.split('\n'))
from_line = 5
to_line = 0
if command or load:
from_line += 4
if load:
from_line += 2
if save:
to_line = -1
lines = lines[from_line:]
if to_line:
lines = lines[:to_line]
try:
where_line = lines.pop(0).replace('>', '').strip()
except IndexError:
# TODO: there's an off-by-1 here somewhere, investigate
lines = filter(lambda l: l and l != '>', output.split('\n'))
lines = lines[:to_line]
return lines[-1].replace('>', '').strip()
where_parts = where_line.split(' ')
if command and not len(where_parts) > 1:
# no where line
lines.insert(0, where_parts[0])
return ' '.join(lines)
else:
return '[%s] %s' % (where_parts[0], ' '.join(lines))
def execute(game_path, command=None, save_path=None):
args = [DFROTZ, game_path]
proc = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
load = save_path and os.path.exists(save_path)
save = save_path is not None
lines = []
if load:
# load previously saved game
lines.extend([
'restore',
save_path,
])
if command:
lines.append(command)
if save:
lines.extend([
'save',
save_path,
])
if load:
# confirm overwrite
lines.append('y')
if lines:
output, _ = proc.communicate(input='\n'.join(lines) + '\n')
else:
output, _ = proc.communicate()
return format_output(output, load, command is not None, save)