-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatchmaking_ping.py
66 lines (48 loc) · 1.87 KB
/
matchmaking_ping.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
import flask
import util
logged_in_users = []
next_game_users = []
users_notified = [0, 0]
next_game_id = [-1, 0]
def already_in_array(new_uid):
for uid in logged_in_users:
if uid == new_uid:
return True
return False
def matchmaking_ping(uid):
print("UID " + uid + " is looking for a match")
ready = 0
partner = ""
game_id = -1
# If this user isn't already in the array of logged in users, add them
if (not uid in logged_in_users) and (not uid in next_game_users):
logged_in_users.append(uid)
# Store the pair of users we need to notify
if len(logged_in_users) >= 2 and len(next_game_users) == 0:
next_game_users.append(logged_in_users[0])
next_game_users.append(logged_in_users[1])
del logged_in_users[0:2]
print("Pairing UIDs " + str(next_game_users))
if len(next_game_users) != 0:
# TODO: no clue why this has to be an array but it's 3:30am so prolly something dumb
if next_game_id[0] == -1:
next_game_id[0] = util.get_next_game_id()
for i, matched_uid in enumerate(next_game_users):
if uid == matched_uid:
# Notify this user that they have been matched
ready = 1
partner = util.get_username(next_game_users[1 - i]) #Hacky way to get the other element since we know there are only 2
users_notified[i] = 0
game_id = next_game_id[0]
# If both players have been notified, reset matchmaking stuff
# TODO: maybe put this after response has been sent?
if users_notified[0] == 1 and users_notified[1] == 1:
next_game_users.clear()
users_notified[0] = 0
users_notified[1] = 0
response = {
"ready": ready,
"partner": partner,
"game_id": game_id
}
return flask.jsonify(response)