Skip to content

Commit

Permalink
reorganisation and game starting in public matchmaking in process
Browse files Browse the repository at this point in the history
  • Loading branch information
a-boring-man committed Mar 27, 2023
1 parent dda4424 commit 3f8550e
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 68 deletions.
195 changes: 128 additions & 67 deletions back/nest_project/src/game_update_center/game_update_center.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,137 +5,205 @@ import { GameEngineService } from 'src/game_engine/game_engine.service';
import { PongEngineService } from 'src/pong_engine/pong_engine.service';
import { OnGatewayInit } from '@nestjs/websockets';

/**
* struct use to share the ball position
*/
interface ballpos {
x: number,
y: number,
}

/**
* struct use to share the game state
*/
interface gameState {
ballPosition: ballpos[],
paddleOne: {x: number, y: number },
paddleTwo: {x: number, y:number },
}

/**
* use in the waiting room for private matchmaking store
*/
export class Waiting_socket {
socket_id: Socket;
target;
game;
login;
}

/**
* struct use to store a pong game instance
*/
export class Pong_instance {
game_engine;
player: Socket[];
spectator: Socket[];
}

/**
* struct use to store a game game instance
*/
export class Game_instance {
game_engine;
player: Socket[];
spectator: Socket[];
}

/**
* use to share the player login at the start of a match
*/
export class Players {
l1: string;
l2: string;
}

