Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -68,12 +68,14 @@ def handle_leave(data):
|
|
68 |
emit("status", {"msg": "Partner left."}, room=other)
|
69 |
leave_room(room, sid=other)
|
70 |
ROOMS.pop(room)
|
|
|
71 |
def try_match():
|
72 |
import random
|
73 |
random.shuffle(WAITING) # To ensure fairness
|
74 |
used = set()
|
75 |
pairs = []
|
76 |
|
|
|
77 |
for i, sid1 in enumerate(WAITING):
|
78 |
if sid1 in used: continue
|
79 |
p1 = PROFILES.get(sid1)
|
@@ -93,11 +95,31 @@ def try_match():
|
|
93 |
# Interests matching
|
94 |
if set(p1['interests']) & set(p2['interests']):
|
95 |
pairs.append((sid1, sid2))
|
96 |
-
used.
|
97 |
-
used.add(sid2)
|
98 |
break
|
99 |
|
100 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
for sid1, sid2 in pairs:
|
102 |
for sid in (sid1, sid2):
|
103 |
if sid in WAITING:
|
@@ -107,6 +129,7 @@ def try_match():
|
|
107 |
ROOMS[room] = (sid1, sid2)
|
108 |
join_room(room, sid=sid1)
|
109 |
join_room(room, sid=sid2)
|
|
|
110 |
emit("matched", {
|
111 |
"room": room,
|
112 |
"my_id": PROFILES[sid1]["id"],
|
@@ -121,7 +144,6 @@ def try_match():
|
|
121 |
"my_sid": sid2,
|
122 |
"partner_id": PROFILES[sid1]["id"],
|
123 |
"partner_sid": sid1
|
124 |
-
}, room=sid2)
|
125 |
-
|
126 |
if __name__ == "__main__":
|
127 |
socketio.run(app, host="0.0.0.0", port=7860)
|
|
|
68 |
emit("status", {"msg": "Partner left."}, room=other)
|
69 |
leave_room(room, sid=other)
|
70 |
ROOMS.pop(room)
|
71 |
+
|
72 |
def try_match():
|
73 |
import random
|
74 |
random.shuffle(WAITING) # To ensure fairness
|
75 |
used = set()
|
76 |
pairs = []
|
77 |
|
78 |
+
# === FIRST: Try matching by interest ===
|
79 |
for i, sid1 in enumerate(WAITING):
|
80 |
if sid1 in used: continue
|
81 |
p1 = PROFILES.get(sid1)
|
|
|
95 |
# Interests matching
|
96 |
if set(p1['interests']) & set(p2['interests']):
|
97 |
pairs.append((sid1, sid2))
|
98 |
+
used.update([sid1, sid2])
|
|
|
99 |
break
|
100 |
|
101 |
+
# === SECOND: Fallback to random match (respect gender) ===
|
102 |
+
for i, sid1 in enumerate(WAITING):
|
103 |
+
if sid1 in used: continue
|
104 |
+
p1 = PROFILES.get(sid1)
|
105 |
+
if not p1: continue
|
106 |
+
for j in range(i + 1, len(WAITING)):
|
107 |
+
sid2 = WAITING[j]
|
108 |
+
if sid2 in used: continue
|
109 |
+
p2 = PROFILES.get(sid2)
|
110 |
+
if not p2: continue
|
111 |
+
|
112 |
+
# Gender filtering only
|
113 |
+
if p1['looking'] != 'any' and p2['gender'].lower() != p1['looking'].lower():
|
114 |
+
continue
|
115 |
+
if p2['looking'] != 'any' and p1['gender'].lower() != p2['looking'].lower():
|
116 |
+
continue
|
117 |
+
|
118 |
+
pairs.append((sid1, sid2))
|
119 |
+
used.update([sid1, sid2])
|
120 |
+
break
|
121 |
+
|
122 |
+
# === Finalize pairs and assign rooms ===
|
123 |
for sid1, sid2 in pairs:
|
124 |
for sid in (sid1, sid2):
|
125 |
if sid in WAITING:
|
|
|
129 |
ROOMS[room] = (sid1, sid2)
|
130 |
join_room(room, sid=sid1)
|
131 |
join_room(room, sid=sid2)
|
132 |
+
|
133 |
emit("matched", {
|
134 |
"room": room,
|
135 |
"my_id": PROFILES[sid1]["id"],
|
|
|
144 |
"my_sid": sid2,
|
145 |
"partner_id": PROFILES[sid1]["id"],
|
146 |
"partner_sid": sid1
|
147 |
+
}, room=sid2)
|
|
|
148 |
if __name__ == "__main__":
|
149 |
socketio.run(app, host="0.0.0.0", port=7860)
|