2020-04-07 20:41:35 +02:00
|
|
|
import {GameSceneInterface, GameScene} from "./GameScene";
|
|
|
|
import {ROOM} from "../../Enum/EnvironmentVariable"
|
2020-04-10 12:54:05 +02:00
|
|
|
import {Connexion, ListMessageUserPositionInterface} from "../../Connexion";
|
|
|
|
|
|
|
|
export enum StatusGameManagerEnum {
|
|
|
|
IN_PROGRESS = 1,
|
|
|
|
CURRENT_USER_CREATED = 2
|
|
|
|
}
|
2020-04-07 20:41:35 +02:00
|
|
|
|
|
|
|
export let ConnexionInstance : Connexion;
|
|
|
|
|
|
|
|
export interface GameManagerInterface {
|
|
|
|
GameScenes: Array<GameSceneInterface>;
|
2020-04-10 12:54:05 +02:00
|
|
|
status : number;
|
|
|
|
createCurrentPlayer() : void;
|
|
|
|
shareUserPosition(ListMessageUserPosition : ListMessageUserPositionInterface): void;
|
2020-04-07 20:41:35 +02:00
|
|
|
}
|
|
|
|
export class GameManager implements GameManagerInterface {
|
|
|
|
GameScenes: Array<GameSceneInterface> = [];
|
2020-04-10 12:54:05 +02:00
|
|
|
status: number;
|
2020-04-07 20:41:35 +02:00
|
|
|
|
|
|
|
constructor() {
|
2020-04-10 12:54:05 +02:00
|
|
|
this.status = StatusGameManagerEnum.IN_PROGRESS;
|
2020-04-07 20:41:35 +02:00
|
|
|
ConnexionInstance = new Connexion("test@gmail.com", this);
|
|
|
|
}
|
|
|
|
|
2020-04-10 12:54:05 +02:00
|
|
|
createGame(){
|
|
|
|
return ConnexionInstance.createConnexion().then((data: any) => {
|
|
|
|
this.configureGame();
|
|
|
|
/** TODO add loader in the page **/
|
|
|
|
}).catch((err) => {
|
|
|
|
console.error(err);
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* permit to config rooms
|
|
|
|
*/
|
2020-04-07 20:41:35 +02:00
|
|
|
configureGame() {
|
|
|
|
ROOM.forEach((roomId) => {
|
|
|
|
let newGame = new GameScene(roomId, this);
|
2020-04-10 12:54:05 +02:00
|
|
|
this.GameScenes.push((newGame as GameSceneInterface));
|
2020-04-07 20:41:35 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-10 12:54:05 +02:00
|
|
|
/**
|
|
|
|
* Permit to create player in started room
|
|
|
|
* @param RoomId
|
|
|
|
* @param UserId
|
|
|
|
*/
|
|
|
|
createCurrentPlayer(): void {
|
|
|
|
let game: GameSceneInterface = this.GameScenes.find((Game: GameSceneInterface) => Game.RoomId === ConnexionInstance.startedRoom);
|
|
|
|
game.createCurrentPlayer(ConnexionInstance.userId);
|
|
|
|
this.status = StatusGameManagerEnum.CURRENT_USER_CREATED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Share position in game
|
|
|
|
* @param ListMessageUserPosition
|
|
|
|
*/
|
|
|
|
shareUserPosition(ListMessageUserPosition: ListMessageUserPositionInterface): void {
|
|
|
|
if (this.status === StatusGameManagerEnum.IN_PROGRESS) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
let Game: GameSceneInterface = this.GameScenes.find((Game: GameSceneInterface) => Game.RoomId === ListMessageUserPosition.roomId);
|
|
|
|
if (!Game) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Game.shareUserPosition(ListMessageUserPosition.listUsersPosition)
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
2020-04-07 20:41:35 +02:00
|
|
|
}
|
|
|
|
}
|