fix error useEffect pong by putting it in field.tsx, add game session, when page closed nbr session work for me but maybe not at school
This commit is contained in:
parent
c578ee926c
commit
18706e168e
@ -6,7 +6,7 @@
|
|||||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/06/17 01:00:00 by apommier #+# #+# */
|
/* Created: 2023/06/17 01:00:00 by apommier #+# #+# */
|
||||||
/* Updated: 2023/06/26 10:16:19 by apommier ### ########.fr */
|
/* Updated: 2023/06/28 13:57:06 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -198,6 +198,24 @@ export class AppController {
|
|||||||
//========================================================================================================
|
//========================================================================================================
|
||||||
//========================================================================================================
|
//========================================================================================================
|
||||||
|
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Post('/addGame')
|
||||||
|
async addGame(@Request() req, @Body() data: any) {
|
||||||
|
const user = await this.userService.findOne(req.user.username);
|
||||||
|
user.gameSession += 1;
|
||||||
|
await this.userService.save(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Post('/rmGame')
|
||||||
|
async removeGame(@Request() req, @Body() data: any) {
|
||||||
|
const user = await this.userService.findOne(req.user.username);
|
||||||
|
user.gameSession -= 1;
|
||||||
|
if (user.gameSession === 0)
|
||||||
|
user.status = 1;
|
||||||
|
await this.userService.save(user);
|
||||||
|
}
|
||||||
|
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
@Post('/win')
|
@Post('/win')
|
||||||
async addWin(@Request() req, @Body() data: any) {
|
async addWin(@Request() req, @Body() data: any) {
|
||||||
|
|||||||
@ -63,6 +63,7 @@ export class loginClass {
|
|||||||
blocked: null,
|
blocked: null,
|
||||||
photo: null,
|
photo: null,
|
||||||
sessionNumber: 0,
|
sessionNumber: 0,
|
||||||
|
gameSession: 0,
|
||||||
};
|
};
|
||||||
await this.usersService.create(user);
|
await this.usersService.create(user);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -50,6 +50,9 @@ export class User {
|
|||||||
@Column({ default: 0 })
|
@Column({ default: 0 })
|
||||||
sessionNumber: number;
|
sessionNumber: number;
|
||||||
|
|
||||||
|
@Column({ default: 0 })
|
||||||
|
gameSession: number;
|
||||||
|
|
||||||
@Column({ default: 0 })
|
@Column({ default: 0 })
|
||||||
rank: number;
|
rank: number;
|
||||||
|
|
||||||
|
|||||||
@ -17,6 +17,7 @@ export interface User {
|
|||||||
friends: string[];
|
friends: string[];
|
||||||
blocked: string[];
|
blocked: string[];
|
||||||
sessionNumber: number;
|
sessionNumber: number;
|
||||||
|
gameSession : number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Conv {
|
export interface Conv {
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import React, { useEffect } from "react";
|
import React, { useEffect, useState } from "react";
|
||||||
import { Routes, Route, Navigate } from 'react-router-dom';
|
import { Routes, Route, Navigate } from 'react-router-dom';
|
||||||
import HomeLogin from "../pages/LoginButton.tsx";
|
import HomeLogin from "../pages/LoginButton.tsx";
|
||||||
|
|
||||||
@ -23,16 +23,17 @@ import api from "../script/axiosApi.tsx"
|
|||||||
|
|
||||||
function AnimatedRoute() {
|
function AnimatedRoute() {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const handleBeforeUnload = async (event: { preventDefault: () => void; returnValue: string; }) => {
|
// const handleBeforeUnload = async (event: { preventDefault: () => void; returnValue: string; }) => {
|
||||||
console.log("git ")
|
// console.log("git ")
|
||||||
if (!localStorage.getItem('token'))
|
// if (!localStorage.getItem('token'))
|
||||||
return;
|
// return;
|
||||||
try {
|
// try {
|
||||||
await api.post("/quit");
|
// await api.post("/quit");
|
||||||
} catch (err) {
|
// // navigator.sendBeacon("http://" + process.env.REACT_APP_BASE_URL + "/api/quit", {username: user.username})
|
||||||
console.log(err);
|
// } catch (err) {
|
||||||
}
|
// console.log(err);
|
||||||
};
|
// }
|
||||||
|
// };
|
||||||
|
|
||||||
const handleLoad = async () => {
|
const handleLoad = async () => {
|
||||||
if (!localStorage.getItem('token'))
|
if (!localStorage.getItem('token'))
|
||||||
@ -45,9 +46,17 @@ function AnimatedRoute() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
handleLoad();
|
handleLoad();
|
||||||
window.addEventListener('beforeunload', handleBeforeUnload);
|
|
||||||
|
window.addEventListener("beforeunload", async (event) => {
|
||||||
|
await api.post("/quit");
|
||||||
|
});
|
||||||
|
window.addEventListener("unload", async (event) => {
|
||||||
|
await api.post("/quit");
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
window.removeEventListener('beforeunload', handleBeforeUnload);
|
// window.removeEventListener('beforeunload', handleBeforeUnload);
|
||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
|||||||
@ -3,7 +3,7 @@ import DrawCanvas from './canvas.tsx';
|
|||||||
import queryString from 'query-string';
|
import queryString from 'query-string';
|
||||||
import '../styles/field.css';
|
import '../styles/field.css';
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
import api from '../script/axiosApi.tsx';
|
||||||
|
|
||||||
interface GameProps {
|
interface GameProps {
|
||||||
privateParty: boolean,
|
privateParty: boolean,
|
||||||
@ -13,6 +13,34 @@ interface GameProps {
|
|||||||
|
|
||||||
function Field()
|
function Field()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
|
||||||
|
const addGameSession = async () => {
|
||||||
|
try {
|
||||||
|
await api.post("/addGame");
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
addGameSession();
|
||||||
|
|
||||||
|
const handleBeforeUnload = async () => {
|
||||||
|
try {
|
||||||
|
await api.post("/rmGame");
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
window.addEventListener('beforeunload', handleBeforeUnload);
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener('beforeunload', handleBeforeUnload);
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const queryParams = queryString.parse(window.location.search);
|
const queryParams = queryString.parse(window.location.search);
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { useEffect } from 'react';
|
// import { useEffect } from 'react';
|
||||||
import api from '../script/axiosApi.tsx';
|
import api from '../script/axiosApi.tsx';
|
||||||
import io from 'socket.io-client';
|
import io from 'socket.io-client';
|
||||||
|
|
||||||
@ -11,20 +11,20 @@ interface GameProps {
|
|||||||
function DrawCanvas(option: number, gameParam: GameProps) {
|
function DrawCanvas(option: number, gameParam: GameProps) {
|
||||||
|
|
||||||
|
|
||||||
useEffect(() => {
|
// useEffect(() => {
|
||||||
const handleBeforeUnload = async (event: { preventDefault: () => void; returnValue: string; }) => {
|
// const handleBeforeUnload = async (event: { preventDefault: () => void; returnValue: string; }) => {
|
||||||
try {
|
// try {
|
||||||
await api.post("/status", {status: 1});
|
// await api.post("/status", {status: 1});
|
||||||
} catch (err) {
|
// } catch (err) {
|
||||||
console.log(err);
|
// console.log(err);
|
||||||
}
|
// }
|
||||||
};
|
// };
|
||||||
|
|
||||||
window.addEventListener('beforeunload', handleBeforeUnload);
|
// window.addEventListener('beforeunload', handleBeforeUnload);
|
||||||
return () => {
|
// return () => {
|
||||||
window.removeEventListener('beforeunload', handleBeforeUnload);
|
// window.removeEventListener('beforeunload', handleBeforeUnload);
|
||||||
};
|
// };
|
||||||
}, []);
|
// }, []);
|
||||||
|
|
||||||
console.log(`option= ${option}`);
|
console.log(`option= ${option}`);
|
||||||
const superpowerModifier = option & 1; // Retrieves the superpower modifier
|
const superpowerModifier = option & 1; // Retrieves the superpower modifier
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user