@@ -41,7 +41,6 @@ def check_params(self):
41
41
return True
42
42
43
43
def connect (self ):
44
- self .accept ()
45
44
query_string = self .scope ['query_string' ].decode ()
46
45
params = urllib .parse .parse_qs (query_string )
47
46
@@ -53,12 +52,19 @@ def connect(self):
53
52
self .channel_name
54
53
)
55
54
if self .check_params ():
55
+ self .send_error ("invalid_params" )
56
56
self .close (code = 1000 )
57
+ return
58
+ self .accept ()
57
59
instance = Profile .objects .get (nickname = self .nickname )
58
60
serializer = TournamentProfileSerializer (instance )
59
61
cache_key = f"user_{ self .tournament_id } "
60
62
created_by = False
61
63
tournament = Tournament .objects .get (id = self .tournament_id )
64
+ if tournament .is_finished :
65
+ self .send_error ("tournament_finished" )
66
+ self .close (code = 1000 )
67
+ return
62
68
if tournament .created_by == instance :
63
69
created_by = True
64
70
data = add_player_to_cache (serializer .data , cache_key , created_by )
@@ -76,11 +82,12 @@ def connect(self):
76
82
77
83
def receive (self , text_data ):
78
84
data = json .loads (text_data )
85
+ print ("data" , data )
79
86
if data ['send_type' ] == 'checkMatch' :
80
87
self .checkMatch (self .nickname , self .tournament_id )
81
88
elif data ['send_type' ] == 'start' :
82
89
self .check_start_conditions ()
83
- # self.StartTournament(data)
90
+ self .StartTournament (data )
84
91
85
92
def check_start_conditions (self ):
86
93
player = get_player_from_cache (f"user_{ self .tournament_id } " , self .nickname )
@@ -91,10 +98,6 @@ def check_start_conditions(self):
91
98
if len (players ) < 3 :
92
99
self .send_error ("invalid_tournament" )
93
100
return
94
- for player in players :
95
- if not player ['is_ready' ]:
96
- self .send_error ("players_not_ready" )
97
- return
98
101
99
102
def send_error (self , error_type ):
100
103
if error_type == "invalid_profile" :
@@ -106,7 +109,10 @@ def send_error(self, error_type):
106
109
self .send (text_data = json .dumps ({"error" : "tournament_started" , "message" : "Tournament Already Started" }))
107
110
elif error_type == "players_not_ready" :
108
111
self .send (text_data = json .dumps ({"error" : "players_not_ready" , "message" : "All players must be ready" }))
109
-
112
+ elif error_type == "invalid_params" :
113
+ self .send (text_data = json .dumps ({"error" : "invalid_params" , "message" : "Something went wrong" }))
114
+ elif error_type == "tournament_finished" :
115
+ self .send (text_data = json .dumps ({"error" : "tournament_finished" , "message" : "Tournament is finished" }))
110
116
def disconnect (self , close_code ):
111
117
async_to_sync (self .channel_layer .group_discard )(
112
118
self .room_group_name ,
@@ -129,9 +135,6 @@ def disconnect(self, close_code):
129
135
def StartTournament (self , data ):
130
136
tournament = Tournament .objects .get (id = self .tournament_id )
131
137
participants = tournament .current_participants .all ()
132
- if tournament .rounds .exists ():
133
- self .send_error ("tournament_started" )
134
- return
135
138
if tournament .current_participants .count () > 2 :
136
139
round_number = 1
137
140
round_obj = Round .objects .create (round_number = round_number )
@@ -153,6 +156,7 @@ def StartTournament(self, data):
153
156
return
154
157
game = Game .objects .create (player1 = profile1 , player2 = profile2 , tournament_id = self .tournament_id )
155
158
game_id = str (game .id )
159
+ game .tournament_id = self .tournament_id
156
160
player1_nick = str (profile1 .nickname )
157
161
player2_nick = str (profile2 .nickname )
158
162
game_info = {
@@ -169,11 +173,7 @@ def StartTournament(self, data):
169
173
170
174
def checkMatch (self , profile_id1 , tournament_id ):
171
175
try :
172
- profile_id = int (profile_id1 )
173
- except ValueError :
174
- print ("Tournament ID metin olarak beklenen türde değil." )
175
- try :
176
- tournament = Tournament .objects .get (pk = tournament_id )
176
+ tournament = Tournament .objects .get (pk = self .tournament_id )
177
177
last_round = tournament .rounds .order_by ('-round_number' ).first ()
178
178
except Tournament .DoesNotExist :
179
179
print ("Turnuva Yok" )
@@ -190,36 +190,42 @@ def checkMatch(self, profile_id1, tournament_id):
190
190
191
191
player_participated = False
192
192
for game in last_round .matches .all ():
193
- if game .player1 .id == profile_id or game .player2 .id == profile_id :
194
- print (game .player1 .id , game .player2 .id )
193
+ if game .player1 .nickname == self .nickname or game .player2 .nickname == self .nickname :
195
194
player_participated = True
196
- if game .winner is None :
197
- game .winner = game .player2
198
- game .save ()
199
- print ("Maçı Player 2 kazandı" )
200
- return
201
195
202
196
if last_round .matches .count () == 1 and not last_round .participants .exists () and last_round .matches .first ().winner :
203
197
game = last_round .matches .first ()
204
198
tournament .winner = game .winner
205
199
tournament .is_finished = True
206
200
tournament .save ()
201
+ self .send_to_group ({"winner" : game .winner .nickname , "profile_picture" : game .winner .profile_picture .url },
202
+ "tournament_finished" )
207
203
if not player_participated :
208
204
print ("Player Maclarda Yok" )
209
205
return
210
206
211
207
winners = [game .winner for game in last_round .matches .all ()]
212
208
if len (last_round .participants .all ()) == 1 :
213
209
winners .append (last_round .participants .first ())
210
+ print (winners )
214
211
new_round .participants .set (winners )
215
212
tournament .rounds .add (new_round )
216
-
213
+ all_games = []
217
214
for i in range (0 , len (winners ), 2 ):
218
215
if i + 1 < len (winners ):
219
- game = Game .objects .create (player1 = winners [i ], player2 = winners [i + 1 ])
216
+ game = Game .objects .create (player1 = winners [i ], player2 = winners [i + 1 ], tournament = tournament )
217
+ game_id = str (game .id )
218
+ game .tournament_id = self .tournament_id
219
+ player1_nick = str (winners [i ].nickname )
220
+ player2_nick = str (winners [i + 1 ].nickname )
221
+ game_info = {
222
+ "game_id" : game_id ,
223
+ "players" : [player1_nick , player2_nick ]
224
+ }
225
+ all_games .append (game_info )
220
226
new_round .matches .add (game )
221
227
new_round .participants .remove (winners [i ])
222
228
new_round .participants .remove (winners [i + 1 ])
223
229
new_round .save ()
224
- print ( "Yeni Turnuva Oluşturuldu " )
230
+ self . send_to_group ( all_games , "game_info " )
225
231
return
0 commit comments