mirror of
https://github.com/AudebertAdrien/ft_transcendence.git
synced 2025-12-16 14:07:49 +01:00
conflict result
This commit is contained in:
commit
41c10ffe13
@ -3,6 +3,7 @@
|
||||
import json
|
||||
import asyncio
|
||||
import random
|
||||
from .utils import endfortheouche
|
||||
|
||||
class BotPlayer:
|
||||
def __init__(self, game_state, speed):
|
||||
@ -68,6 +69,10 @@ class Game:
|
||||
self.speed = 1
|
||||
self.game_loop_task = None
|
||||
self.ended = False
|
||||
self.p1_mov = 0
|
||||
self.p2_mov = 0
|
||||
self.bt1 = 0
|
||||
self.bt2 = 0
|
||||
|
||||
if self.botgame:
|
||||
self.bot_player = BotPlayer(self.game_state, self.speed)
|
||||
@ -81,8 +86,8 @@ class Game:
|
||||
while True:
|
||||
if self.botgame:
|
||||
#print('still ok')
|
||||
await self.bot_player.update_bot_position()
|
||||
#await self.update_bot_position()
|
||||
#await self.bot_player.update_bot_position()
|
||||
await self.update_bot_position()
|
||||
#print('is it ok ?? ')
|
||||
self.update_game_state()
|
||||
await self.send_game_state()
|
||||
@ -109,11 +114,13 @@ class Game:
|
||||
self.game_state['player1_position'] - 10 <= self.game_state['ball_position']['y'] <= self.game_state['player1_position'] + 90:
|
||||
if self.game_state['ball_velocity']['x'] < 0:
|
||||
self.game_state['ball_velocity']['x'] *= -1
|
||||
self.bt1 += 1
|
||||
self.update_ball_velocity()
|
||||
elif self.game_state['ball_position']['x'] >= 760 and \
|
||||
self.game_state['player2_position'] - 10 <= self.game_state['ball_position']['y'] <= self.game_state['player2_position'] + 90:
|
||||
if self.game_state['ball_velocity']['x'] > 0:
|
||||
self.game_state['ball_velocity']['x'] *= -1
|
||||
self.bt2 += 1
|
||||
self.update_ball_velocity()
|
||||
# Check for scoring
|
||||
if self.game_state['ball_position']['x'] <= 10:
|
||||
@ -159,14 +166,30 @@ class Game:
|
||||
return
|
||||
if player == self.player1:
|
||||
if key == 'arrowup':
|
||||
self.game_state['player1_position'] = max(self.game_state['player1_position'] - 25, 0)
|
||||
self.p1_mov = -1
|
||||
#self.game_state['player1_position'] = max(self.game_state['player1_position'] - 25, 0)
|
||||
elif key == 'arrowdown':
|
||||
self.game_state['player1_position'] = min(self.game_state['player1_position'] + 25, 300)
|
||||
self.p1_mov = 1
|
||||
#self.game_state['player1_position'] = min(self.game_state['player1_position'] + 25, 300)
|
||||
elif not self.botgame and player == self.player2:
|
||||
if key == 'arrowup':
|
||||
self.game_state['player2_position'] = max(self.game_state['player2_position'] - 25, 0)
|
||||
self.p2_mov = -1
|
||||
#self.game_state['player2_position'] = max(self.game_state['player2_position'] - 25, 0)
|
||||
elif key == 'arrowdown':
|
||||
self.game_state['player2_position'] = min(self.game_state['player2_position'] + 25, 300)
|
||||
self.p2_mov = 1
|
||||
#self.game_state['player2_position'] = min(self.game_state['player2_position'] + 25, 300)
|
||||
|
||||
async def handle_pad_movement(self):
|
||||
#print(f"P1 mov: {self.p1_mov}")
|
||||
#print(f"P2 mov: {self.p2_mov}")
|
||||
if self.p1_mov == -1:
|
||||
self.game_state['player1_position'] = max(self.game_state['player1_position'] - (5 * self.speed), 0)
|
||||
elif self.p1_mov == 1:
|
||||
self.game_state['player1_position'] = min(self.game_state['player1_position'] + (5 * self.speed), 300)
|
||||
if self.p2_mov == -1:
|
||||
self.game_state['player2_position'] = max(self.game_state['player2_position'] - (5 * self.speed), 0)
|
||||
elif self.p2_mov == 1:
|
||||
self.game_state['player2_position'] = min(self.game_state['player2_position'] + (5 * self.speed), 300)
|
||||
|
||||
async def end_game(self, disconnected_player=None):
|
||||
if not self.ended:
|
||||
@ -192,4 +215,22 @@ class Game:
|
||||
await self.player1.send(end_message)
|
||||
if not self.botgame:
|
||||
await self.player2.send(end_message)
|
||||
#endfortheouche(p1, p2, s_p1, s_p2, winner, bt_p1, bt_p2, dur, is_tournoi, name_tournament)
|
||||
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, 42, False, None)
|
||||
|
||||
### pour Theo ###
|
||||
# nickname player1
|
||||
# nickname player2
|
||||
# score p1
|
||||
# score p2
|
||||
# winner
|
||||
# ball touch p1
|
||||
# ball touch p2
|
||||
# match time
|
||||
# match type: 'quick match'
|
||||
|
||||
# match type: 'tournament'
|
||||
# -> tournament id
|
||||
|
||||
#endfortheouche(p1, p2, s_p1, s_p2, bt_p1, bt_p2, dur, is_tournoi, name_tournament)
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
# Generated by Django 5.0.7 on 2024-07-23 16:04
|
||||
# Generated by Django 5.0.7 on 2024-07-30 19:44
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.db import migrations, models
|
||||
@ -17,6 +17,17 @@ class Migration(migrations.Migration):
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('name', models.CharField(max_length=100)),
|
||||
('total_match', models.PositiveSmallIntegerField(default=0)),
|
||||
('total_win', models.PositiveSmallIntegerField(default=0)),
|
||||
('p_win', models.DecimalField(blank=True, decimal_places=2, max_digits=5, null=True)),
|
||||
('m_score_match', models.DecimalField(blank=True, decimal_places=2, max_digits=5, null=True)),
|
||||
('m_score_adv_match', models.DecimalField(blank=True, decimal_places=2, max_digits=5, null=True)),
|
||||
('best_score', models.PositiveSmallIntegerField(default=0)),
|
||||
('m_nbr_ball_touch', models.DecimalField(blank=True, decimal_places=2, max_digits=5, null=True)),
|
||||
('total_duration', models.DurationField(blank=True, null=True)),
|
||||
('m_duration', models.DecimalField(blank=True, decimal_places=2, max_digits=5, null=True)),
|
||||
('num_participated_tournaments', models.PositiveSmallIntegerField(default=0)),
|
||||
('num_won_tournaments', models.PositiveSmallIntegerField(default=0)),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
|
||||
18
pong/game/migrations/0002_alter_match_duration.py
Normal file
18
pong/game/migrations/0002_alter_match_duration.py
Normal file
@ -0,0 +1,18 @@
|
||||
# Generated by Django 5.0.7 on 2024-07-31 13:17
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('game', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='match',
|
||||
name='duration',
|
||||
field=models.DecimalField(blank=True, decimal_places=2, max_digits=5, null=True),
|
||||
),
|
||||
]
|
||||
@ -40,7 +40,7 @@ class Match(models.Model):
|
||||
winner = models.ForeignKey('Player', related_name='won_matches',on_delete=models.CASCADE)
|
||||
nbr_ball_touch_p1 = models.PositiveIntegerField()
|
||||
nbr_ball_touch_p2 = models.PositiveIntegerField()
|
||||
duration = models.DurationField()
|
||||
duration = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)
|
||||
date = models.DateField(auto_now_add=True)
|
||||
is_tournoi = models.BooleanField()
|
||||
tournoi = models.ForeignKey('Tournoi', related_name='matches', on_delete=models.SET_NULL, null=True)
|
||||
@ -55,7 +55,7 @@ class Match(models.Model):
|
||||
super().clean()
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
self.full_clean() # Appel de la méthode clean() avant d'enregistrer
|
||||
self.clean() # Appel de la méthode clean() avant d'enregistrer
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
def __str__(self):
|
||||
|
||||
@ -3,6 +3,7 @@ from django.core.exceptions import ValidationError
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.db.models import Max, Sum, F
|
||||
from datetime import timedelta
|
||||
<<<<<<< HEAD
|
||||
|
||||
def endfortheouche(p1, p2, s_p1, s_p2, winner, bt_p1, bt_p2, dur, is_tournoi, name_tournament) :
|
||||
#If he doesn't exist, create player p1
|
||||
@ -21,6 +22,31 @@ def endfortheouche(p1, p2, s_p1, s_p2, winner, bt_p1, bt_p2, dur, is_tournoi, na
|
||||
uptdate_player_statistics(p2)
|
||||
|
||||
|
||||
=======
|
||||
from channels.db 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)
|
||||
else:
|
||||
player_1 = await database_sync_to_async(Player.objects.get)(name=p1)
|
||||
|
||||
# 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)
|
||||
else:
|
||||
player_2 = await database_sync_to_async(Player.objects.get)(name=p2)
|
||||
|
||||
# create Match
|
||||
await create_match(player_1, player_2, s_p1, s_p2, bt_p1, bt_2, dur, is_tournoi, name_tournament)
|
||||
|
||||
# Update data p1 et p2
|
||||
await uptdate_player_statistics(p1)
|
||||
await uptdate_player_statistics(p2)
|
||||
|
||||
@database_sync_to_async
|
||||
>>>>>>> 067f24f5b87b0db75b90d5a86f47b30d1509ba99
|
||||
def create_player(
|
||||
name,
|
||||
total_match=0,
|
||||
@ -55,11 +81,13 @@ def create_player(
|
||||
player.save()
|
||||
return player
|
||||
|
||||
@database_sync_to_async
|
||||
def create_tournoi(name, nbr_player, date, winner):
|
||||
tournoi = Tournoi(name=name, nbr_player=nbr_player, date=date, winner=winner)
|
||||
tournoi.save()
|
||||
return tournoi
|
||||
|
||||
@database_sync_to_async
|
||||
def create_match(player1, player2, score_player1, score_player2, nbr_ball_touch_p1, nbr_ball_touch_p2, duration, is_tournoi, tournoi):
|
||||
match = Match(
|
||||
player1=player1,
|
||||
@ -83,7 +111,7 @@ def create_match(player1, player2, score_player1, score_player2, nbr_ball_touch_
|
||||
match.save()
|
||||
return match
|
||||
|
||||
|
||||
@database_sync_to_async
|
||||
def uptdate_player_statistics(player_name):
|
||||
player = get_object_or_404(Player, name=player_name)
|
||||
|
||||
@ -103,8 +131,8 @@ def uptdate_player_statistics(player_name):
|
||||
player.m_score_adv_match = 0
|
||||
player.best_score = 0
|
||||
player.m_nbr_ball_touch = 0
|
||||
player.total_duration = timedelta()
|
||||
player.m_duration = timedelta()
|
||||
player.total_duration = 0
|
||||
player.m_duration = 0
|
||||
player.num_participated_tournaments = 0
|
||||
player.num_won_tournaments = 0
|
||||
player.save()
|
||||
@ -131,8 +159,8 @@ def uptdate_player_statistics(player_name):
|
||||
nbr_ball_touch += matches_as_player2.aggregate(Sum('nbr_ball_touch_p2'))['nbr_ball_touch_p2__sum'] or 0
|
||||
m_nbr_ball_touch = nbr_ball_touch / total_match
|
||||
|
||||
total_duration = matches_as_player1.aggregate(Sum('duration'))['duration__sum'] or timedelta()
|
||||
total_duration += matches_as_player2.aggregate(Sum('duration'))['duration__sum'] or timedelta()
|
||||
total_duration = matches_as_player1.aggregate(Sum('duration'))['duration__sum']
|
||||
total_duration += matches_as_player2.aggregate(Sum('duration'))['duration__sum']
|
||||
m_duration = total_duration / total_match
|
||||
|
||||
""" total_tourn_p = part_tourn_as_p1.count() + part_tourn_as_p2.count()
|
||||
@ -193,4 +221,3 @@ def uptdate_player_statistics(player_name):
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -2,6 +2,9 @@
|
||||
|
||||
from django.shortcuts import render
|
||||
|
||||
def index(request):
|
||||
return render(request, 'index.html')
|
||||
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from .models import Player, Tournoi, Match
|
||||
from .utils import create_player, create_tournoi, create_match
|
||||
|
||||
@ -150,7 +150,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
}
|
||||
|
||||
async function checkUserExists(username) {
|
||||
const response = await fetch('/api/check_user_exists/', {
|
||||
const response = await fetch('/check_user_exists/', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
@ -186,7 +186,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
}
|
||||
|
||||
async function registerUser(username, password) {
|
||||
const response = await fetch('/api/register_user/', {
|
||||
const response = await fetch('/register_user/', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
@ -218,7 +218,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
}
|
||||
|
||||
async function authenticateUser(username, password) {
|
||||
const response = await fetch('/api/authenticate_user/', {
|
||||
const response = await fetch('/authenticate_user/', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
|
||||
@ -8,7 +8,7 @@ from django.conf.urls.static import static
|
||||
urlpatterns = [
|
||||
# Disable the admin page
|
||||
# path('admin/', admin.site.urls),
|
||||
path('api/', include('pong.game.urls')),
|
||||
# path('api/', include('pong.game.urls')),
|
||||
path('', include('pong.game.urls')),
|
||||
]
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user