From a8aa74bfa858e3f9312fc27b9aff5443f84b439f Mon Sep 17 00:00:00 2001 From: Joffrey BION Date: Fri, 3 May 2019 00:26:07 +0200 Subject: Convert api package to typescript --- frontend/src/api/sevenWondersApi.ts | 104 ++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 frontend/src/api/sevenWondersApi.ts (limited to 'frontend/src/api/sevenWondersApi.ts') diff --git a/frontend/src/api/sevenWondersApi.ts b/frontend/src/api/sevenWondersApi.ts new file mode 100644 index 00000000..4f76a677 --- /dev/null +++ b/frontend/src/api/sevenWondersApi.ts @@ -0,0 +1,104 @@ +import { + ApiError, + ApiLobby, + ApiPlayer, + ApiPlayerMove, + ApiPlayerTurnInfo, + ApiPreparedCard, + ApiSettings, + ApiTable, +} from './model'; +import { JsonStompClient, SubscribeFn } from './websocket'; +import { createJsonStompClient } from './websocket'; + +const WS_URL = '/seven-wonders-websocket'; + +export class SevenWondersSession { + client: JsonStompClient; + + constructor(client: JsonStompClient) { + this.client = client; + } + + watchErrors(): SubscribeFn { + return this.client.subscriber('/user/queue/errors'); + } + + watchNameChoice(): SubscribeFn { + return this.client.subscriber('/user/queue/nameChoice'); + } + + chooseName(displayName: string): void { + this.client.send('/app/chooseName', { playerName: displayName }); + } + + watchGames(): SubscribeFn { + return this.client.subscriber('/topic/games'); + } + + watchLobbyJoined(): SubscribeFn { + return this.client.subscriber('/user/queue/lobby/joined'); + } + + createGame(gameName: string): void { + this.client.send('/app/lobby/create', { gameName }); + } + + joinGame(gameId: number): void { + this.client.send('/app/lobby/join', { gameId }); + } + + watchLobbyUpdated(currentGameId: number): SubscribeFn { + return this.client.subscriber(`/topic/lobby/${currentGameId}/updated`); + } + + watchGameStarted(currentGameId: number): SubscribeFn { + return this.client.subscriber(`/topic/lobby/${currentGameId}/started`); + } + + leave(): void { + this.client.send('/app/lobby/leave'); + } + + reorderPlayers(orderedPlayers: Array): void { + this.client.send('/app/lobby/reorderPlayers', { orderedPlayers }); + } + + updateSettings(settings: ApiSettings): void { + this.client.send('/app/lobby/updateSettings', { settings }); + } + + startGame(): void { + this.client.send('/app/lobby/startGame'); + } + + watchPlayerReady(currentGameId: number): SubscribeFn { + return this.client.subscriber(`/topic/game/${currentGameId}/playerReady`); + } + + watchTableUpdates(currentGameId: number): SubscribeFn { + return this.client.subscriber(`/topic/game/${currentGameId}/tableUpdates`); + } + + watchPreparedCards(currentGameId: number): SubscribeFn { + return this.client.subscriber(`/topic/game/${currentGameId}/prepared`); + } + + watchTurnInfo(): SubscribeFn { + return this.client.subscriber('/user/queue/game/turn'); + } + + sayReady(): void { + this.client.send('/app/game/sayReady'); + } + + prepareMove(move: ApiPlayerMove): void { + this.client.send('/app/game/prepareMove', { move }); + } +} + +export async function connectToGame(): Promise { + const jsonStompClient: JsonStompClient = createJsonStompClient(WS_URL); + await jsonStompClient.connect(); + return new SevenWondersSession(jsonStompClient); +} -- cgit