mirror of
https://github.com/AudebertAdrien/ft_transcendence.git
synced 2026-11-09 17:37:08 +01:00
database saving works
This commit is contained in:
@@ -10,7 +10,7 @@ from .models import Tournoi
|
||||
|
||||
class Game:
|
||||
# Global variable to handle the using of the database
|
||||
USING_DB = False
|
||||
#USING_DB = False
|
||||
|
||||
def __init__(self, game_id, player1, player2, localgame):
|
||||
self.game_id = game_id
|
||||
@@ -46,6 +46,7 @@ class Game:
|
||||
}
|
||||
self.speed = 1
|
||||
self.game_loop_task = None
|
||||
self.database = None
|
||||
self.ended = False
|
||||
self.p1_mov = 0
|
||||
self.p2_mov = 0
|
||||
@@ -236,7 +237,8 @@ class Game:
|
||||
})
|
||||
if not self.botgame:
|
||||
if not self.localgame:
|
||||
await remaining_player.send(message)
|
||||
await remaining_player.send(message)
|
||||
|
||||
# Notify both players that the game has ended
|
||||
end_message = json.dumps({
|
||||
'type': 'game_ended',
|
||||
@@ -246,17 +248,37 @@ class Game:
|
||||
if not self.botgame:
|
||||
if not self.localgame:
|
||||
await self.player2.send(end_message)
|
||||
while (Game.USING_DB):
|
||||
await asyncio.sleep(1)
|
||||
Game.USING_DB = True
|
||||
|
||||
#while (Game.USING_DB):
|
||||
# await asyncio.sleep(1)
|
||||
#Game.USING_DB = True
|
||||
if hasattr(self, 'tournament'):
|
||||
print(f"*** Game #{self.game_id} from tournament: {self.tournament.tournoi_reg.name} ENDED ***")
|
||||
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, self.tournament.tournoi_reg)
|
||||
print(f"*** Game #{self.game_id} from tournament: {self.tournament.tournoi_reg.name} is REGISTERED ***")
|
||||
print(f"*** Game #{self.game_id} from tournament: {self.tournament.tournoi_reg.name} ENDED ***")
|
||||
|
||||
# Create the async task
|
||||
self.database_task = asyncio.create_task(
|
||||
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, self.tournament.tournoi_reg
|
||||
)
|
||||
)
|
||||
# Optionally wait for the task to complete if necessary
|
||||
#await self.database
|
||||
|
||||
'''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, self.tournament.tournoi_reg)'''
|
||||
print(f"*** Game #{self.game_id} from tournament: {self.tournament.tournoi_reg.name} is REGISTERED ***")
|
||||
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.bt1, self.bt2, duration, False, None)
|
||||
Game.USING_DB = False
|
||||
self.database_task = asyncio.create_task(
|
||||
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, False, None
|
||||
)
|
||||
)
|
||||
'''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, False, None)'''
|
||||
#Game.USING_DB = False
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
# /pong/game/utils.py
|
||||
|
||||
from .models import Player, Tournoi, Match
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.shortcuts import get_object_or_404
|
||||
|
||||
@@ -29,14 +29,15 @@ ALLOWED_HOSTS = ['*']
|
||||
# Application definition
|
||||
|
||||
INSTALLED_APPS = [
|
||||
'django.contrib.admin',
|
||||
#'django.contrib.admin',
|
||||
'django.contrib.auth',
|
||||
'django.contrib.contenttypes',
|
||||
'django.contrib.sessions',
|
||||
'django.contrib.messages',
|
||||
#'django.contrib.sessions',
|
||||
#'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
'channels',
|
||||
'pong.game',
|
||||
#'django_db_conn_pool',
|
||||
'rest_framework'
|
||||
]
|
||||
|
||||
@@ -46,7 +47,7 @@ MIDDLEWARE = [
|
||||
'django.middleware.common.CommonMiddleware',
|
||||
'django.middleware.csrf.CsrfViewMiddleware',
|
||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||
'django.contrib.messages.middleware.MessageMiddleware',
|
||||
#'django.contrib.messages.middleware.MessageMiddleware',
|
||||
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||
]
|
||||
|
||||
@@ -81,6 +82,7 @@ DATABASES = {
|
||||
'PASSWORD': os.getenv('DB_PASSWORD'),
|
||||
'HOST': os.getenv('DB_HOST'),
|
||||
'PORT': '5432',
|
||||
'CONN_MAX_AGE': None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -136,7 +138,7 @@ CHANNEL_LAYERS = {
|
||||
},
|
||||
}
|
||||
|
||||
'''LOGGING = {
|
||||
LOGGING = {
|
||||
'version': 1, # The version of the logging configuration schema
|
||||
'disable_existing_loggers': False, # Allows existing loggers to keep logging
|
||||
'formatters': { # Defines how log messages will be formatted
|
||||
@@ -169,4 +171,4 @@ CHANNEL_LAYERS = {
|
||||
'propagate': True, # If True, messages will be passed to the parent loggers as well
|
||||
},
|
||||
},
|
||||
}'''
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user