-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
172 lines (150 loc) · 4.89 KB
/
server.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import os
import random
from flask import Flask,render_template, url_for, request, session, redirect
from flask_socketio import SocketIO, send, emit, join_room, close_room
from game import game
app = Flask(__name__)
app.secret_key = os.environ.get('SECRET')
SocketIO(app,cors_allowed_origins="*")
socketio = SocketIO(app)
gameBoards = {}
userPairs = {}
@socketio.on('connect')
def handleConnect():
#assign either "X" or "O" to user based on placement in dict
#every user joins a room on their own
join_room(session["user"])
opponent = ""
for x, y in userPairs.items():
if x == session["user"]:
opponent = y
break
if y == session["user"]:
opponent = x
break
for x, y in userPairs.items():
if y == session["user"]:
emit("connect", "X "+ session["user"], room = opponent)
emit("connect", "O "+ opponent, room = session["user"])
break
print(userPairs)
#if user disconnects, close their room, take them out of userPairs
#delete their gameboard
#tell other player their opponent left
@socketio.on('disconnect')
def handleDisconnect():
try:
close_room(session["user"])
del gameBoards[session["user"]]
except Exception as e:
print(e)
for x, y in userPairs.items():
if x == session["user"]:
emit("disconnect", y, room = y)
if(y):
gameBoards[y] = game()
if(userPairs[x]):
userPairs[y] = False;
del userPairs[x]
break
y= False;
else:
del userPairs[x]
break
if y == session["user"]:
emit("disconnect", x, room = x)
gameBoards[x] = game()
userPairs[x] = False
break
print(userPairs)
print(gameBoards)
@socketio.on('victory')
def victory(msg):
#get opponent
opponent = ""
for x, y in userPairs.items():
if x == session["user"]:
opponent = y
break
if y == session["user"]:
opponent = x
break
emit("victory", msg, room = opponent)
emit("victory", msg, room = session["user"])
@socketio.on('message')
def handleMessage(msg):
#get opponent
opponent = ""
for x, y in userPairs.items():
if x == session["user"]:
opponent = y
break
if y == session["user"]:
opponent = x
break
# message sends which player made a move and to which cell
player = msg.split()[0]
cell = msg.split()[1]
boardNum = msg.split()[2]
win = 'f'
#if player wins or draw
move = game.move(gameBoards[session["user"]], player, int(cell),int(boardNum))
if(move=="win 10"):
win = 't 10'
game.reset(gameBoards[session["user"]])
elif(move.split()[0]=="win"):
win = 't '+move.split()[1]
elif(move=="draw 10"):
win = 'd'
game.reset(gameBoards[session["user"]])
if(not move=="bad 10"):
#send player, move, and if they have won
send(player+" "+cell+" "+boardNum+" "+win, room = opponent)
send(player+" "+cell+" "+boardNum+" "+win, room = session["user"])
@app.route("/", methods = ["POST","GET"])
def home():
session.clear()
#place username in session
if request.method == "POST" :
if (request.form["name"] not in userPairs) and (request.form["name"] not in userPairs.values()):
session["user"] = request.form["name"].replace(" ", "-")
# get list of users looking for a game, connect to random one
openGames = []
for x, y in userPairs.items():
if not y:
openGames.append(x)
#if there are no games, create one
if not openGames:
userPairs[session["user"]] = False;
gameBoards[session["user"]] = game()
else:
opp = random.choice(openGames)
userPairs[opp]= session["user"]
gameBoards[session["user"]] = gameBoards[opp]
return redirect(url_for("play"))
#if name is taken, stay on this page and try again
else:
return render_template("home.html",taken = "That name is currently taken")
return render_template("home.html")
@app.route("/game")
def play():
# if the username hasnt been set, return home
#if player quits a game, return home
if not session.get("user"):
return redirect(url_for("home"))
if session.get("inGame"):
return redirect(url_for("home"))
try:
user = session["user"]
session["inGame"] = True
return render_template("game.html", user=user)
except:
return redirect(url_for("home"))
if __name__ == "__main__":
app.run()
def getOpponent():
for x, y in userPairs.items():
if x == session["user"]:
return y
if y == session["user"]:
return x