matchmaking OK
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
import { ClientPlayer } from "./Client";
|
import { ClientPlayer } from "./Client";
|
||||||
import {gameUpdate} from "../gameUpdate.js"
|
|
||||||
import { GameComponents } from "../../shared_js/class/GameComponents.js";
|
import { GameComponents } from "../../shared_js/class/GameComponents.js";
|
||||||
import { clientInputListener } from "../wsServer.js";
|
import { clientInputListener } from "../wsServer.js";
|
||||||
import * as c from "../constants.js"
|
import * as c from "../constants.js"
|
||||||
@@ -106,8 +105,8 @@ class GameSession {
|
|||||||
private _newRound(s: GameSession) {
|
private _newRound(s: GameSession) {
|
||||||
const gc = s.components;
|
const gc = s.components;
|
||||||
// https://fr.wikipedia.org/wiki/Tennis_de_table#Nombre_de_manches
|
// https://fr.wikipedia.org/wiki/Tennis_de_table#Nombre_de_manches
|
||||||
if (gc.scoreLeft >= 11
|
if (gc.scoreLeft >= 11 || gc.scoreRight >= 11)
|
||||||
|| gc.scoreRight >= 11)
|
// if (gc.scoreLeft >= 2 || gc.scoreRight >= 2) // WIP: for testing
|
||||||
{
|
{
|
||||||
if (Math.abs(gc.scoreLeft - gc.scoreRight) >= 2)
|
if (Math.abs(gc.scoreLeft - gc.scoreRight) >= 2)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,26 +0,0 @@
|
|||||||
|
|
||||||
import {EventTypes} from "../shared_js/enums.js"
|
|
||||||
import {EventGameUpdate} from "../shared_js/class/Event.js"
|
|
||||||
import { random } from "../shared_js/utils.js"; // temp
|
|
||||||
|
|
||||||
/*
|
|
||||||
import {Rectangle, MovingRectangle, Racket, Ball, Line} from "../shared_js/class/Rectangle.js";
|
|
||||||
import { Vector } from "../shared_js/class/Vector.js";
|
|
||||||
class CanvasRenderingContext2D {} // Empty object replacement to the web-API (web-API useless on server-side)
|
|
||||||
const mockCtx = new CanvasRenderingContext2D;
|
|
||||||
// @ts-ignore
|
|
||||||
const playerLeft = new Racket(mockCtx, new Vector(), "white", 1, 1, 1);
|
|
||||||
*/
|
|
||||||
|
|
||||||
function gameUpdate() : EventGameUpdate
|
|
||||||
{
|
|
||||||
const update: EventGameUpdate = {
|
|
||||||
type: EventTypes.gameUpdate,
|
|
||||||
playerLeft: {y: random(50, 650)},
|
|
||||||
playerRight: {y: random(50, 650)},
|
|
||||||
ball: {x: 0, y: 0, speed: 0}
|
|
||||||
};
|
|
||||||
return update;
|
|
||||||
}
|
|
||||||
|
|
||||||
export {gameUpdate}
|
|
||||||
@@ -18,6 +18,7 @@ const wsPort = 8042;
|
|||||||
export const wsServer = new WebSocketServer<WebSocket>({port: wsPort, path: "/pong"});
|
export const wsServer = new WebSocketServer<WebSocket>({port: wsPort, path: "/pong"});
|
||||||
|
|
||||||
const clientsMap: Map<string, Client> = new Map; // socket.id/Client
|
const clientsMap: Map<string, Client> = new Map; // socket.id/Client
|
||||||
|
const matchmakingPlayersMap: Map<string, ClientPlayer> = new Map; // socket.id/ClientPlayer (duplicates with clientsMap)
|
||||||
const gameSessionsMap: Map<string, GameSession> = new Map; // GameSession.id(url)/GameSession
|
const gameSessionsMap: Map<string, GameSession> = new Map; // GameSession.id(url)/GameSession
|
||||||
|
|
||||||
wsServer.on("connection", connectionListener);
|
wsServer.on("connection", connectionListener);
|
||||||
@@ -38,6 +39,13 @@ function connectionListener(socket: WebSocket, request: IncomingMessage)
|
|||||||
});
|
});
|
||||||
|
|
||||||
socket.on("message", function log(data: string) {
|
socket.on("message", function log(data: string) {
|
||||||
|
try {
|
||||||
|
const event: ev.ClientEvent = JSON.parse(data);
|
||||||
|
if (event.type === en.EventTypes.clientInput) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (e) {}
|
||||||
console.log("data: " + data);
|
console.log("data: " + data);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -50,8 +58,8 @@ function clientAnnounceListener(this: WebSocket, data: string)
|
|||||||
try {
|
try {
|
||||||
const msg : ev.ClientAnnounce = JSON.parse(data);
|
const msg : ev.ClientAnnounce = JSON.parse(data);
|
||||||
if (msg.type === en.EventTypes.clientAnnounce) {
|
if (msg.type === en.EventTypes.clientAnnounce) {
|
||||||
// TODO: reconnection with msg.id
|
// TODO: reconnection with msg.id ?
|
||||||
// TODO: spectator/player distinction with msg.type
|
// TODO: spectator/player distinction with msg.type ?
|
||||||
|
|
||||||
this.send(JSON.stringify( new ev.EventAssignId(this.id) ))
|
this.send(JSON.stringify( new ev.EventAssignId(this.id) ))
|
||||||
this.send(JSON.stringify( new ev.ServerEvent(en.EventTypes.matchmakingInProgress) ))
|
this.send(JSON.stringify( new ev.ServerEvent(en.EventTypes.matchmakingInProgress) ))
|
||||||
@@ -63,7 +71,7 @@ function clientAnnounceListener(this: WebSocket, data: string)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
console.log("Invalid JSON");
|
console.log("Invalid JSON (clientAnnounceListener)");
|
||||||
}
|
}
|
||||||
this.once("message", clientAnnounceListener);
|
this.once("message", clientAnnounceListener);
|
||||||
}
|
}
|
||||||
@@ -71,25 +79,41 @@ function clientAnnounceListener(this: WebSocket, data: string)
|
|||||||
|
|
||||||
function matchmaking(socket: WebSocket)
|
function matchmaking(socket: WebSocket)
|
||||||
{
|
{
|
||||||
// TODO Actual Matchmaking
|
const player: ClientPlayer = clientsMap.get(socket.id) as ClientPlayer;
|
||||||
|
if (matchmakingPlayersMap.size < 1)
|
||||||
// TODO: Only once
|
{
|
||||||
|
matchmakingPlayersMap.set(socket.id, player);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
const id = uuidv4();
|
const id = uuidv4();
|
||||||
const gameSession = new GameSession(id);
|
const gameSession = new GameSession(id);
|
||||||
gameSessionsMap.set(id, gameSession);
|
gameSessionsMap.set(id, gameSession);
|
||||||
|
|
||||||
// TODO: Per player
|
// for player
|
||||||
const player: ClientPlayer = clientsMap.get(socket.id) as ClientPlayer;
|
gameSession.playersMap.set(player.id, player);
|
||||||
player.gameSession = gameSession;
|
player.racket = gameSession.components.playerRight;
|
||||||
gameSession.playersMap.set(socket.id, player);
|
socket.send(JSON.stringify( new ev.EventMatchmakingComplete(en.PlayerSide.right) ));
|
||||||
gameSession.unreadyPlayersMap.set(socket.id, player);
|
|
||||||
|
|
||||||
player.racket = gameSession.components.playerLeft;
|
// for opponent
|
||||||
socket.send(JSON.stringify( new ev.EventMatchmakingComplete(en.PlayerSide.left) ));
|
const opponent: ClientPlayer = matchmakingPlayersMap.values().next().value;
|
||||||
|
gameSession.playersMap.set(opponent.id, opponent);
|
||||||
|
matchmakingPlayersMap.delete(opponent.id);
|
||||||
socket.once("message", playerReadyConfirmationListener);
|
opponent.racket = gameSession.components.playerLeft;
|
||||||
|
opponent.socket.send(JSON.stringify( new ev.EventMatchmakingComplete(en.PlayerSide.left) ));
|
||||||
|
|
||||||
|
// for both
|
||||||
|
gameSession.playersMap.forEach( (client) => {
|
||||||
|
gameSession.unreadyPlayersMap.set(client.id, client);
|
||||||
|
client.gameSession = gameSession;
|
||||||
|
});
|
||||||
|
gameSession.playersMap.forEach( (client) => {
|
||||||
|
/* set listener last to be absolutly sure there no early game launch
|
||||||
|
(unlikely, but theoretically possible) */
|
||||||
|
client.socket.once("message", playerReadyConfirmationListener);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -109,12 +133,12 @@ function playerReadyConfirmationListener(this: WebSocket, data: string)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
console.log("Invalid PlayerReady confirmation");
|
console.log("Invalid playerReadyConfirmation");
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
console.log("Invalid JSON");
|
console.log("Invalid JSON (playerReadyConfirmationListener)");
|
||||||
}
|
}
|
||||||
this.once("message", playerReadyConfirmationListener);
|
this.once("message", playerReadyConfirmationListener);
|
||||||
}
|
}
|
||||||
@@ -130,11 +154,11 @@ export function clientInputListener(this: WebSocket, data: string)
|
|||||||
client.gameSession.handleInput(client as ClientPlayer, input.input);
|
client.gameSession.handleInput(client as ClientPlayer, input.input);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
console.log("Invalid EventInput");
|
console.log("Invalid clientInput");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
console.log("Invalid JSON");
|
console.log("Invalid JSON (clientInputListener)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -146,6 +170,9 @@ const pingInterval = setInterval( () => {
|
|||||||
if (client.isAlive === false) {
|
if (client.isAlive === false) {
|
||||||
client.socket.terminate();
|
client.socket.terminate();
|
||||||
map.delete(key);
|
map.delete(key);
|
||||||
|
if (matchmakingPlayersMap.has(key)) {
|
||||||
|
matchmakingPlayersMap.delete(key);
|
||||||
|
}
|
||||||
console.log("%i: client %s is no more :'(", Date.now(), key);
|
console.log("%i: client %s is no more :'(", Date.now(), key);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,6 +48,7 @@ class EventMatchEnd extends ServerEvent {
|
|||||||
winner: en.PlayerSide;
|
winner: en.PlayerSide;
|
||||||
constructor(winner: en.PlayerSide) {
|
constructor(winner: en.PlayerSide) {
|
||||||
super(en.EventTypes.matchEnd);
|
super(en.EventTypes.matchEnd);
|
||||||
|
this.winner = winner;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user