-
Notifications
You must be signed in to change notification settings - Fork 0
/
flask_API.py
192 lines (143 loc) · 5.7 KB
/
flask_API.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 30 19:49:18 2018
@author: ivanskya
"""
from flask import Flask, request, render_template, flash, redirect, session, url_for
from flask import send_file
import json, os
import redis
import random
obj_data_queue = redis.Redis(host='localhost', port=6379, db=0)
con_data_queue = redis.Redis(host='localhost', port=6379, db=1)
user_data_queue = redis.Redis(host='localhost', port=6379, db=2)
log_queue = redis.Redis(host='localhost', port=6379, db=3)
app = Flask(__name__)
app.debug = True
app.secret_key = "super secret key"
def rand_player_name():
numbers = ['1','2','3','4','5','6','7','8','9','0']
random_list = [random.choice(numbers) for i in range(0,6) ]
str1 = ''.join(str(e) for e in random_list)
return('player_' + str1)
def get_instr_update(player_id):
data_new_raw = obj_data_queue.get(player_id)
if not(data_new_raw is None):
data_new = json.loads( data_new_raw )
else:
data_new = []
return(data_new)
@app.route("/update", methods=['GET', 'POST'])
def update():
if request.method == "POST":
control_data = request.get_json()
con_data_queue.set(str(session['user_id']), json.dumps( control_data ), px = 200 )
instruction_list = get_instr_update( session['user_id'] )
return( json.dumps( instruction_list) )
return("ERROR")
@app.route('/init_score_screen', methods=['GET', 'POST'])
def init_score_screen():
return render_template("game_renderer.html")
@app.route('/login', methods=['GET', 'POST'])
def do_login():
if ((len(request.form['user_id'])<8) | (len(request.form['user_id'])>20)) :
return login_screen('short_user_id')
else:
player_id = request.form['user_id']
objects_new_raw = obj_data_queue.get(player_id)
if (objects_new_raw is None):
session['user_id'] = player_id
session['objects'] = []
user_data_queue.set('new_user',json.dumps( session['user_id'] ), px = 1000)
log_queue.lpush(
'website_log',
json.dumps(
{
'ip':request.environ['REMOTE_ADDR'],
'func':'do_login',
'user_id':player_id
}
)
)
return game_renderer()
else:
return login_screen('player_exists')
@app.route('/get_website_img/<img_name>', methods=['GET', 'POST'])
def get_website_img(img_name):
log_queue.lpush(
'website_log',
json.dumps(
{
'ip':request.environ['REMOTE_ADDR'],
'func':'get_website_img',
'img_name':img_name
}
)
)
return send_file('templates/static/images/' + img_name, mimetype='image/jpeg')
@app.route("/game_renderer", methods=['GET', 'POST'])
def game_renderer():
log_queue.lpush(
'website_log',
json.dumps(
{
'ip':request.environ['REMOTE_ADDR'],
'func':'game_renderer'
}
)
)
return render_template("game_renderer.html")
@app.route("/login_screen/<warning_type>", methods=['GET', 'POST'])
def login_screen(warning_type):
prefilled_player_name = rand_player_name()
log_queue.lpush(
'website_log',
json.dumps(
{
'ip':request.environ['REMOTE_ADDR'],
'func':'login_screen',
'warning_type':warning_type
}
)
)
if (warning_type=='initial_ok'):
return render_template('login.html',prefilled_player_name=prefilled_player_name)
if (warning_type=='short_user_id'):
return render_template('login.html',prefilled_player_name=prefilled_player_name,context_info = "Your username must have 8-20 characters!")
if (warning_type=='incorrect_capcha'):
return render_template('login.html',prefilled_player_name=prefilled_player_name,context_info = "The capcha you have typed in is incorrect! Are you a robot ?")
if (warning_type=='game_exit'):
return render_template('login.html',prefilled_player_name=prefilled_player_name,context_info = "The game exited.")
@app.route("/frame_set", methods=['GET', 'POST'])
def frame_set():
return render_template('frame_set.html')
@app.route("/", methods=['GET', 'POST'])
def index():
log_queue.lpush(
'website_log',
json.dumps(
{
'request':request.environ['REMOTE_ADDR'],
'func':'index'
}
)
)
return render_template('index.html')
#@app.route("/add_chat_post", methods=['GET', 'POST'])
#def add_chat_post():
# return render_template('add_chat_post.html')
@app.route("/get_image/<img_file>", methods=['GET', 'POST'])
def get_img(img_file):
return send_file("img/rotations/"+img_file, mimetype='image/png')
@app.route("/get_preload_image_list", methods=['GET', 'POST'])
def get_preload_image_list():
file_list = ['get_image/' + x for x in os.listdir('img/rotations/')]
return( json.dumps( file_list) )
@app.route("/get_all_players", methods=['GET', 'POST'])
def get_all_players():
user_list = user_data_queue.keys()
return( json.dumps( user_list ) )
if __name__ == "__main__":
app.run(host='0.0.0.0')
#app.run()