summaryrefslogtreecommitdiff
path: root/frontend/src/api/sevenWondersApi.js
blob: 4ba0fff38d7dd23b31693bbdbd895f5d06b08fdf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { createJsonSubscriptionChannel, createStompSession } from './websocket';
import type { Client } from 'webstomp-client';
import type { Channel } from 'redux-saga';

const wsURL = '/seven-wonders-websocket';

export class SevenWondersSession {
  client: Client;

  constructor(client: Client) {
    this.client = client;
  }

  watchErrors(): Channel<ApiError> {
    return createJsonSubscriptionChannel(this.client, '/user/queue/errors');
  }

  chooseName(displayName: string): void {
    this.client.send('/app/chooseName', JSON.stringify({ playerName: displayName }));
  }

  watchNameChoice(): Channel<Object> {
    return createJsonSubscriptionChannel(this.client, '/user/queue/nameChoice');
  }

  watchGames(): Channel<Object> {
    return createJsonSubscriptionChannel(this.client, '/topic/games');
  }

  watchLobbyJoined(): Channel<Object> {
    return createJsonSubscriptionChannel(this.client, '/user/queue/lobby/joined');
  }

  watchLobbyUpdated(currentGameId: number): Channel<Object> {
    return createJsonSubscriptionChannel(this.client, `/topic/lobby/${currentGameId}/updated`);
  }

  watchGameStarted(currentGameId: number): Channel<Object> {
    return createJsonSubscriptionChannel(this.client, `/topic/lobby/${currentGameId}/started`);
  }

  createGame(gameName: string): void {
    this.client.send('/app/lobby/create', JSON.stringify({ gameName }));
  }

  joinGame(gameId: number): void {
    this.client.send('/app/lobby/join', JSON.stringify({ gameId }));
  }

  startGame(): void {
    this.client.send('/app/lobby/startGame', {});
  }
}

export function createSession(): Promise<SevenWondersSession> {
  return createStompSession(wsURL).then(client => new SevenWondersSession(client));
}

export class ApiError {
  message: string;
  details: ApiErrorDetail[];
}

export class ApiErrorDetail {
  message: string;
}
bgstack15