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.

165 lines
4.0 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 mockService from './mockApiStore.service';
4 years ago
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;
4 years ago
// 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) {
// mock mode
const p1 = mockService.getVersion()
.then((version) => {
if (version) this.setVersion(version);
});
const p2 = mockService.getGame()
.then((game) => {
if (game) this.setCurrentGame(game);
});
const promise = Promise.all([p1, p2]);
return promise.then(() => {
});
} else {
// api mode
const p1 = service.getVersion()
.then((version) => version && this.setVersion(version));
const p2 = service.getGame()
.then((game) => game && this.setCurrentGame(game));
const promise = Promise.all([p1, p2]);
return promise.then(() => {
});
4 years ago
}
}
4 years ago
async turn(witchGuid: string): Promise<boolean> {
if (!this.currentGame) return Promise.resolve(false);
4 years ago
if (!this.currentGame?.guid) return Promise.resolve(false);
let promise;
if (this.mockMode) {
// mock mode
promise = mockService.gameTurn(this.currentGame.guid, witchGuid);
} else {
promise = service.gameTurn(this.currentGame?.guid, witchGuid);
}
4 years ago
return promise.then((game) => {
if (!game) return false;
const result = (game?.guid !== this.currentGame?.guid);
this.setCurrentGame(game);
return result;
});
4 years ago
}
4 years ago
// 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;
}
}
4 years ago
setErrorState(errorState: string) {
this.errorState = errorState;
if (errorState) {
this.gameStage = EGameStage.Start;
}
}
4 years ago
setGameStage(gameStage: EGameStage) {
if (gameStage === this.gameStage) return;
this.gameStage = gameStage;
}
4 years ago
//endregion
startNewGame() {
const promise = (this.mockMode)
? mockService.getGame()
: service.getGame();
promise.then((game) => {
if (game) {
this.setCurrentGame(game);
this.setGameStage(EGameStage.Process)
4 years ago
}
});
4 years ago
}
tryToSetMockMode(flagEnabled: boolean) {
if (!flagEnabled) {
this.setMockMode(true);
this.setGameStage(EGameStage.Start);
return;
}
service.getVersion()
.then((version) => {
if (!version) {
this.setMockMode(true);
this.setGameStage(EGameStage.Start);
return;
}
this.setVersion(version);
if (version) {
this.setMockMode(false);
this.setGameStage(EGameStage.Start);
}
})
.catch((error) => {
this.setMockMode(true);
this.setErrorState(error.message);
this.setGameStage(EGameStage.Start);
})
}
4 years ago
}
export default new ApiStore();