WIP token identification
This commit is contained in:
@@ -244,7 +244,6 @@ export class GameSession {
|
||||
id: this.id,
|
||||
scoreLeft: gc.scoreLeft,
|
||||
scoreRight: gc.scoreRight,
|
||||
isPrivateMatch: this.isPrivateMatch,
|
||||
})
|
||||
}); */
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ import { gameSessionIdPLACEHOLDER } from "./constants.js";
|
||||
const wsPort = 8042;
|
||||
export const wsServer = new WebSocketServer<WebSocket>({host: "0.0.0.0", port: wsPort, path: "/pong"});
|
||||
const clientsMap: Map<string, Client> = new Map; // socket.id/Client
|
||||
const matchmakingPlayersMap: Map<string, ClientPlayer> = new Map; // socket.id/ClientPlayer (duplicates with clientsMap) // TODO: rename in matchmakingMap
|
||||
const matchmakingMap: Map<string, ClientPlayer> = new Map; // socket.id/ClientPlayer (duplicates with clientsMap)
|
||||
const privateMatchmakingMap: Map<string, ClientPlayer> = new Map; // socket.id/ClientPlayer (duplicates with clientsMap)
|
||||
const gameSessionsMap: Map<string, GameSession> = new Map; // GameSession.id(url)/GameSession
|
||||
|
||||
@@ -67,36 +67,38 @@ async function clientAnnounceListener(this: WebSocket, data: string)
|
||||
{
|
||||
const announce: ev.ClientAnnouncePlayer = <ev.ClientAnnouncePlayer>msg;
|
||||
|
||||
/* // WIP nest, fetch token validation
|
||||
// WIP nest, fetch token validation
|
||||
const body = {
|
||||
playerOneUsername: announce.username,
|
||||
playerTwoUsername: "",
|
||||
gameOptions: announce.matchOptions,
|
||||
isGameIsWithInvitation: announce.privateMatch,
|
||||
token: announce.token,
|
||||
};
|
||||
if (announce.privateMatch) {
|
||||
body.playerTwoUsername = announce.playerTwoUsername;
|
||||
}
|
||||
const response = await fetch(c.addressBackEnd + "/game/validateToken",
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
token: announce.token,
|
||||
username: announce.username,
|
||||
})
|
||||
body: JSON.stringify(body)
|
||||
});
|
||||
switch (response.status)
|
||||
if (!response.ok)
|
||||
{
|
||||
case 200:
|
||||
case 204:
|
||||
break;
|
||||
case 403:
|
||||
default:
|
||||
// send message to client ? or just gtfo ?
|
||||
clientTerminate(clientsMap.get(this.id));
|
||||
return;
|
||||
} */
|
||||
|
||||
// send message to client ? or just gtfo ?
|
||||
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;
|
||||
|
||||
this.send(JSON.stringify( new ev.EventAssignId(this.id) ));
|
||||
this.send(JSON.stringify( new ev.EventAssignId(this.id) )); // unused
|
||||
this.send(JSON.stringify( new ev.ServerEvent(en.EventTypes.matchmakingInProgress) ));
|
||||
if (announce.privateMatch) {
|
||||
privateMatchmaking(player);
|
||||
@@ -136,11 +138,11 @@ function publicMatchmaking(player: ClientPlayer)
|
||||
{
|
||||
const minPlayersNumber = 2;
|
||||
const maxPlayersNumber = 2;
|
||||
matchmakingPlayersMap.set(player.id, player);
|
||||
matchmakingMap.set(player.id, player);
|
||||
const matchOptions = player.matchOptions;
|
||||
|
||||
const compatiblePlayers: ClientPlayer[] = [];
|
||||
for (const [id, client] of matchmakingPlayersMap)
|
||||
for (const [id, client] of matchmakingMap)
|
||||
{
|
||||
if (client.matchOptions === matchOptions)
|
||||
{
|
||||
@@ -153,7 +155,7 @@ function publicMatchmaking(player: ClientPlayer)
|
||||
|
||||
if (compatiblePlayers.length >= minPlayersNumber) {
|
||||
compatiblePlayers.forEach((client) => {
|
||||
matchmakingPlayersMap.delete(client.id);
|
||||
matchmakingMap.delete(client.id);
|
||||
});
|
||||
createGameSession(compatiblePlayers, matchOptions);
|
||||
}
|
||||
@@ -309,7 +311,8 @@ const pingInterval = setInterval( () => {
|
||||
}
|
||||
console.log("gameSessionMap size: " + gameSessionsMap.size);
|
||||
console.log("clientsMap size: " + clientsMap.size);
|
||||
console.log("matchmakingPlayersMap size: " + matchmakingPlayersMap.size);
|
||||
console.log("matchmakingMap size: " + matchmakingMap.size);
|
||||
console.log("privateMatchmakingMap size: " + privateMatchmakingMap.size);
|
||||
console.log("");
|
||||
}, 4200);
|
||||
|
||||
@@ -329,8 +332,8 @@ function clientTerminate(client: Client)
|
||||
}
|
||||
}
|
||||
clientsMap.delete(client.id);
|
||||
if (matchmakingPlayersMap.has(client.id)) {
|
||||
matchmakingPlayersMap.delete(client.id);
|
||||
if (matchmakingMap.has(client.id)) {
|
||||
matchmakingMap.delete(client.id);
|
||||
}
|
||||
else if (privateMatchmakingMap.has(client.id)) {
|
||||
privateMatchmakingMap.delete(client.id);
|
||||
|
||||
@@ -94,12 +94,16 @@ export class ClientAnnouncePlayer extends ClientAnnounce {
|
||||
token: string;
|
||||
username: string;
|
||||
privateMatch: boolean;
|
||||
constructor(matchOptions: en.MatchOptions, token: string, username: string, privateMatch: boolean = false) {
|
||||
playerTwoUsername?: string;
|
||||
constructor(matchOptions: en.MatchOptions, token: string, username: string, privateMatch: boolean = false, playerTwoUsername?: string) {
|
||||
super(en.ClientRole.player);
|
||||
this.matchOptions = matchOptions;
|
||||
this.token = token;
|
||||
this.username = username;
|
||||
this.privateMatch = privateMatch;
|
||||
if (playerTwoUsername) {
|
||||
this.playerTwoUsername = playerTwoUsername;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user