-
Notifications
You must be signed in to change notification settings - Fork 0
/
socketio_main.py
363 lines (304 loc) · 15.9 KB
/
socketio_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
355
356
357
358
359
360
361
362
363
import sys
import time
import os
from datetime import datetime
from flask import Flask, request, jsonify, Response, url_for
from flask import render_template
from flask_socketio import SocketIO, emit, join_room, leave_room
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import create_engine, and_
from flask_bower import Bower
from flask_cors import CORS
from time_ago import time_ago
app = Flask(__name__)
app.url_map.strict_slashes = False
Bower(app)
CORS(app)
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('SQLALCHEMY_DATABASE_URI') \
or 'sqlite:////Users/alex/code/ddstats/app.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
socketio = SocketIO(app, async_mode='gevent_uwsgi')
engine = create_engine(app.config['SQLALCHEMY_DATABASE_URI'])
class Game(db.Model):
id = db.Column(db.Integer, primary_key=True, nullable=False)
player_id = db.Column(db.Integer, nullable=False)
granularity = db.Column(db.Integer, nullable=False)
game_time = db.Column(db.Float, nullable=False)
death_type = db.Column(db.Integer, nullable=False)
gems = db.Column(db.Integer, nullable=False)
homing_daggers = db.Column(db.Integer, nullable=False)
daggers_fired = db.Column(db.Integer, nullable=False)
daggers_hit = db.Column(db.Integer, nullable=False)
enemies_alive = db.Column(db.Integer, nullable=False)
enemies_killed = db.Column(db.Integer, nullable=False)
time_stamp = db.Column(db.DateTime, default=datetime.utcnow, nullable=False)
replay_player_id = db.Column(db.Integer, default=0, nullable=False)
survival_hash = db.Column(db.String(32))
version = db.Column(db.String(16))
level_two_time = db.Column(db.Float, default=0.0, nullable=False)
level_three_time = db.Column(db.Float, default=0.0, nullable=False)
level_four_time = db.Column(db.Float, default=0.0, nullable=False)
homing_daggers_max_time = db.Column(db.Float, default=0.0, nullable=False)
enemies_alive_max_time = db.Column(db.Float, default=0.0, nullable=False)
homing_daggers_max = db.Column(db.Integer, default=0, nullable=False)
enemies_alive_max = db.Column(db.Integer, default=0, nullable=False)
class State(db.Model):
id = db.Column(db.Integer, primary_key=True, nullable=False)
game_id = db.Column(db.Integer, db.ForeignKey('game.id'), nullable=False)
game_time = db.Column(db.Float, nullable=False)
gems = db.Column(db.Integer, nullable=False)
homing_daggers = db.Column(db.Integer, nullable=False)
daggers_hit = db.Column(db.Integer, nullable=False)
daggers_fired = db.Column(db.Integer, nullable=False)
enemies_alive = db.Column(db.Integer, nullable=False)
enemies_killed = db.Column(db.Integer, nullable=False)
@property
def serialize(self):
return {
'game_time': self.game_time,
'gems': self.gems,
'homing_daggers': self.homing_daggers,
'daggers_hit': self.daggers_hit,
'daggers_fired': self.daggers_fired,
'enemies_alive': self.enemies_alive,
'enemies_killed': self.enemies_killed
}
class User(db.Model):
id = db.Column(db.Integer, primary_key=True, nullable=False)
username = db.Column(db.String(128), index=True, nullable=False)
rank = db.Column(db.Integer, nullable=False)
game_time = db.Column(db.Float, nullable=False)
death_type = db.Column(db.Integer, nullable=False)
gems = db.Column(db.Integer, nullable=False)
daggers_fired = db.Column(db.Integer, nullable=False)
daggers_hit = db.Column(db.Integer, nullable=False)
enemies_killed = db.Column(db.Integer, nullable=False)
accuracy = db.Column(db.Float, nullable=False)
time_total = db.Column(db.Float, nullable=False)
deaths_total = db.Column(db.Integer, nullable=False)
gems_total = db.Column(db.Integer, nullable=False)
enemies_killed_total = db.Column(db.Integer, nullable=False)
daggers_fired_total = db.Column(db.Integer, nullable=False)
daggers_hit_total = db.Column(db.Integer, nullable=False)
accuracy_total = db.Column(db.Float, nullable=False)
class Live(db.Model):
player_id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True, nullable=False)
sid = db.Column(db.String(32))
@app.before_first_request
def clear_live_table():
db.session.query(Live).delete()
db.session.commit()
########################################################
# socketio stuff #
########################################################
threshold = 1000
user_list = []
player_dict = {}
@socketio.on('connect', namespace="/index")
def index_connect():
pass
@socketio.on('connect', namespace='/ddstats-bot')
def ddstats_bot_connect():
print('Bot connected.', file=sys.stdout)
@socketio.on('connect', namespace='/test')
def test_connect():
print('This standard output', file=sys.stdout)
emit('my response', {'data': 'Connected'}, broadcast=True)
emit('welcome', broadcast=True)
@socketio.on('disconnect', namespace='/user_page')
def user_page_disconnect():
global user_list
user = next((u for u in user_list if u['sid'] == request.sid), None)
user_list = [u for u in user_list if u['sid'] != request.sid]
if user is not None:
player_id = user['player_id']
users_in_room = [u for u in user_list if u['player_id'] == player_id]
user_count = len(users_in_room)
emit('update_user_count', user_count, room=player_id)
# player = db.session.query(Live).filter_by(player_id=player_id).first()
# if player is not None:
# emit('update_user_count', user_count, name_space='/stats', room=player.sid)
# print(str(len(users_in_room)) + ' user(s) in room ' + str(player_id), file=sys.stdout)
# @socketio.on('my event', namespace='/admin')
# def add_to_live():
# print(player_id, file=sys.stdout)
@socketio.on('join', namespace='/user_page')
def user_page_join(player_id):
global user_list
join_room(player_id)
user_list.append({'sid': request.sid, 'player_id': player_id})
# print(player_id + ': someone joined the room.', file=sys.stdout)
users_in_room = [u for u in user_list if u['player_id'] == player_id]
user_count = len(users_in_room)
emit('update_user_count', user_count, room=player_id)
# player = db.session.query(Live).filter_by(player_id=player_id).first()
# if player is not None:
# emit('update_user_count', user_count, name_space='/stats', room=player.sid)
@socketio.on('login')
def login(player_id):
global player_dict
player = Live(player_id=int(player_id), sid=request.sid)
db.session.add(player)
db.session.flush()
db.session.commit()
emit('user_login', player_id, broadcast=True, namespace='/ddstats-bot')
if player_id is not -1:
name = db.session.query(User.username).filter_by(id=player_id).scalar()
player_dict[str(player_id)] = {}
player_dict[str(player_id)]["name"] = name
player_dict[str(player_id)]["game_time"] = 0.0
player_dict[str(player_id)]["death_type"] = -2 # in menu
player_dict[str(player_id)]["is_replay"] = False
emit('live_users_update', player_dict, include_self=False, broadcast=True, namespace='/index')
# users_in_room = [u for u in user_list if u['player_id'] == player_id]
# user_count = len(users_in_room)
# emit('update_user_count', user_count, name_space='/stats', room=request.sid)
# emit('test_send', {'data': 'test'}, broadcast=True, namespace='/leaderbot')
@socketio.on('disconnect')
def handle_disconnect():
player_id = db.session.query(Live.player_id).filter_by(sid=request.sid).scalar()
if player_id is not None:
if str(player_id) in player_dict:
del player_dict[str(player_id)]
Live.query.filter_by(sid=request.sid).delete()
db.session.commit()
emit('live_users_update', player_dict, include_self=False, broadcast=True, namespace='/index')
emit('offline', room=str(player_id), include_self=False, broadcast=True, namespace='/user_page')
@socketio.on('message')
def handle_message(message):
print('received message: ' + message, file=sys.stdout)
#######################################################################################################################
# post 0.4.0
#######################################################################################################################
@socketio.on('status_update')
def on_status_update(player_id, status):
if player_id is -1:
return
emit('status_update', (status), room=str(player_id), namespace='/user_page',
include_self=False, broadcast=True)
@socketio.on('submit')
def receive_stats(player_id, game_time, gems, homing_daggers,
enemies_alive, enemies_killed, daggers_hit,
daggers_fired, level_two, level_three, level_four,
is_replay, death_type, notify_player_best, notify_above_1000):
if player_id is -1:
return
global player_dict
global threshold
user = db.session.query(User).filter_by(id=player_id).first()
if (str(player_id) in player_dict) and (is_replay == False) and notify_above_1000:
if player_dict[str(player_id)]['game_time'] < threshold and game_time >= threshold:
emit('threshold_alert', (user.username, player_id, threshold), namespace='/ddstats-bot', broadcast=True)
# get user stats, compare to previous and current time
player_best = db.session.query(User.game_time).filter_by(id=player_id).first()
if (player_best is not None) and (is_replay == False) and notify_player_best:
player_best = player_best[0]
if str(player_id) in player_dict:
if player_dict[str(player_id)]['game_time'] < player_best and game_time >= player_best:
emit('player_best', (user.username, player_id, player_best, game_time), namespace='/ddstats-bot', broadcast=True)
# finally, set game time
if str(player_id) not in player_dict:
player_dict[str(player_id)] = {}
player_dict[str(player_id)]['game_time'] = game_time
player_dict[str(player_id)]['death_type'] = death_type
player_dict[str(player_id)]['is_replay'] = is_replay
emit('receive', (game_time, gems, homing_daggers, enemies_alive,
enemies_killed, daggers_hit, daggers_fired, level_two, level_three,
level_four, is_replay, death_type), room=str(player_id),
namespace='/user_page', include_self=False, broadcast=True)
@socketio.on('game_submitted')
def game_submitted(game_id, notify_player_best, notify_above_1000):
global threshold
# print(game_id, file=sys.stdout)
game = db.session.query(Game).filter_by(id=game_id).first()
if game:
emit('game_received', (game.id, game.game_time, game.death_type, game.gems,
game.homing_daggers,
game.enemies_alive, game.enemies_killed,
game.daggers_hit, game.daggers_fired),
namespace='/user_page', room=str(game.player_id), broadcast=True)
user = db.session.query(User).filter_by(id=game.player_id).first()
if str(game.player_id) in player_dict and player_dict[str(game.player_id)]["is_replay"] == False:
if (user is not None):
if game.game_time >= threshold and notify_above_1000:
emit('threshold_submit', (user.username, game.game_time, game.death_type, game_id), namespace='/ddstats-bot', broadcast=True)
if game.game_time >= user.game_time and notify_player_best:
emit('player_best_submit', (user.username, game.game_time, game.death_type, user.game_time, game_id), namespace='/ddstats-bot', broadcast=True)
#######################################################################################################################
# end post 0.4.0
#######################################################################################################################
@socketio.on('submit', namespace='/stats')
def receive_stats(player_id, game_time, gems, homing_daggers,
enemies_alive, enemies_killed, daggers_hit,
daggers_fired, level_two, level_three, level_four,
is_replay, death_type):
if player_id is -1:
return
global player_dict
global threshold
user = db.session.query(User).filter_by(id=player_id).first()
if (str(player_id) in player_dict) and (is_replay == False):
if player_dict[str(player_id)]['game_time'] < threshold and game_time >= threshold:
emit('threshold_alert', (user.username, player_id, threshold), namespace='/ddstats-bot', broadcast=True)
# get user stats, compare to previous and current time
player_best = db.session.query(User.game_time).filter_by(id=player_id).first()
if (player_best is not None) and (is_replay == False):
player_best = player_best[0]
if str(player_id) in player_dict:
if player_dict[str(player_id)]['game_time'] < player_best and game_time >= player_best:
emit('player_best', (user.username, player_id, player_best, game_time), namespace='/ddstats-bot', broadcast=True)
# finally, set game time
if str(player_id) not in player_dict:
player_dict[str(player_id)] = {}
player_dict[str(player_id)]['game_time'] = game_time
player_dict[str(player_id)]['death_type'] = death_type
player_dict[str(player_id)]['is_replay'] = is_replay
emit('receive', (game_time, gems, homing_daggers, enemies_alive,
enemies_killed, daggers_hit, daggers_fired, level_two, level_three,
level_four, is_replay, death_type), room=str(player_id),
namespace='/user_page', include_self=False, broadcast=True)
@socketio.on('game_submitted', namespace='/stats')
def game_submitted(game_id):
global threshold
print(game_id, file=sys.stdout)
game = db.session.query(Game).filter_by(id=game_id).first()
if game:
emit('game_received', (game.id, game.game_time, game.death_type, game.gems,
game.homing_daggers,
game.enemies_alive, game.enemies_killed,
game.daggers_hit, game.daggers_fired),
namespace='/user_page', room=str(game.player_id), broadcast=True)
user = db.session.query(User).filter_by(id=game.player_id).first()
if str(game.player_id) in player_dict and player_dict[str(game.player_id)]["is_replay"] == False:
if (user is not None):
if game.game_time >= threshold:
emit('threshold_submit', (user.username, game.game_time, game.death_type, game_id), namespace='/ddstats-bot', broadcast=True)
if game.game_time >= user.game_time:
emit('player_best_submit', (user.username, game.game_time, game.death_type, user.game_time, game_id), namespace='/ddstats-bot', broadcast=True)
@socketio.on('get_status', namespace='/stats')
def get_status(player_id):
sid = db.session.query(Live.sid).filter_by(player_id=player_id).first()
if sid is not None:
emit('get_status', room=sid)
else:
emit('status', (-3))
###############
# ddstats-bot #
###############
@socketio.on('get_live_users', namespace='/ddstats-bot')
def get_live_users(channel_id):
with engine.connect() as conn:
users = conn.execute('select user.username from user inner join live on live.player_id = user.id').fetchall()
user_list = []
for user in users:
user_list.append(user[0])
if len(users) is 0:
user_list.append('No users are live.')
# emit('live_users', (user_list, channel_id))
emit('live_users', (player_dict, channel_id))
########################################################
# end socketio stuff #
########################################################
if __name__ == '__main__':
socketio.run(app, host='0.0.0.0', port=7666, debug=True)