added victory by forfeit

This commit is contained in:
LuckyLaszlo
2022-12-08 04:08:16 +01:00
parent d2cdc564ac
commit b3bd43c423
8 changed files with 69 additions and 16 deletions

View File

@@ -1,6 +1,4 @@
TODO:
- lors d'un newRound() verifier si tout les joueurs sont encore en ligne et stopper le match sinon
(victoire si encore un joueur en ligne, annulation du match si aucun joueur en ligne)
- mode spectateur
- certaines utilisations de Math.floor() superflu ? Vérifier les appels.
(éventuellement Math.round() ?)
@@ -26,6 +24,8 @@ Done:
- Selection des modes de jeu via HTML
- Selection audio on/off via HTML
- Match Abort si tout les joueurs n'ont pas répondus assez vite (5 secondes)
- lors d'un newRound() verifier si tout les joueurs sont encore en ligne et stopper le match sinon
(victoire si encore un joueur en ligne, annulation du match si aucun joueur en ligne)
-----------
BUG:

View File

@@ -68,6 +68,8 @@ class GameComponentsClient extends GameComponentsExtensionForClient {
scoreLeft: TextNumericValue;
scoreRight: TextNumericValue;
text1: TextElem;
text2: TextElem;
text3: TextElem;
w_grid_mid: RectangleClient;
w_grid_u1: RectangleClient;
@@ -90,6 +92,8 @@ class GameComponentsClient extends GameComponentsExtensionForClient {
// Text
pos.assign(0, c.h_mid);
this.text1 = new TextElem(pos, Math.floor(c.w/8), ctx, "white");
this.text2 = new TextElem(pos, Math.floor(c.w/24), ctx, "white");
this.text3 = new TextElem(pos, Math.floor(c.w/24), ctx, "white");
// Dotted Midline
pos.assign(c.w_mid-c.midLineSize/2, 0+c.wallSize);

View File

@@ -15,6 +15,8 @@ function drawLoop()
drawStatic();
gc.text1.update();
gc.text2.update();
gc.text3.update();
drawDynamic();
}

View File

@@ -12,6 +12,7 @@ body {
background-color: #222425;
}
#canvas_container {
margin-top: 20px;
text-align: center;
/* border: dashed rgb(245, 245, 245) 5px; */
/* max-height: 80vh; */

View File

@@ -78,12 +78,14 @@ function matchAbort()
gc.text1.text = text;
gc.text1.update();
gc.text1.pos.assign(c.w*0.44, c.h*0.6);
gc.text1.text = "sorry =(";
const oriSize = gc.text1.size;
gc.text1.size = gc.text1.size*0.2;
gc.text1.update();
gc.text1.size = oriSize;
setTimeout(() => {
gc.text2.pos.assign(c.w*0.44, c.h*0.6);
gc.text2.text = "pardon =(";
const oriSize = gc.text2.size;
gc.text2.size = c.w*0.025;
gc.text2.update();
gc.text2.size = oriSize;
}, 3000);
}
function matchStart()

View File

@@ -173,14 +173,30 @@ function scoreUpdate(data: ev.EventScoreUpdate)
function matchEnd(data: ev.EventMatchEnd)
{
if (data.winner === clientInfo.side) {
gc.text1.pos.assign(c.w*0.415, c.h_mid);
gc.text1.pos.assign(c.w*0.415, c.h*0.5);
gc.text1.text = "WIN";
if (data.forfeit) {
if (clientInfo.side === en.PlayerSide.left) {
gc.text2.pos.assign(c.w*0.65, c.h*0.42);
gc.text3.pos.assign(c.w*0.65, c.h*0.52);
}
else {
gc.text2.pos.assign(c.w*0.09, c.h*0.42);
gc.text3.pos.assign(c.w*0.09, c.h*0.52);
}
setTimeout(() => {
gc.text2.text = "par forfait";
}, 2500);
setTimeout(() => {
gc.text3.text = "calme ta joie";
}, 5000);
}
}
else {
gc.text1.pos.assign(c.w*0.383, c.h_mid);
gc.text1.pos.assign(c.w*0.383, c.h*0.5);
gc.text1.text = "LOSE";
}
// matchEnded = true;
// matchEnded = true; // unused
}
// export let matchEnded = false;
// export let matchEnded = false; // unused

View File

@@ -149,10 +149,13 @@ class GameSession {
});
}
private _newRound(s: GameSession, ball: Ball) {
const gc = s.components;
if (s._checkDisconnexions()) {
return;
}
// https://fr.wikipedia.org/wiki/Tennis_de_table#Nombre_de_manches
if (gc.scoreLeft >= 11 || gc.scoreRight >= 11)
// if (gc.scoreLeft >= 2 || gc.scoreRight >= 2) // WIP: for testing
const gc = s.components;
const minScore = 11;// can be changed for testing
if (gc.scoreLeft >= minScore || gc.scoreRight >= minScore)
{
if (Math.abs(gc.scoreLeft - gc.scoreRight) >= 2)
{
@@ -165,6 +168,29 @@ class GameSession {
ball.speed = ball.baseSpeed;
ball.ballInPlay = true;
}
private _checkDisconnexions() {
if (this.playersMap.size !== 2)
{
this.matchEnded = true;
if (this.playersMap.size != 0)
{
const gc = this.components;
const luckyWinner: ClientPlayer = this.playersMap.values().next().value;
let eventEnd: ev.EventMatchEnd;
if (luckyWinner.racket === gc.playerLeft) {
eventEnd = new ev.EventMatchEnd(en.PlayerSide.left, true);
console.log("Player Left WIN (by forfeit)");
}
else {
eventEnd = new ev.EventMatchEnd(en.PlayerSide.right, true);
console.log("Player Right WIN (by forfeit)");
}
luckyWinner.socket.send(JSON.stringify(eventEnd));
}
return true;
}
return false;
}
private _matchEnd(s: GameSession) {
s.matchEnded = true;
const gc = s.components;

View File

@@ -63,9 +63,11 @@ class EventScoreUpdate extends ServerEvent {
class EventMatchEnd extends ServerEvent {
winner: en.PlayerSide;
constructor(winner: en.PlayerSide) {
forfeit: boolean;
constructor(winner: en.PlayerSide, forfeit = false) {
super(en.EventTypes.matchEnd);
this.winner = winner;
this.forfeit = forfeit;
}
}