-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmud.py
More file actions
144 lines (122 loc) · 3.18 KB
/
mud.py
File metadata and controls
144 lines (122 loc) · 3.18 KB
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
import sys
import json
from MyEvent import MyEvent
from Ship import Ship
from Action import Action
from termcolor import colored, cprint
# globals
HELP_FILE = "help.txt"
SAVE_FILE = ".save"
class Mud:
def __init__(self):
self.ore = 0
self.met = 0
self.gas = 0
self.fuel = 0
self.cry = 0
self.bases = []
self.ships = []
self.space = []
self.events = []
def new_event(self, max_power):
eve = MyEvent(max_power)
self.events.append(eve)
return eve
# returns the action class
# no longer used
def check_params(mud):
"""
:param mud: Mud
:return: Action
"""
if len(sys.argv) != 2:
with open(HELP_FILE, 'r') as fin:
print(fin.read())
sys.exit()
else:
a = sys.argv[1]
if a == 'load':
act = load_params(mud)
else:
act = Action(mud)
return act
def show_intro():
print(colored(" ---[ Welcome to Space Fleet ]---", "green"))
print(colored(" |--- ~~~ ---| ", "green"))
def load_params(mud):
"""
:type mud: Mud
"""
bases = []
ships = []
space = []
events = []
try:
with open(SAVE_FILE) as json_file:
data = json.load(json_file)
except OSError:
print("Starting a new game! Enter 'help' for commands.")
return Action(mud)
mud.ore = int(data['ore'])
mud.met = int(data['met'])
mud.cry = int(data['cry'])
mud.gas = int(data['gas'])
mud.fuel = int(data['fuel'])
for b in data['bases']:
bases.append(b)
mud.bases = bases
for s in data['ships']:
ships.append(Ship.load(s))
mud.ships = ships
for s in data['space']:
space.append(Ship.load(s))
mud.space = space
for e in data['events']:
eve = MyEvent.load(e)
events.append(eve)
mud.events = events
return Action(mud)
def save(mud):
data = {'ore': mud.ore, 'met': mud.met, 'cry': mud.cry, 'gas': mud.gas, 'fuel': mud.fuel, 'bases': []}
for b in mud.bases:
data['bases'].append({b})
data['ships'] = []
for s in mud.ships:
data['ships'].append(s.__dict__)
data['space'] = []
for s in mud.space:
data['space'].append(s.__dict__)
data['events'] = []
for e in mud.events:
data['events'].append(e.__dict__)
with open(SAVE_FILE, 'w') as outfile:
json.dump(data, outfile)
# split user input on space and call method on action object
def exec_command(user_input, act_obj):
s = user_input.split(" ")
command = s[0]
method = getattr(Action, command)
if len(s) > 1:
arg = s[1]
method(act_obj, arg)
else:
method(act_obj)
def run(action):
"""
:type action: Action
"""
prompt = 'Enter command: '
u_input = input(prompt)
while u_input != 'exit' and u_input != 'bye' and u_input != 'quit':
try:
exec_command(u_input, action)
except AttributeError:
print("Bad Command try again")
u_input = input(prompt)
print("Seeeee ya")
# initialize stuff
m = Mud()
new_action = load_params(m)
show_intro()
run(new_action)
save(m)