Merge branch 'luke' of bitbucket.org:LuckyLaszlo/ft_transcendence into cherif_back_and_game

This commit is contained in:
batche
2022-12-22 13:17:49 +01:00
7 changed files with 55 additions and 18 deletions

View File

@@ -2,9 +2,8 @@ DONE :
TODO : TODO :
- refactoring, - timeout in gameserver for private match (TO test)
Write independent init script like old pong.ts,
to limit the numbers of imports and clutter in svelte script.
- If in game, destroy game scripts stuff when changing page. - If in game, destroy game scripts stuff when changing page.
(I need to dig deeper in svelte to know how this could work) (I need to dig deeper in svelte to know how this could work)
- mode spectateur - mode spectateur

View File

@@ -168,12 +168,12 @@ function privateMatchmaking(player: ClientPlayer)
const maxPlayersNumber = 2; const maxPlayersNumber = 2;
privateMatchmakingMap.set(player.id, player); privateMatchmakingMap.set(player.id, player);
const matchOptions = player.matchOptions; const matchOptions = player.matchOptions;
console.log(player)
const token = player.token; const token = player.token;
const compatiblePlayers: ClientPlayer[] = []; const compatiblePlayers: ClientPlayer[] = [];
for (const [id, client] of privateMatchmakingMap) for (const [id, client] of privateMatchmakingMap)
{ {
if (client.token === token) if (client.token === token && client.username !== player.username)
{ {
compatiblePlayers.push(client); compatiblePlayers.push(client);
if (compatiblePlayers.length === maxPlayersNumber) { if (compatiblePlayers.length === maxPlayersNumber) {
@@ -188,6 +188,16 @@ function privateMatchmaking(player: ClientPlayer)
}); });
createGameSession(compatiblePlayers, matchOptions); createGameSession(compatiblePlayers, matchOptions);
} }
else {
setTimeout(function abortMatch() {
if (!player.gameSession)
{
// TODO: informe le back de l'invalidité du token d'invitation
player.socket.send(JSON.stringify( new ev.EventMatchAbort() ));
clientTerminate(player);
}
}, 60000);
}
} }
@@ -223,7 +233,7 @@ function createGameSession(playersArr: ClientPlayer[], matchOptions: en.MatchOpt
{ {
gameSessionsMap.delete(gameSession.id); gameSessionsMap.delete(gameSession.id);
gameSession.playersMap.forEach((client) => { gameSession.playersMap.forEach((client) => {
client.socket.send(JSON.stringify( new ev.ServerEvent(en.EventTypes.matchAbort) )); client.socket.send(JSON.stringify( new ev.EventMatchAbort() ));
client.gameSession = null; client.gameSession = null;
clientTerminate(client); clientTerminate(client);
}); });
@@ -245,27 +255,27 @@ async function playerReadyConfirmationListener(this: WebSocket, data: string)
{ {
// WIP nest , send gameSession.id // WIP nest , send gameSession.id
const gameSessionPlayersIterator = gameSession.playersMap.values(); const gameSessionPlayersIterator = gameSession.playersMap.values();
const response = await fetch(c.addressBackEnd + "/game/gameserver/creategame", const body = {
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
gameServerIdOfTheMatch : gameSession.id, gameServerIdOfTheMatch : gameSession.id,
gameOptions: gameSession.matchOptions, gameOptions: gameSession.matchOptions,
playerOneUsername: (<ClientPlayer>gameSessionPlayersIterator.next().value).username, playerOneUsername: (<ClientPlayer>gameSessionPlayersIterator.next().value).username,
playerTwoUsername: (<ClientPlayer>gameSessionPlayersIterator.next().value).username, playerTwoUsername: (<ClientPlayer>gameSessionPlayersIterator.next().value).username,
playerOneUsernameResult : 0, playerOneUsernameResult : 0,
playerTwoUsernameResult : 0 playerTwoUsernameResult : 0
};
}) const response = await fetch(c.addressBackEnd + "/game/gameserver/creategame",
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(body)
}); });
if (!response.ok) if (!response.ok)
{ {
gameSessionsMap.delete(gameSession.id); gameSessionsMap.delete(gameSession.id);
gameSession.playersMap.forEach((client) => { gameSession.playersMap.forEach((client) => {
client.socket.send(JSON.stringify( new ev.ServerEvent(en.EventTypes.matchAbort) )); client.socket.send(JSON.stringify( new ev.EventMatchAbort() ));
client.gameSession = null; client.gameSession = null;
clientTerminate(client); clientTerminate(client);
}); });

View File

@@ -71,6 +71,13 @@ export class EventMatchEnd extends ServerEvent {
} }
} }
export class EventMatchAbort extends ServerEvent {
constructor() {
super(en.EventTypes.matchAbort);
}
}
/* From Client */ /* From Client */
export class ClientEvent { export class ClientEvent {

View File

@@ -7,6 +7,7 @@
import * as enumeration from './shared_js/enums'; import * as enumeration from './shared_js/enums';
import * as pong from "./client/pong" import * as pong from "./client/pong"
import * as pongSpectator from "./client/pongSpectator" // TODO init spectator import * as pongSpectator from "./client/pongSpectator" // TODO init spectator
import { matchEnded, matchAbort } from "./client/ws";
//user's stuff //user's stuff
let user; let user;
@@ -102,7 +103,8 @@
if (invitation.token) if (invitation.token)
{ {
options.playerOneUsername = invitation.playerOneUsername; options.playerOneUsername = invitation.playerOneUsername;
options.playerTwoUsername = user.username; options.playerTwoUsername = invitation.playerTwoUsername;
options.isSomeoneIsInvited = true;
pong.init(options, gameAreaId, invitation.token); pong.init(options, gameAreaId, invitation.token);
showWaitPage = false showWaitPage = false
hiddenGame = false; hiddenGame = false;
@@ -148,7 +150,7 @@
console.log(error) console.log(error)
}) })
showInvitation() showInvitation() // maybe useless
initGameForPrivateParty(invitation) initGameForPrivateParty(invitation)
} }
</script> </script>
@@ -268,6 +270,9 @@
#game_page { #game_page {
margin: 0; margin: 0;
background-color: #222425; background-color: #222425;
position: relative;
width: 100%;
height: 100%;
} }
#canvas_container { #canvas_container {
margin-top: 20px; margin-top: 20px;

