Skip to content

Commit

Permalink
public matchmaking and disconnection seems to work finem further test…
Browse files Browse the repository at this point in the history
… need front, remove the use of the error pronne delete function and foreach loop
  • Loading branch information
a-boring-man committed Mar 29, 2023
1 parent 86b78c0 commit 9bf51e6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ export class GameUpdateCenterGateway implements OnGatewayInit, OnGatewayConnecti
this.game_instance = [];
this.pong_instance = [];
this.pong_public_space = [];
this.game_public_space = [];
this.private_space = [];
this.socket_login = new Map<string, string>();
}

Expand Down Expand Up @@ -137,7 +139,8 @@ export class GameUpdateCenterGateway implements OnGatewayInit, OnGatewayConnecti
let p = new Pong_instance();
p.game_engine = new PongEngineService();
p.game_engine.set_player(player1, player2);
console.log(player1);
p.player = [];
p.spectator = [];
p.player.push(player1);
p.player.push(player2);
this.pong_instance.push(p);
Expand All @@ -163,17 +166,20 @@ export class GameUpdateCenterGateway implements OnGatewayInit, OnGatewayConnecti
*/
@SubscribeMessage('public matchmaking')
handlePublicMatchmaking(@ConnectedSocket() client: Socket, body: any) {
if (body === "game") {
if (body === "game" && this.game_public_space[0] != client) {
this.game_public_space.push(client);
console.log("socket :" + client + "has been added to game public space");
console.log("socket :" + client.id + "has been added to game public space");
if (this.game_public_space.length > 1) {
this.StartGameRoom(client);
}
}
else {
else if (this.pong_public_space[0] != client) {
console.log("array avant ", this.pong_public_space);
this.pong_public_space.push(client);
console.log("socket :" + client + "has been added to pong public space");
console.log("array apres ", this.pong_public_space);
console.log("socket :" + client.id + "has been added to pong public space");
if (this.pong_public_space.length > 1) {
console.log("pas normal");
this.StartPongRoom(client);
}
}
Expand All @@ -186,7 +192,7 @@ export class GameUpdateCenterGateway implements OnGatewayInit, OnGatewayConnecti
*/
@SubscribeMessage('handshake')
handlehandsshake(@MessageBody() body: string, @ConnectedSocket() client: Socket) {
console.log(client.id);
console.log("client : " + client.id + "has this pseudo :" + body);
this.socket_login.set(client.id, body); // map the socket to a login
}

Expand All @@ -202,7 +208,7 @@ export class GameUpdateCenterGateway implements OnGatewayInit, OnGatewayConnecti
for (let j = 0; j < element.player.length; j++) {
const player = element.player[j];
if (player === client) {
element.game_engine.set_player_ready(player, element.player[0].id)
element.game_engine.set_player_ready(player, this.server)
}
}
}
Expand All @@ -211,12 +217,11 @@ export class GameUpdateCenterGateway implements OnGatewayInit, OnGatewayConnecti
for (let j = 0; j < element.player.length; j++) {
const player = element.player[j];
if (player === client) {
element.game_engine.set_player_ready(player, element.player[0].id)
element.game_engine.set_player_ready(player, this.server)
}
}
}
}


afterInit(server: Server) { // log module initialization
this.logger.log("Initialized");
Expand Down Expand Up @@ -251,16 +256,16 @@ export class GameUpdateCenterGateway implements OnGatewayInit, OnGatewayConnecti
const spec = element.spectator[j];
if (client === spec) {
this.server.to(element.player[0].id).emit('spectateur disconnected');
delete element.spectator[j];
break;
element.spectator.splice(j, 1);
break game;
}
}
for (let j = 0; j < element.player.length; j++) {
const player = element.player[j];
if (player === client) {
this.server.to(element.player[0].id).emit('player_disconnection', this.socket_login.get(player.id));
element.game_engine.stop_game();
delete this.pong_instance[i];
this.pong_instance.splice(i, 1);
break game;
}
}
Expand All @@ -273,41 +278,47 @@ export class GameUpdateCenterGateway implements OnGatewayInit, OnGatewayConnecti
const spec = element.spectator[j];
if (client === spec) {
this.server.to(element.player[0].id).emit('spectateur disconnected');
delete element.spectator[j];
break;
element.spectator.splice(j, 1);
break pong;
}
}
for (let j = 0; j < element.player.length; j++) {
const player = element.player[j];
if (player === client) {
this.server.to(element.player[0].id).emit('player_disconnection', this.socket_login.get(player.id));
element.game_engine.stop_game();
delete this.pong_instance[i];
this.pong_instance.splice(i, 1);
break pong;
}
}
}

// // if the client was in pong_public_space
// this.pong_public_space.forEach((player, index) => {
// if (player === client) {
// delete this.pong_public_space[index];
// }
// });
// if the client was in pong_public_space
for (let i = 0; i < this.pong_public_space.length; i++) {
const element = this.pong_public_space;
if (element[i] === client) {
element.splice(i, 1);
break;
}
}

// // if the client was in game_public_space
// this.game_public_space.forEach((player, index) => {
// if (player === client) {
// delete this.game_public_space[index];
// }
// });
// if the client was in game_public_space
for (let i = 0; i < this.game_public_space.length; i++) {
const element = this.game_public_space;
if (element[i] === client) {
element.splice(i, 1);
break;
}
}

// // if the client was waiting for a private match
// this.private_space.forEach((element, index) => {
// if (element.socket === client) {
// delete this.private_space[index];
// }
// });
// if the client was waiting for a private match
for (let i = 0; i < this.private_space.length; i++) {
const element = this.private_space;
if (element[i].socket === client) {
element.splice(i, 1);
break;
}
}
}

// @SubscribeMessage('private matchmaking')
Expand Down
15 changes: 8 additions & 7 deletions back/nest_project/src/pong_engine/pong_engine.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class PongEngineService {
* check if both player are ready and start the game loop
* @param player the socket of the ready player
*/
set_player_ready(player: Socket, room) {
set_player_ready(player: Socket, server) {
if (player === this.pl1) {
this.pl1_ready = !this.pl1_ready;
}
Expand All @@ -83,14 +83,15 @@ export class PongEngineService {
}
if (this.pl1_ready && this.pl2_ready) {
this.game_is_ready = true;
let thiss = this;
this.loop = setInterval(function() {
if (this.game_most_stop) {
clearInterval(this.loop);
this.pl1_ready = false;
this.pl2_ready = false;
if (thiss.game_must_stop) {
clearInterval(thiss.loop);
thiss.pl1_ready = false;
thiss.pl2_ready = false;
}
this.main_loop();
room.emit('Game_Update', this.gs)
thiss.main_loop();
server.to(thiss.pl1.id).emit('Game_Update', thiss.gs)
}, 1000/60);
}
}
Expand Down

0 comments on commit 9bf51e6

Please sign in to comment.