-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
93 lines (75 loc) · 3.33 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
import os
from flask import Flask, request, session, render_template
from flask_sslify import SSLify
from bokeh.resources import CDN
from utils import install_secret_key, load_world, save_world, make_custom_js
from data_plotting import build_game_data_plots
app = Flask(__name__)
sslify = SSLify(app)
install_secret_key(app)
@app.route('/')
def init(is_reset=False):
if not os.path.exists("worlds"):
raise Exception("Admin, please create the 'worlds' directory to save worlds!")
world = load_world()
num_turns = len(world.solar_park.generation)
available_tokens = [world.available_tokens(i) for i in range(num_turns)]
max_tokens = int(max(available_tokens))
turn_hours = [str(h).rjust(2, "0") for h in range(9, 9+num_turns)]
safe_js = make_custom_js(world.charging_stations, max_tokens, world.current_step)
data_plots_html, data_plots_js = build_game_data_plots(world)
return render_template("board.html", **dict(world=world),
num_turns=num_turns,
available_tokens=available_tokens,
max_tokens=max_tokens,
turn_hours=turn_hours,
safe_js=safe_js,
resetted_the_game=is_reset,
bokeh_css_resources=CDN.render_css(),
bokeh_js_resources=CDN.render_js(),
data_plots_html=data_plots_html,
data_plots_js=data_plots_js)
@app.route('/next', methods=['GET', 'POST'])
def next_step():
world = load_world()
orders = dict()
for sid, station in world.charging_stations.items():
if "order_%s" % sid not in request.form:
raise Exception("Missing order_%s in request." % sid)
orders[sid] = int(request.form.get("order_%s" % sid))
move_summary = world.next_step(orders)
save_world(world)
num_turns = len(world.solar_park.generation)
available_tokens = [world.available_tokens(i) for i in range(num_turns)]
max_tokens = int(max(available_tokens))
turn_hours = [str(h).rjust(2, "0") for h in range(9, 9+num_turns)]
if world.current_step < num_turns:
safe_js = make_custom_js(world.charging_stations, max_tokens, world.current_step)
else:
safe_js = ""
data_plots_html, data_plots_js = build_game_data_plots(world)
return render_template("board.html", **dict(world=world),
num_turns=num_turns,
available_tokens=available_tokens,
max_tokens=max_tokens,
completed_a_move=True,
move_summary=move_summary,
turn_hours=turn_hours,
safe_js=safe_js,
bokeh_css_resources=CDN.render_css(),
bokeh_js_resources=CDN.render_js(),
data_plots_html=data_plots_html,
data_plots_js=data_plots_js)
@app.route('/reset', methods=['GET'])
def reset():
world = load_world()
world.reset()
save_world(world)
return init(is_reset=True)
@app.route('/new', methods=['GET'])
def new():
if "world_id" in session:
del session["world_id"]
return init(is_reset=True)
if __name__ == '__main__':
app.run()