-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
197 lines (149 loc) · 5.12 KB
/
app.py
File metadata and controls
197 lines (149 loc) · 5.12 KB
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
from flask import Flask, request
from flask_restful import Resource, Api
import redis
from rq import Queue
import json
import time
import math
import os
app = Flask(__name__)
api = Api(app)
r = redis.Redis(host=os.environ['REDIS_URL'], port=6379)
q = Queue(connection=r)
def matcher(userid):
if r.get(f"user:{userid}") == None:
return False
rooms = list(r.smembers("groups"))
config_max_players = r.hmget("config", "max-players")[0].decode('utf-8')
config = {
"max-players": config_max_players
}
def createMatches():
# try to make a match for each roster in the queue
if not tryMakeMatch(userid):
# move rosters we couldn't find match for to the end of the queue
job = q.enqueue_call(func=matcher,
args=(userid,),
timeout=30,
job_id=str(userid))
def joinRoom(room):
jroom = json.loads(r.get(room.decode('utf-8')))
user_score = json.loads(r.get(f"user:{userid}"))["score"]
suma = 0
print(jroom["users"])
if jroom["users"] != None:
for i in jroom["users"]:
suma += json.loads(r.get(f"user:{i}"))["score"]
jroom["num_users"] = int(jroom["num_users"]) + 1
if jroom["users"] != None:
jroom["users"].append(userid)
else:
jroom["users"] = [userid]
jroom["avg_score"] = (suma + user_score) / jroom["num_users"]
x = {
"num_users": jroom["num_users"],
"users": jroom["users"],
"avg_score": int(jroom["avg_score"])
}
print(x)
if jroom["num_users"] >= int(config["max-players"]):
## Notificar que ya se tiene un grupo
r.srem("groups", room)
r.delete(room)
print("Se encontró un equipo completo")
return True
json_res = json.dumps(x)
r.set(room, str(json_res))
def tryMakeMatch(target):
# gather rosters that are good potential matches
potentials = gatherPotentials(target)
for room in potentials:
size_room = size = json.loads(r.get(room).decode('utf-8'))["num_users"]
if canJoinTeam(size_room):
print(">> Joined in team")
"""join team"""
joinRoom(room)
return True
return False
# CO
def gatherPotentials(target):
rooms = list(r.smembers("groups"))
potentials = []
jtarget = json.loads(r.get(f"user:{target}").decode('utf-8'))
if len(rooms) == 0:
x = {
"num_users": 0,
"users": [],
"avg_score": 0
}
json_res = json.dumps(x)
r.incr("id_user")
id_user = r.get("id_user")
r.set(f"group:{id_user}", str(json_res))
r.sadd("groups", f"group:{id_user}")
rooms = list(r.smembers("groups"))
for room in rooms:
jroom = json.loads(r.get(room))
avg_score = jroom['avg_score']
target_score = int(jtarget['score'])
# check conditions where rosters are never allowed to match
#if roster.gameModes != target.gameModes:
# continue
if avg_score > target_score + 100:
continue
if avg_score < target_score - 100:
continue
potentials.append(room)
# limit choices for performance
#if len(potentials) >= config.filter.potentials.max:
# break
# return potential rooms user can join
return potentials
def canJoinTeam(size_team):
# roster is too big for this team
if int(size_team) + 1 > int(config["max-players"]):
return False
# don't pick rosters that are much different size than what exists
# if abs(len(roster) - otherTeam.maxRosterSize) > config.rosterSize.maxDiff:
# return False
return True
""" Function that returns len(n) and simulates a delay """
createMatches()
print("Task running")
time.sleep(2)
print(f"Dequeue user {userid}")
print("Task complete")
return userid
class queue(Resource):
def post(self):
""" If userid is not in POST form then return 400 Bad Request """
# if hasattr(request,'userid'):
# return {'error': 'userid not provided'}, 400
try:
userid = request.get_json()["userid"]
except:
return {"error": "userid not provided"}, 400
""" Enqueue the user and dequeue with matcher function """
job = q.enqueue_call(func=matcher,
args=(userid,),
timeout=30,
job_id=str(userid))
return {"userid": job.id, "enqueued_at": str(job.enqueued_at)}, 200
class create(Resource):
def post(self):
try:
userid = request.get_json()["userid"]
except:
return {"error": "userid not provided"}, 400
x = {
"id": userid,
"score": 100
}
json_res = json.dumps(x)
r.set(f"user:{userid}", str(json_res))
r.sadd("users", f"user:{userid}")
return {"message": f"user {userid} created"}, 201
api.add_resource(queue, '/')
api.add_resource(create, '/create')
if __name__ == "__main__":
app.run(debug=True, host= '0.0.0.0', port=6000)