-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
130 lines (109 loc) · 3.46 KB
/
app.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import os
from flask import Flask, render_template
from flask_socketio import SocketIO
from flask_cors import CORS
from flask import request,jsonify
from flask_socketio import SocketIO,emit
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
CORS(app,resources={r"/*":{"origins":"*"}})
socketio = SocketIO(app,cors_allowed_origins="*")
clients0 = []
clients1 = []
host0 = ""
host1 = ""
# Send HTML!
@app.route('/')
def root():
return render_template('clientHostJoin.html')
@app.route('/clientLoading')
def clientLoading():
return render_template('clientLoading.html')
@app.route('/clientPlaying')
def clientPlaying():
return render_template('clientPlaying.html')
@app.route('/clientPlaying')
def clientPlaying0():
return render_template('clientPlaying.html')
@app.route('/hostJoin')
def hostJoin():
return render_template('hostJoin.html')
@app.route('/howToPlay')
def howToPlay():
return render_template('howToPlay.html')
@app.route('/hostPlaying')
def hostPlaying():
return render_template('hostPlaying.html')
@app.route('/hostPlaying0')
def hostPlaying0():
return render_template('hostPlaying.html')
@socketio.on('disconnect')
def disconnect():
global host0
global clients0
global host1
global clients1
sid = request.sid
print(sid)
if host0 == sid:
host0 = ""
elif host1 == sid:
host1 = ""
else:
for i in clients0:
if i == sid:
clients0.remove(sid)
for i in clients1:
if i == sid:
clients1.remove(sid)
# Receive a message from the front end HTML
@socketio.on('send_message')
def message_recieved(data):
global host0
global host1
global clients0
global clients1
text = data['text']
print(text)
sid = request.sid
if text == "newP?0":
if len(clients0) < 4:
clients0.append(sid)
emit('message_from_server', {'text': "@" + str(clients0.index(sid)) + "0"}, to=sid)
else:
emit('message_from_server', {'text': "E"}, to=sid)
elif text == "newH?0":
if host0 == "":
host0 = request.sid
emit('message_from_server', {'text': "SH0"}, to=sid)
else:
emit('message_from_server', {'text': "E"}, to=sid)
if text == "newP?1":
if len(clients1) < 4:
clients1.append(sid)
emit('message_from_server', {'text': "@" + str(clients1.index(sid)) + "1"}, to=sid)
else:
emit('message_from_server', {'text': "E"}, to=sid)
elif text == "newH?1":
if host1 == "":
host1 = request.sid
emit('message_from_server', {'text': "SH1"}, to=sid)
else:
emit('message_from_server', {'text': "E"}, to=sid)
elif text[0] == 'H':
if text[2] == '0':
emit('message_from_server', {'text': text}, to=host0)
print(host0 + "sent")
return
elif text[2] == '1':
emit('message_from_server', {'text': text}, to=host1)
print(host1 + "sent")
return
elif text[0] == 'C':
if text[2] == '0':
emit('message_from_server', {'text': text}, to=clients0[int(text[1])])
elif text[2] == '1':
emit('message_from_server', {'text': text}, to=clients1[int(text[1])])
if __name__ == '__main__':
port = int(os.environ.get("PORT", 5000))
socketio.run(app, host="0.0.0.0", debug=True, allow_unsafe_werkzeug=True, port=port)