-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
252 lines (211 loc) · 6.45 KB
/
app.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/usr/bin/python3
"""
This is a classic console interface for textelite.
You may enter game commands in the command line.
"""
from cmd import Cmd
from telite import TradingGame
__VERSION__ = '1.0'
class TradingGameCmd(Cmd):
"""Command interface to a TradingGame"""
prompt = "> "
def __init__(self, debug=False):
self.debug = debug
Cmd.__init__(self)
self.game = TradingGame()
self.set_prompt()
def set_prompt(self):
self.prompt = 'Cash : %0.2f>' % self.game.ship.cash
@staticmethod
def do_intro(line):
print(
'''
Welcome to pyElite {version}
Available commands:
/// CONTROLS ///
info (or i) [planet name] - planet information
buy (or b) [trade good] [amount] - buy from a local store
sell (or s) [trade good] [amount] - sell to a local store
fuel (or f) [amount] - buy amount LY of fuel
jump (or j) [planet name] - hyperjump
use (or u) [equipment name] - use installed equipment
dump [trade good] [amount] - dump cargo into space
upgrade [upgrade name] - buy and install upgrade
local (or l) - list of planets within the ship hyperjump range
mkt (or m) - show local market
cargo (or c) - show cargo bay
com - commander status
galhyp - jump to the next galaxy
/// OTHER /////
intro (or h) - display this text
quit (or q) - quit the game
run [filepath] - run a script (one command per line)
'''.format(version=__VERSION__)
)
@staticmethod
def do_quit(line):
print("Goodbye, commander!")
return True
@staticmethod
def do_q(line):
return TradingGameCmd.do_quit(line)
@staticmethod
def do_EOF(line):
return True
def do_jump(self, planetname):
status, msg = self.game.jump(planetname.strip())
print(msg)
self.do_info('')
return
def do_j(self, planetname):
return self.do_jump(planetname)
def do_local(self, line):
print("Galaxy number %d" % self.game.galaxy.galaxy_number)
status, (head, choices) = self.game.info_local_systems()
print(head.upper())
for __, (name, desc) in choices:
print(''.join(desc))
return
def do_l(self, line):
return self.do_local(line)
def do_galhyp(self, line):
status, ret = self.game.hyperjump()
print(ret)
return
def do_mkt(self, line):
status, (head, data) = self.game.info_trade()
print(head.upper())
for g in data:
print(g)
print("Fuel : {f} Holdspace : {s}".format(
f=self.game.ship.fuel / 10.0, s=self.game.ship.hold_remaining))
return
def do_m(self, line):
return self.do_mkt(line)
def do_sell(self, line):
v = self._check_good(line)
if not v:
return
name, amt = v
status, msg = self.game.sell(name, amt)
print(msg)
self.set_prompt()
return
def do_s(self, line):
return self.do_sell(line)
def do_buy(self, line):
v = self._check_good(line)
if not v:
return
name, amt = v
status, msg = self.game.buy(name, amt)
print(msg)
self.set_prompt()
return
def do_b(self, line):
return self.do_buy(line)
def do_fuel(self, line):
status, msg = self.game.buy_fuel()
print(msg)
self.set_prompt()
return
def do_f(self, line):
return self.do_fuel(line)
def do_info(self, systemname):
status, msg = self.game.info_selected_system(systemname)
if status:
(head, info) = msg
print(head.upper())
print('\n'.join(info))
else:
print(msg)
return
def do_i(self, name):
return self.do_info(name)
def do_com(self, line):
status, msg = self.game.info_commander()
if status:
head, info = msg
print(head.upper())
for __, desc in info:
if __:
name, desc = desc
print(''.join(desc))
return
def do_cargo(self, line):
status, msg = self.game.info_cargo()
if status:
head, info = msg
print(head.upper())
for __, desc in info:
if __:
name, desc = desc
print(''.join(desc))
return
def do_c(self, line):
return self.do_cargo(line)
def do_dump(self, line):
v = self._check_good(line)
if not v:
return
name, amt = v
status, msg = self.game.dump(name, amt)
print(msg)
return
def do_upgrade(self, line):
status, msg = self.game.install_upgrade(line)
print(msg)
return
def do_use(self, line):
status, msg = self.game.use_equipment(line)
print(msg)
return
def do_u(self, line):
return self.do_use(line)
def do_run(self, fname):
import os
if os.path.isfile(fname):
lines = open(fname, 'r').read().split('\n')
for line in lines:
print(line)
self.onecmd(line)
print("Cash > ", self.game.ship.cash)
else:
print("Could not open file")
return
@staticmethod
def do_h(line):
return TradingGameCmd.do_intro(line)
@staticmethod
def _check_value(value):
try:
value = float(value)
except ValueError:
print("Value must be a number")
return None
if value <= 0:
print("Nice try pilot.")
return None
else:
return value
def _check_good(self, line):
"""
:param str line:
:return iterable:
"""
line = line.strip().lower()
try:
good, number = line.split(' ')
except ValueError:
good = line
amt = 9999
else:
amt = self._check_value(number)
if not amt:
return False
amt = int(amt)
return good, amt
if __name__ == "__main__":
tg = TradingGameCmd()
tg.do_intro('')
tg.cmdloop()