You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
132 lines
3.2 KiB
132 lines
3.2 KiB
|
4 years ago
|
/**
|
||
|
|
* @file Хранилище для источника данных
|
||
|
|
* @version 2022.01.26
|
||
|
|
* @author Verevkin S.A.
|
||
|
|
* @copyright Verevkin S.A.
|
||
|
|
*/
|
||
|
|
|
||
|
|
import { action, makeObservable, observable } from 'mobx';
|
||
|
|
import { Game } from '../../model/Game';
|
||
|
|
import service from './apiStore.service';
|
||
|
|
import { Witch } from '../../model/Witch';
|
||
|
|
|
||
|
|
export enum EGameStage {
|
||
|
|
Start = 0,
|
||
|
|
Process = 1
|
||
|
|
}
|
||
|
|
|
||
|
|
class ApiStore {
|
||
|
|
// region observable properties
|
||
|
|
mockMode: boolean = true;
|
||
|
|
gameStage: EGameStage = EGameStage.Start;
|
||
|
|
version: string = "";
|
||
|
|
currentGame: Game | undefined;
|
||
|
|
errorState: string = "";
|
||
|
|
// endregion
|
||
|
|
// region consts
|
||
|
|
apiUrl = process.env.REACT_APP_API_URL;
|
||
|
|
// endregion
|
||
|
|
|
||
|
|
constructor() {
|
||
|
|
makeObservable(this, {
|
||
|
|
/* observable properties */
|
||
|
|
currentGame: observable,
|
||
|
|
mockMode: observable,
|
||
|
|
version: observable,
|
||
|
|
errorState: observable,
|
||
|
|
gameStage: observable,
|
||
|
|
/* actions */
|
||
|
|
setCurrentGame: action.bound,
|
||
|
|
setMockMode: action.bound,
|
||
|
|
setVersion: action.bound,
|
||
|
|
setErrorState: action.bound,
|
||
|
|
setGameStage: action.bound,
|
||
|
|
});
|
||
|
|
this.backgroundLoad();
|
||
|
|
}
|
||
|
|
|
||
|
|
// region public methods
|
||
|
|
async backgroundLoad(): Promise<void> {
|
||
|
|
if (this.mockMode) {
|
||
|
|
const ar: Witch[] = [{
|
||
|
|
guid: '1', position: 0, flagReal: false,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
guid: '2', position: 1, flagReal: true,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
guid: '3', position: 2, flagReal: false,
|
||
|
|
},
|
||
|
|
];
|
||
|
|
this.setCurrentGame({
|
||
|
|
guid: '1',
|
||
|
|
witches: ar,
|
||
|
|
score: 0,
|
||
|
|
})
|
||
|
|
return Promise.resolve();
|
||
|
|
}
|
||
|
|
service.getVersion()
|
||
|
|
.then((version) => version && this.setVersion(version));
|
||
|
|
service.getGame()
|
||
|
|
.then((game) => game && this.setCurrentGame(game));
|
||
|
|
|
||
|
|
}
|
||
|
|
async turn(witchGuid: string): Promise<boolean> {
|
||
|
|
if (this.mockMode) return Promise.resolve(false);
|
||
|
|
if (!this.currentGame?.guid) return Promise.resolve(false);
|
||
|
|
|
||
|
|
return service.gameTurn(this.currentGame?.guid, witchGuid)
|
||
|
|
.then((game) => {
|
||
|
|
if (!game) return false;
|
||
|
|
const result = (game?.guid !== this.currentGame?.guid);
|
||
|
|
this.setCurrentGame(game);
|
||
|
|
return result;
|
||
|
|
})
|
||
|
|
}
|
||
|
|
// endregion
|
||
|
|
|
||
|
|
// region setters
|
||
|
|
setCurrentGame(game: Game) {
|
||
|
|
this.currentGame = game;
|
||
|
|
}
|
||
|
|
|
||
|
|
setMockMode(mockMode: boolean) {
|
||
|
|
if (mockMode !== this.mockMode) this.mockMode = mockMode;
|
||
|
|
}
|
||
|
|
|
||
|
|
setVersion(version: string) {
|
||
|
|
if (version && version !== this.version) {
|
||
|
|
this.version = version;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
setErrorState(errorState: string) {
|
||
|
|
this.errorState = errorState;
|
||
|
|
if (errorState) {
|
||
|
|
this.gameStage = EGameStage.Start;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
setGameStage(gameStage: EGameStage) {
|
||
|
|
if (gameStage === this.gameStage) return;
|
||
|
|
this.gameStage = gameStage;
|
||
|
|
}
|
||
|
|
//endregion
|
||
|
|
startNewGame() {
|
||
|
|
if (this.mockMode) {
|
||
|
|
if (this.currentGame) {
|
||
|
|
this.currentGame.guid = Date.now().toString(10);
|
||
|
|
}
|
||
|
|
this.setGameStage(EGameStage.Process)
|
||
|
|
} else {
|
||
|
|
service.getGame()
|
||
|
|
.then((game) => {
|
||
|
|
if (game) {
|
||
|
|
this.setCurrentGame(game);
|
||
|
|
this.setGameStage(EGameStage.Process)
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export default new ApiStore();
|