summaryrefslogtreecommitdiff
path: root/frontend/src/api/sevenWondersApi.js
blob: 9a68ec6661f0a8658f0ea85e0fc3d4e0d2f41452 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// @flow
import { createJsonStompClient } from './websocket';
import type { JsonStompClient, SubscribeFn } from './websocket'
import type { ApiError, ApiLobby, ApiPlayer, ApiPlayerMove, ApiPlayerTurnInfo, ApiPreparedCard, ApiTable } from './model';

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

export class SevenWondersSession {
  client: JsonStompClient;

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

  watchErrors(): SubscribeFn<ApiError> {
    return this.client.subscriber('/user/queue/errors');
  }

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

  watchNameChoice(): SubscribeFn<ApiPlayer> {
    return this.client.subscriber('/user/queue/nameChoice');
  }

  watchGames(): SubscribeFn<ApiLobby[]> {
    return this.client.subscriber('/topic/games');
  }

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

  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`);
  }

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

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

  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`);
  }

  watchPreparedMove(currentGameId: number): SubscribeFn<ApiPreparedCard> {
    return this.client.subscriber(`/topic/game/${currentGameId}/prepared`);
  }

  watchTurnInfo(): SubscribeFn<ApiPlayerTurnInfo> {
    return this.client.subscriber('/user/queue/game/turnInfo');
  }

  sayReady(): void {
    this.client.send('/app/game/sayReady');
  }

  prepareMove(move: ApiPlayerMove): void {
    this.client.send('/app/game/sayReady', { move });
  }
}

export async function connectToGame(): Promise<SevenWondersSession> {
  const jsonStompClient: JsonStompClient = createJsonStompClient(wsURL);
  await jsonStompClient.connect();
  return new SevenWondersSession(jsonStompClient);
}
bgstack15