View File

@@ -53,6 +53,13 @@ export function init(options: InitOptions, gameAreaId: string, token: string)
export function destroy() export function destroy()
{ {
// TODO // TODO
if (pong)
{
clearInterval(pong.handleInputInterval);
clearInterval(pong.gameLoopInterval);
clearInterval(pong.drawLoopInterval);
initPong(null);
}
} }
function start() function start()

View File

@@ -11,6 +11,7 @@ import { sleep } from "./utils.js";
import { Vector, VectorInteger } from "../shared_js/class/Vector.js"; import { Vector, VectorInteger } from "../shared_js/class/Vector.js";
export let matchEnded = false; export let matchEnded = false;
export let matchAbort = false;
class ClientInfo { class ClientInfo {
id = ""; id = "";
@@ -87,6 +88,7 @@ function preMatchListener(this: WebSocket, event: MessageEvent)
startFunction(); startFunction();
break; break;
case en.EventTypes.matchAbort: case en.EventTypes.matchAbort:
matchAbort = true;
socket.removeEventListener("message", preMatchListener); socket.removeEventListener("message", preMatchListener);
msg.matchAbort(); msg.matchAbort();
break; break;

View File

@@ -71,6 +71,13 @@ export class EventMatchEnd extends ServerEvent {
} }
} }
export class EventMatchAbort extends ServerEvent {
constructor() {
super(en.EventTypes.matchAbort);
}
}
/* From Client */ /* From Client */
export class ClientEvent { export class ClientEvent {