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

View File

@ -40,7 +40,7 @@ class MatchMaker:
await asyncio.sleep(1) await asyncio.sleep(1)
self.timer += 1 self.timer += 1
# Waiting for more than 30s -> BOT game # 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) player1 = self.waiting_players.pop(0)
print(f"*** MATCH FOUND: {player1.user.username} vs BOT") print(f"*** MATCH FOUND: {player1.user.username} vs BOT")
self.botgame = True 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 django.db.models import Max, Sum, F
from datetime import timedelta from datetime import timedelta
from channels.db import database_sync_to_async 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): 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 try:
if not await database_sync_to_async(Player.objects.filter(name=p1).exists)(): print("here endfortheouche §!!!")
player_1 = await create_player(p1)
print("############# PLAYER DONE")
else:
player_1 = await database_sync_to_async(Player.objects.get)(name=p1)
# Check if player p2 exists, if not create """ await database_sync_to_async(Player.objects.filter(name=p1).exists)()
if not await database_sync_to_async(Player.objects.filter(name=p2).exists)(): print("ok") """
player_2 = await create_player(p2)
print("############# PLAYER DONE") if not await database_sync_to_async(Player.objects.filter(name=p1).exists)():
else: print("############# player 1 not existed .... creating player #############")
player_2 = await database_sync_to_async(Player.objects.get)(name=p2) player_1 = await create_player(p1)
print("############# PLAYER DONE ############")
# create Match else:
print("############# BEFORE MATCH") print("############# Player 1 find, get data #############")
await create_match(player_1, player_2, s_p1, s_p2, bt_p1, bt_2, dur, is_tournoi, name_tournament) player_1 = await database_sync_to_async(Player.objects.get)(name=p1)
print("############# AFTER DONE") print("############# Player get #############")
# Update data p1 et p2 if not await database_sync_to_async(Player.objects.filter(name=p2).exists)():
print("############# player 2 not existed .... creating player #############")
await uptdate_player_statistics(p1) player_2 = await create_player(p2)
print("############# END STAT P1") print("############# PLAYER DONE")
await uptdate_player_statistics(p2) 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 @database_sync_to_async
def create_player( def create_player(
@ -46,22 +58,23 @@ def create_player(
num_participated_tournaments=0, num_participated_tournaments=0,
num_won_tournaments=0 num_won_tournaments=0
): ):
if Player.objects.filter(name=name).exists(): print("create player !!!")
raise ValueError(f"A player with the name '{name}' already exists.") """ if database_sync_to_async(Player.objects.filter(name=name).exists)():
raise ValueError(f"A player with the name '{name}' already exists.") """
player = Player( player = Player(
name=name, name=name,
total_match = total_match, total_match=total_match,
total_win = total_win, total_win=total_win,
p_win = p_win, p_win=p_win,
m_score_match = m_score_match, m_score_match=m_score_match,
m_score_adv_match = m_score_adv_match, m_score_adv_match=m_score_adv_match,
best_score = best_score, best_score=best_score,
m_nbr_ball_touch = m_nbr_ball_touch, m_nbr_ball_touch=m_nbr_ball_touch,
total_duration = total_duration, total_duration=total_duration,
m_duration = m_duration, m_duration=m_duration,
num_participated_tournaments = num_participated_tournaments, num_participated_tournaments=num_participated_tournaments,
num_won_tournaments = num_won_tournaments num_won_tournaments=num_won_tournaments
) )
player.save() player.save()
return player return player