Moved models out of index.ts
This commit is contained in:
parent
a118bda224
commit
f4e75bd90b
2 changed files with 160 additions and 159 deletions
160
src/index.ts
160
src/index.ts
|
|
@ -5,6 +5,7 @@ import { v4 as uuidv4 } from "uuid";
|
||||||
import { Server, Socket } from "socket.io";
|
import { Server, Socket } from "socket.io";
|
||||||
import { instrument } from "@socket.io/admin-ui";
|
import { instrument } from "@socket.io/admin-ui";
|
||||||
|
|
||||||
|
import { Client, AvailableGame, Game } from "./models";
|
||||||
import { pallette, availableColors } from "./pallette";
|
import { pallette, availableColors } from "./pallette";
|
||||||
|
|
||||||
const app = express();
|
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<string>;
|
|
||||||
private colors: Set<string>;
|
|
||||||
|
|
||||||
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<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 {
|
|
||||||
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 registeredClients: Client[] = [];
|
||||||
let onlineClients: Map<Socket, Client> = new Map<Socket, Client>();
|
let onlineClients: Map<Socket, Client> = new Map<Socket, Client>();
|
||||||
let availableGames: Map<string, AvailableGame> = new Map<string, AvailableGame>();
|
let availableGames: Map<string, AvailableGame> = new Map<string, AvailableGame>();
|
||||||
|
|
|
||||||
159
src/models.ts
Normal file
159
src/models.ts
Normal file
|
|
@ -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<string>;
|
||||||
|
private colors: Set<string>;
|
||||||
|
|
||||||
|
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<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 {
|
||||||
|
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,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue