Repository navigation
Expand file tree
/
Copy pathserver.py
More file actions
265 lines (230 loc) · 10.3 KB
/
Copy pathserver.py
File metadata and controls
265 lines (230 loc) · 10.3 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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
import cherrypy
import os
import random as r
import sys
import operator
from gurpsspace import starsystem as starsys
from namegenerator import namegenerator
from jinja2 import Environment, FileSystemLoader
env = Environment(loader=FileSystemLoader('webgui/templates'))
class WebServer(object):
random_seed = None
def set_seed(self, seed=None):
if seed is None:
seed = r.randint(1, sys.maxsize)
self.random_seed = int(seed)
@cherrypy.expose
def index(self):
# List the available naming schemes
naming_schemes = []
for scheme in namegenerator.NameGenerator().list_available_corpuses():
naming_schemes.append(scheme)
tmpl = env.get_template('index.html')
return tmpl.render(naming_schemes=naming_schemes)
@cherrypy.expose
def starsystem(self, must_have_garden="False", open_cluster=None, num_stars=0, age=None, naming="", use_chain=False, depth=1, seed=None):
input_seed = None if seed == '' or None else seed # Correctly interpret "no input"
self.set_seed(input_seed) # reseed the PRNG, so that there is a unique seed every time
r.seed(self.random_seed)
if num_stars == "":
num_stars = None
elif int(num_stars) < 1 or int(num_stars) > 3:
num_stars = None
else:
num_stars = int(num_stars)
namegen = None
if naming != "": # A naming scheme has been selected that is not the simple "A-1", "B-1" scheme.
namegen = namegenerator.NameGenerator(int(depth), self.random_seed)
namegen.read_file(naming)
namegen.use_chain = use_chain
cherrypy.session['namegen'] = namegen
else:
cherrypy.session['namegen'] = None
arguments = {
'open_cluster': open_cluster == "True",
'num_stars': num_stars,
'age': age
}
# Generate star systems until one is made that contains a Garden world if it's required.
if must_have_garden == "True":
garden = False
while garden is not True:
mysys = starsys.StarSystem(**arguments)
garden = mysys.has_garden()
else:
mysys = starsys.StarSystem(**arguments)
for star in mysys.stars:
for key, v in star.planetsystem.get_orbitcontents().items():
if namegen is not None:
simple_name = v.get_name().replace("-", "")
if cherrypy.session.get('name_of_' + simple_name) is None:
name = namegen.get_random_name()
star.planetsystem.get_orbitcontents()[key].set_name(name)
# For some reason, using simple_name here leads to storing stuff improperly and
# generating new names every time. No idea why.
cherrypy.session['name_of_' + v.get_name().replace("-", "")] = name
else:
name = cherrypy.session.get('name_of_' + simple_name)
star.planetsystem.get_orbitcontents()[key].set_name(name)
cherrypy.session.save()
tmpl = env.get_template('overview.html')
cherrypy.session['starsystem'] = mysys
cherrypy.response.cookie['names'] = {}
return tmpl.render(starsystem=mysys, seed=self.random_seed)
@cherrypy.expose
def planetsystem(self, star_id=""):
starsystem = cherrypy.session.get('starsystem')
if starsystem is None:
raise cherrypy.HTTPRedirect('/', 307)
if star_id == "":
raise cherrypy.HTTPRedirect('/', 307)
else:
star_id = int(star_id)
tmpl = env.get_template('planetsystem.html')
env.globals['translate_row'] = self.translate_row
t_count = 0
a_count = 0
g_count = 0
for key, _ in starsystem.stars[star_id].planetsystem.get_orbitcontents().items():
if starsystem.stars[star_id].planetsystem.get_orbitcontents()[key].type() == 'Terrestrial':
t_count += 1
if starsystem.stars[star_id].planetsystem.get_orbitcontents()[key].type() == 'Ast. Belt':
a_count += 1
if starsystem.stars[star_id].planetsystem.get_orbitcontents()[key].type() == 'Gas Giant':
g_count += 1
cherrypy.session['planetsystem'] = starsystem.stars[star_id].planetsystem
return tmpl.render(planetsystem=starsystem.stars[star_id].planetsystem, terrestrial_count=t_count, asteroid_count=a_count, gas_giant_count=g_count)
@cherrypy.expose
def satellites(self, planet_id=""):
planetsystem = cherrypy.session.get('planetsystem')
if planetsystem is None:
raise cherrypy.HTTPRedirect('/', 307)
if planet_id == "":
raise cherrypy.HTTPRedirect('/', 307)
else:
planet_id = float(planet_id)
planet = planetsystem.get_orbitcontents()[planet_id]
if planet.type() == 'Terrestrial':
moons = planet.get_satellites()
else:
moons = planet.get_moons()
for moon in moons:
if len(moon.get_name().split('-')) == 3:
index = moon.get_name().split('-')[2]
moon.set_name(planet.get_name() + '-' + index)
cherrypy.session['moons'] = moons
tmpl = env.get_template('moons.html')
return tmpl.render(moons=moons, planet_name=planet.get_name())
@cherrypy.expose
def printable(self):
try:
starsystem = cherrypy.session['starsystem']
except KeyError:
raise cherrypy.HTTPError(404)
t_count = 0
a_count = 0
g_count = 0
for star in starsystem.stars:
for key, _ in star.planetsystem.get_orbitcontents().items():
if star.planetsystem.get_orbitcontents()[key].type() == 'Terrestrial':
t_count += 1
if star.planetsystem.get_orbitcontents()[key].type() == 'Ast. Belt':
a_count += 1
if star.planetsystem.get_orbitcontents()[key].type() == 'Gas Giant':
g_count += 1
tmpl = env.get_template('printable.html')
env.globals['translate_row'] = self.translate_row
return tmpl.render(starsystem=starsystem, seed=self.random_seed, terrestrial_count=t_count, asteroid_count=a_count, gas_giant_count=g_count)
def translate_row(self, planet, row):
"""
It is difficult in HTML and Jinja to make a table where each column is a single item, rather than each row.
This function helps out by returning the appropriate data for each row.
:param planet: The orbital body to be queried.
:param row: The row which determines the query.
:return: The data appropriate to the given row.
"""
if row == '':
return planet.get_name()
if row == 'World Size':
return planet.get_size()
if row == 'World Type':
return planet.get_type()
if row == 'Atm. Mass*':
return str(planet.get_atmospheric_mass())
if row == 'Atm. Composition' and planet.get_atmospheric_mass() > 0:
retval = ''
atmospheric_components = sorted(planet.atmcomp.items(), key=operator.itemgetter(0))
for name, present in atmospheric_components:
if present:
retval += name + '<br/>'
if len(retval) == 0:
retval = 'Breathable'
return retval
elif row == 'Atm. Composition' and planet.get_atmospheric_mass() == 0:
return 'Trace or No Atmosphere'
if row == 'Hydr. Coverage':
return str(round(planet.get_hydrographic_cover(), 2)) + ' %'
if row == 'Avg. Surface Temperature':
return str(round(planet.get_average_surface_temp(), 2)) + ' K / ' + str(round(planet.get_average_surface_temp() - 273.15, 2)) + '°C'
if row == 'Climate Type':
return planet.get_climate()
if row == 'Axial Tilt':
return str(planet.get_axial_tilt()) + '°'
if row == 'Density*':
return planet.get_density()
if row == 'Diameter*':
return round(planet.get_diameter(), 2)
if row == 'Surface Gravity':
return round(planet.get_gravity(), 2)
if row == 'Mass*':
return round(planet.get_mass(), 2)
if row == 'Atm. Pressure':
return str(round(planet.get_pressure(), 2)) + ' atm'
if row == 'Pressure Category':
return planet.get_pressure_category()
if row == 'Total Tidal Effect':
return round(planet.get_total_tidal_effect(), 2)
if row == 'Volcanics':
return planet.get_volcanism()
if row == 'Tectonics':
return planet.get_tectonics()
if row == 'Resource Value Modifier':
return planet.get_rvm()
if row == 'Habitability':
return planet.get_habitability()
if row == 'Affinity':
return planet.get_affinity()
if row == 'Rotational Period*':
retval = str(round(planet.get_rotation(), 2))
retval += " days"
return retval
if row == 'Blackbody Temperature':
return str(round(planet.get_blackbody_temp(), 2)) + ' K'
if row == 'Cloudtop Gravity':
return round(planet.get_gravity(), 2)
else:
return 'Not implemented yet'
if __name__ == '__main__':
# Configure CherryPy with a Python dictionary for Python 3.5 compatibility.
conf = {
'global': {
'server.socket_host': '127.0.0.1',
'server.socket_port': 8080
},
'/': {
'tools.sessions.on': True,
'tools.sessions.timeout': 60,
'tools.staticdir.on': True,
'tools.staticdir.dir': "webgui/static",
'tools.staticdir.root': os.path.abspath(os.getcwd())
},
'/favicon': {
'tools.staticfile.on': True,
'tools.staticfile.filename': os.path.abspath(os.getcwd()) + "/webgui/static/favicon.ico"
},
'/scripts': {
'tools.staticdir.on': True,
'tools.staticdir.dir': "webgui/scripts"
}
}
cherrypy.quickstart(WebServer(), '/', conf)