-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
154 lines (125 loc) · 5.31 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
import time
from socketio import *
from gevent import pywsgi
import GameClass
import copy
server = Server(async_mode='gevent')
All_mohre = [('Black', 'Tall', 'ToPor', 'Circular'),
('Black', 'Tall', 'ToPor', 'Squerness'),
('Black', 'Tall', 'ToKhali', 'Circular'),
('Black', 'Tall', 'ToKhali', 'Squerness'),
('Black', 'Short', 'ToPor', 'Circular'),
('Black', 'Short', 'ToPor', 'Squerness'),
('Black', 'Short', 'ToKhali', 'Circular'),
('Black', 'Short', 'ToKhali', 'Squerness'),
('White', 'Tall', 'ToPor', 'Circular'),
('White', 'Tall', 'ToPor', 'Squerness'),
('White', 'Tall', 'ToKhali', 'Circular'),
('White', 'Tall', 'ToKhali', 'Squerness'),
('White', 'Short', 'ToPor', 'Circular'),
('White', 'Short', 'ToPor', 'Squerness'),
('White', 'Short', 'ToKhali', 'Circular'),
('White', 'Short', 'ToKhali', 'Squerness')]
all_mohre_backup = copy.deepcopy(All_mohre)
occupied_positions = []
players_info = {}
players_id = []
id = 0
startGame = 0
def printBoard(board):
for i in board:
for j in i:
print(j, end=' ')
print()
@server.event
def connect(sid, environ, auth):
print(sid, "connected!")
players_id.append(sid)
@server.event
def startGame(sid, data): # catchs players info list
global startGame
players_info[sid] = data
if len(players_info) == 2:
startGame = 1
server.emit('show_partner_info', data=players_info[players_id[1]], room=players_id[0])
server.emit('show_partner_info', data=players_info[players_id[0]], room=players_id[1])
GameClass.mohre_giri(server,players_id[0])
server.emit('show_message', data='---> Wait for your partner to choose the mohre\n(Thanks for your patience :))', room=players_id[1])
GameClass.addPlayerToScoreBoard(players_info[players_id[0]][0], players_info[players_id[1]][0])
@server.event
def mohreGir(sid, data): # catchs player mohre and shows mohre to other player
if id == 0 and startGame == 1:
GameClass.makan_giri(server, data, players_id[1])
elif id == 1 and startGame == 1:
GameClass.makan_giri(server, data, players_id[0])
@server.event
def mohre_pardazesh(sid,data):
global All_mohre
if id == 0 and startGame == 1:
All_mohre.remove(tuple(data))
GameClass.makan_giri(server, data, players_id[1])
elif id == 1 and startGame == 1:
All_mohre.remove(tuple(data))
GameClass.makan_giri(server, data, players_id[0])
@server.event
def makanGir(sid, data): # pardazesh mohre\makan ersal update
global occupied_positions, id, startGame, temp , All_mohre
print(f'{data} is hereeee !!')
print(f'id is {id}')
occupied_positions.append(data)
if id == 0 and startGame == 1:
GameClass.makan_pardazesh(data, players_info[players_id[1]][2])
print(f'{data} sent to makan pardazesh in GameClass')
GameClass.send_updates(server, All_mohre, occupied_positions)
print('Updates are cominggg ...')
printBoard(GameClass.board_for_check)
if GameClass.win_check(GameClass.board_for_check) == True:
print('win check is running')
server.emit('show_message',data=f'{players_info[players_id[0]][0]} win !')
startGame = 0
players_info.clear()
server.emit('new_game',data=0)
#end of game
players_info.clear()
occupied_positions.clear()
All_mohre = copy.deepcopy(all_mohre_backup)
else:
print('win check is failed')
GameClass.mohre_giri(server, players_id[1])
print('mohre giri is runing')
id = 1
elif id == 1 and startGame == 1:
GameClass.makan_pardazesh(data, players_info[players_id[0]][2])
print(f'{data} sent to makan pardazesh in GameClass')
GameClass.send_updates(server, All_mohre, occupied_positions)
print('Updates are cominggg ...')
printBoard(GameClass.board_for_check)
if GameClass.win_check(GameClass.board_for_check) == True:
print('win check is running')
server.emit('show_message',data=f'{players_info[players_id[1]][0]} win !')
startGame = 0
players_info.clear()
server.emit('new_game', data=0)
#end of game
players_info.clear()
occupied_positions.clear()
All_mohre = copy.deepcopy(all_mohre_backup)
else:
print('win check is failed')
GameClass.mohre_giri(server, players_id[0])
print('mohre giri is runing')
id = 0
occupied_positions.append(data)
@server.event
def exitAndSave(sid, data):
if data == 'Exit':
server.emit('show_message', data=f'---> {players_info[sid][0]} left the game')
GameClass.Save()
elif data == 'New Game':
server.emit('show_message', data=f'---> {players_info[sid][0]} wants to play a new game')
@server.event
def disconnect(sid):
print('disconnect ', sid)
server.disconnect(sid)
app = WSGIApp(server)
pywsgi.WSGIServer(("127.0.0.1", 5000), app).serve_forever()