Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import uuid
|
| 2 |
+
time
|
| 3 |
+
from flask import Flask, render_template, request, redirect, url_for, session
|
| 4 |
+
from flask_socketio import SocketIO, join_room, leave_room, emit
|
| 5 |
+
|
| 6 |
+
app = Flask(__name__)
|
| 7 |
+
app.secret_key = 'replace-with-secure-key'
|
| 8 |
+
socketio = SocketIO(app, cors_allowed_origins='*')
|
| 9 |
+
|
| 10 |
+
# In-memory storage (reset when container restarts)
|
| 11 |
+
PEOPLE = {} # sid -> {id, gender, age, interests:list}
|
| 12 |
+
WAITING = [] # list of sids waiting for match
|
| 13 |
+
ROOMS = {} # room_id -> (sid1, sid2)
|
| 14 |
+
|
| 15 |
+
@app.route('/', methods=['GET', 'POST'])
|
| 16 |
+
def index():
|
| 17 |
+
if request.method == 'POST':
|
| 18 |
+
# Create profile
|
| 19 |
+
profile = {
|
| 20 |
+
'id': request.form['id'],
|
| 21 |
+
'gender': request.form['gender'],
|
| 22 |
+
'age': request.form['age'],
|
| 23 |
+
'interests': request.form.getlist('interests')
|
| 24 |
+
}
|
| 25 |
+
session['profile'] = profile
|
| 26 |
+
return render_template('index.html', waiting=True)
|
| 27 |
+
return render_template('index.html', waiting=False)
|
| 28 |
+
|
| 29 |
+
@socketio.on('connect')
|
| 30 |
+
def on_connect():
|
| 31 |
+
# assign user sid
|
| 32 |
+
if 'profile' not in session:
|
| 33 |
+
return False
|
| 34 |
+
sid = request.sid
|
| 35 |
+
PEOPLE[sid] = session['profile']
|
| 36 |
+
WAITING.append(sid)
|
| 37 |
+
emit('status', {'msg': 'Finding partner...'}, room=sid)
|
| 38 |
+
try_match()
|
| 39 |
+
|
| 40 |
+
@socketio.on('disconnect')
|
| 41 |
+
def on_disconnect():
|
| 42 |
+
sid = request.sid
|
| 43 |
+
# remove from waiting
|
| 44 |
+
if sid in WAITING:
|
| 45 |
+
WAITING.remove(sid)
|
| 46 |
+
# remove from PEOPLE
|
| 47 |
+
PEOPLE.pop(sid, None)
|
| 48 |
+
# tear down any room
|
| 49 |
+
for room, pair in list(ROOMS.items()):
|
| 50 |
+
if sid in pair:
|
| 51 |
+
other = pair[1] if pair[0]==sid else pair[0]
|
| 52 |
+
emit('status', {'msg': 'Partner left.'}, room=other)
|
| 53 |
+
leave_room(room)
|
| 54 |
+
ROOMS.pop(room)
|
| 55 |
+
|
| 56 |
+
@socketio.on('chat')
|
| 57 |
+
def on_chat(data):
|
| 58 |
+
room = data['room']
|
| 59 |
+
text = data['msg']
|
| 60 |
+
emit('chat', {'msg': text}, room=room)
|
| 61 |
+
|
| 62 |
+
@socketio.on('leave')
|
| 63 |
+
def on_leave(data):
|
| 64 |
+
room = data['room']
|
| 65 |
+
sid = request.sid
|
| 66 |
+
# confirm leave
|
| 67 |
+
emit('left', {}, room=sid)
|
| 68 |
+
# notify partner
|
| 69 |
+
other = [s for s in ROOMS[room] if s != sid][0]
|
| 70 |
+
emit('status', {'msg': 'Partner left.'}, room=other)
|
| 71 |
+
leave_room(room)
|
| 72 |
+
ROOMS.pop(room, None)
|
| 73 |
+
|
| 74 |
+
def try_match():
|
| 75 |
+
# match waiting users
|
| 76 |
+
while len(WAITING) >= 2:
|
| 77 |
+
s1 = WAITING.pop(0)
|
| 78 |
+
# find interest match
|
| 79 |
+
prefs1 = set(PEOPLE[s1]['interests'])
|
| 80 |
+
# scan for s2
|
| 81 |
+
idx = 0; found = None
|
| 82 |
+
for i, s2 in enumerate(WAITING):
|
| 83 |
+
if prefs1 & set(PEOPLE[s2]['interests']):
|
| 84 |
+
found = i; break
|
| 85 |
+
if found is None:
|
| 86 |
+
# no common interest: pick random
|
| 87 |
+
idx = 0; s2 = WAITING.pop(0)
|
| 88 |
+
else:
|
| 89 |
+
idx = found; s2 = WAITING.pop(found)
|
| 90 |
+
room = str(uuid.uuid4())
|
| 91 |
+
ROOMS[room] = (s1, s2)
|
| 92 |
+
join_room(room, sid=s1)
|
| 93 |
+
join_room(room, sid=s2)
|
| 94 |
+
# notify both
|
| 95 |
+
emit('matched', {'room': room}, room=s1)
|
| 96 |
+
emit('matched', {'room': room}, room=s2)
|
| 97 |
+
|
| 98 |
+
if __name__ == '__main__':
|
| 99 |
+
socketio.run(app, host='0.0.0.0', port=7860)
|