|
|
@ -21,23 +21,27 @@ class ApiStore { |
|
|
gameStage: EGameStage = EGameStage.Start; |
|
|
gameStage: EGameStage = EGameStage.Start; |
|
|
version: string = ""; |
|
|
version: string = ""; |
|
|
currentGame: Game | undefined; |
|
|
currentGame: Game | undefined; |
|
|
errorState: string = ""; |
|
|
errorState: string | undefined; |
|
|
// endregion
|
|
|
|
|
|
// region consts
|
|
|
apiUrl: string | undefined; |
|
|
apiUrl = process.env.REACT_APP_API_URL; |
|
|
|
|
|
|
|
|
|
|
|
// endregion
|
|
|
// endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constructor() { |
|
|
constructor() { |
|
|
|
|
|
this.apiUrl = process.env.REACT_APP_API_URL_LOCAL; // REACT_APP_API_URL;
|
|
|
|
|
|
|
|
|
makeObservable(this, { |
|
|
makeObservable(this, { |
|
|
/* observable properties */ |
|
|
/* observable properties */ |
|
|
currentGame: observable, |
|
|
currentGame: observable, |
|
|
mockMode: observable, |
|
|
mockMode: observable, |
|
|
|
|
|
apiUrl: observable, |
|
|
version: observable, |
|
|
version: observable, |
|
|
errorState: observable, |
|
|
errorState: observable, |
|
|
gameStage: observable, |
|
|
gameStage: observable, |
|
|
/* actions */ |
|
|
/* actions */ |
|
|
setCurrentGame: action.bound, |
|
|
setCurrentGame: action.bound, |
|
|
|
|
|
setApiUrl: action.bound, |
|
|
setMockMode: action.bound, |
|
|
setMockMode: action.bound, |
|
|
setVersion: action.bound, |
|
|
setVersion: action.bound, |
|
|
setErrorState: action.bound, |
|
|
setErrorState: action.bound, |
|
|
@ -48,7 +52,7 @@ class ApiStore { |
|
|
|
|
|
|
|
|
// region public methods
|
|
|
// region public methods
|
|
|
async backgroundLoad(): Promise<void> { |
|
|
async backgroundLoad(): Promise<void> { |
|
|
if (this.mockMode) { |
|
|
if (this.mockMode || !this.apiUrl) { |
|
|
// mock mode
|
|
|
// mock mode
|
|
|
const p1 = mockService.getVersion() |
|
|
const p1 = mockService.getVersion() |
|
|
.then((version) => { |
|
|
.then((version) => { |
|
|
@ -63,9 +67,9 @@ class ApiStore { |
|
|
}); |
|
|
}); |
|
|
} else { |
|
|
} else { |
|
|
// api mode
|
|
|
// api mode
|
|
|
const p1 = service.getVersion() |
|
|
const p1 = service.getVersion(this.apiUrl) |
|
|
.then((version) => version && this.setVersion(version)); |
|
|
.then((version) => version && this.setVersion(version)); |
|
|
const p2 = service.getGame() |
|
|
const p2 = service.getGame(this.apiUrl) |
|
|
.then((game) => game && this.setCurrentGame(game)); |
|
|
.then((game) => game && this.setCurrentGame(game)); |
|
|
const promise = Promise.all([p1, p2]); |
|
|
const promise = Promise.all([p1, p2]); |
|
|
return promise.then(() => { |
|
|
return promise.then(() => { |
|
|
@ -77,11 +81,11 @@ class ApiStore { |
|
|
if (!this.currentGame) return Promise.resolve(false); |
|
|
if (!this.currentGame) return Promise.resolve(false); |
|
|
if (!this.currentGame?.guid) return Promise.resolve(false); |
|
|
if (!this.currentGame?.guid) return Promise.resolve(false); |
|
|
let promise; |
|
|
let promise; |
|
|
if (this.mockMode) { |
|
|
if (this.mockMode || !this.apiUrl) { |
|
|
// mock mode
|
|
|
// mock mode
|
|
|
promise = mockService.gameTurn(this.currentGame.guid, witchGuid); |
|
|
promise = mockService.gameTurn(this.currentGame.guid, witchGuid); |
|
|
} else { |
|
|
} else { |
|
|
promise = service.gameTurn(this.currentGame?.guid, witchGuid); |
|
|
promise = service.gameTurn(this.apiUrl, this.currentGame?.guid, witchGuid); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return promise.then((game) => { |
|
|
return promise.then((game) => { |
|
|
@ -103,6 +107,10 @@ class ApiStore { |
|
|
if (mockMode !== this.mockMode) this.mockMode = mockMode; |
|
|
if (mockMode !== this.mockMode) this.mockMode = mockMode; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
setApiUrl(apiUrl: string) { |
|
|
|
|
|
if (apiUrl !== this.apiUrl) this.apiUrl = apiUrl; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
setVersion(version: string) { |
|
|
setVersion(version: string) { |
|
|
if (version && version !== this.version) { |
|
|
if (version && version !== this.version) { |
|
|
this.version = version; |
|
|
this.version = version; |
|
|
@ -123,9 +131,9 @@ class ApiStore { |
|
|
|
|
|
|
|
|
//endregion
|
|
|
//endregion
|
|
|
startNewGame() { |
|
|
startNewGame() { |
|
|
const promise = (this.mockMode) |
|
|
const promise = (this.mockMode || !this.apiUrl) |
|
|
? mockService.getGame() |
|
|
? mockService.getGame() |
|
|
: service.getGame(); |
|
|
: service.getGame(this.apiUrl); |
|
|
promise.then((game) => { |
|
|
promise.then((game) => { |
|
|
if (game) { |
|
|
if (game) { |
|
|
this.setCurrentGame(game); |
|
|
this.setCurrentGame(game); |
|
|
@ -135,12 +143,12 @@ class ApiStore { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
tryToSetMockMode(flagEnabled: boolean) { |
|
|
tryToSetMockMode(flagEnabled: boolean) { |
|
|
if (!flagEnabled) { |
|
|
if (!flagEnabled || !this.apiUrl) { |
|
|
this.setMockMode(true); |
|
|
this.setMockMode(true); |
|
|
this.setGameStage(EGameStage.Start); |
|
|
this.setGameStage(EGameStage.Start); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
service.getVersion() |
|
|
service.getVersion(this.apiUrl) |
|
|
.then((version) => { |
|
|
.then((version) => { |
|
|
if (!version) { |
|
|
if (!version) { |
|
|
this.setMockMode(true); |
|
|
this.setMockMode(true); |
|
|
|