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
@@ -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;
}
@@ -87,10 +87,12 @@ export class GameService {
if (user.status === STATUS.IN_POOL || user.status === STATUS.IN_GAME)
{
await this.deleteToken(user);
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)
{
@@ -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);
}
@@ -59,8 +59,8 @@
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`, {
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({
@@ -70,16 +70,21 @@
isGameIsWithInvitation : options.isSomeoneIsInvited
})
});
const responseFromServer = await responseWhenGrantToken;
const responseInjson = await responseFromServer.json();
const token : string = responseInjson.token;
console.log("status : " + response.status);
const responseBody = await response.json();
const token : string = responseBody.token;
showWaitPage = false;
console.log("status : " + responseFromServer.status);
if (responseFromServer.status != 200)
if (response.ok && token)
{
console.log(responseInjson);
watchGameStateInterval = setInterval(watchGameState, watchGameStateIntervalRate);
pong.init(matchOptions, options, gameAreaId, token);
hiddenGame = false;
}
else
{
console.log(responseBody);
console.log("On refuse le ticket");
errorMessage = responseInjson.message;
errorMessage = responseBody.message;
showError = true;
options.reset(user.username);
setTimeout(() => {
@@ -88,14 +93,9 @@
errorMessage = "";
}, 5000);
}
else if (token)
{
watchGameStateInterval = setInterval(watchGameState, watchGameStateIntervalRate);
pong.init(matchOptions, options, gameAreaId, token);
hiddenGame = false;
} catch (e) {
console.log(e);
}
// 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) =>
@@ -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) => {
@@ -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
}
}
}
@@ -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,12 +198,15 @@ function gameUpdate(data: ev.EventGameUpdate)
function scoreUpdate(data: ev.EventScoreUpdate)
{
// console.log("scoreUpdate");
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;
}