fixed tournament byes

This commit is contained in:
CHIBOUB Chakib 2024-09-10 16:49:10 +02:00
parent a64fadf103
commit 8ad33f8acb
3 changed files with 15 additions and 13 deletions

View File

@ -27,9 +27,10 @@ class Game:
'game_text': '' 'game_text': ''
} }
else: else:
self.botgame = player2 is None # Set botgame to True if either player1 or player2 is None
self.botgame = player1 is None or player2 is None
self.game_state = { self.game_state = {
'player1_name': player1.user.username, 'player1_name': player1.user.username if player1 else 'BOT',
'player2_name': player2.user.username if player2 else 'BOT', 'player2_name': player2.user.username if player2 else 'BOT',
'player1_position': 150, 'player1_position': 150,
'player2_position': 150, 'player2_position': 150,

View File

@ -1,4 +1,4 @@
<!-- pong/tournament_waiting_room.html --> jik<!-- pong/tournament_waiting_room.html -->
<div class="tournament-waiting-room"> <div class="tournament-waiting-room">
<h2>Tournament Waiting Room</h2> <h2>Tournament Waiting Room</h2>

View File

@ -21,7 +21,8 @@ class TournamentMatch(Game):
await super().end_game(disconnected_player) await super().end_game(disconnected_player)
# Handle the end of the match in the tournament context # Handle the end of the match in the tournament context
await self.tournament.handle_match_end(self) await self.tournament.handle_match_end(self)
del match_maker.active_games[self.game_id] if self.game_id in match_maker.active_games:
del match_maker.active_games[self.game_id]
class TournamentMatchMaker: class TournamentMatchMaker:
def __init__(self): def __init__(self):
@ -51,10 +52,10 @@ class TournamentMatchMaker:
def generate_waiting_room_html(self): def generate_waiting_room_html(self):
context = { context = {
'players': [player.user.username if player else 'BOT' for player in self.waiting_players], 'players': [player.user.username if player else 'BYE' for player in self.waiting_players],
'tournament_state': self.tournament_state, 'tournament_state': self.tournament_state,
'players_count': len(self.waiting_players), 'players_count': len(self.waiting_players),
'min_players_to_start': 2 # You can adjust this number as needed 'min_players_to_start': 3 # You can adjust this number as needed
} }
return render_to_string('pong/tournament_waiting_room.html', context) return render_to_string('pong/tournament_waiting_room.html', context)
@ -139,8 +140,8 @@ class TournamentMatchMaker:
return [ return [
[ [
{ {
'player1': match.player1.user.username if match.player1 else 'BOT', 'player1': match.player1.user.username if match.player1 else 'BYE',
'player2': match.player2.user.username if match.player2 else 'BOT', 'player2': match.player2.user.username if match.player2 else 'BYE',
'winner': match.game_state['player1_name'] if match.game_state['player1_score'] > match.game_state['player2_score'] else match.game_state['player2_name'] if match.ended else None, 'winner': match.game_state['player1_name'] if match.game_state['player1_score'] > match.game_state['player2_score'] else match.game_state['player2_name'] if match.ended else None,
'score1': match.game_state['player1_score'], 'score1': match.game_state['player1_score'],
'score2': match.game_state['player2_score'] 'score2': match.game_state['player2_score']
@ -159,18 +160,18 @@ class TournamentMatchMaker:
elif match.player1: elif match.player1:
# Handle BYE match # Handle BYE match
await match_maker.notify_players(match.player1, match.player2, match.game_id, False) await match_maker.notify_players(match.player1, match.player2, match.game_id, False)
asyncio.create_task(match.start_game()) #asyncio.create_task(match.start_game())
'''match.game_state['player1_score'] = 3 match.game_state['player1_score'] = 3
match.game_state['player2_score'] = 0 match.game_state['player2_score'] = 0
await match.end_game()''' await match.end_game()
def get_round_winners(self): def get_round_winners(self):
winners = [] winners = []
for match in self.rounds[-1]: for match in self.rounds[-1]:
if match.ended: if match.ended:
winner = match.player1 if match.game_state['player1_score'] > match.game_state['player2_score'] else match.player2 winner = match.player1 if match.game_state['player1_score'] > match.game_state['player2_score'] else match.player2
if winner: #if winner:
winners.append(winner) winners.append(winner)
return winners return winners
async def end_tournament(self, winner): async def end_tournament(self, winner):