-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
354 lines (321 loc) · 9.67 KB
/
main.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import httplib2
import io
import json
import os
import random
import time
from oauth2client.client import GoogleCredentials
from threading import Thread
from flask import Flask
from flask import jsonify
from flask import make_response
from flask import render_template
from flask import request
from flask import Response
from flask import send_file
from flask import send_from_directory
from flask_cors import CORS
app = Flask(__name__)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
cors = CORS(app, resources={"/net/*":{"origins": "*"}})
scripts = [
"lib/simplepeer.min.js",
"lib/lz-string.min.js",
"game.js",
"project scribble.js",
"resource.js",
"level.js",
"animation.js",
"controls.js",
"collision.js",
"classes.js",
"gui.js",
"devTools.js",
"editorTools.js"
]
gamemodes = [
"title.js",
"editor.js",
"sandbox.js",
"survival.js",
"online-lobby.js"
]
static_folders = [
"animations",
"data",
"levels",
"res",
"scripts"
]
def render_main(launch_mode,literals={},strings={}):
scs = []
for script in scripts:
scs.append(f"scripts/{script}")
for script in gamemodes:
scs.append(f"scripts/gamemodes/{script}")
lits = {"GAME_LAUNCH": launch_mode, "ALLOW_SW": "true"}
strs = {"NET_URL": net_url}
for key, item in literals.items():
lits[key] = item
for key, item in strings.items():
strs[key] = item
return render_template("main.html",scripts=scs,literals=lits,strings=strs)
def get_content_type(ext):
types = {
"css": "text/css",
"js": "text/javascript",
"json": "application/json",
"ogg": "audio/ogg",
"png": "image/png"
}
if ext in types.keys():
return types[ext]
else:
return "text/html"
def send404(msg=""):
return render_template("404.html",msg=msg), 404
@app.errorhandler(404)
def default404(e):
return send404("Page not found")
@app.route("/")
def game():
return render_main(0)
@app.route("/index.html")
def compiled_game():
return render_main(0,strings={"NET_URL":"https://doodle-man.appspot.com/net/"},literals={"ALLOW_SW":"false"})
@app.route("/edit")
def editor():
return render_main(1)
@app.route("/online")
def online_lobby():
return render_main(4)
@app.route("/join")
def join():
id = request.args.get("id")
if id:
return render_main(4,strings={"NET_INVITE": id})
return render_main(0)
@app.route("/<folder>/<path:subpath>")
def get_static(folder,subpath):
if folder in static_folders:
return send_from_directory(folder,subpath)
else:
return send404("Directory does not exist")
@app.route("/list/<path:folderpath>.json")
def get_static_list(folderpath):
folder = folderpath.split("/").pop(0)
if folder in static_folders:
paths = []
for dirpath, _dirnames, filenames in os.walk(folderpath):
for filename in filenames:
path = os.path.join(dirpath,filename).replace('\\','/').replace(folderpath+"/","")
paths.append(path)
return jsonify(paths)
else:
return send404()
@app.route("/imagelist.json")
def get_preload_images():
paths = []
for filename in os.listdir("res"):
ext = filename.split(".").pop()
if ext == "png":
paths.append(filename)
for filename in os.listdir("res/GUI"):
ext = filename.split(".").pop()
if ext == "png":
paths.append("GUI/"+filename)
return jsonify(paths)
@app.route("/favicon.ico")
def get_ico():
return send_file('favicon.ico')
@app.route("/manifest.json")
def get_manifest():
return send_file("manifest.json")
@app.route("/sw.js")
def build_service_worker():
paths = ["/","/index.html","/edit", "/imagelist.json", "/favicon.ico", "/manifest.json"]
for dir in static_folders:
paths.append(f"/list/{dir}.json")
for dirpath, dirnames, filenames in os.walk(dir):
for subpath in dirnames:
paths.append(f"/list/{dirpath}/{subpath}.json")
for filename in filenames:
paths.append("/"+os.path.join(dirpath,filename).replace("\\","/"))
str = ""
with open("sw.js") as file:
str = file.read()
str = str.replace('["staticFiles"]',f"{paths}")
return send_file(io.BytesIO(str.encode()), mimetype="script")
# Firebase - Realtime Database
database = "https://doodle-man.firebaseio.com/"
scopes = [
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/firebase.database"
]
net_url = "/net/"
if os.getenv("GAE_ENV","").startswith("standard"):
creds = GoogleCredentials.get_application_default().create_scoped(scopes)
else:
try:
creds = GoogleCredentials.from_stream("cred.json").create_scoped(scopes)
except:
# rely on app engine server if credentials not found on local server
net_url = "https://doodle-man.appspot.com/net/"
def fire_authed():
h = httplib2.Http()
return creds.authorize(h)
def fire_put(path,value=None):
content = fire_authed().request(f"{database}{path}.json",method="PUT",body=json.dumps(value))[1]
return json.loads(content)
def fire_get(path):
content = fire_authed().request(f"{database}{path}.json",method="GET")[1]
return json.loads(content)
def fire_delete(path):
content = fire_authed().request(f"{database}{path}.json",method="DELETE")[1]
return json.loads(content)
def fire_post(path,value=None):
content = fire_authed().request(f"{database}{path}.json",method="POST",body=json.dumps(value))[1]
return json.loads(content)
def fire_patch(path,value=None):
content = fire_authed().request(f"{database}{path}.json",method="PATCH",body=json.dumps(value))[1]
return json.loads(content)
# custom
def fire_append(path,value=None):
data = fire_get(path)
if data == 0:
return fire_put(path,[value])
elif isinstance(data,list):
data.append(value)
print(data)
return fire_put(path,data)
def fire_pop(path,value=None,index=-1):
data = fire_get(path)
if isinstance(data,list):
first = data.pop(index)
if len(data)==0:
fire_put(path,0)
else:
fire_put(path,data)
return {"data":first}
def fire_popleft(path,value=None):
return fire_pop(path,value,0)
# NET CODE
ROOM_TIMEOUT = 60 # minutes
ROOM_CLEANUP_PERIOD = 24 * 60 # 1 day
ROOM_CREATE_ATTEMPTS = 50
def net_gen_room_code():
acceptable = "0123456789ABCDEFGHJKMNPQRTUVWXY"
code = ""
for _ in range(4):
code += acceptable[round(random.random()*(len(acceptable)-1))]
return code
def net_room_alive(room):
timestamp = fire_get(f"rooms/{room}/timestamp")
if timestamp == None:
return False
else:
age = (time.time() - float(timestamp)) / 60 # seconds to minutes
if age > ROOM_TIMEOUT:
fire_delete(f"rooms/{room}")
return False
else:
return True
def net_send_DNE():
return jsonify({"doesNotExist":True})
def net_check_for_cleanup():
last_cleanup = fire_get("lastCleanup")
if last_cleanup == None or (time.time() - float(last_cleanup))/60 > ROOM_CLEANUP_PERIOD:
thrd = Thread(target=net_cleanup_rooms)
thrd.start()
def net_cleanup_rooms():
fire_put("lastCleanup",str(time.time()))
rooms = fire_get("rooms")
if rooms:
for room, data in rooms.items():
if data:
if "timestamp" in data:
stamp = float(data["timestamp"])
if (time.time() - stamp)/60 > ROOM_TIMEOUT:
fire_delete(f"rooms/{room}")
else:
fire_delete(f"rooms/{room}")
@app.route("/net/createroom",methods=["POST"])
def net_create_room():
net_check_for_cleanup()
for _ in range(ROOM_CREATE_ATTEMPTS):
room = net_gen_room_code()
if not net_room_alive(room):
fire_put(f"rooms/{room}",{
"timestamp": str(time.time()),
"hostSignal": 0,
"clientSignal": 0,
"clientQueue": 0,
"locked": False
})
return jsonify({'success':True,'room':room})
return jsonify({"success":False})
@app.route("/net/keepalive",methods=["POST"])
def net_room_keep_alive():
data = request.get_json()
if fire_get(f"rooms/{data['room']}/timestamp"):
return jsonify(fire_put(f"rooms/{data['room']}/timestamp",str(time.time())))
else:
return net_send_DNE()
@app.route("/net/closeroom",methods=["POST"])
def net_close_room():
data = request.get_json()
if data and data["room"]:
return jsonify(fire_delete(f"rooms/{data['room']}"))
else:
return send404("No room given")
def net_post_signal(data,role):
if data and net_room_alive(data["room"]):
return jsonify(fire_put(f"rooms/{data['room']}/{role}Signal",data["signal"]))
return net_send_DNE()
@app.route("/net/posthost",methods=["POST"])
def net_post_host_code():
return net_post_signal(request.get_json(),"host")
@app.route("/net/postclient",methods=["POST"])
def net_post_client_code():
return net_post_signal(request.get_json(),"client")
def net_take_signal(data,role):
if data and net_room_alive(data["room"]):
return jsonify(fire_put(f"rooms/{data['room']}/{role}Signal",0))
return net_send_DNE()
@app.route("/net/takehost",methods=["POST"])
def net_take_host_code():
return net_take_signal(request.get_json(),"host")
@app.route("/net/takeclient",methods=["POST"])
def net_take_client_code():
return net_take_signal(request.get_json(),"client")
@app.route("/net/enterqueue",methods=["POST"])
def net_enter_queue():
data = request.get_json()
if data and net_room_alive(data["room"]):
if fire_get(f"rooms/{data['room']}/locked"):
return jsonify({"locked": True})
else:
stamp = str(time.time())
if fire_append(f"rooms/{data['room']}/clientQueue",stamp):
return jsonify({"stamp":stamp,"locked":False})
return net_send_DNE()
@app.route("/net/leavequeue",methods=["POST"])
def net_leave_queue():
data = request.get_json()
if data and net_room_alive(data["room"]):
return jsonify(fire_popleft(f"rooms/{data['room']}/clientQueue"))
return net_send_DNE()
@app.route("/net/lockroom",methods=["POST"])
def net_lock_room():
data = request.get_json()
if net_room_alive(data["room"]):
return jsonify(fire_put(f"rooms/{data['room']}/locked",True))
return net_send_DNE()
@app.route("/net/unlockroom",methods=["POST"])
def net_unlock_room():
data = request.get_json()
if net_room_alive(data["room"]):
return jsonify(fire_put(f"rooms/{data['room']}/locked",False))
return net_send_DNE()
if __name__ == "__main__":
app.run()