-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
64 lines (53 loc) · 1.55 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
from flask import Flask, redirect, render_template, request
from flask_socketio import SocketIO, emit
import uuid
import random
app = Flask(__name__)
socketio = SocketIO(app)
GAME_ID_LENGTH=6
games = {}
def genID(length):
chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"
ID = ""
for i in range(length):
ID += random.choice(chars)
return ID
@app.route("/")
def root():
return render_template('index.html')
@app.route("/newgame")
def newGame():
ID = genID(GAME_ID_LENGTH)
games[ID] = {"players":{},"data":{}}
return redirect("/games/" + ID)
@app.route("/games/<game_ID>")
def gameRoom(game_ID):
if game_ID in games:
return render_template("game.html", gameID = game_ID)
else:
return render_template("notfound.html", gameID = game_ID)
@socketio.on("joinGame")
def join_Game(data):
print request.sid + " joined game"
if request.sid not in games[data["id"]]["players"]:
newPlayer = {}
newPlayer["username"] = data["name"]
newPlayer["score"] = 0
games[data["id"]]["players"][request.sid] = newPlayer
for user in games[data["id"]]["players"]:
emit("update",games[data["id"]]["players"], room=user)
print games
@socketio.on("connect")
def con():
print "connected"
@socketio.on("disconnect")
def disc():
print request.sid + " left game"
for game in games:
if request.sid in games[game]["players"]:
games[game]["players"].pop(request.sid,None)
for user in games[game]["players"]:
emit("update",games[game]["players"], room=user)
print games
if __name__ == "__main__":
socketio.run(app,port=5001, debug=True)