/resetuserstatus when player leave the gameserver

This commit is contained in:
lperrey
2023-01-12 19:52:28 +01:00
parent 20905b88de
commit 7e13dc3f61
5 changed files with 38 additions and 7 deletions

View File

@@ -26,7 +26,7 @@ services:
volumes:
- ./requirements/nestjs/api_back/src:/usr/app/src
- ./requirements/nestjs/api_back/test:/usr/app/test/
- nestjs_photos_volume:/usr/app/src/uploads/avatars
- nestjs_photos_volume:/usr/app/uploads/avatars
restart: unless-stopped
depends_on:
- postgresql

View File

@@ -6,6 +6,7 @@ import * as ev from "../../shared_js/class/Event.js"
import * as en from "../../shared_js/enums.js"
export class Client {
role: en.ClientRole;
socket: WebSocket;
id: string; // same as "socket.id"
isAlive: boolean = true;
@@ -23,9 +24,8 @@ export class ClientPlayer extends Client {
inputBuffer: ev.EventInput = new ev.EventInput();
lastInputId: number = 0;
racket: Racket;
constructor(socket: WebSocket, id: string, racket: Racket) {
constructor(socket: WebSocket, id: string) {
super(socket, id);
this.racket = racket;
}
}

View File

@@ -76,6 +76,8 @@ async function clientAnnounceListener(this: WebSocket, data: string)
if (msg.role === en.ClientRole.player)
{
const announce: ev.ClientAnnouncePlayer = <ev.ClientAnnouncePlayer>msg;
const player = clientsMap.get(this.id) as ClientPlayer;
player.role = msg.role;
const body = {
playerOneUsername: announce.username,
@@ -106,8 +108,6 @@ async function clientAnnounceListener(this: WebSocket, data: string)
clientTerminate(clientsMap.get(this.id));
return;
}
const player = clientsMap.get(this.id) as ClientPlayer;
player.matchOptions = announce.matchOptions;
player.token = announce.token;
player.username = announce.username;
@@ -126,13 +126,15 @@ async function clientAnnounceListener(this: WebSocket, data: string)
else if (msg.role === en.ClientRole.spectator)
{
const announce: ev.ClientAnnounceSpectator = <ev.ClientAnnounceSpectator>msg;
const spectator = clientsMap.get(this.id) as ClientSpectator;
spectator.role = msg.role;
const gameSession = gameSessionsMap.get(announce.gameSessionId);
if (!gameSession) {
this.send(JSON.stringify( new ev.EventError("invalid gameSessionId") ));
clientTerminate(clientsMap.get(this.id));
return;
}
const spectator = clientsMap.get(this.id) as ClientSpectator;
spectator.gameSession = gameSession;
gameSession.spectatorsMap.set(spectator.id, spectator);
spectator.socket.once("message", spectatorReadyConfirmationListener);
@@ -419,7 +421,7 @@ const pingInterval = setInterval( () => {
}, 4200);
export function clientTerminate(client: Client)
export async function clientTerminate(client: Client)
{
client.socket.terminate();
if (client.gameSession)
@@ -439,6 +441,23 @@ export function clientTerminate(client: Client)
else if (privateMatchmakingMap.has(client.id)) {
privateMatchmakingMap.delete(client.id);
}
if (client.role === en.ClientRole.player)
{
const player = client as ClientPlayer;
console.log("/resetuserstatus " + player.username);
const response = await fetch(c.addressBackEnd + "/game/gameserver/resetuserstatus",
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({username: player.username})
});
if (!response.ok) {
console.log("/resetuserstatus " + player.username + " failed");
}
}
}

View File

@@ -101,4 +101,9 @@ export class GameController {
{
return this.gameService.destroySession(token);
}
@Post('gameserver/resetuserstatus')
async resetUserStatus(@Body('username') username){
this.gameService.resetStatus(username);
}
}

View File

@@ -300,5 +300,12 @@ export class GameService {
this.userService.updateStatus(playerTwo.id, STATUS.CONNECTED)
return HttpStatus.OK
}
async resetStatus(username : string){
const user : User = await this.userRepository.findOneBy({username : username})
if (!user)
return HttpStatus.NOT_FOUND;
this.userService.updateStatus(user.id, STATUS.CONNECTED);
}
}