mirror of
https://github.com/AudebertAdrien/ft_transcendence.git
synced 2026-11-09 18:37:07 +01:00
tournament working but still some problems
This commit is contained in:
@@ -49,7 +49,7 @@ class Game:
|
||||
self.start_time = datetime.now()
|
||||
|
||||
async def start_game(self):
|
||||
print(f"- Game #{self.game_id} STARTED")
|
||||
print(f"- Game #{self.game_id} STARTED ({self.game_state['player1_name']} vs {self.game_state['player2_name']})")
|
||||
self.game_loop_task = asyncio.create_task(self.game_loop())
|
||||
print(f" Begin MATCH at: {self.start_time}")
|
||||
|
||||
|
||||
@@ -7,78 +7,82 @@
|
||||
<style>
|
||||
.tournament-bracket {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
justify-content: space-around;
|
||||
padding: 20px;
|
||||
font-family: Arial, sans-serif;
|
||||
overflow-x: auto;
|
||||
}
|
||||
.round {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: space-around;
|
||||
width: 200px;
|
||||
margin-right: 40px;
|
||||
}
|
||||
.match {
|
||||
margin: 10px 0;
|
||||
border: 1px solid #ccc;
|
||||
border-radius: 5px;
|
||||
overflow: hidden;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
.player {
|
||||
padding: 5px;
|
||||
background-color: #f0f0f0;
|
||||
border-bottom: 1px solid #ccc;
|
||||
width: 150px;
|
||||
height: 30px;
|
||||
line-height: 30px;
|
||||
background-color: #000000;
|
||||
border: 1px solid #ffffff;
|
||||
margin: 2px 0;
|
||||
padding: 0 10px;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
.player.winner {
|
||||
background-color: #00ff3c;
|
||||
font-weight: bold;
|
||||
background-color: #e6ffe6;
|
||||
}
|
||||
.player.loser {
|
||||
color: #999;
|
||||
}
|
||||
.score {
|
||||
float: right;
|
||||
font-weight: normal;
|
||||
}
|
||||
.connector {
|
||||
border-right: 2px solid #ccc;
|
||||
height: 50px;
|
||||
position: relative;
|
||||
left: 200px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
}
|
||||
.connector:before, .connector:after {
|
||||
.connector::before, .connector::after {
|
||||
content: '';
|
||||
position: absolute;
|
||||
left: 0;
|
||||
border-top: 2px solid #ccc;
|
||||
width: 10px;
|
||||
border-top: 1px solid #ffffff;
|
||||
width: 20px;
|
||||
height: 50%;
|
||||
}
|
||||
.connector:before {
|
||||
top: 0;
|
||||
.connector::before {
|
||||
border-right: 1px solid #ffffff;
|
||||
}
|
||||
.connector:after {
|
||||
bottom: 0;
|
||||
.connector::after {
|
||||
border-right: 1px solid #ffffff;
|
||||
align-self: flex-end;
|
||||
}
|
||||
.round:last-child .connector {
|
||||
display: none;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="tournament-bracket">
|
||||
{% for round in tournament_rounds %}
|
||||
<div class="round">
|
||||
<h3>Round {{ forloop.counter }}</h3>
|
||||
{% for match in round %}
|
||||
<div class="match">
|
||||
<div class="player {% if match.winner == match.player1 %}winner{% elif match.winner %}loser{% endif %}">
|
||||
{{ match.player1 }}
|
||||
{% if match.score1 is not None %}<span class="score">{{ match.score1 }}</span>{% endif %}
|
||||
</div>
|
||||
<div class="player {% if match.winner == match.player2 %}winner{% elif match.winner %}loser{% endif %}">
|
||||
{{ match.player2|default:"BYE" }}
|
||||
{% if match.score2 is not None %}<span class="score">{{ match.score2 }}</span>{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
{% if not forloop.last %}<div class="connector"></div>{% endif %}
|
||||
{% endfor %}
|
||||
<div class="round">
|
||||
{% for match in round %}
|
||||
<div class="match">
|
||||
<div class="player {% if match.winner == match.player1 %}winner{% endif %}">
|
||||
{{ match.player1 }}
|
||||
{% if match.score1 is not None %}<span class="score">{{ match.score1 }}</span>{% endif %}
|
||||
</div>
|
||||
<div class="player {% if match.winner == match.player2 %}winner{% endif %}">
|
||||
{{ match.player2|default:"BYE" }}
|
||||
{% if match.score2 is not None %}<span class="score">{{ match.score2 }}</span>{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
{% if not forloop.last %}
|
||||
<div class="connector"></div>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
</div>
|
||||
{% if not forloop.last %}
|
||||
<div class="connector"></div>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
</div>
|
||||
</body>
|
||||
|
||||
@@ -33,6 +33,7 @@ class TournamentMatchMaker:
|
||||
self.matches = []
|
||||
self.rounds = []
|
||||
self.current_round = 0
|
||||
self.games = 0
|
||||
self.tournament_state = "waiting" # Can be "waiting", "in_progress", or "ended"
|
||||
|
||||
async def add_player(self, player):
|
||||
@@ -41,11 +42,6 @@ class TournamentMatchMaker:
|
||||
print(f"User {player.user.username} joins the TOURNAMENT WAITING ROOM")
|
||||
await self.update_waiting_room()
|
||||
|
||||
async def remove_player(self, player):
|
||||
if player in self.waiting_players:
|
||||
self.waiting_players.remove(player)
|
||||
await self.update_waiting_room()
|
||||
|
||||
async def update_waiting_room(self):
|
||||
html = self.generate_waiting_room_html()
|
||||
for player in self.waiting_players:
|
||||
@@ -63,25 +59,51 @@ class TournamentMatchMaker:
|
||||
}
|
||||
return render_to_string('pong/tournament_waiting_room.html', context)
|
||||
|
||||
async def send_to_player(self, player, data):
|
||||
await player.send(json.dumps(data))
|
||||
|
||||
async def remove_player(self, player):
|
||||
if player in self.waiting_players:
|
||||
self.waiting_players.remove(player)
|
||||
await self.update_waiting_room()
|
||||
|
||||
# Tournament start method
|
||||
async def start_tournament(self):
|
||||
if len(self.waiting_players) < 2:
|
||||
return False
|
||||
|
||||
return False
|
||||
self.tournament_state = "in_progress"
|
||||
random.shuffle(self.waiting_players)
|
||||
self.current_round = 1
|
||||
self.create_matches(self.waiting_players)
|
||||
await self.update_brackets()
|
||||
await self.start_round_matches()
|
||||
self.current_round = 0
|
||||
await self.advance_tournament()
|
||||
return True
|
||||
|
||||
async def advance_tournament(self):
|
||||
players = self.waiting_players
|
||||
while len(players) > 1:
|
||||
self.current_round += 1
|
||||
print(f"Starting round {self.current_round} with {len(players)} players")
|
||||
self.create_matches(players)
|
||||
await self.update_brackets()
|
||||
await self.start_round_matches()
|
||||
# Wait for all matches in the current round to finish
|
||||
current_round_matches = self.rounds[-1]
|
||||
while not all(match.ended for match in current_round_matches):
|
||||
await asyncio.sleep(1) # Wait for 1 second before checking again
|
||||
# Get winners for the next round
|
||||
players = self.get_round_winners()
|
||||
print(f"Round {self.current_round} finished. {len(players)} players advancing.")
|
||||
# Tournament has ended
|
||||
await self.update_brackets()
|
||||
await self.end_tournament(players[0] if players else None)
|
||||
|
||||
def create_matches(self, players):
|
||||
matches = []
|
||||
for i in range(0, len(players), 2):
|
||||
self.games += 1
|
||||
if i + 1 < len(players):
|
||||
matches.append(TournamentMatch(len(self.matches) + 1, players[i], players[i + 1], self))
|
||||
matches.append(TournamentMatch(self.games, players[i], players[i + 1], self))
|
||||
else:
|
||||
matches.append(TournamentMatch(len(self.matches) + 1, players[i], None, self)) # Bye
|
||||
matches.append(TournamentMatch(self.games, players[i], None, self)) # BYE match
|
||||
self.rounds.append(matches)
|
||||
self.matches.extend(matches)
|
||||
|
||||
@@ -103,8 +125,8 @@ class TournamentMatchMaker:
|
||||
return [
|
||||
[
|
||||
{
|
||||
'player1': match.player1.user.username if match.player1 else 'BYE',
|
||||
'player2': match.player2.user.username if match.player2 else 'BYE',
|
||||
'player1': match.player1.user.username if match.player1 else 'BOT',
|
||||
'player2': match.player2.user.username if match.player2 else 'BOT',
|
||||
'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'],
|
||||
'score2': match.game_state['player2_score']
|
||||
@@ -115,45 +137,46 @@ class TournamentMatchMaker:
|
||||
]
|
||||
|
||||
async def start_round_matches(self):
|
||||
print(f"Starting TOURNAMENT round #{self.current_round}")
|
||||
for match in self.rounds[-1]:
|
||||
if match.player1 and match.player2:
|
||||
await match_maker.notify_players(match.player1, match.player2, match.game_id, False)
|
||||
asyncio.create_task(match.start_game())
|
||||
elif match.player1:
|
||||
# Handle bye
|
||||
# Handle BYE match
|
||||
await match_maker.notify_players(match.player1, match.player2, match.game_id, False)
|
||||
match.game_state['player1_score'] = 3
|
||||
match.game_state['player2_score'] = 0
|
||||
await match.end_game()
|
||||
#asyncio.create_task(match.start_game())
|
||||
|
||||
async def handle_match_end(self, match):
|
||||
await self.update_brackets()
|
||||
if all(m.ended for m in self.rounds[-1]):
|
||||
await self.advance_tournament()
|
||||
|
||||
async def advance_tournament(self):
|
||||
winners = [match.player1 if match.game_state['player1_score'] > match.game_state['player2_score'] else match.player2 for match in self.rounds[-1] if match.player1 and match.player2]
|
||||
if len(winners) > 1:
|
||||
self.current_round += 1
|
||||
self.create_matches(winners)
|
||||
await self.update_brackets()
|
||||
await self.start_round_matches()
|
||||
else:
|
||||
await self.end_tournament(winners[0])
|
||||
def get_round_winners(self):
|
||||
winners = []
|
||||
for match in self.rounds[-1]:
|
||||
if match.ended:
|
||||
winner = match.player1 if match.game_state['player1_score'] > match.game_state['player2_score'] else match.player2
|
||||
if winner:
|
||||
winners.append(winner)
|
||||
return winners
|
||||
|
||||
async def end_tournament(self, winner):
|
||||
self.tournament_state = "ended"
|
||||
winner_username = winner.user.username if winner else "No winner"
|
||||
print(f"The TOURNAMENT winner is {winner_username}")
|
||||
for player in self.waiting_players:
|
||||
await self.send_to_player(player, {
|
||||
'type': 'tournament_end',
|
||||
'winner': winner.user.username
|
||||
'winner': winner_username
|
||||
})
|
||||
# Reset tournament state
|
||||
self.waiting_players = []
|
||||
self.matches = []
|
||||
self.rounds = []
|
||||
self.current_round = 0
|
||||
self.games = 0
|
||||
|
||||
async def send_to_player(self, player, data):
|
||||
await player.send(json.dumps(data))
|
||||
async def handle_match_end(self, match):
|
||||
await self.update_brackets()
|
||||
|
||||
# Instance of the class
|
||||
tournament_match_maker = TournamentMatchMaker()
|
||||
Reference in New Issue
Block a user