So I've been trying to make a queueing system for an omegle clone. Here, there is an empty "queue" dict. Whenever there is post request, i.e someone clicked on the CHAT button, one of 2 things can happen.
(1) If the queue dictionary is empty, create a random code, create a new item in queue, and save the info via sessions. After that, display a waiting screen (waiting.html). And check in a While loop if there are 2 members, in that case, redirect to /chat.
(2) If the queue dictionary is NOT empty, the code will be the first item in the dictionary's key, which is a random 4 digit code, append your name in the member's list of the item, and save all the stuff via sessions. After that, if the number of members (len(members_list)) is 2, then redirect to /chat
Here's the code:
from flask import Flask, render_template, request, session, redirect, url_for
from flask_socketio import join_room, leave_room, emit, SocketIO
import random
from string import ascii_uppercase
import time
app = Flask(__name__)
app.config["SECRET_KEY"] = "verrriii_Sekretttt_kIeyeee__:DDDD"
socketio = SocketIO(app)
queue = {}
@app.route('/', methods=["GET", "POST"])
def home():
session.clear()
if request.method == "POST":
name = request.form.get("name")
favColor = request.form.get("favColor")
submit = request.form.get("submit", False)
#check if there is all the information
if not name:
return render_template("home.html",
error="GIVE MOTHERLAND OUR NAME, COMRADE!",
name=name)
if favColor == "select":
return render_template(
"home.html",
error="GIVE MOTHERLAND YOUR COLOR OF CHOICE, COMRADE!",
name=name)
#on submitting / on post request
code = "".join(random.choices(ascii_uppercase, k=4))
#the important part
if len(queue) == 0:
queue[code] = {"members": [name]}
session["name"] = name
session["favColor"] = favColor
session["code"] = code
while len(queue[code]["members"]) == 1:
return render_template("waiting.html")
else:
return redirect(url_for("chat"))
else:
code = next(iter(queue))
queue[code]["members"].append(name)
session["name"] = name
session["favColor"] = favColor
session["code"] = code
if len(queue[code]["members"]) == 2:
return redirect(url_for("chat"))
else:
return render_template("waiting.html")
return render_template("home.html")
#This part is not required for the question
@app.route('/chat', methods=["GET", "POST"])
def chat():
name = session.get("name")
favColor = session.get("favColor")
code = session.get("code")
if name is None or code is None or favColor is None or code not in queue:
return redirect(url_for("home"))
return render_template("chat.html")
if __name__ == "__main__":
socketio.run(app, host='0.0.0.0', port=81, debug=True)
base.html (use it as a base for other docs, by injecting them into base.html via jenga)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{{url_for('static', filename='css/style.css')}}">
<!-- <link rel="icon" type="image/x-icon" href="{{url_for('static', filename='favicon.ico')}}"> -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.js" integrity="sha512-q/dWJ3kcmjBLU4Qc47E4A9kTB4m3wuTY7vkFJDTZKjTs8jhyGQnaUrxa0Ytd0ssMZhbNua9hE+E7Qv1j+DyZwA==" crossorigin="anonymous"></script>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Nunito:wght@700;900&family=Rubik+Mono+One&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@700&display=swap" rel="stylesheet">
<script src="https://kit.fontawesome.com/50ee6035c0.js" crossorigin="anonymous"></script>
{% block title %}{% endblock %}
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>
home.html (home page):
{% extends 'base.html' %}
{% block title %}
<title>Omwegle • Soviet Omegle</title>
{% endblock %}
{% block content %}
<div class="wrapper">
<div class="branding">
<h1 class="logo">OM<span class="red">WE</span>GLE</h1>
<h5 class="slogan">Soviet Omegle</h5>
<h5 class="slogan">Коммунизм будет процветать!</h5>
</div>
<div class="container">
<form method="post" autocomplete="off">
<div class="info">
<h5 class="label">TELL MOTHERLAND ABOUT <span class="red">WE</span></h5>
<input type="text" class="name" value="{{name}}" name="name" placeholder="Tell Motherland our Name">
<select name="favColor" id="favColor">
<option value="select">Select Color</option>
<option value="#ff0000">Soviet Red</option>
<option value="#ff2400">Stalin Red</option>
<option value="#ffd800">Soviet Yellow</option>
<option value="#ffc40c">Stalin Yellow</option>
</select>
<br><br>
<button class="submit" type="submit" name="submit" id="submit">SPEAK WITH FELLOW COMRADES</button>
</div>
</form>
</div>
{% if error %}
<div class="errors">
<h2 class="errors_text">{{error}}</h2>
</div>
{% endif %}
</div>
{% endblock %}
waiting.html (waiting page):
{% extends 'base.html' %}
{% block title %}
<title>Omwegle • Soviet Omegle</title>
{% endblock %}
{% block content %}
<div class="wrapper">
<div class="branding">
<h1 class="logo">OM<span class="red">WE</span>GLE</h1>
<h5 class="slogan">Soviet Omegle</h5>
<h5 class="slogan">Коммунизм будет процветать!</h5>
</div>
<div class="container">
<h1 class="loading">SEARCHING FOR FELLOW COMRADES...</h1>
</div>
</div>
{% endblock %}
Right now, my chat.html is just a blank page.
First to check if the number of members are 2, I used an IF statement in the if len(queue) == 0: block. Later when it didn't work, i used the while loop. ITS STILL NOT WORKING