From f4e75bd90b7973b49e6c79f746980d98238da6fe Mon Sep 17 00:00:00 2001 From: Andrew nuark G Date: Sun, 5 Mar 2023 14:26:55 +0700 Subject: [PATCH] Moved models out of index.ts --- src/index.ts | 160 +------------------------------------------------- src/models.ts | 159 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 160 insertions(+), 159 deletions(-) create mode 100644 src/models.ts diff --git a/src/index.ts b/src/index.ts index 4030e9a..df5956f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,6 +5,7 @@ import { v4 as uuidv4 } from "uuid"; import { Server, Socket } from "socket.io"; import { instrument } from "@socket.io/admin-ui"; +import { Client, AvailableGame, Game } from "./models"; import { pallette, availableColors } from "./pallette"; const app = express(); @@ -17,165 +18,6 @@ const io = new Server(server, { }, }); -class Client { - private _login: string; - private _password: string; - private _inGame: Boolean; - - public get login() { - return this._login; - } - public get password() { - return this._password; - } - public get inGame() { - return this._inGame; - } - - constructor(login: string, password: string) { - this._login = login; - this._password = password; - this._inGame = false; - } - - public setInGame(inGame: Boolean) { - this._inGame = inGame; - } - - public simplify() { - return { - login: this.login, - inGame: this.inGame, - }; - } -} - -class Game { - private id: string; - private guesser: Client; - private suggester: Client; - private tries: number; - - private guesses: Set; - private colors: Set; - - public get Id() { - return this.id; - } - public get Guesser() { - return this.guesser; - } - public get Suggester() { - return this.suggester; - } - public get Tries() { - return this.tries; - } - public get Guesses() { - return this.guesses; - } - public get Colors() { - return this.colors; - } - - constructor(id: string, guesser: Client, suggester: Client, tries: number) { - this.id = id; - this.guesser = guesser; - this.suggester = suggester; - this.tries = tries; - this.guesses = new Set(); - this.colors = new Set(); - - while (this.colors.size < 6) { - const key = availableColors[Math.floor(Math.random() * availableColors.length)]; - this.colors.add(key); - } - } - - public Opponent(player: Client): Client { - if (this.guesser === player) { - return this.suggester; - } - return this.guesser; - } - - 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() { - const countOfGuesses = this.guesses.size; - const countOfCorrectGuesses = Array.from(this.guesses).filter((guess) => this.colors.has(guess)).length; - return countOfGuesses - countOfCorrectGuesses >= this.tries; - } - - public simplify() { - return { - id: this.id, - guesser: this.guesser.login, - suggester: this.suggester.login, - tries: this.tries, - }; - } -} - -class AvailableGame { - private _id: string; - private _player: Client; - private _tries: number; - private _neededRole: string; - - public get id(): string { - return this._id; - } - public get player(): Client { - return this._player; - } - public get tries(): number { - return this._tries; - } - public get neededRole(): string { - return this._neededRole; - } - - constructor(id: string, player: Client, tries: number, neededRole: string) { - this._id = id; - this._player = player; - this._tries = tries; - this._neededRole = neededRole; - } - - simplify() { - return { - id: this.id, - player: this.player.login, - tries: this.tries, - neededRole: this.neededRole, - }; - } -} - let registeredClients: Client[] = []; let onlineClients: Map = new Map(); let availableGames: Map = new Map(); diff --git a/src/models.ts b/src/models.ts new file mode 100644 index 0000000..fa1b41b --- /dev/null +++ b/src/models.ts @@ -0,0 +1,159 @@ +import { Socket } from "socket.io"; +import { availableColors } from "./pallette"; + +export class Client { + private _login: string; + private _password: string; + private _inGame: Boolean; + private _socket: Socket; + + public get login() { + return this._login; + } + public get password() { + return this._password; + } + public get inGame() { + return this._inGame; + } + public get socket() { + return this._socket; + } + + constructor(login: string, password: string, socket: Socket) { + this._login = login; + this._password = password; + this._inGame = false; + this._socket = socket; + } + + public setInGame(inGame: Boolean) { + this._inGame = inGame; + } + + public simplify() { + return { + login: this.login, + inGame: this.inGame, + }; + } +} + +export class Game { + private id: string; + private guesser: Client; + private suggester: Client; + private tries: number; + + private guesses: Set; + private colors: Set; + + public get Id() { + return this.id; + } + public get Guesser() { + return this.guesser; + } + public get Suggester() { + return this.suggester; + } + public get Tries() { + return this.tries; + } + public get Guesses() { + return this.guesses; + } + public get Colors() { + return this.colors; + } + + constructor(id: string, guesser: Client, suggester: Client, tries: number) { + this.id = id; + this.guesser = guesser; + this.suggester = suggester; + this.tries = tries; + this.guesses = new Set(); + this.colors = new Set(); + + while (this.colors.size < 6) { + const key = availableColors[Math.floor(Math.random() * availableColors.length)]; + this.colors.add(key); + } + } + + public Opponent(player: Client): Client { + if (this.guesser === player) { + return this.suggester; + } + return this.guesser; + } + + public Guess(player: Client, guess: string) { + if (this.guesses.has(guess)) { + return true; + } + this.guesses.add(guess); + this.Opponent(player).socket.emit("guess", guess); + return true; + } + + public GameWon() { + for (const color of this.colors) { + if (!this.guesses.has(color)) { + return false; + } + } + return true; + } + + public GameLost() { + const countOfGuesses = this.guesses.size; + const countOfCorrectGuesses = Array.from(this.guesses).filter((guess) => this.colors.has(guess)).length; + return countOfGuesses - countOfCorrectGuesses >= this.tries; + } + + public simplify() { + return { + id: this.id, + guesser: this.guesser.login, + suggester: this.suggester.login, + tries: this.tries, + }; + } +} + +export class AvailableGame { + private _id: string; + private _player: Client; + private _tries: number; + private _neededRole: string; + + public get id(): string { + return this._id; + } + public get player(): Client { + return this._player; + } + public get tries(): number { + return this._tries; + } + public get neededRole(): string { + return this._neededRole; + } + + constructor(id: string, player: Client, tries: number, neededRole: string) { + this._id = id; + this._player = player; + this._tries = tries; + this._neededRole = neededRole; + } + + simplify() { + return { + id: this.id, + player: this.player.login, + tries: this.tries, + neededRole: this.neededRole, + }; + } +}