Implemented game room as chat for now

This commit is contained in:
Andrew 2023-03-03 21:27:48 +07:00
parent 299760c52a
commit 79d439e10a
3 changed files with 150 additions and 17 deletions

View file

@ -1,5 +1,6 @@
import express, { Express, Request, Response } from "express";
import http from "http";
import { v4 as uuidv4 } from "uuid";
import { Server, Socket } from "socket.io";
import { instrument } from "@socket.io/admin-ui";
@ -7,7 +8,12 @@ import { instrument } from "@socket.io/admin-ui";
const app = express();
const server = http.createServer(app);
const io = new Server(server);
const io = new Server(server, {
cors: {
origin: ["https://admin.socket.io"],
credentials: true,
},
});
class Client {
private _login: String;
@ -33,23 +39,49 @@ class Client {
public setInGame(inGame: Boolean) {
this._inGame = inGame;
}
public simplify() {
return {
login: this.login,
inGame: this.inGame,
};
}
}
class Game {
private id: String;
private player1: Client;
private player2: Client;
private turn: Number;
private board: Number[][];
constructor(player1: Client, player2: Client) {
public get Id() {
return this.id;
}
public get Player1() {
return this.player1;
}
public get Player2() {
return this.player2;
}
constructor(id: String, player1: Client, player2: Client) {
this.id = id;
this.player1 = player1;
this.player2 = player2;
this.turn = 1;
this.board = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
];
}
public Opponent(player: Client): Client {
if (this.player1 === player) {
return this.player2;
}
return this.player1;
}
public simplify() {
return {
id: this.id,
player1: this.player1.login,
player2: this.player2.login,
};
}
}
@ -77,6 +109,7 @@ let registeredClients: Client[] = [];
let onlineClients: Map<Socket, Client> = new Map<Socket, Client>();
let guessersRoom: Room = new Room();
let suggestersRoom: Room = new Room();
let runningGames: Map<String, Game> = new Map<String, Game>();
app.get("/", (req, res) => {
res.send("<h1>Hello world</h1>");
@ -209,7 +242,7 @@ io.on("connection", (socket) => {
return;
}
const otherClientSocket = Array.from(onlineClients.keys()).find((key) => onlineClients.get(key)!!.login === sendTo);
const otherClientSocket = Array.from(onlineClients.keys()).find((key) => onlineClients.get(key)?.login === sendTo);
if (otherClientSocket === undefined) {
socket.emit("sendRequestResponse", false, 404);
return;
@ -219,12 +252,90 @@ io.on("connection", (socket) => {
socket.emit("sendRequestResponse", [true]);
});
socket.on("requestResponse", (requester: String, response: Boolean) => {
const client = onlineClients.get(socket);
if (client === undefined) {
socket.emit("requestResponseResult", false, 403);
return;
}
if (client.inGame || requester === client.login) {
socket.emit("requestResponseResult", false, 400);
return;
}
const otherClientSocket = Array.from(onlineClients.keys()).find((key) => onlineClients.get(key)?.login === requester);
if (otherClientSocket === undefined) {
socket.emit("requestResponseResult", false, 404);
return;
}
if (response) {
const game = new Game(uuidv4(), client, onlineClients.get(otherClientSocket)!);
runningGames.set(game.Id, game);
client.setInGame(true);
onlineClients.get(otherClientSocket)!.setInGame(true);
socket.emit("requestResponseResult", true, game.simplify());
otherClientSocket.emit("requestResponseResult", true, game.simplify());
} else {
socket.emit("requestResponseResult", false, `You declined ${client.login} request`);
otherClientSocket.emit("requestResponseResult", false, `${client.login} declined your request`);
}
});
socket.on("chat", (gameId, message) => {
const client = onlineClients.get(socket);
if (client === undefined) {
socket.emit("chatResponse", false, 403);
return;
}
const game = runningGames.get(gameId);
if (game === undefined || !client.inGame) {
socket.emit("chatResponse", false, 400);
console.log("user " + client.login + " tried to chat in non existing game");
return;
}
const opponentPlayer = game.Opponent(client);
const otherClientSocket = Array.from(onlineClients.keys()).find((key) => onlineClients.get(key)?.login === opponentPlayer.login);
if (otherClientSocket === undefined) {
socket.emit("chatResponse", false, 404);
console.log("user " + client.login + " tried to chat with non existing user");
return;
}
socket.emit("chatResponse", true, {
from: client.login,
message,
});
otherClientSocket.emit("chatResponse", true, {
from: client.login,
message,
});
});
socket.on("disconnect", () => {
const client = onlineClients.get(socket);
if (client !== undefined) {
console.log("user " + client.login + " disconnected");
guessersRoom.removePlayer(client);
suggestersRoom.removePlayer(client);
if (client.inGame) {
const game = Array.from(runningGames.values()).find((game) => game.Player1 === client || game.Player2 === client);
if (game !== undefined) {
const opponentPlayer = game.Opponent(client);
const otherClientSocket = Array.from(onlineClients.keys()).find((key) => onlineClients.get(key)?.login === opponentPlayer.login);
if (otherClientSocket !== undefined) {
// TODO: For now chatResponse used
otherClientSocket.emit("chatResponse", false, 410);
}
runningGames.delete(game.Id);
}
}
onlineClients.delete(socket);
} else {
console.log("anonymous disconnected");
@ -235,7 +346,12 @@ io.on("connection", (socket) => {
});
instrument(io, {
auth: false,
mode: "development",
auth: {
type: "basic",
username: "admin",
password: "$2a$12$84FqmSh7yVv46tdygZ2rNuJYqWPXYtYC3JxjSJBY8PyXB0Oe8qCfO",
},
});
server.listen(9800, () => {