Merge branch 'main' of codeberg.org:adrien-lsh/ft_transcendence

This commit is contained in:
2024-05-14 03:28:36 +02:00
16 changed files with 518 additions and 653 deletions

View File

@ -2,6 +2,7 @@ import { AExchangeable } from "../AExchangable.js";
import { APlayer } from "./APlayer.js";
import { Client } from "../Client.js"
import { sleep } from "../../utils/sleep.js";
import { Profile } from "../Profile.js";
export class AGame extends AExchangeable
{
@ -44,9 +45,9 @@ export class AGame extends AExchangeable
this._disconntectHandler = disconntectHandler;
/**
* @type {Number}
* @type {Profile}
*/
this.winnerId;
this.winner;
/**
* @type {Number}

View File

@ -6,7 +6,6 @@ import { Client } from "../../Client.js";
import { PongBall } from "./PongBall.js";
import { sleep } from "../../../utils/sleep.js";
import { Wall } from "./Wall.js"
import { Point } from "./Point.js";
import { Position } from "./Position.js";
export class PongGame extends AGame
@ -16,9 +15,10 @@ export class PongGame extends AGame
* @param {CallableFunction} goal_handler
* @param {CallableFunction} finish_handler
* @param {CallableFunction} disconnect_handler
* @param {CallableFunction} startHandler
* @param {Number} id
*/
constructor(client, id, disconnectHandler, goalHandler, finishHandler)
constructor(client, id, disconnectHandler, goalHandler, startHandler, finishHandler)
{
super(client, id, undefined, disconnectHandler, "pong");
@ -28,6 +28,11 @@ export class PongGame extends AGame
* @type {CallableFunction}
*/
this._goalHandler = goalHandler;
/**
* @type {CallableFunction}
*/
this._startHandler = startHandler;
/**
* @type {CallableFunction}
@ -39,7 +44,6 @@ export class PongGame extends AGame
*/
this.time;
/**
* @type {Boolean}
*/
@ -53,7 +57,7 @@ export class PongGame extends AGame
/**
* @type {Ball}
*/
this.ball = new PongBall(this, undefined, new Position());
this.ball = new PongBall(this, undefined, new Position(), 0, 0);
/**
* @type {[Wall]}
@ -122,18 +126,14 @@ export class PongGame extends AGame
*/
render(ctx)
{
if(ctx instanceof CanvasRenderingContext2D)
ctx.clearRect(0, 0, this.config.MAP_SIZE_Y, this.config.MAP_SIZE_Y);
ctx.clearRect(0, 0, this.config.MAP_SIZE_Y, this.config.MAP_SIZE_Y);
this.drawSides(ctx);
this.ball.render(ctx);
if(ctx instanceof CanvasRenderingContext2D)
{
ctx.strokeStyle = "#000000";
ctx.lineWidth = this.config.STROKE_THICKNESS;
ctx.stroke();
}
ctx.strokeStyle = "#000000";
ctx.lineWidth = this.config.STROKE_THICKNESS;
ctx.stroke();
}
/**
@ -149,6 +149,8 @@ export class PongGame extends AGame
*/
async _receive(data)
{
console.log(data)
if (this._inited === false && data.detail === "init_game")
{
this._initGame(data);
@ -162,7 +164,23 @@ export class PongGame extends AGame
else if (data.detail === "goal")
await this._receiveGoal(data);
else if (data.detail === "finish")
await this._finishHandler(data);
await this._receiveFinish(data);
else if (data.detail === "start")
await this._receiveStart();
}
async _receiveFinish(data)
{
const winner = this.players.find(player => player.id === data.winner_id)
this.finished = true;
await this._finishHandler(winner);
}
async _receiveStart()
{
this.started = true;
await this._startHandler();
}
async _receiveGoal(data)
@ -176,6 +194,7 @@ export class PongGame extends AGame
}
player.score.push(data.timestamp)
console.log(player)
await this._goalHandler(player);
}
@ -192,26 +211,6 @@ export class PongGame extends AGame
this.ball.import(data);
}
async _updateGoal(data)
{
/**
* @type { Player }
*/
let player_found;
this.players.forEach(player => {
if (data.player_id === player.id)
{
player_found = player;
return;
}
});
player_found.score.push(data.timestamp);
await this._goalHandler(player_found);
}
_initGame(data)
{
data.walls.forEach((wall_data) => {

View File

@ -4,7 +4,7 @@ import { Segment } from "./Segment.js";
import { PongGame } from "./PongGame.js";
import { Position } from "./Position.js";
export class MyPlayer extends PongPlayer
export class PongMyPlayer extends PongPlayer
{
/**
* @param {Client} client

View File

@ -17,8 +17,9 @@ export class PongPlayer extends APlayer
* @param {String} username
* @param {String} avatar
* @param {Client} client
* @param {Boolean} isEliminated
*/
constructor(client, game, id, username, avatar, score = [], rail = new Segment(game), position = new Position(0.5), isConnected)
constructor(client, game, id, username, avatar, score = [], rail = new Segment(game), position = new Position(0.5), isConnected, isEliminated)
{
super(client, game, id, username, avatar, isConnected)
@ -41,6 +42,11 @@ export class PongPlayer extends APlayer
* @type {PongPlayer}
*/
this.game = game;
/**
* @type {Boolean}
*/
this.isEliminated = isEliminated;
}
/**
@ -57,13 +63,10 @@ export class PongPlayer extends APlayer
*/
draw(ctx)
{
if (this.isConnected === false)
if (this.isConnected === false || this.isEliminated === true)
{
if(ctx instanceof CanvasRenderingContext2D)
{
ctx.moveTo(this.rail.start.x, this.rail.start.y);
ctx.lineTo(this.rail.stop.x, this.rail.stop.y);
}
ctx.moveTo(this.rail.start.x, this.rail.start.y);
ctx.lineTo(this.rail.stop.x, this.rail.stop.y);
return;
}