end match but really big probleme to register our data in the baskend =(

This commit is contained in:
Theouche 2024-08-14 18:46:05 +02:00
parent 53d1f969c0
commit d96ec4ea87
3 changed files with 63 additions and 43 deletions

View File

@ -47,7 +47,7 @@ class Game:
await self.update_bot_position()
await self.handle_pad_movement()
self.update_game_state()
await self.update_game_state()
await self.send_game_state()
await asyncio.sleep(1/60) # Around 60 FPS
@ -61,7 +61,7 @@ class Game:
self.game_state['player2_position'] = max(self.game_state['player2_position'] - (5 * self.speed), 0)
def update_game_state(self):
async def update_game_state(self):
if self.ended:
return
# Update ball position
@ -84,15 +84,19 @@ class Game:
self.bt2 += 1
self.update_ball_velocity()
# Check for scoring
print(f"########### score user 1 {self.game_state['player1_score']} ###########")
print(f"§§§§§§§§§§§ score user 2 {self.game_state['player2_score']} §§§§§§§§§§§")
if self.game_state['ball_position']['x'] <= 10:
self.game_state['player2_score'] += 1
if self.game_state['player2_score'] >= 5:
self.end_game()
if self.game_state['player2_score'] >= 2:
print("Here !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
await self.end_game()
self.reset_ball()
elif self.game_state['ball_position']['x'] >= 790:
self.game_state['player1_score'] += 1
if self.game_state['player1_score'] >= 5:
self.end_game()
if self.game_state['player1_score'] >= 2:
await self.end_game()
self.reset_ball()
def reset_ball(self):
@ -160,7 +164,9 @@ class Game:
self.game_state['player2_position'] = min(self.game_state['player2_position'] + (5 * self.speed), 300)
async def end_game(self, disconnected_player=None):
print("end GAME CALLED")
if not self.ended:
print("Game ended !!!!! ")
self.ended = True
if self.game_loop_task:
self.game_loop_task.cancel()
@ -187,6 +193,7 @@ class Game:
await self.player1.send(end_message)
if not self.botgame:
await self.player2.send(end_message)
print("save data")
await endfortheouche(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, False, None)

View File

@ -40,7 +40,7 @@ class MatchMaker:
await asyncio.sleep(1)
self.timer += 1
# Waiting for more than 30s -> BOT game
if self.timer >= 30 and self.waiting_players:
if self.timer >= 5 and self.waiting_players:
player1 = self.waiting_players.pop(0)
print(f"*** MATCH FOUND: {player1.user.username} vs BOT")
self.botgame = True

View File

@ -4,32 +4,44 @@ from django.shortcuts import get_object_or_404
from django.db.models import Max, Sum, F
from datetime import timedelta
from channels.db import database_sync_to_async
#from asgiref.sync import database_sync_to_async
async def endfortheouche(p1, p2, s_p1, s_p2, bt_p1, bt_2, dur, is_tournoi, name_tournament):
# Check if player p1 exists, if not create
if not await database_sync_to_async(Player.objects.filter(name=p1).exists)():
player_1 = await create_player(p1)
print("############# PLAYER DONE")
else:
player_1 = await database_sync_to_async(Player.objects.get)(name=p1)
try:
print("here endfortheouche §!!!")
# Check if player p2 exists, if not create
if not await database_sync_to_async(Player.objects.filter(name=p2).exists)():
player_2 = await create_player(p2)
print("############# PLAYER DONE")
else:
player_2 = await database_sync_to_async(Player.objects.get)(name=p2)
# create Match
print("############# BEFORE MATCH")
await create_match(player_1, player_2, s_p1, s_p2, bt_p1, bt_2, dur, is_tournoi, name_tournament)
print("############# AFTER DONE")
""" await database_sync_to_async(Player.objects.filter(name=p1).exists)()
print("ok") """
if not await database_sync_to_async(Player.objects.filter(name=p1).exists)():
print("############# player 1 not existed .... creating player #############")
player_1 = await create_player(p1)
print("############# PLAYER DONE ############")
else:
print("############# Player 1 find, get data #############")
player_1 = await database_sync_to_async(Player.objects.get)(name=p1)
print("############# Player get #############")
# Update data p1 et p2
await uptdate_player_statistics(p1)
print("############# END STAT P1")
await uptdate_player_statistics(p2)
if not await database_sync_to_async(Player.objects.filter(name=p2).exists)():
print("############# player 2 not existed .... creating player #############")
player_2 = await create_player(p2)
print("############# PLAYER DONE")
else:
print("############# Player 2 find, get data #############")
player_2 = await database_sync_to_async(Player.objects.get)(name=p2)
print("############# Player get #############")
print("############# BEFORE MATCH")
await create_match(player_1, player_2, s_p1, s_p2, bt_p1, bt_2, dur, is_tournoi, name_tournament)
print("############# AFTER DONE")
await uptdate_player_statistics(p1)
print("############# END STAT P1")
await uptdate_player_statistics(p2)
except Exception as e:
print(f"Error in endfortheouche: {e}")
@database_sync_to_async
def create_player(
@ -46,22 +58,23 @@ def create_player(
num_participated_tournaments=0,
num_won_tournaments=0
):
if Player.objects.filter(name=name).exists():
raise ValueError(f"A player with the name '{name}' already exists.")
print("create player !!!")
""" if database_sync_to_async(Player.objects.filter(name=name).exists)():
raise ValueError(f"A player with the name '{name}' already exists.") """
player = Player(
name=name,
total_match = total_match,
total_win = total_win,
p_win = p_win,
m_score_match = m_score_match,
m_score_adv_match = m_score_adv_match,
best_score = best_score,
m_nbr_ball_touch = m_nbr_ball_touch,
total_duration = total_duration,
m_duration = m_duration,
num_participated_tournaments = num_participated_tournaments,
num_won_tournaments = num_won_tournaments
total_match=total_match,
total_win=total_win,
p_win=p_win,
m_score_match=m_score_match,
m_score_adv_match=m_score_adv_match,
best_score=best_score,
m_nbr_ball_touch=m_nbr_ball_touch,
total_duration=total_duration,
m_duration=m_duration,
num_participated_tournaments=num_participated_tournaments,
num_won_tournaments=num_won_tournaments
)
player.save()
return player