-
Notifications
You must be signed in to change notification settings - Fork 1
/
editor_site.py
138 lines (118 loc) · 4.14 KB
/
editor_site.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
from flask import Flask, render_template, send_from_directory, url_for, redirect, session
from flask import request
import json, codecs
import os
import requests
#import subprocess
from flask_cors import CORS #comment this on deployment
import numpy as np
from PIL import Image
from render import pixel_conversion, inverse_conversion, get_star_cells
import render as Renderer
app = Flask(__name__, static_folder='')
CORS(app)
#input_image = Renderer.render()
#input_array = np.array(input_image) / 255
def saveGalaxy(path = "galaxy.json", render = True):
global input_image
json.dump(galaxy, codecs.open(path, 'w', encoding='utf-8'),
separators=(',', ':'),
sort_keys=True,
indent=4)
if render:
input_image = Renderer.render()
#input_array = np.array(input_image) / 255
galaxy = json.load(open("galaxy.json"))
@app.route("/")
def director():
return render_template("index.html")
@app.route("/edit-galaxy")
def edit_systems():
return render_template("edit_systems.html")
@app.route("/edit-countries")
def edit_nations():
return render_template("edit_nations.html")
@app.route("/view")
def view_systems():
return render_template("view_systems.html")
@app.route("/view-resources")
def view_resources():
return render_template("view_resources.html")
# @app.route("/api/getMask")
# def getMask():
# x = float(request.args.get('x'))
# y = float(request.args.get('y'))
# coord = pixel_conversion((x, y), True)
# return {'pixel':np.ndarray.tolist(input_image[int(coord[1]), int(coord[0])][::-1])}
@app.route("/api/DeleteStar")
def delStar():
id = int(request.args.get('id', None))
star = galaxy['stars'][id]
galaxy['stars'][id] = [-1, -1]
#check for and delete any duplicates
ids = [i for i in range(len(galaxy)) if galaxy['stars'][i] == star]
for i in ids:
galaxy['stars'][i] = [-1, -1]
ids.append(id)
for lane in galaxy['hyperlanes']:
for i in ids:
if i in lane:
print(lane)
galaxy['hyperlanes'].remove(lane)
saveGalaxy()
return f"Successfully Deleted Star {id}"
@app.route("/api/AddStar")
def addStar():
x = int(request.args.get('x'))
y = int(request.args.get('y'))
print(f"Add star @ {x}, {y}")
coord = inverse_conversion([x, y])
print(f"Converted to {coord}")
#If star already exists, do nothing
if coord not in galaxy['stars']:
galaxy['stars'].append(coord)
saveGalaxy()
return f"Successfully Added Star {len(galaxy['stars']) - 1}"
@app.route("/api/DeleteLane")
def delLane():
id = int(request.args.get('id', None))
lane = galaxy['hyperlanes'].pop(id)
#check for and delete any duplicates
if lane in galaxy['hyperlanes']:
galaxy['hyperlanes'] = list(filter((lane).__ne__, galaxy['hyperlanes']))
saveGalaxy()
return f"Successfully Deleted Lane {id}"
@app.route("/api/AddLane")
def addLane():
id1 = int(request.args.get('id1', None))
id2 = int(request.args.get('id2', None))
lane = [id1, id2]
if lane not in galaxy['hyperlanes']:
galaxy['hyperlanes'].append(lane)
saveGalaxy()
return f"Successfully Connected Stars {id1} and {id2}"
@app.route("/api/GetCells", methods=['POST'])
def getCells():
if request.content_type == "application/json":
stars = request.json
regions = get_star_cells(stars)
return json.dumps(regions)
else:
return "Invalid Request Type!"
@app.route("/api/SaveGalaxy", methods=['POST'])
def save_galaxy():
if request.content_type == "application/json":
galaxy = request.json
json.dump(galaxy, codecs.open("galaxy.json", 'w', encoding='utf-8'),
separators=(',', ':'),
sort_keys=True,
indent=4)
return "Successfully Saved"
else:
return "Invalid Request Type!"
if __name__ == '__main__':
#pgcr_thread = subprocess.run(['python', 'PGCRscanner.py'], capture_output=True, text=True, check=True)
#CORS(app) #comment this on deployment
port = int(os.environ.get("PORT", 8080))
app.run(host='0.0.0.0', port=port)
#pgcr_thread.terminate()