Merge branch 'luke'

This commit is contained in:
LuckyLaszlo
2023-01-09 11:02:58 +01:00
7 changed files with 74 additions and 53 deletions

View File

@@ -274,7 +274,7 @@ export class GameSession {
gc.scoreRight = 3;
}
else
{ // TODO: match draw, verifier la getion coté Nest
{
gc.scoreLeft = 0;
gc.scoreRight = 0;
}

View File

@@ -87,10 +87,12 @@ export class GameService {
if (user.status === STATUS.IN_POOL || user.status === STATUS.IN_GAME)
{
await this.deleteToken(user);
user.status = STATUS.CONNECTED;
if (user.status === STATUS.IN_POOL) {
user.status = STATUS.CONNECTED;
}
this.userRepository.save(user);
}
if (grantTicketDto.isGameIsWithInvitation === true)
if (grantTicketDto.isGameIsWithInvitation === true && user.status !== STATUS.IN_GAME)
{
const secondUser : Partial<User> = await this.userService.findOne(grantTicketDto.playerTwoUsername)
if (!secondUser || secondUser.username === user.username)
@@ -105,7 +107,7 @@ export class GameService {
this.userService.updateStatus(user.id, "In Pool")
return res.status(HttpStatus.OK).json({ token : encryptedTextToReturn });
}
else if (grantTicketDto.isGameIsWithInvitation === false) {
else if (grantTicketDto.isGameIsWithInvitation === false && user.status !== STATUS.IN_GAME) {
const encryptedTextToReturn = await this.encryptToken(user.username + '_'
+ grantTicketDto.gameOptions + '_' + grantTicketDto.isGameIsWithInvitation + '_' + new Date())
const tok = this.tokenGameRepository.create(grantTicketDto);
@@ -280,6 +282,7 @@ export class GameService {
{
this.userService.incrementDraws(playerOne.id)
this.userService.incrementDraws(playerTwo.id)
console.log("DRAW NEST");
}
else if (game.playerOneUsernameResult < game.playerTwoUsernameResult)
{

View File

@@ -193,7 +193,7 @@ export class UsersService {
.getOne();
if (!user.stats || !user)
throw new HttpException(`The user's stats could not be found.`,HttpStatus.NOT_FOUND);
user.stats.winGame++;
user.stats.drawGame++;
user.stats.totalGame++;
this.userRepository.save(user);
}

View File

@@ -59,43 +59,43 @@
optionsAreNotSet = false;
showWaitPage = true;
const matchOptions = pong.computeMatchOptions(options);
const responseWhenGrantToken = fetch(`http://${process.env.WEBSITE_HOST}:${process.env.WEBSITE_PORT}/api/v2/game/ticket`, {
method : "POST",
headers : {'Content-Type': 'application/json'},
body : JSON.stringify({
playerOneUsername : options.playerOneUsername,
playerTwoUsername : options.playerTwoUsername,
gameOptions : matchOptions,
isGameIsWithInvitation : options.isSomeoneIsInvited
})
});
const responseFromServer = await responseWhenGrantToken;
const responseInjson = await responseFromServer.json();
const token : string = responseInjson.token;
showWaitPage = false;
console.log("status : " + responseFromServer.status);
if (responseFromServer.status != 200)
{
console.log(responseInjson);
console.log("On refuse le ticket");
errorMessage = responseInjson.message;
showError = true;
options.reset(user.username);
setTimeout(() => {
optionsAreNotSet = true;
showError = false;
errorMessage = "";
}, 5000);
try {
const response = await fetch(`http://${process.env.WEBSITE_HOST}:${process.env.WEBSITE_PORT}/api/v2/game/ticket`, {
method : "POST",
headers : {'Content-Type': 'application/json'},
body : JSON.stringify({
playerOneUsername : options.playerOneUsername,
playerTwoUsername : options.playerTwoUsername,
gameOptions : matchOptions,
isGameIsWithInvitation : options.isSomeoneIsInvited
})
});
console.log("status : " + response.status);
const responseBody = await response.json();
const token : string = responseBody.token;
showWaitPage = false;
if (response.ok && token)
{
watchGameStateInterval = setInterval(watchGameState, watchGameStateIntervalRate);
pong.init(matchOptions, options, gameAreaId, token);
hiddenGame = false;
}
else
{
console.log(responseBody);
console.log("On refuse le ticket");
errorMessage = responseBody.message;
showError = true;
options.reset(user.username);
setTimeout(() => {
optionsAreNotSet = true;
showError = false;
errorMessage = "";
}, 5000);
}
} catch (e) {
console.log(e);
}
else if (token)
{
watchGameStateInterval = setInterval(watchGameState, watchGameStateIntervalRate);
pong.init(matchOptions, options, gameAreaId, token);
hiddenGame = false;
}
// TODO: Un "else" peut-être ? Si pas de token on fait un truc ?
// Si on ne rentre pas dans le else if, du coup il ne se passe rien.
}
const initGameForInvitedPlayer = async(invitation : any) =>

View File

@@ -2,20 +2,29 @@
import * as c from "./constants.js"
// export const soundPongArr: HTMLAudioElement[] = [];
export let muteFlag: boolean;
export const soundPongArr: HTMLAudioElement[] = [];
export let soundRoblox: HTMLAudioElement;
export function initAudio(sound: string)
{
let muteFlag: boolean;
soundPongArr.length = 0;
soundRoblox = null;
if (sound === "on") {
muteFlag = false;
}
else {
muteFlag = true;
return; // Could be changed
/*
Stop initAudio() here because in the current state of the game
there no way to change muteFlag after game start.
If it becomes an option,
we should continue initAudio() regardless of the muteFlag.
*/
}
soundPongArr.length = 0;
soundPongArr.push(new Audio("http://" + process.env.WEBSITE_HOST + ":" + process.env.WEBSITE_PORT + "/sound/pong/"+1+".ogg"));
soundPongArr.push(new Audio("http://" + process.env.WEBSITE_HOST + ":" + process.env.WEBSITE_PORT + "/sound/pong/"+2+".ogg"));
soundPongArr.forEach((value) => {

View File

@@ -2,7 +2,7 @@
import { Vector, VectorInteger } from "../../shared_js/class/Vector.js";
import type { GraphicComponent } from "../../shared_js/class/interface.js";
import { Rectangle, MovingRectangle, Racket, Ball } from "../../shared_js/class/Rectangle.js";
import { soundPongArr } from "../audio.js"
import { muteFlag, soundPongArr } from "../audio.js"
import { random } from "../utils.js";
function updateRectangle(this: RectangleClient) {
@@ -70,6 +70,7 @@ export class BallClient extends Ball implements GraphicComponent {
color: string;
update: () => void;
clear: (pos?: VectorInteger) => void;
soundSwitch = false;
constructor(pos: VectorInteger, size: number, baseSpeed: number, speedIncrease: number,
ctx: CanvasRenderingContext2D, color: string)
{
@@ -81,9 +82,14 @@ export class BallClient extends Ball implements GraphicComponent {
}
bounce(collider?: Rectangle) {
this._bounceAlgo(collider);
let i = Math.floor(random(0, soundPongArr.length));
soundPongArr[ i ].play();
console.log(`sound_i=${i}`); // debug log
if (!muteFlag)
{
this.soundSwitch = !this.soundSwitch;
soundPongArr[this.soundSwitch ? 1 : 0].play();
// let i = Math.floor(random(0, soundPongArr.length));
// soundPongArr[ i ].play();
// console.log(`sound_i=${i}`); // debug log
}
}
}

View File

@@ -6,7 +6,7 @@ import * as en from "../shared_js/enums.js"
import * as msg from "./message.js";
import type { RacketClient } from "./class/RectangleClient.js";
import { repeatInput } from "./handleInput.js";
import { soundRoblox } from "./audio.js"
import { muteFlag, soundRoblox } from "./audio.js"
import { sleep } from "./utils.js";
import { Vector, VectorInteger } from "../shared_js/class/Vector.js";
@@ -198,11 +198,14 @@ function gameUpdate(data: ev.EventGameUpdate)
function scoreUpdate(data: ev.EventScoreUpdate)
{
// console.log("scoreUpdate");
if (clientInfo.side === en.PlayerSide.left && data.scoreRight > gc.scoreRight.value) {
soundRoblox.play();
}
else if (clientInfo.side === en.PlayerSide.right && data.scoreLeft > gc.scoreLeft.value) {
soundRoblox.play();
if (!muteFlag)
{
if (clientInfo.side === en.PlayerSide.left && data.scoreRight > gc.scoreRight.value) {
soundRoblox.play();
}
else if (clientInfo.side === en.PlayerSide.right && data.scoreLeft > gc.scoreLeft.value) {
soundRoblox.play();
}
}
gc.scoreLeft.value = data.scoreLeft;
gc.scoreRight.value = data.scoreRight;