summaryrefslogtreecommitdiff
path: root/sw-ui/src/api/sevenWondersApi.ts
blob: 4f76a677bd37841bab3be93b4b6938738a121ddb (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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