summaryrefslogtreecommitdiff
path: root/sw-ui/src/api/sevenWondersApi.ts
diff options
context:
space:
mode:
authorJoffrey BION <joffrey.bion@gmail.com>2019-05-16 23:48:38 +0200
committerJoffrey BION <joffrey.bion@gmail.com>2019-05-16 23:48:38 +0200
commit2382a452456e4bdef4584e1046925e372624cb79 (patch)
tree0e49b2e5d81facb55fb8b08228abeb218a27d466 /sw-ui/src/api/sevenWondersApi.ts
parentRemove GRADLE_METADATA feature to avoid breaking frontend build (diff)
downloadseven-wonders-2382a452456e4bdef4584e1046925e372624cb79.tar.gz
seven-wonders-2382a452456e4bdef4584e1046925e372624cb79.tar.bz2
seven-wonders-2382a452456e4bdef4584e1046925e372624cb79.zip
Rationalize module names
Diffstat (limited to 'sw-ui/src/api/sevenWondersApi.ts')
-rw-r--r--sw-ui/src/api/sevenWondersApi.ts104
1 files changed, 104 insertions, 0 deletions
diff --git a/sw-ui/src/api/sevenWondersApi.ts b/sw-ui/src/api/sevenWondersApi.ts
new file mode 100644
index 00000000..4f76a677
--- /dev/null
+++ b/sw-ui/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<ApiError> {
+ return this.client.subscriber('/user/queue/errors');
+ }
+
+ watchNameChoice(): SubscribeFn<ApiPlayer> {
+ return this.client.subscriber('/user/queue/nameChoice');
+ }
+
+ chooseName(displayName: string): void {
+ this.client.send('/app/chooseName', { playerName: displayName });
+ }
+
+ watchGames(): SubscribeFn<ApiLobby[]> {
+ return this.client.subscriber('/topic/games');
+ }
+
+ watchLobbyJoined(): SubscribeFn<Object> {
+ 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<Object> {
+ return this.client.subscriber(`/topic/lobby/${currentGameId}/updated`);
+ }
+
+ watchGameStarted(currentGameId: number): SubscribeFn<Object> {
+ return this.client.subscriber(`/topic/lobby/${currentGameId}/started`);
+ }
+
+ leave(): void {
+ this.client.send('/app/lobby/leave');
+ }
+
+ reorderPlayers(orderedPlayers: Array<string>): 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<string> {
+ return this.client.subscriber(`/topic/game/${currentGameId}/playerReady`);
+ }
+
+ watchTableUpdates(currentGameId: number): SubscribeFn<ApiTable> {
+ return this.client.subscriber(`/topic/game/${currentGameId}/tableUpdates`);
+ }
+
+ watchPreparedCards(currentGameId: number): SubscribeFn<ApiPreparedCard> {
+ return this.client.subscriber(`/topic/game/${currentGameId}/prepared`);
+ }
+
+ watchTurnInfo(): SubscribeFn<ApiPlayerTurnInfo> {
+ 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<SevenWondersSession> {
+ const jsonStompClient: JsonStompClient = createJsonStompClient(WS_URL);
+ await jsonStompClient.connect();
+ return new SevenWondersSession(jsonStompClient);
+}
bgstack15