-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.py
248 lines (225 loc) · 10.4 KB
/
routes.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
# routes.py
import json
import os
from flask import Blueprint, render_template, request, redirect, url_for, send_from_directory, jsonify
from utils import read_json, write_json, parse_grid, formatCellContent, create_theme, convert_theme
routes = Blueprint('routes', __name__)
@routes.route('/')
def home():
themes_directory = 'gamedata'
themes = os.listdir(themes_directory)
themes = [theme for theme in themes if os.path.isdir(os.path.join(themes_directory, theme))]
return render_template('index.html', themes=themes)
@routes.route('/<theme>')
def theme(theme):
file_path = f'gamedata/{theme}/CombinedGameData.json'
data = read_json(file_path)
crusher_grids = data.get("CrusherGrids", [])
# Process crusher grids to add an enumerated list of grid elements
processed_crusher_grids = []
for grid in crusher_grids:
enumerated_grid = list(enumerate(grid["Grid"].split(',')))
processed_crusher_grids.append({
"Id": grid["Id"],
"EnumeratedGrid": enumerated_grid
})
return render_template('theme.html', theme=theme, data=data, crusher_grids=processed_crusher_grids)
@routes.route('/<theme>/<zone_id>')
def zone(theme, zone_id):
file_path = f'gamedata/{theme}/CombinedGameData.json'
data = read_json(file_path)
zone = next((z for z in data["Zones"] if z["Id"] == zone_id), None)
if zone:
grid = parse_grid(zone["Grid"], zone["WidthCells"])
return render_template('zone.html', theme=theme, zone=zone, grid=grid, formatCellContent=formatCellContent)
return "Zone not found", 404
@routes.route('/create_theme', methods=['POST'])
def create_new_theme():
theme_name = request.form.get('theme_name')
if theme_name:
create_theme(theme_name)
return redirect(url_for('routes.home'))
@routes.route('/update_zone_name/<theme>/<zone_id>', methods=['POST'])
def update_zone_name(theme, zone_id):
new_zone_name = request.form.get('new_zone_name')
if new_zone_name:
file_path = f'gamedata/{theme}/CombinedGameData.json'
data = read_json(file_path)
for zone in data["Zones"]:
if zone["Id"] == zone_id:
zone["Id"] = new_zone_name
break
write_json(file_path, data)
return redirect(url_for('routes.zone', theme=theme, zone_id=new_zone_name))
@routes.route('/add_rows_top/<theme>/<zone_id>', methods=['POST'])
def add_rows_top(theme, zone_id):
num_rows = int(request.form.get('num_rows'))
if num_rows > 0:
file_path = f'gamedata/{theme}/CombinedGameData.json'
data = read_json(file_path)
for zone in data["Zones"]:
if zone["Id"] == zone_id:
width = zone["WidthCells"]
new_rows = ['.' for _ in range(width * num_rows)]
grid_elements = zone["Grid"].split(',')
updated_grid = ','.join(new_rows + grid_elements)
zone["Grid"] = updated_grid
zone["DepthCells"] += num_rows
break
write_json(file_path, data)
return redirect(url_for('routes.zone', theme=theme, zone_id=zone_id))
@routes.route('/add_rows_bottom/<theme>/<zone_id>', methods=['POST'])
def add_rows_bottom(theme, zone_id):
num_rows = int(request.form.get('num_rows'))
if num_rows > 0:
file_path = f'gamedata/{theme}/CombinedGameData.json'
data = read_json(file_path)
for zone in data["Zones"]:
if zone["Id"] == zone_id:
width = zone["WidthCells"]
new_rows = ['.' for _ in range(width * num_rows)]
grid_elements = zone["Grid"].split(',')
updated_grid = ','.join(grid_elements + new_rows)
zone["Grid"] = updated_grid
zone["DepthCells"] += num_rows
break
write_json(file_path, data)
return redirect(url_for('routes.zone', theme=theme, zone_id=zone_id))
@routes.route('/remove_rows_top/<theme>/<zone_id>', methods=['POST'])
def remove_rows_top(theme, zone_id):
num_rows = int(request.form.get('num_rows'))
if num_rows > 0:
file_path = f'gamedata/{theme}/CombinedGameData.json'
data = read_json(file_path)
for zone in data["Zones"]:
if zone["Id"] == zone_id:
width = zone["WidthCells"]
grid_elements = zone["Grid"].split(',')
if len(grid_elements) >= width * num_rows:
updated_grid = grid_elements[width * num_rows:]
zone["Grid"] = ','.join(updated_grid)
zone["DepthCells"] -= num_rows
break
write_json(file_path, data)
return redirect(url_for('routes.zone', theme=theme, zone_id=zone_id))
@routes.route('/remove_rows_bottom/<theme>/<zone_id>', methods=['POST'])
def remove_rows_bottom(theme, zone_id):
num_rows = int(request.form.get('num_rows'))
if num_rows > 0:
file_path = f'gamedata/{theme}/CombinedGameData.json'
data = read_json(file_path)
for zone in data["Zones"]:
if zone["Id"] == zone_id:
width = zone["WidthCells"]
grid_elements = zone["Grid"].split(',')
if len(grid_elements) >= width * num_rows:
updated_grid = grid_elements[:-width * num_rows]
zone["Grid"] = ','.join(updated_grid)
zone["DepthCells"] -= num_rows
break
write_json(file_path, data)
return redirect(url_for('routes.zone', theme=theme, zone_id=zone_id))
@routes.route('/add_columns_left/<theme>/<zone_id>', methods=['POST'])
def add_columns_left(theme, zone_id):
num_columns = int(request.form.get('num_columns'))
if num_columns > 0:
file_path = f'gamedata/{theme}/CombinedGameData.json'
data = read_json(file_path)
for zone in data["Zones"]:
if zone["Id"] == zone_id:
width = zone["WidthCells"]
height = zone["DepthCells"]
grid_elements = zone["Grid"].split(',')
new_grid = []
for i in range(height):
new_grid.extend(['.' for _ in range(num_columns)] + grid_elements[i*width:(i+1)*width])
zone["Grid"] = ','.join(new_grid)
zone["WidthCells"] += num_columns
break
write_json(file_path, data)
return redirect(url_for('routes.zone', theme=theme, zone_id=zone_id))
@routes.route('/add_columns_right/<theme>/<zone_id>', methods=['POST'])
def add_columns_right(theme, zone_id):
num_columns = int(request.form.get('num_columns'))
if num_columns > 0:
file_path = f'gamedata/{theme}/CombinedGameData.json'
data = read_json(file_path)
for zone in data["Zones"]:
if zone["Id"] == zone_id:
width = zone["WidthCells"]
height = zone["DepthCells"]
grid_elements = zone["Grid"].split(',')
new_grid = []
for i in range(height):
new_grid.extend(grid_elements[i*width:(i+1)*width] + ['.' for _ in range(num_columns)])
zone["Grid"] = ','.join(new_grid)
zone["WidthCells"] += num_columns
break
write_json(file_path, data)
return redirect(url_for('routes.zone', theme=theme, zone_id=zone_id))
@routes.route('/remove_columns_left/<theme>/<zone_id>', methods=['POST'])
def remove_columns_left(theme, zone_id):
num_columns = int(request.form.get('num_columns'))
if num_columns > 0:
file_path = f'gamedata/{theme}/CombinedGameData.json'
data = read_json(file_path)
for zone in data["Zones"]:
if zone["Id"] == zone_id:
width = zone["WidthCells"]
height = zone["DepthCells"]
grid_elements = zone["Grid"].split(',')
if width > num_columns:
new_grid = []
for i in range(height):
new_grid.extend(grid_elements[i*width+num_columns:(i+1)*width])
zone["Grid"] = ','.join(new_grid)
zone["WidthCells"] -= num_columns
break
write_json(file_path, data)
return redirect(url_for('routes.zone', theme=theme, zone_id=zone_id))
@routes.route('/remove_columns_right/<theme>/<zone_id>', methods=['POST'])
def remove_columns_right(theme, zone_id):
num_columns = int(request.form.get('num_columns'))
if num_columns > 0:
file_path = f'gamedata/{theme}/CombinedGameData.json'
data = read_json(file_path)
for zone in data["Zones"]:
if zone["Id"] == zone_id:
width = zone["WidthCells"]
height = zone["DepthCells"]
grid_elements = zone["Grid"].split(',')
if width > num_columns:
new_grid = []
for i in range(height):
new_grid.extend(grid_elements[i*width:(i+1)*width-num_columns])
zone["Grid"] = ','.join(new_grid)
zone["WidthCells"] -= num_columns
break
write_json(file_path, data)
return redirect(url_for('routes.zone', theme=theme, zone_id=zone_id))
@routes.route('/convert_theme/<theme>', methods=['GET'])
def convert_theme_route(theme):
file_path = f'gamedata/{theme}/CombinedGameData.json'
data = read_json(file_path)
output_json = convert_theme(data)
output_file_path = f'gamedata/{theme}/ConvertedGameData.json'
write_json(output_file_path, output_json)
return jsonify(output_json)
@routes.route('/generate-savedata/<theme>', methods=['GET'])
def generate_savedata(theme):
# Load the converted game data
converted_game_data_path = os.path.join('gamedata', theme, 'ConvertedGameData.json')
with open(converted_game_data_path, 'r') as file:
converted_game_data = json.load(file)
# Load the base SaveData.json
with open('SaveData.json', 'r') as file:
save_data = json.load(file)
# Replace the "LteWorldModel" in SaveData.json with the one from ConvertedGameData.json
save_data["LteWorldModel"] = converted_game_data["LteWorldModel"]
# Save the new SaveData.json to the theme directory
save_data_output_path = os.path.join('gamedata', theme, 'SaveData.json')
write_json(save_data_output_path, save_data)
return jsonify(save_data)
@routes.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(routes.root_path, 'static'), 'favicon.ico', mimetype='image/vnd.microsoft.icon')