Implemented normal game mod with winning and losing
This commit is contained in:
parent
79d439e10a
commit
943af813e6
3 changed files with 357 additions and 10 deletions
159
src/index.ts
159
src/index.ts
|
|
@ -5,6 +5,8 @@ import { v4 as uuidv4 } from "uuid";
|
|||
import { Server, Socket } from "socket.io";
|
||||
import { instrument } from "@socket.io/admin-ui";
|
||||
|
||||
import { pallette, availableColors } from "./pallette";
|
||||
|
||||
const app = express();
|
||||
const server = http.createServer(app);
|
||||
|
||||
|
|
@ -53,20 +55,36 @@ class Game {
|
|||
private player1: Client;
|
||||
private player2: Client;
|
||||
|
||||
private guesses: Set<String>;
|
||||
private colors: Set<String>;
|
||||
|
||||
public get Id() {
|
||||
return this.id;
|
||||
}
|
||||
public get Player1() {
|
||||
public get Guesser() {
|
||||
return this.player1;
|
||||
}
|
||||
public get Player2() {
|
||||
public get Suggester() {
|
||||
return this.player2;
|
||||
}
|
||||
public get Guesses() {
|
||||
return this.guesses;
|
||||
}
|
||||
public get Colors() {
|
||||
return this.colors;
|
||||
}
|
||||
|
||||
constructor(id: String, player1: Client, player2: Client) {
|
||||
this.id = id;
|
||||
this.player1 = player1;
|
||||
this.player2 = player2;
|
||||
this.guesses = new Set<String>();
|
||||
this.colors = new Set<String>();
|
||||
|
||||
while (this.colors.size < 6) {
|
||||
const key = availableColors[Math.floor(Math.random() * availableColors.length)];
|
||||
this.colors.add(key);
|
||||
}
|
||||
}
|
||||
|
||||
public Opponent(player: Client): Client {
|
||||
|
|
@ -76,6 +94,35 @@ class Game {
|
|||
return this.player1;
|
||||
}
|
||||
|
||||
public Guess(player: Client, guess: String) {
|
||||
if (this.guesses.has(guess)) {
|
||||
return true;
|
||||
}
|
||||
this.guesses.add(guess);
|
||||
const opponent = this.Opponent(player);
|
||||
const opponentSocket = Array.from(onlineClients.keys()).find((socket) => onlineClients.get(socket) === opponent);
|
||||
if (opponentSocket) {
|
||||
opponentSocket.emit("guess", guess);
|
||||
} else {
|
||||
console.log("opponent socket not found");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public GameWon() {
|
||||
for (const color of this.colors) {
|
||||
if (!this.guesses.has(color)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public GameLost() {
|
||||
return this.guesses.size >= 10;
|
||||
}
|
||||
|
||||
public simplify() {
|
||||
return {
|
||||
id: this.id,
|
||||
|
|
@ -115,6 +162,10 @@ app.get("/", (req, res) => {
|
|||
res.send("<h1>Hello world</h1>");
|
||||
});
|
||||
|
||||
app.get("/pallette", (req, res) => {
|
||||
res.json(pallette);
|
||||
});
|
||||
|
||||
io.on("connection", (socket) => {
|
||||
console.log("someone connected");
|
||||
|
||||
|
|
@ -269,14 +320,22 @@ io.on("connection", (socket) => {
|
|||
socket.emit("requestResponseResult", false, 404);
|
||||
return;
|
||||
}
|
||||
const otherClient = onlineClients.get(otherClientSocket)!;
|
||||
|
||||
if (response) {
|
||||
const game = new Game(uuidv4(), client, onlineClients.get(otherClientSocket)!);
|
||||
const isClientInGuessers = guessersRoom.availablePlayers.has(client.login);
|
||||
guessersRoom.removePlayer(client);
|
||||
suggestersRoom.removePlayer(client);
|
||||
guessersRoom.removePlayer(otherClient);
|
||||
suggestersRoom.removePlayer(otherClient);
|
||||
io.emit("updateNeeded");
|
||||
|
||||
const game = new Game(uuidv4(), isClientInGuessers ? client : otherClient, isClientInGuessers ? otherClient : client);
|
||||
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());
|
||||
otherClient.setInGame(true);
|
||||
socket.emit("requestResponseResult", true, game.simplify(), Array.from(game.Guesses));
|
||||
otherClientSocket.emit("requestResponseResult", true, game.simplify(), Array.from(game.Colors));
|
||||
} else {
|
||||
socket.emit("requestResponseResult", false, `You declined ${client.login} request`);
|
||||
otherClientSocket.emit("requestResponseResult", false, `${client.login} declined your request`);
|
||||
|
|
@ -317,6 +376,87 @@ io.on("connection", (socket) => {
|
|||
});
|
||||
});
|
||||
|
||||
socket.on("guess", (gameId, guess) => {
|
||||
const client = onlineClients.get(socket);
|
||||
if (client === undefined) {
|
||||
socket.emit("guessResponse", false, 403);
|
||||
return;
|
||||
}
|
||||
|
||||
const game = runningGames.get(gameId);
|
||||
if (game === undefined || !client.inGame) {
|
||||
socket.emit("guessResponse", false, 400);
|
||||
console.log("user " + client.login + " tried to guess in non existing game");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (game.Guesser.login !== client.login) {
|
||||
socket.emit("guessResponse", false, 400);
|
||||
console.log("user " + client.login + " tried to guess in game he is not guessing in");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (!game.Guess(client, guess)) {
|
||||
socket.emit("guessResponse", false, 400);
|
||||
console.log("user " + client.login + " tried to guess but something gone terribly wrong");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
const opponentPlayer = game.Opponent(client);
|
||||
const otherClientSocket = Array.from(onlineClients.keys()).find((key) => onlineClients.get(key)?.login === opponentPlayer.login)!;
|
||||
if (game.GameWon()) {
|
||||
console.log(`game ${gameId} won by ${client.login} and ${opponentPlayer.login}`);
|
||||
|
||||
socket.emit("gameStatus", [true]);
|
||||
otherClientSocket.emit("gameStatus", [true]);
|
||||
|
||||
client.setInGame(false);
|
||||
opponentPlayer.setInGame(false);
|
||||
runningGames.delete(gameId);
|
||||
return;
|
||||
}
|
||||
if (game.GameLost()) {
|
||||
console.log(`game ${gameId} lost by ${client.login} and ${opponentPlayer.login}`);
|
||||
|
||||
socket.emit("gameStatus", [false]);
|
||||
otherClientSocket.emit("gameStatus", [false]);
|
||||
|
||||
client.setInGame(false);
|
||||
opponentPlayer.setInGame(false);
|
||||
runningGames.delete(gameId);
|
||||
}
|
||||
});
|
||||
|
||||
socket.on("leaveGame", (gameId) => {
|
||||
const client = onlineClients.get(socket);
|
||||
if (client === undefined) {
|
||||
socket.emit("guessResponse", false, 403);
|
||||
return;
|
||||
}
|
||||
|
||||
const game = runningGames.get(gameId);
|
||||
if (game === undefined || !client.inGame) {
|
||||
socket.emit("guessResponse", false, 400);
|
||||
console.log("user " + client.login + " tried to guess in non existing game");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
socket.emit("leaveGameResponse", true, 200);
|
||||
const opponentPlayer = game.Opponent(client);
|
||||
const otherClientSocket = Array.from(onlineClients.keys()).find((key) => onlineClients.get(key)?.login === opponentPlayer.login);
|
||||
if (otherClientSocket !== undefined) {
|
||||
otherClientSocket.emit("leaveGameResponse", true, 410);
|
||||
}
|
||||
|
||||
client.setInGame(false);
|
||||
opponentPlayer.setInGame(false);
|
||||
runningGames.delete(game.Id);
|
||||
});
|
||||
|
||||
socket.on("disconnect", () => {
|
||||
const client = onlineClients.get(socket);
|
||||
if (client !== undefined) {
|
||||
|
|
@ -324,14 +464,15 @@ io.on("connection", (socket) => {
|
|||
guessersRoom.removePlayer(client);
|
||||
suggestersRoom.removePlayer(client);
|
||||
if (client.inGame) {
|
||||
const game = Array.from(runningGames.values()).find((game) => game.Player1 === client || game.Player2 === client);
|
||||
const game = Array.from(runningGames.values()).find((game) => game.Guesser === client || game.Suggester === 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);
|
||||
otherClientSocket.emit("leaveGameResponse", true, 410);
|
||||
}
|
||||
client.setInGame(false);
|
||||
opponentPlayer.setInGame(false);
|
||||
|
||||
runningGames.delete(game.Id);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue