mirror of
https://github.com/AudebertAdrien/ft_transcendence.git
synced 2025-12-16 22:17:48 +01:00
Merge branch 'chaku' of github.com:AudebertAdrien/ft_transcendence into merge
This commit is contained in:
commit
8164f33522
@ -4,8 +4,9 @@ import json
|
|||||||
import asyncio
|
import asyncio
|
||||||
import random
|
import random
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from .utils import handle_game_data
|
from .utils import handle_game_data, getlen
|
||||||
from asgiref.sync import sync_to_async
|
from asgiref.sync import sync_to_async
|
||||||
|
from .models import Tournoi
|
||||||
|
|
||||||
class Game:
|
class Game:
|
||||||
def __init__(self, game_id, player1, player2, localgame):
|
def __init__(self, game_id, player1, player2, localgame):
|
||||||
@ -27,9 +28,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,
|
||||||
@ -241,6 +243,14 @@ class Game:
|
|||||||
if not self.botgame:
|
if not self.botgame:
|
||||||
if not self.localgame:
|
if not self.localgame:
|
||||||
await self.player2.send(end_message)
|
await self.player2.send(end_message)
|
||||||
await sync_to_async(handle_game_data)(self.game_state['player1_name'], self.game_state['player2_name'],
|
if hasattr(self, 'tournament'):
|
||||||
|
len_tournament = await sync_to_async(getlen)()
|
||||||
|
name_tournament = self.tournament.name + " #" + str(len_tournament + 1)
|
||||||
|
print(f"- Saving match game #{self.game_id} of tournament: {name_tournament}")
|
||||||
|
await sync_to_async(handle_game_data)(self.game_state['player1_name'], self.game_state['player2_name'],
|
||||||
|
self.game_state['player1_score'], self.game_state['player2_score'],
|
||||||
|
self.bt1, self.bt2, duration, True, name_tournament)
|
||||||
|
else:
|
||||||
|
await sync_to_async(handle_game_data)(self.game_state['player1_name'], self.game_state['player2_name'],
|
||||||
self.game_state['player1_score'], self.game_state['player2_score'],
|
self.game_state['player1_score'], self.game_state['player2_score'],
|
||||||
self.bt1, self.bt2, duration, False, None)
|
self.bt1, self.bt2, duration, False, None)
|
||||||
|
|||||||
@ -7,6 +7,12 @@ import random
|
|||||||
from .matchmaking import match_maker
|
from .matchmaking import match_maker
|
||||||
from .game import Game
|
from .game import Game
|
||||||
|
|
||||||
|
TOURNAMENT_NAMES = [
|
||||||
|
"Champion's Clash", "Ultimate Showdown", "Battle Royale",
|
||||||
|
"Victory's Cup", "Legends Tournament", "Elite Series", "Clash of 42",
|
||||||
|
"Shibuya Incident", "Cunning Game", "Elite of the Stars", "Chaku's Disciples"
|
||||||
|
]
|
||||||
|
|
||||||
class TournamentMatch(Game):
|
class TournamentMatch(Game):
|
||||||
def __init__(self, game_id, player1, player2, tournament):
|
def __init__(self, game_id, player1, player2, tournament):
|
||||||
# Initialize the parent Game class with the provided parameters
|
# Initialize the parent Game class with the provided parameters
|
||||||
@ -21,7 +27,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):
|
||||||
@ -31,6 +38,7 @@ class TournamentMatchMaker:
|
|||||||
self.current_round = 0
|
self.current_round = 0
|
||||||
self.games = 0
|
self.games = 0
|
||||||
self.tournament_state = "waiting" #Can be "waiting", "in_progress", or "ended"
|
self.tournament_state = "waiting" #Can be "waiting", "in_progress", or "ended"
|
||||||
|
self.name = random.choice(TOURNAMENT_NAMES)
|
||||||
|
|
||||||
async def add_player(self, player):
|
async def add_player(self, player):
|
||||||
if self.tournament_state == "waiting" and player not in self.waiting_players:
|
if self.tournament_state == "waiting" and player not in self.waiting_players:
|
||||||
@ -51,10 +59,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)
|
||||||
|
|
||||||
@ -69,6 +77,7 @@ class TournamentMatchMaker:
|
|||||||
|
|
||||||
# Tournament start method
|
# Tournament start method
|
||||||
async def start_tournament(self):
|
async def start_tournament(self):
|
||||||
|
|
||||||
if len(self.waiting_players) < 2:
|
if len(self.waiting_players) < 2:
|
||||||
return False
|
return False
|
||||||
if len(self.waiting_players) % 2 == 0:
|
if len(self.waiting_players) % 2 == 0:
|
||||||
@ -139,8 +148,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 +168,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):
|
||||||
|
|||||||
@ -172,3 +172,5 @@ def get_player_p_win(player_name):
|
|||||||
player = get_object_or_404(Player, name=player_name)
|
player = get_object_or_404(Player, name=player_name)
|
||||||
return player.p_win
|
return player.p_win
|
||||||
|
|
||||||
|
def getlen():
|
||||||
|
return Tournoi.objects.count()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user