-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflask-test.py
106 lines (83 loc) · 2.81 KB
/
flask-test.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
import flask, redis, time, threading
from flask import jsonify, request
r = redis.Redis(host='localhost', port=6379, db=0)
app = flask.Flask(__name__)
app.config["DEBUG"] = True
@app.route('/messages/sent/', methods=['GET', 'POST'])
def sendMessage():
username = request.form['username']
message = request.form['message']
room = request.form['roomname']
value = {
"username": username,
"message": message
}
roomname = "room:" + room
r.xadd(roomname, value)
r.zincrby("leaderboard", 1, username)
return '1'
@app.route('/messages/readLast/<roomname>', methods=['GET', 'POST'])
def readLastMessage(roomname):
roomname = "room:" + roomname
value = {
roomname: "0-0"
}
readdata = r.xread(value)
streamLength = len(readdata[0][1])
lastmessagedict = {}
messagesDict = {}
# pojedyncza wiadomosc
lastmessagedict = readdata[0][1][streamLength-1][1]
decoded = {k.decode('utf8'): v.decode('utf8') for k, v in lastmessagedict.items()}
return jsonify(decoded)
@app.route('/messages/readAll/<roomname>', methods=['GET'])
def readAllMessages(roomname):
roomname = "room:" + roomname
value = {
roomname: "0-0"
}
readdata = r.xread(value)
streamLength = len(readdata[0][1])
lastmessagedict = {}
messagesDict = {}
messagesDict2 = {}
messagesList = []
# wszyskie wiadomosci
x = 0
for x in range(streamLength):
lastmessagedict = readdata[0][1][x][1]
decoded = {k.decode('utf8'): v.decode('utf8') for k, v in lastmessagedict.items()}
messagesList.append(decoded)
messagesDict2['messages'] = messagesList
return jsonify(messagesDict2)
@app.route('/leaderboard/', methods=['GET'])
def displayLeaderboard():
readdata = r.zrevrange("leaderboard", 0, -1, withscores = True)
leaderboardDict = {}
leaderboardList = []
x = 0
for x in range(len(readdata)):
leaderboardList.append({ 'userName' : readdata[x][0].decode('utf8'), 'score' : readdata[x][1] })
# leaderboardDict[x] = { 'userName' : readdata[x][0].decode('utf8'), 'score' : readdata[x][1] }
leaderboardDict['leaderboard'] = leaderboardList
return jsonify(leaderboardDict)
StartTime=time.time()
class setInterval :
def __init__(self,interval,action) :
self.interval=interval
self.action=action
self.stopEvent=threading.Event()
thread=threading.Thread(target=self.__setInterval)
thread.start()
def __setInterval(self) :
nextTime=time.time()+self.interval
while not self.stopEvent.wait(nextTime-time.time()) :
nextTime+=self.interval
self.action()
def cancel(self) :
self.stopEvent.set()
def dumpDatabase():
save = r.bgsave()
return save
inter = setInterval(30,dumpDatabase)
app.run()