-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
163 lines (131 loc) · 3.67 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
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
from flask import Flask
from flask import send_file
from flask import redirect
from flask import request
import os
from flask import Flask, render_template
from flask_socketio import SocketIO
from flask_socketio import send, emit
app = Flask(__name__)
app.secret_key = os.environ.get("SECRET")
app.config['WTF_CSRF_SECRET_KEY'] = "b'\xc72\x7f\xfb\xc1\xa4\xc6Y\xc84\xe8\xfcf\xf5\xdb\x12'"
socketio = SocketIO(app, manage_session=False)
color = 0
users = []
row = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
board = []
step_by_step = []
boards = []
from copy import deepcopy
for i in range(0, 15):
board.append(deepcopy(row))
for i in range(0,3):
boards.append(deepcopy(board))
# socket start
# connection
@socketio.on('connected', namespace='/socket')
def connected(data):
sid = request.sid
users.append(sid)
emit('sid', sid)
emit('init', [step_by_step,board])
print(board)
print(users)
@socketio.on('disconnect', namespace='/socket')
def disconnect():
users.remove(request.sid)
print(request.sid, "disconnected")
print("Remaaining: ",users)
# connection end
# processor start
def iswin(x,y):
global board
print(board)
count=max(search(x,y,[1,0])+search(x,y,[-1,0]),search(x,y,[0,1])+search(x,y,[0,-1]),search(x,y,[1,1])+search(x,y,[-1,-1]),search(x,y,[1,-1])+search(x,y,[-1,1]))
print(count)
if count>=6:
print("win")
return True
def search(x,y, position):
global board
global color
count = 1
for i in range(1, 5):
try:
status = board[x + i*position[0]][y+i*position[1]]
except IndexError:
status = -1
if status == color:
count += 1
else:
print(str(count)+str(position))
return count
print(str(count) + str(position))
return count
@socketio.on('go', namespace='/socket')
def go(place):
global color
global step_by_step
print(place)
x = place['x']
y = place['y']
global board
if [x, y] in step_by_step:
return
board[x][y] = color
step_by_step.append([x,y])
for i in users:
emit('step', place, room=i)
if iswin(x,y):
if color==0:
co="black"
else:
co="white"
for i in users:
emit('win', co, room=i)
print(step_by_step)
color = (color + 1) % 2
@socketio.on('reset', namespace='/socket')
def reset(password):
row = [-1, -1, -1, -1, -1,-1, -1, -1, -1, -1,-1, -1, -1, -1, -1]
global board, step_by_step, color
color=0
step_by_step=[]
board = []
for i in range(0, 15):
board.append(deepcopy(row))
for i in users:
emit('init', [step_by_step,board],room=i)
emit('init', [step_by_step,board],room=i)#do not delete this line or you will see weird thing, trust me
print(board)
print('reset')
@socketio.on('regret', namespace='/socket')
def regret(place, board_id):
print("regret on board", board_id)
global color
x=place[0]
y=place[1]
if place == step_by_step[-1]:
print("regret on "+str(step_by_step[-1]))
for i in users:
emit('regret',step_by_step[-1],room=i)
step_by_step.pop()
board[x][y]=-1
color = (color + 1) % 2
print(board)
# processor end
# static files start
@app.route('/')
def index():
return send_file("static/index.html")
@app.route('/<path:path>')
def statics(path):
try:
return send_file("static/" + path)
except IOError:
try:
return send_file("static/" + path + ".html")
except IOError:
pass
if __name__ == "__main__":
socketio.run(app, port=80, allow_unsafe_werkzeug=True)