/**
* main class regrouping all thing related to soket receiving and sending stuff
*/
@WebSocketGateway({
cors: {
origin: '*',
},
})
export class GameUpdateCenterGateway implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect{

game_instance: Game_instance[];
pong_instance: Pong_instance[];
logger: Logger = new Logger("GameGateway");
state;
pong_public_space: Socket[];
game_public_space: Socket[];
private_space: Waiting_socket[];
socket_login: Map<string, string>;
game_instance: Game_instance[]; // all gane instance
pong_instance: Pong_instance[]; // all pong instance
logger: Logger = new Logger("GameGateway"); // use to log thing into the console (inside the container)
pong_public_space: Socket[]; // waiting room for pong public matchmaking
game_public_space: Socket[]; // waiting room for game public matchmaking
private_space: Waiting_socket[]; // waiting room for private matchmaking
socket_login: Map<string, string>; // map all the socket with their login

constructor() {
this.game_instance = [];
this.pong_instance = [];
this.pong_public_space = [];
this.state = "pong";
}

@WebSocketServer()
server: Server;
/**
* take the socket in the public game room and shove them into a room to start a game instance for them
* @param client the second player socket
*/
startgameroom(client: Socket) {
// shortcut
let player1 = this.game_public_space[0];
let player2 = this.game_public_space[1];

// make the player join the room of name player1.socket.id
player1.join(player1.id);
player2.join(player1.id);

// set a game instance for the player and add it to the game instance []
let p = new Game_instance();
p.game_engine = new GameEngineService();
p.game_engine.set_player(player1, player2);
p.player.push(player1);
p.player.push(player2);
this.game_instance.push(p);

// emit the Player struct to the front to display the player login
let players = new Players();
players.l1 = this.socket_login[player1.id];
players.l2 = this.socket_login[player2.id];
this.server.to(player1.id).emit('players', players);

// debug line
console.log(player1.id + " and : " +player2.id + "where moved in the game room : " + player1.id);

// remove the player frome the waiting room
this.game_public_space.pop();
this.game_public_space.pop();
}

/**
* take the socket in the public pong room and shove them into a room to start a pong instance for them
* @param client the second player socker
*/
startpongroom(client: Socket) {
// shortcut
let player1 = this.pong_public_space[0];
let player2 = this.pong_public_space[1];

// make the player join the room of name player1.socket.id
player1.join(player1.id);
player2.join(player1.id);

// set a pong instance for the player and add it to the pong instance []
let p = new Pong_instance();
p.game_engine = new PongEngineService();
p.game_engine.set_player(player1, player2);
p.player.push(player1);
p.player.push(player2);
this.pong_instance.push(p);

// emit the Player struct to the front to display the player login
let players = new Players();
players.l1 = this.socket_login[player1.id];
players.l2 = this.socket_login[player2.id];
this.server.to(player1.id).emit('players', players);

// debug line
console.log(player1.id + " and : " +player2.id + "where moved in the pong room : " + player1.id);

// remove the player frome the waiting room
this.pong_public_space.pop();
this.pong_public_space.pop();
}

/**
*
* @param client the client's socket wanting to join a public matchmaking room
* @param body anything will start pong execpt if the string === "game"
*/
@SubscribeMessage('public matchmaking')
handlePublicMatchmaking(client: Socket, body: any) {
if (body === "game") {
this.game_public_space.push(client);
console.log("socket :" + client + "has been added to game public space");
if (this.game_public_space.length > 1) {
let player1 = this.game_public_space[0];
let player2 = this.game_public_space[1];

player1.join(player1.id);
player2.join(player1.id);

let p = new Game_instance();
p.game_engine = new GameEngineService();
p.game_engine.set_player(player1, player2);
p.player.push(player1);
p.player.push(player2);
this.game_instance.push(p);

let players = new Players();
players.l1 = this.socket_login[player1.id];
players.l2 = this.socket_login[player2.id];
this.server.to(player1.id).emit('players', players);

console.log(player1.id + " and : " +player2.id + "where moved in the game room : " + player1.id);

this.game_public_space.pop();
this.game_public_space.pop();
this.startgameroom(client);
}
}
else {
this.pong_public_space.push(client);
console.log("socket :" + client + "has been added to pong public space");
if (this.pong_public_space.length > 1) {
let player1 = this.pong_public_space[0];
let player2 = this.pong_public_space[1];

player1.join(player1.id);
player2.join(player1.id);

let p = new Pong_instance();
p.game_engine = new PongEngineService();
p.game_engine.set_player(player1, player2);
p.player.push(player1);
p.player.push(player2);
this.pong_instance.push(p);

let players = new Players();
players.l1 = this.socket_login[player1.id];
players.l2 = this.socket_login[player2.id];
this.server.to(player1.id).emit('players', players);

console.log(player1.id + " and : " +player2.id + "where moved in the pong room : " + player1.id);

this.pong_public_space.pop();
this.pong_public_space.pop();
this.startpongroom(client);
}
}
}

/**
*
* @param body the login associated with the socket
* @param client the soket of the client
*/
@SubscribeMessage('handshake')
handlehandsshake(@MessageBody() body: string, client: Socket) {
this.socket_login[client.id] = body;
this.socket_login[client.id] = body; // map the socket to a login
}

// @SubscribeMessage('ready')
// handleReady(client: Socket) {
// this.game_instance.forEach(element => {
// if (element.player.forEach);
// });
// }
@SubscribeMessage('ready')
handleReady(client: Socket) {
this.game_instance.forEach(element => {
element.player.forEach(player => {
if (client === player) {
element.game_engine.set_player_ready(player);
}
});
});
this.pong_instance.forEach(element => {
element.player.forEach(player => {
if (client === player) {
element.game_engine.set_player_ready(player);
}
});
});
}

@SubscribeMessage('private matchmaking')
handlePrivateMatchmaking(@MessageBody() body: any, client: Socket) {
Expand Down Expand Up @@ -181,13 +249,6 @@ export class GameUpdateCenterGateway implements OnGatewayInit, OnGatewayConnecti
// }
// }

@SubscribeMessage('Game_init')
OnGame_init(@MessageBody() body: any) {
if (body === "complex") {
this.state = "game";
}
}

@SubscribeMessage('joinning_room')
handleJoinRoom(@MessageBody() body: any, client: Socket) {
client.join(body.room);
Expand Down
22 changes: 21 additions & 1 deletion back/nest_project/src/pong_engine/pong_engine.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export class PongEngineService {
p2: Simple_paddle;
pl1: Socket;
pl2: Socket;
pl1_ready: boolean;
pl2_ready: boolean;
spectator: Socket[];
aspect_ratio = 16/9;
cooldown = 60;
Expand All @@ -34,8 +36,10 @@ export class PongEngineService {
this.ball = new Simple_ball();
this.p1 = new Simple_paddle();
this.p2 = new Simple_paddle();
this.spectator = [];
this.pl1_ready = false;
this.pl2_ready = false;
this.game_is_ready = false;
this.spectator = [];
this.cooldown_start = 0;
this.p2.x_position = this.aspect_ratio - 0.025;
this.gs = {ballPosition: [{x: this.ball.x_position, y: this.ball.y_position, r: this.ball.r}],
Expand All @@ -49,6 +53,22 @@ export class PongEngineService {
this.pl2 = player2;
console.log("2 player has been set the match can start player 1 :" + this.pl1.id + "player 2 :" + this.pl2.id);
}

/**
* check if both player are readym if so set the corresponding flag to true
* @param player the socket of the ready player
*/
set_player_ready(player: Socket) {
if (player === this.pl1) {
this.pl1_ready = true;
}
else if (player === this.pl2) {
this.pl2_ready = true;
}
if (this.pl1_ready && this.pl2_ready) {
this.game_is_ready = true;
}
}

main_loop() {
if (!this.game_is_ready) {
Expand Down

0 comments on commit 3f8550e

Please sign in to comment.