97 lines
4.2 KiB
TypeScript
97 lines
4.2 KiB
TypeScript
import { Ref, ref } from 'vue'
|
|
import { callApi, QueryParams } from '@/composables/engine/callApi'
|
|
import { start, stop } from '@/composables/engine/startStop'
|
|
import { fetchUpdate } from '@/composables/engine/fetchUpdate'
|
|
import { loadQuotes, getQuotesRef, deleteQuote, saveQuote } from '@/composables/engine/quotes'
|
|
import { fetchGameInfo, fetchGameInfos, setGameLang, setGameName, savePlayer, deletePlayer, createGame, cameo, logoutCameo } from '@/composables/engine/gameManagement'
|
|
import { collectQuotes, startGame, continueGame, resetGame, finishGame, disableGame, removeGame } from '@/composables/engine/gameState'
|
|
import { saveSelection } from '@/composables/engine/play'
|
|
import type { Quotes, GameInfo, GameInfos, PlayerEdit, Lang, CreateGameStatus } from '@/composables/engine.d'
|
|
|
|
export interface EngineContext {
|
|
isActive: boolean
|
|
shouldStop: boolean
|
|
version: number
|
|
lastFetched: Array<Date>
|
|
isConnected: Ref<boolean>
|
|
retry: Ref<number>
|
|
callApi: (path: string, queryParams?: QueryParams) => Promise<unknown>
|
|
fetchUpdate: () => Promise<void>
|
|
loadQuotes: () => Promise<void>
|
|
}
|
|
|
|
export interface useEngine {
|
|
isConnected: Ref<boolean>
|
|
retry: Ref<number>
|
|
start(): void
|
|
stop(): void
|
|
fetchUpdate(): void
|
|
loadQuotes(): Promise<void>
|
|
getQuotesRef(): Ref<Quotes>
|
|
createQuote(quote: string): Promise<void>
|
|
saveQuote(id: string, quote: string): Promise<void>
|
|
deleteQuote(id: string): Promise<void>
|
|
collectQuotes: () => Promise<void>
|
|
startGame: () => Promise<void>
|
|
continueGame: () => Promise<void>
|
|
resetGame: () => Promise<void>
|
|
finishGame: () => Promise<void>
|
|
disableGame: () => Promise<void>
|
|
removeGame: () => Promise<void>
|
|
saveSelection: (selection: string) => Promise<void>
|
|
fetchGameInfo: () => Promise<GameInfo>
|
|
fetchGameInfos: () => Promise<GameInfos>
|
|
setGameLang: (lang: string) => Promise<void>
|
|
setGameName: (name: string) => Promise<void>
|
|
savePlayer: (player: PlayerEdit) => Promise<void>
|
|
deletePlayer: (id: string) => Promise<void>
|
|
createGame: (name: string, teamname: string, lang: Lang) => Promise<CreateGameStatus>
|
|
cameo: (authcode: string) => Promise<void>
|
|
logoutCameo: () => Promise<void>
|
|
}
|
|
|
|
export default (): useEngine => {
|
|
const now = new Date(0)
|
|
const context: EngineContext = {
|
|
lastFetched: [now, now, now, now, now],
|
|
isActive: false,
|
|
shouldStop: false,
|
|
version: -1,
|
|
isConnected: ref(false),
|
|
retry: ref(0),
|
|
callApi,
|
|
fetchUpdate,
|
|
loadQuotes,
|
|
}
|
|
|
|
return {
|
|
isConnected: context.isConnected,
|
|
retry: context.retry,
|
|
start: () => start.apply(context),
|
|
stop: () => stop.apply(context),
|
|
fetchUpdate: () => fetchUpdate.apply(context),
|
|
loadQuotes: () => loadQuotes.apply(context),
|
|
getQuotesRef: () => getQuotesRef.apply(context),
|
|
createQuote: (quote: string) => saveQuote.apply(context, [':new:', quote]),
|
|
saveQuote: (id: string, quote: string) => saveQuote.apply(context, [id, quote]),
|
|
deleteQuote: (id) => deleteQuote.apply(context, [id]),
|
|
collectQuotes: () => collectQuotes.apply(context),
|
|
startGame: () => startGame.apply(context),
|
|
continueGame: () => continueGame.apply(context),
|
|
resetGame: () => resetGame.apply(context),
|
|
finishGame: () => finishGame.apply(context),
|
|
disableGame: () => disableGame.apply(context),
|
|
removeGame: () => removeGame.apply(context),
|
|
saveSelection: (selection: string) => saveSelection.apply(context, [selection]),
|
|
fetchGameInfo: () => fetchGameInfo.apply(context),
|
|
fetchGameInfos: () => fetchGameInfos.apply(context),
|
|
setGameLang: (lang: string) => setGameLang.apply(context, [lang]),
|
|
setGameName: (name: string) => setGameName.apply(context, [name]),
|
|
savePlayer: (player: PlayerEdit) => savePlayer.apply(context, [player]),
|
|
deletePlayer: (id: string) => deletePlayer.apply(context, [id]),
|
|
createGame: (name: string, teamname: string, lang: Lang) => createGame.apply(context, [name, teamname, lang]),
|
|
cameo: (authcode: string) => cameo.apply(context,[authcode]),
|
|
logoutCameo: () => logoutCameo.apply(context),
|
|
}
|
|
}
|