-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathayaqdop.py
67 lines (52 loc) · 1.53 KB
/
ayaqdop.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
67
import os
import json
from uuid import uuid4
from datetime import datetime
from flask import Flask, request, make_response, jsonify, render_template
from flask_cors import CORS
from flask_socketio import SocketIO
app = Flask(__name__)
app.config["SECRET_KEY"] = os.environ.get('FLASK_SECRET', 'Secret!')
CORS(app, origins=["http://localhost:8000", "https://ayaqdop.com"], supports_credentials=True)
socketio = SocketIO(app, cors_allowed_origins='*', cors_credentials=True)
users = []
@app.route("/")
def home():
return render_template("index.html", users=users)
@app.route("/uuid", methods=["POST"])
def create_uuid():
id = str(uuid4())
users.append({
"id": id,
"in_game": False,
"connection_time": datetime.now()
})
response = make_response(jsonify({"uuid": id}))
return response
@app.route("/init", methods=["POST"])
def init_game():
with open("game.json", "r") as f:
game = json.load(f)
return make_response(game)
@socketio.on("connect")
def connect():
sid = request.sid
users.append({
"id": sid,
"in_game": False,
"connection_time": datetime.now()
})
socketio.emit("sid", { "sid": sid }, room=sid)
print("Connected")
@socketio.on('server')
def server(msg):
socketio.emit('client', msg)
@socketio.on('disconnect')
def disconnect():
print('Disconnected')
@socketio.on('send_moves')
def move(msg):
recipient = msg["to"]
socketio.emit("receive_moves", msg["game"], room=recipient)
if __name__ == "__main__":
socketio.run(app)