@netifyjs/protocol 1.0.0-alpha.2 → 1.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (183) hide show
  1. package/package.json +2 -2
  2. package/src/codec/index.ts +82 -157
  3. package/src/dispatch/dispatcher/index.ts +116 -33
  4. package/src/dispatch/session/index.ts +9 -3
  5. package/src/index.ts +2 -4
  6. package/src/photon/balancing/codec/index.ts +27 -53
  7. package/src/photon/balancing/format/authenticate/request/index.ts +52 -6
  8. package/src/photon/balancing/format/friends/request/index.ts +21 -5
  9. package/src/photon/balancing/format/game/create/request/index.ts +45 -8
  10. package/src/photon/balancing/format/game/debug/request/index.ts +19 -4
  11. package/src/photon/balancing/format/game/join/request/index.ts +65 -13
  12. package/src/photon/balancing/format/game/list/request/index.ts +29 -3
  13. package/src/photon/balancing/format/game/random/request/index.ts +58 -10
  14. package/src/photon/balancing/format/groups/request/index.ts +19 -3
  15. package/src/photon/balancing/format/index.ts +7 -0
  16. package/src/photon/balancing/format/leave/request/index.ts +19 -3
  17. package/src/photon/balancing/format/lobby/join/request/index.ts +23 -3
  18. package/src/photon/balancing/format/lobby/stats/request/index.ts +19 -3
  19. package/src/photon/balancing/format/once/request/index.ts +31 -8
  20. package/src/photon/balancing/format/properties/get/request/index.ts +31 -4
  21. package/src/photon/balancing/format/properties/set/request/index.ts +36 -7
  22. package/src/photon/balancing/format/raise/request/index.ts +76 -10
  23. package/src/photon/balancing/format/reader/index.ts +75 -6
  24. package/src/photon/balancing/format/regions/request/index.ts +16 -1
  25. package/src/photon/balancing/format/rpc/request/index.ts +26 -3
  26. package/src/photon/balancing/format/settings/request/index.ts +15 -2
  27. package/src/photon/balancing/format/shared/credentials/index.ts +77 -33
  28. package/src/photon/balancing/format/shared/lobby/index.ts +41 -5
  29. package/src/photon/balancing/format/shared/options/index.ts +56 -24
  30. package/src/photon/balancing/format/table/index.ts +399 -0
  31. package/src/photon/balancing/index.ts +11 -1
  32. package/src/photon/balancing/travel/index.ts +15 -12
  33. package/src/photon/binary/v18/index.ts +26 -1
  34. package/src/photon/binary/value/index.ts +52 -3
  35. package/src/photon/client/connection/index.ts +6 -6
  36. package/src/photon/cluster/back/index.ts +32 -45
  37. package/src/photon/cluster/dresser/index.ts +7 -8
  38. package/src/photon/cluster/front/index.ts +25 -49
  39. package/src/photon/cluster/grid/index.ts +1 -1
  40. package/src/photon/cluster/mover/index.ts +12 -12
  41. package/src/photon/cluster/press/index.ts +2 -18
  42. package/src/photon/cluster/route/game/index.ts +9 -11
  43. package/src/photon/cluster/route/master/index.ts +15 -34
  44. package/src/photon/cluster/route/name/index.ts +4 -21
  45. package/src/photon/cluster/seat/game/index.ts +6 -26
  46. package/src/photon/cluster/seat/master/index.ts +1 -1
  47. package/src/photon/cluster/sessions/index.ts +5 -6
  48. package/src/photon/cluster/spread/index.ts +8 -147
  49. package/src/photon/enet/client/index.ts +1 -8
  50. package/src/photon/enet/gateway/index.ts +5 -15
  51. package/src/photon/enet/index.ts +1 -1
  52. package/src/photon/enet/link/index.ts +34 -15
  53. package/src/photon/enet/outbound/index.ts +279 -0
  54. package/src/photon/enet/peer/index.ts +59 -0
  55. package/src/photon/enet/transport/index.ts +8 -24
  56. package/src/photon/game/admission/index.ts +353 -0
  57. package/src/photon/game/bag/index.ts +13 -0
  58. package/src/photon/game/broadcast/index.ts +79 -0
  59. package/src/photon/game/editor/index.ts +446 -0
  60. package/src/photon/game/gate/index.ts +9 -8
  61. package/src/photon/game/handler/index.ts +30 -3
  62. package/src/photon/game/history/index.ts +234 -0
  63. package/src/photon/game/hooked/index.ts +16 -15
  64. package/src/photon/game/index.ts +12 -1
  65. package/src/photon/game/lifecycle/index.ts +193 -0
  66. package/src/photon/game/peer/index.ts +1 -10
  67. package/src/photon/game/properties/index.ts +342 -0
  68. package/src/photon/game/room/index.ts +435 -1793
  69. package/src/photon/game/rooms/index.ts +2 -1
  70. package/src/photon/game/seats/index.ts +237 -0
  71. package/src/photon/game/server/index.ts +12 -2
  72. package/src/photon/game/services/index.ts +41 -0
  73. package/src/photon/game/settings/index.ts +22 -0
  74. package/src/photon/game/snapshot/index.ts +133 -0
  75. package/src/photon/game/throttle/index.ts +60 -0
  76. package/src/photon/game/touch/index.ts +2 -5
  77. package/src/photon/game/venue/index.ts +31 -0
  78. package/src/photon/master/application/index.ts +7 -0
  79. package/src/photon/master/gate/index.ts +12 -8
  80. package/src/photon/master/handler/index.ts +36 -748
  81. package/src/photon/master/index.ts +5 -0
  82. package/src/photon/master/list/sql/index.ts +37 -3
  83. package/src/photon/master/listing/index.ts +133 -0
  84. package/src/photon/master/lobby/index.ts +9 -0
  85. package/src/photon/master/login/index.ts +424 -0
  86. package/src/photon/master/matchmaking/index.ts +282 -0
  87. package/src/photon/master/query/index.ts +45 -0
  88. package/src/photon/master/redirect/index.ts +8 -7
  89. package/src/photon/master/services/index.ts +35 -0
  90. package/src/photon/master/settings/index.ts +44 -0
  91. package/src/photon/master/sql/cost/index.ts +263 -0
  92. package/src/photon/master/sql/index.ts +1 -0
  93. package/src/photon/message/binary/index.ts +35 -1
  94. package/src/photon/message/client/index.ts +14 -11
  95. package/src/photon/message/format/index.ts +9 -0
  96. package/src/photon/message/handler/index.ts +48 -63
  97. package/src/photon/message/pipeline/index.ts +12 -12
  98. package/src/photon/message/server/index.ts +87 -43
  99. package/src/photon/message/watch/index.ts +127 -15
  100. package/src/photon/server/config/index.ts +5 -0
  101. package/src/photon/server/deferral/index.ts +57 -0
  102. package/src/photon/server/host/index.ts +36 -10
  103. package/src/photon/server/index.ts +1 -0
  104. package/src/photon/server/listeners/index.ts +11 -0
  105. package/src/photon/tcp/client/index.ts +1 -1
  106. package/src/photon/tcp/codec/index.ts +41 -38
  107. package/src/photon/tcp/framing/index.ts +24 -4
  108. package/src/photon/tcp/server/index.ts +4 -4
  109. package/src/photon/websocket/bridge/index.ts +16 -20
  110. package/src/photon/websocket/message/index.ts +27 -39
  111. package/src/photon/websocket/path/index.ts +2 -5
  112. package/src/scale/batch/index.ts +14 -6
  113. package/src/scale/fanout/index.ts +6 -6
  114. package/src/scale/index.ts +2 -4
  115. package/src/scale/outbound/index.ts +192 -0
  116. package/src/scale/relay/index.ts +24 -16
  117. package/src/scale/sink/index.ts +6 -11
  118. package/src/scale/transport/index.ts +27 -44
  119. package/src/scale/tunnel/index.ts +56 -86
  120. package/src/smartfox/common/bluebox/gateway/index.ts +401 -0
  121. package/src/smartfox/common/bluebox/index.ts +4 -0
  122. package/src/smartfox/{x2 → common}/bluebox/transport/index.ts +36 -23
  123. package/src/smartfox/common/index.ts +5 -0
  124. package/src/smartfox/common/strikes/index.ts +74 -0
  125. package/src/smartfox/pro/bluebox/index.ts +176 -4
  126. package/src/smartfox/pro/codec/index.ts +8 -7
  127. package/src/smartfox/pro/escalation/index.ts +28 -20
  128. package/src/smartfox/pro/flood/index.ts +2 -2
  129. package/src/smartfox/pro/format/extension/raw/index.ts +12 -2
  130. package/src/smartfox/pro/handler/chat/index.ts +11 -8
  131. package/src/smartfox/pro/handler/login/index.ts +2 -25
  132. package/src/smartfox/pro/handler/shared/index.ts +4 -3
  133. package/src/smartfox/pro/index.ts +11 -5
  134. package/src/smartfox/pro/login/gate/index.ts +166 -0
  135. package/src/smartfox/pro/login/index.ts +1 -0
  136. package/src/smartfox/pro/moderation/index.ts +220 -0
  137. package/src/smartfox/pro/options/index.ts +5 -5
  138. package/src/smartfox/pro/protocol/index.ts +62 -8
  139. package/src/smartfox/pro/web/index.ts +33 -26
  140. package/src/smartfox/pro/zone/index.ts +59 -282
  141. package/src/smartfox/pro/zones/index.ts +3 -3
  142. package/src/smartfox/x2/bluebox/index.ts +149 -4
  143. package/src/smartfox/x2/chat/index.ts +9 -19
  144. package/src/smartfox/x2/codec/index.ts +34 -40
  145. package/src/smartfox/x2/config/index.ts +6 -1
  146. package/src/smartfox/x2/data/array/index.ts +96 -60
  147. package/src/smartfox/x2/data/object/index.ts +144 -82
  148. package/src/smartfox/x2/data/serializer/index.ts +41 -23
  149. package/src/smartfox/x2/flood/gate/index.ts +19 -13
  150. package/src/smartfox/x2/format/buddy/init/response/index.ts +4 -3
  151. package/src/smartfox/x2/format/find/rooms/response/index.ts +4 -3
  152. package/src/smartfox/x2/format/find/users/response/index.ts +4 -3
  153. package/src/smartfox/x2/format/group/subscribe/response/index.ts +4 -3
  154. package/src/smartfox/x2/format/login/response/index.ts +4 -3
  155. package/src/smartfox/x2/format/mmo/position/response/index.ts +8 -6
  156. package/src/smartfox/x2/format/room/join/response/index.ts +4 -3
  157. package/src/smartfox/x2/games/index.ts +35 -8
  158. package/src/smartfox/x2/hall/index.ts +0 -6
  159. package/src/smartfox/x2/handler/session/index.ts +1 -1
  160. package/src/smartfox/x2/index.ts +8 -3
  161. package/src/smartfox/x2/member/index.ts +10 -3
  162. package/src/smartfox/x2/packet/login/response/index.ts +12 -0
  163. package/src/smartfox/x2/protocol/index.ts +11 -1
  164. package/src/smartfox/x2/rooms/index.ts +1 -2
  165. package/src/smartfox/x2/sessions/index.ts +6 -0
  166. package/src/smartfox/x2/udp/index.ts +61 -24
  167. package/src/smartfox/x2/web/index.ts +35 -17
  168. package/src/smartfox/x2/websocket/index.ts +18 -36
  169. package/src/smartfox/x2/zone/index.ts +77 -28
  170. package/src/smartfox/x2/zones/index.ts +17 -5
  171. package/src/photon/enet/handler/index.ts +0 -65
  172. package/src/scale/hosted/index.ts +0 -68
  173. package/src/scale/outlet/index.ts +0 -121
  174. package/src/smartfox/pro/bluebox/gateway/index.ts +0 -310
  175. package/src/smartfox/pro/bluebox/session/index.ts +0 -63
  176. package/src/smartfox/pro/bluebox/transport/index.ts +0 -104
  177. package/src/smartfox/x2/bluebox/gateway/index.ts +0 -315
  178. /package/src/smartfox/{x2 → common}/bluebox/session/index.ts +0 -0
  179. /package/src/smartfox/{pro → common}/counters/index.ts +0 -0
  180. /package/src/smartfox/{pro → common}/counters/memory/index.ts +0 -0
  181. /package/src/smartfox/{pro → common}/counters/offender/index.ts +0 -0
  182. /package/src/smartfox/{pro → common}/counters/store/index.ts +0 -0
  183. /package/src/smartfox/{pro → common}/sequence/index.ts +0 -0
@@ -1,104 +1,38 @@
1
- import { randomUUID } from 'node:crypto'
2
-
3
1
  import { IdleStateEvent } from '@netifyjs/netify'
4
2
 
5
3
  import { Closed } from '../../../dispatch/event/index.ts'
6
4
  import { on, when } from '../../../dispatch/route/index.ts'
7
5
  import type { Session } from '../../../dispatch/session/index.ts'
8
- import type { Applications } from '../../application/index.ts'
9
- import type { CustomAuthenticator } from '../../authentication/authenticator/index.ts'
10
- import { AuthenticationFailure } from '../../authentication/failure/index.ts'
11
6
  import { TokenGate } from '../../authentication/gate/index.ts'
12
- import type { AuthenticationAttempt } from '../../authentication/provider/index.ts'
13
- import type { AuthenticationResult } from '../../authentication/result/index.ts'
14
- import {
15
- CustomAuthenticationResult,
16
- CustomAuthenticationType,
17
- } from '../../balancing/code/authentication/index.ts'
18
7
  import { ErrorCode } from '../../balancing/code/error/index.ts'
19
- import { JoinMode } from '../../balancing/code/join/index.ts'
20
- import { LobbyType } from '../../balancing/code/lobby/index.ts'
21
8
  import { ErrorMessage } from '../../balancing/code/message/index.ts'
22
9
  import { OperationCode } from '../../balancing/code/operation/index.ts'
23
- import { RoomOptionFlag } from '../../balancing/code/room/index.ts'
24
- import type { Lobby } from '../../balancing/entity/lobby/index.ts'
25
- import type { RoomOptions } from '../../balancing/entity/options/index.ts'
26
- import { Grant } from '../../balancing/entity/grant/index.ts'
27
- import { GrantFormat } from '../../balancing/format/shared/grant/index.ts'
28
- import { ParameterWriter } from '../../balancing/format/writer/index.ts'
29
10
  import type { Packet } from '../../balancing/packet/adapter/index.ts'
30
11
  import { AuthenticateRequest } from '../../balancing/packet/authenticate/request/index.ts'
31
- import { AuthenticateResponse } from '../../balancing/packet/authenticate/response/index.ts'
32
12
  import { FindFriendsRequest } from '../../balancing/packet/friends/request/index.ts'
33
- import { FindFriendsResponse } from '../../balancing/packet/friends/response/index.ts'
34
13
  import { CreateGameRequest } from '../../balancing/packet/game/create/request/index.ts'
35
- import { CreateGameResponse } from '../../balancing/packet/game/create/response/index.ts'
36
14
  import { JoinGameRequest } from '../../balancing/packet/game/join/request/index.ts'
37
- import { JoinGameResponse } from '../../balancing/packet/game/join/response/index.ts'
38
15
  import { GetGameListRequest } from '../../balancing/packet/game/list/request/index.ts'
39
- import { GetGameListResponse } from '../../balancing/packet/game/list/response/index.ts'
40
16
  import { JoinRandomGameRequest } from '../../balancing/packet/game/random/request/index.ts'
41
- import { JoinRandomGameResponse } from '../../balancing/packet/game/random/response/index.ts'
42
17
  import { JoinLobbyRequest } from '../../balancing/packet/lobby/join/request/index.ts'
43
- import { JoinLobbyResponse } from '../../balancing/packet/lobby/join/response/index.ts'
44
18
  import { LeaveLobbyRequest } from '../../balancing/packet/lobby/leave/request/index.ts'
45
- import { LeaveLobbyResponse } from '../../balancing/packet/lobby/leave/response/index.ts'
46
19
  import { GetLobbyStatsRequest } from '../../balancing/packet/lobby/stats/request/index.ts'
47
20
  import type { ReplyAdapter } from '../../balancing/packet/reply/index.ts'
48
21
  import { WebRpcRequest } from '../../balancing/packet/rpc/request/index.ts'
49
22
  import { WebRpcResponse } from '../../balancing/packet/rpc/response/index.ts'
50
23
  import { ServerSettingsRequest } from '../../balancing/packet/settings/request/index.ts'
51
24
  import { UnknownResponse } from '../../balancing/packet/unknown/index.ts'
52
- import { PhotonDictionary } from '../../binary/dictionary/index.ts'
53
- import type { PhotonHashtable } from '../../binary/hashtable/index.ts'
54
- import { Kind } from '../../binary/kind/index.ts'
55
- import { OperationResponse } from '../../binary/response/index.ts'
56
- import { Shape } from '../../binary/shape/index.ts'
57
- import { PhotonValue } from '../../binary/value/index.ts'
58
- import type { TokenAuthority } from '../../crypto/token/authority/index.ts'
59
- import type { AuthToken } from '../../crypto/token/record/index.ts'
60
- import { Envelope } from '../../envelope/index.ts'
61
25
  import { AddressFamily } from '../../family/index.ts'
62
26
  import { InitRequest } from '../../message/init/request/index.ts'
63
- import { JsonValues } from '../../message/json/values/index.ts'
64
- import { OnceAnswer } from '../../message/once/index.ts'
65
27
  import { PhotonPeer } from '../../message/peer/index.ts'
66
28
  import { WebRpcRelay } from '../../rpc/relay/index.ts'
67
- import type { MasterApplication } from '../application/index.ts'
29
+ import { Listing } from '../listing/index.ts'
68
30
  import { MasterFault } from '../fault/index.ts'
69
- import type { AppLobby, Placement, Seat } from '../lobby/index.ts'
70
- import { MasterPeer, PeerStage } from '../peer/index.ts'
71
- import { WellKnownProperties } from '../wellknown/index.ts'
72
-
73
- /**
74
- * What the handler needs from its Master
75
- */
76
- export type MasterServices = {
77
- /**
78
- * Lobbies, games, statistics and settings
79
- */
80
- readonly application: MasterApplication
81
-
82
- /**
83
- * Checks the tokens clients bring
84
- */
85
- readonly gate: TokenGate
86
-
87
- /**
88
- * Issues tokens for logins without one, or null
89
- */
90
- readonly authority: TokenAuthority | null
91
-
92
- /**
93
- * Runs custom authentication
94
- */
95
- readonly authenticator: CustomAuthenticator
96
-
97
- /**
98
- * Checks app ids and versions of logins without a token
99
- */
100
- readonly applications: Applications
101
- }
31
+ import { MasterLogin } from '../login/index.ts'
32
+ import { Matchmaker } from '../matchmaking/index.ts'
33
+ import { MasterPeer } from '../peer/index.ts'
34
+ import { QueryScreen } from '../query/index.ts'
35
+ import type { MasterServices } from '../services/index.ts'
102
36
 
103
37
  /**
104
38
  * Serves every operation a client sends to a Master
@@ -111,64 +45,31 @@ export class MasterHandler {
111
45
  private readonly services: MasterServices
112
46
 
113
47
  /**
114
- * Writes the parameters of an authenticate response
48
+ * Logs clients in
115
49
  */
116
- private readonly grants: GrantFormat
50
+ private readonly login: MasterLogin
117
51
 
118
52
  /**
119
- * @param services lobbies, tokens and settings of the server
53
+ * Creates games and finds games to join
120
54
  */
121
- public constructor(services: MasterServices) {
122
- this.services = services
123
- this.grants = new GrantFormat()
124
- }
55
+ private readonly matchmaker: Matchmaker
125
56
 
126
57
  /**
127
- * A user id that's never blank
128
- * @param userId what's known, or null
58
+ * Lobbies, game lists and friends
129
59
  */
130
- private static named(userId: string | null): string {
131
- return userId === null || userId.trim() === '' ? randomUUID() : userId
132
- }
133
-
134
- /**
135
- * A provider's values as the dictionary a client reads, or null
136
- * @param data values from the provider, or null
137
- */
138
- private static dictionary(
139
- data: Readonly<Record<string, unknown>> | null,
140
- ): PhotonDictionary | null {
141
- if (data === null) return null
142
-
143
- const dictionary = PhotonDictionary.of(Shape.string(), Shape.any())
144
- for (const [name, value] of Object.entries<unknown>(data)) {
145
- dictionary.set(name, JsonValues.read(value))
146
- }
147
-
148
- return dictionary
149
- }
60
+ private readonly directory: Listing
150
61
 
151
62
  /**
152
- * The refusal for a lobby type no lobby has
153
- * @param type what the client sent
154
- */
155
- private static lobbyType(type: number): MasterFault {
156
- return MasterFault.invalid(
157
- `${ErrorMessage.INVALID_LOBBY_TYPE} ${String(type)}`,
158
- )
159
- }
160
-
161
- /**
162
- * Whether a create or join asks for its user id to be checked
163
- * @param options the room options block, or null
63
+ * @param services lobbies, tokens and settings of the server
164
64
  */
165
- private static checksUser(options: RoomOptions | null): boolean {
166
- if (options === null) return false
65
+ public constructor(services: MasterServices) {
66
+ const application = services.application
67
+ const screen = new QueryScreen(application.settings)
167
68
 
168
- return (
169
- options.checkUserOnJoin === true ||
170
- (options.roomFlags & RoomOptionFlag.CHECK_USER_ON_JOIN) !== 0
171
- )
69
+ this.services = services
70
+ this.login = new MasterLogin(services)
71
+ this.matchmaker = new Matchmaker(application, screen)
72
+ this.directory = new Listing(application, screen)
172
73
  }
173
74
 
174
75
  /**
@@ -194,7 +95,7 @@ export class MasterHandler {
194
95
 
195
96
  const token = TokenGate.admitted(session.channel)
196
97
  if (token !== null) {
197
- await this.admit(peer, photon, token)
98
+ await this.login.admit(peer, photon, token)
198
99
  return
199
100
  }
200
101
 
@@ -220,21 +121,7 @@ export class MasterHandler {
220
121
  const peer = session.user
221
122
  if (peer === null) return
222
123
 
223
- this.services.application.timers.cancel(peer.noAuth)
224
- peer.noAuth = null
225
-
226
- try {
227
- await this.login(peer, packet)
228
- } catch (cause) {
229
- if (peer.stage === PeerStage.AUTHENTICATING) {
230
- peer.stage = PeerStage.INITIAL
231
- }
232
-
233
- const fault = MasterFault.from(cause)
234
- peer.send(
235
- AuthenticateResponse.failure(fault.code, fault.message).to(packet),
236
- )
237
- }
124
+ await this.login.authenticate(peer, packet)
238
125
  }
239
126
 
240
127
  /**
@@ -251,29 +138,8 @@ export class MasterHandler {
251
138
  session,
252
139
  packet,
253
140
  OperationCode.JOIN_LOBBY,
254
- (peer: MasterPeer): ReplyAdapter | null => {
255
- const application = this.services.application
256
- const type = packet.lobby?.type ?? LobbyType.DEFAULT
257
- if (type > LobbyType.ASYNC_RANDOM) throw MasterHandler.lobbyType(type)
258
-
259
- application.leave(peer)
260
-
261
- const lobby = application.lobbies.resolve(
262
- packet.lobby?.name ?? null,
263
- type,
264
- )
265
- const list = lobby.join(
266
- peer,
267
- packet.gameListCount ?? 0,
268
- peer.properties(packet.gameProperties),
269
- )
270
-
271
- peer.lobby = lobby
272
- peer.send(JoinLobbyResponse.success().to(packet))
273
- if (list !== null) peer.send(list)
274
-
275
- return null
276
- },
141
+ (peer: MasterPeer): ReplyAdapter | null =>
142
+ this.directory.join(peer, packet),
277
143
  )
278
144
  }
279
145
 
@@ -291,12 +157,7 @@ export class MasterHandler {
291
157
  session,
292
158
  packet,
293
159
  OperationCode.LEAVE_LOBBY,
294
- (peer: MasterPeer): ReplyAdapter | null => {
295
- if (peer.lobby === null) return LeaveLobbyResponse.notJoined()
296
-
297
- this.services.application.leave(peer)
298
- return LeaveLobbyResponse.success()
299
- },
160
+ (peer: MasterPeer): ReplyAdapter | null => this.directory.leave(peer),
300
161
  )
301
162
  }
302
163
 
@@ -311,27 +172,8 @@ export class MasterHandler {
311
172
  session,
312
173
  packet,
313
174
  OperationCode.CREATE_GAME,
314
- (peer: MasterPeer): ReplyAdapter | null => {
315
- const placement = this.placement(
316
- peer,
317
- packet.roomName,
318
- peer.properties(packet.options?.gameProperties ?? null),
319
- packet.expectedUsers,
320
- )
321
- const type = packet.lobby?.type ?? LobbyType.DEFAULT
322
- if (type > LobbyType.ASYNC_RANDOM) throw MasterHandler.lobbyType(type)
323
-
324
- const lobby = this.lobby(peer, packet.lobby)
325
- return this.queued(
326
- peer,
327
- OperationCode.CREATE_GAME,
328
- (): ReplyAdapter => {
329
- const seat = lobby.create(peer, placement)
330
-
331
- return CreateGameResponse.master(seat.address, seat.game)
332
- },
333
- )
334
- },
175
+ (peer: MasterPeer): ReplyAdapter | null =>
176
+ this.matchmaker.create(peer, packet),
335
177
  )
336
178
  }
337
179
 
@@ -346,49 +188,8 @@ export class MasterHandler {
346
188
  session,
347
189
  packet,
348
190
  OperationCode.JOIN_GAME,
349
- (peer: MasterPeer): ReplyAdapter | null => {
350
- const placement = this.placement(
351
- peer,
352
- packet.roomName,
353
- peer.properties(packet.options?.gameProperties ?? null),
354
- packet.expectedUsers,
355
- )
356
-
357
- const application = this.services.application
358
- const known = application.find(packet.roomName)
359
- const plain =
360
- packet.joinMode === JoinMode.DEFAULT &&
361
- !application.settings.allowAsyncJoin
362
- if (known === null && plain) {
363
- throw MasterFault.of(
364
- ErrorCode.GAME_DOES_NOT_EXIST,
365
- ErrorMessage.GAME_ID_DOES_NOT_EXIST,
366
- )
367
- }
368
-
369
- const lobby = known?.lobby ?? this.lobby(peer, packet.lobby)
370
- return this.queued(
371
- peer,
372
- OperationCode.JOIN_GAME,
373
- (): ReplyAdapter | null => {
374
- const seat = lobby.enter(peer, packet, {
375
- ...placement,
376
- /**
377
- * Decides between joining, creating and rejoining
378
- */
379
- mode: packet.joinMode,
380
- /**
381
- * Asked for by flag or by the old parameter
382
- */
383
- checkUser: MasterHandler.checksUser(packet.options),
384
- })
385
-
386
- return seat === null
387
- ? null
388
- : JoinGameResponse.master(seat.address, null, null, 0)
389
- },
390
- )
391
- },
191
+ (peer: MasterPeer): ReplyAdapter | null =>
192
+ this.matchmaker.join(peer, packet),
392
193
  )
393
194
  }
394
195
 
@@ -402,41 +203,12 @@ export class MasterHandler {
402
203
  session: Session<MasterPeer>,
403
204
  packet: JoinRandomGameRequest,
404
205
  ): void {
405
- const code = OperationCode.JOIN_RANDOM_GAME
406
-
407
206
  this.answer(
408
207
  session,
409
208
  packet,
410
- code,
411
- (peer: MasterPeer): ReplyAdapter | null => {
412
- const word = this.forbidden(packet.sqlFilter, false)
413
- if (word !== null) throw word
414
-
415
- const lobby = this.lobby(peer, packet.lobby)
416
- return this.queued(peer, code, (): ReplyAdapter => {
417
- const seat =
418
- lobby.random(peer, {
419
- /**
420
- * Properties a game must have
421
- */
422
- filter: peer.filter(packet.gameProperties),
423
- /**
424
- * Where the walk through the games starts
425
- */
426
- mode: packet.matchmakingMode,
427
- /**
428
- * Filters of a SQL lobby
429
- */
430
- sql: packet.sqlFilter,
431
- /**
432
- * Checked by games that check user ids
433
- */
434
- users: packet.expectedUsers,
435
- }) ?? this.fallback(peer, packet)
436
-
437
- return JoinRandomGameResponse.success(seat.address, seat.game, 0)
438
- })
439
- },
209
+ OperationCode.JOIN_RANDOM_GAME,
210
+ (peer: MasterPeer): ReplyAdapter | null =>
211
+ this.matchmaker.random(peer, packet),
440
212
  )
441
213
  }
442
214
 
@@ -451,19 +223,7 @@ export class MasterHandler {
451
223
  session,
452
224
  packet,
453
225
  OperationCode.GET_GAME_LIST,
454
- (): ReplyAdapter | null => {
455
- const word = this.forbidden(packet.sqlFilter, true)
456
- if (word !== null) throw word
457
-
458
- const type = packet.lobby?.type ?? LobbyType.DEFAULT
459
- if (type !== LobbyType.SQL) throw MasterHandler.lobbyType(type)
460
-
461
- const name = packet.lobby?.name ?? ''
462
- if (name === '') throw MasterFault.invalid("Invalid lobby name: ''")
463
-
464
- const lobby = this.services.application.lobbies.resolve(name, type)
465
- return GetGameListResponse.success(lobby.query(packet.sqlFilter ?? ''))
466
- },
226
+ (): ReplyAdapter | null => this.directory.list(packet),
467
227
  )
468
228
  }
469
229
 
@@ -481,14 +241,7 @@ export class MasterHandler {
481
241
  session,
482
242
  packet,
483
243
  OperationCode.FIND_FRIENDS,
484
- (): ReplyAdapter | null => {
485
- const found = this.services.application.players.find(
486
- packet.userIds,
487
- packet.options ?? 0,
488
- )
489
-
490
- return FindFriendsResponse.success(found.online, found.rooms)
491
- },
244
+ (): ReplyAdapter | null => this.directory.friends(packet),
492
245
  )
493
246
  }
494
247
 
@@ -506,8 +259,7 @@ export class MasterHandler {
506
259
  session,
507
260
  packet,
508
261
  OperationCode.GET_LOBBY_STATS,
509
- (): ReplyAdapter | null =>
510
- this.services.application.lobbyStats.answer(packet),
262
+ (): ReplyAdapter | null => this.directory.stats(packet),
511
263
  )
512
264
  }
513
265
 
@@ -524,7 +276,7 @@ export class MasterHandler {
524
276
  const peer = session.user
525
277
  if (peer === null || packet.lobbyStats === null) return
526
278
 
527
- this.watch(peer, packet.lobbyStats)
279
+ this.directory.watch(peer, packet.lobbyStats)
528
280
  }
529
281
 
530
282
  /**
@@ -607,146 +359,6 @@ export class MasterHandler {
607
359
  if (reply !== null) peer.send(reply.to(packet))
608
360
  }
609
361
 
610
- /**
611
- * Runs the lobby part of a create or join and keeps the flood counters
612
- * @param peer who's asking
613
- * @param code operation a failure is answered under
614
- * @param work what the lobby does
615
- */
616
- private queued(
617
- peer: MasterPeer,
618
- code: number,
619
- work: () => ReplyAdapter | null,
620
- ): ReplyAdapter | null {
621
- peer.concurrentJoins += 1
622
-
623
- let reply: ReplyAdapter | null
624
- try {
625
- reply = work()
626
- } catch (cause) {
627
- const fault = MasterFault.from(cause)
628
-
629
- reply = UnknownResponse.failure(code, fault.code, fault.message)
630
- }
631
-
632
- if (reply === null) return null
633
-
634
- peer.concurrentJoins -= 1
635
- if (reply.succeeded) peer.successfulJoins += 1
636
-
637
- return reply
638
- }
639
-
640
- /**
641
- * The lobby a create, join or random join runs in
642
- * @param peer who's asking
643
- * @param lobby what the request names, or null
644
- */
645
- private lobby(peer: MasterPeer, lobby: Lobby | null): AppLobby {
646
- return this.services.application.lobby(
647
- peer,
648
- lobby?.name ?? null,
649
- lobby?.type ?? LobbyType.DEFAULT,
650
- )
651
- }
652
-
653
- /**
654
- * Checks the properties and reserved users of a create or join
655
- * @param peer who's asking
656
- * @param name name of the game, or null
657
- * @param properties properties the client sent, or null
658
- * @param users user ids to keep seats for, or null
659
- */
660
- private placement(
661
- peer: MasterPeer,
662
- name: string | null,
663
- properties: PhotonHashtable | null,
664
- users: readonly string[] | null,
665
- ): Placement {
666
- const known =
667
- properties === null || properties.size === 0
668
- ? null
669
- : WellKnownProperties.request(properties)
670
- const wanted = users ?? known?.expectedUsers ?? null
671
- const unique = wanted === null ? null : [...new Set<string>(wanted)]
672
-
673
- if (unique !== null) {
674
- const needed =
675
- unique.length + (unique.includes(peer.userId ?? '') ? 0 : 1)
676
- const most = known?.maxPlayers ?? 0
677
- if (most !== 0 && needed > most) {
678
- throw MasterFault.invalid(
679
- 'Reserved slots count is bigger then max player value',
680
- )
681
- }
682
-
683
- if (unique.includes('')) {
684
- throw MasterFault.invalid(ErrorMessage.SLOT_CAN_NOT_HAVE_EMPTY_NAME)
685
- }
686
- }
687
-
688
- return { name, properties: known?.table ?? properties, users: unique }
689
- }
690
-
691
- /**
692
- * Creates the game a random join falls back to
693
- * @param peer who's asking
694
- * @param packet what they asked for
695
- */
696
- private fallback(peer: MasterPeer, packet: JoinRandomGameRequest): Seat {
697
- if (packet.joinMode !== JoinMode.CREATE_IF_NOT_EXISTS) {
698
- throw MasterFault.of(ErrorCode.NO_RANDOM_MATCH_FOUND, 'No match found')
699
- }
700
-
701
- const lobby = this.lobby(peer, packet.lobby)
702
- return lobby.create(
703
- peer,
704
- this.placement(
705
- peer,
706
- packet.roomName,
707
- peer.properties(packet.properties ?? packet.gameProperties),
708
- packet.expectedUsers,
709
- ),
710
- )
711
- }
712
-
713
- /**
714
- * Why a filter is refused for what it holds, or null
715
- * @param query the filter, or null
716
- * @param single whether a semicolon counts too
717
- */
718
- private forbidden(query: string | null, single: boolean): MasterFault | null {
719
- const settings = this.services.application.settings
720
- if (query === null || query === '' || settings.onlyLogQueryDataErrors) {
721
- return null
722
- }
723
-
724
- if (single && query.includes(';')) {
725
- return MasterFault.invalid(ErrorMessage.SEMICOLON_IN_QUERY_DATA)
726
- }
727
-
728
- const word = settings.wrongWordsForQueryData.find(
729
- (wrong: string): boolean => query.includes(wrong),
730
- )
731
-
732
- return word === undefined
733
- ? null
734
- : MasterFault.invalid(ErrorMessage.WORD_IN_QUERY_DATA, word)
735
- }
736
-
737
- /**
738
- * Switches lobby statistics on or off for a client
739
- * @param peer who asked
740
- * @param wanted whether they want them
741
- */
742
- private watch(peer: MasterPeer, wanted: boolean): void {
743
- const statistics = this.services.application.lobbyStats
744
-
745
- peer.lobbyStats = wanted
746
- if (wanted) statistics.subscribe(peer)
747
- else statistics.unsubscribe(peer)
748
- }
749
-
750
362
  /**
751
363
  * Runs a WebRPC call through the configured service
752
364
  * @param peer who's calling
@@ -776,328 +388,4 @@ export class MasterHandler {
776
388
  peer.token?.authCookie ?? null,
777
389
  )
778
390
  }
779
-
780
- /**
781
- * Finishes the login of a client whose init request brought a token
782
- * @param peer who connected
783
- * @param photon what's known about the connection
784
- * @param token what the init request brought
785
- */
786
- private async admit(
787
- peer: MasterPeer,
788
- photon: PhotonPeer,
789
- token: AuthToken,
790
- ): Promise<void> {
791
- const refusal = this.refusal(photon, token)
792
- if (refusal !== null) {
793
- peer.send(
794
- Envelope.reliable<OperationResponse>(
795
- OperationResponse.failure(
796
- OperationCode.AUTHENTICATE_ONCE,
797
- refusal.code,
798
- refusal.message,
799
- ),
800
- ),
801
- )
802
- await peer.session.close()
803
- return
804
- }
805
-
806
- this.enter(peer, token, false)
807
- peer.send(OnceAnswer.master(this.services.gate.seal(token)))
808
- }
809
-
810
- /**
811
- * Why a good token still doesn't get a client in, or null
812
- * @param photon what's known about the connection
813
- * @param token what the client brought
814
- */
815
- private refusal(photon: PhotonPeer, token: AuthToken): MasterFault | null {
816
- const application = this.services.application
817
- if (application.settings.requireSecureConnection && !photon.secure) {
818
- return MasterFault.of(
819
- ErrorCode.SECURE_CONNECTION_REQUIRED,
820
- ErrorMessage.SECURE_CONNECTION_REQUIRED,
821
- )
822
- }
823
-
824
- if (application.blocked(token.userId)) {
825
- return MasterFault.of(ErrorCode.USER_BLOCKED, ErrorMessage.USER_BLOCKED)
826
- }
827
-
828
- return null
829
- }
830
-
831
- /**
832
- * Marks a client as logged in
833
- * @param peer who's in
834
- * @param token what they hold from now on
835
- * @param lobbyStats whether they asked for lobby statistics
836
- */
837
- private enter(peer: MasterPeer, token: AuthToken, lobbyStats: boolean): void {
838
- peer.token = token
839
- peer.userId = token.userId
840
- peer.stage = PeerStage.AUTHENTICATED
841
- peer.lobbyStats = lobbyStats
842
- this.services.application.attach(peer)
843
- }
844
-
845
- /**
846
- * Works through one authenticate request
847
- * @param peer who's logging in
848
- * @param packet token or credentials
849
- */
850
- private async login(
851
- peer: MasterPeer,
852
- packet: AuthenticateRequest,
853
- ): Promise<void> {
854
- if (this.services.gate.tokened(packet)) {
855
- await this.byToken(peer, packet)
856
- return
857
- }
858
-
859
- const credentials = packet.credentials
860
- const data = credentials?.authentication?.data ?? null
861
- if (
862
- data !== null &&
863
- data.kind !== Kind.STRING &&
864
- data.kind !== Kind.BYTES
865
- ) {
866
- throw MasterFault.invalid(ErrorMessage.INVALID_TYPE_FOR_AUTH_DATA)
867
- }
868
-
869
- const fault = this.services.applications.fault(
870
- credentials?.applicationId ?? null,
871
- credentials?.appVersion ?? null,
872
- )
873
- if (fault !== null) {
874
- throw MasterFault.of(ErrorCode.INVALID_AUTHENTICATION, fault)
875
- }
876
-
877
- if (this.services.authenticator.enabled) {
878
- await this.custom(peer, packet)
879
- return
880
- }
881
-
882
- const given = credentials?.userId ?? null
883
- const userId = MasterHandler.named(given)
884
- const token = this.issue(packet, userId, {})
885
-
886
- this.enter(peer, token, packet.lobbyStats)
887
- peer.send(
888
- AuthenticateResponse.success(
889
- Grant.of(null, this.sealed(token), userId === given ? null : userId),
890
- ).to(packet),
891
- )
892
- this.greet(peer)
893
- }
894
-
895
- /**
896
- * Logs a client in by the token it brought
897
- * @param peer who's logging in
898
- * @param packet the token
899
- */
900
- private async byToken(
901
- peer: MasterPeer,
902
- packet: AuthenticateRequest,
903
- ): Promise<void> {
904
- const verdict = this.services.gate.verify(packet.token)
905
- const token = verdict.token
906
- if (token === null) {
907
- throw MasterFault.of(
908
- verdict.refusal?.code ?? ErrorCode.INVALID_AUTHENTICATION,
909
- verdict.refusal?.message ?? ErrorMessage.AUTH_TOKEN_TYPE_NOT_SUPPORTED,
910
- )
911
- }
912
-
913
- const photon = PhotonPeer.of(peer.session.channel)
914
- const application = this.services.application
915
- if (application.blocked(token.userId)) {
916
- throw MasterFault.of(ErrorCode.USER_BLOCKED, ErrorMessage.USER_BLOCKED)
917
- }
918
-
919
- if (
920
- application.settings.requireSecureConnection &&
921
- photon?.secure !== true
922
- ) {
923
- peer.send(
924
- AuthenticateResponse.failure(
925
- ErrorCode.SECURE_CONNECTION_REQUIRED,
926
- ErrorMessage.SECURE_CONNECTION_REQUIRED,
927
- ).to(packet),
928
- )
929
- await peer.session.close()
930
- return
931
- }
932
-
933
- const userId = token.userId
934
- this.enter(peer, token, packet.lobbyStats)
935
- peer.send(
936
- AuthenticateResponse.success(
937
- Grant.master(
938
- PhotonValue.string(this.services.gate.seal(token)),
939
- userId === null || userId === '' ? null : userId,
940
- ),
941
- ).to(packet),
942
- )
943
- this.greet(peer)
944
- }
945
-
946
- /**
947
- * Logs a client in through custom authentication
948
- * @param peer who's logging in
949
- * @param packet credentials and auth values
950
- */
951
- private async custom(
952
- peer: MasterPeer,
953
- packet: AuthenticateRequest,
954
- ): Promise<void> {
955
- const credentials = packet.credentials
956
- const attempt: AuthenticationAttempt = {
957
- type:
958
- credentials?.authentication?.type ?? CustomAuthenticationType.CUSTOM,
959
- parameters: credentials?.authentication?.parameters ?? null,
960
- data: credentials?.authentication?.data ?? null,
961
- applicationId: credentials?.applicationId ?? null,
962
- applicationVersion: credentials?.appVersion ?? null,
963
- userId: credentials?.userId ?? null,
964
- }
965
-
966
- peer.stage = PeerStage.AUTHENTICATING
967
-
968
- let result: AuthenticationResult
969
- try {
970
- result = await this.services.authenticator.authenticate(attempt)
971
- } catch (cause) {
972
- if (!(cause instanceof AuthenticationFailure)) throw cause
973
-
974
- throw MasterFault.of(
975
- ErrorCode.CUSTOM_AUTHENTICATION_FAILED,
976
- cause.message,
977
- )
978
- }
979
-
980
- peer.stage = PeerStage.INITIAL
981
- if (!peer.session.channel.isActive) return
982
-
983
- if (result.code === CustomAuthenticationResult.DATA) {
984
- peer.send(
985
- AuthenticateResponse.success(
986
- Grant.of(
987
- null,
988
- null,
989
- null,
990
- null,
991
- null,
992
- null,
993
- MasterHandler.dictionary(result.data),
994
- ),
995
- ).to(packet),
996
- )
997
- return
998
- }
999
-
1000
- if (result.code !== CustomAuthenticationResult.OK) {
1001
- throw MasterFault.of(
1002
- ErrorCode.CUSTOM_AUTHENTICATION_FAILED,
1003
- result.message ?? '',
1004
- )
1005
- }
1006
-
1007
- const provided = result.userId
1008
- const userId = MasterHandler.named(
1009
- provided === null || provided === ''
1010
- ? (credentials?.userId ?? null)
1011
- : provided,
1012
- )
1013
- const token = this.issue(packet, userId, result.cookie ?? {})
1014
- const writer = ParameterWriter.of(OperationCode.AUTHENTICATE)
1015
-
1016
- this.grants.write(
1017
- Grant.of(
1018
- null,
1019
- this.sealed(token),
1020
- userId,
1021
- result.nickname,
1022
- null,
1023
- null,
1024
- MasterHandler.dictionary(result.data),
1025
- ),
1026
- writer,
1027
- )
1028
- this.enter(peer, token, packet.lobbyStats)
1029
- peer.send(
1030
- UnknownResponse.of(
1031
- OperationCode.AUTHENTICATE,
1032
- ErrorCode.OK,
1033
- result.message,
1034
- writer.parameters,
1035
- ).to(packet),
1036
- )
1037
- this.greet(peer)
1038
- }
1039
-
1040
- /**
1041
- * Sends a fresh login its first lobby statistics, if it asked for them
1042
- * @param peer who just logged in
1043
- */
1044
- private greet(peer: MasterPeer): void {
1045
- if (peer.lobbyStats) this.services.application.lobbyStats.subscribe(peer)
1046
- }
1047
-
1048
- /**
1049
- * A new token for a login that brought none
1050
- * @param packet what the client sent
1051
- * @param userId who the token is for
1052
- * @param cookie values custom authentication wants remembered
1053
- */
1054
- private issue(
1055
- packet: AuthenticateRequest,
1056
- userId: string,
1057
- cookie: Readonly<Record<string, unknown>>,
1058
- ): AuthToken {
1059
- const authority = this.services.authority
1060
- if (authority === null) {
1061
- throw new Error(ErrorMessage.AUTH_TOKEN_TYPE_NOT_SUPPORTED)
1062
- }
1063
-
1064
- const credentials = packet.credentials
1065
- const application = credentials?.applicationId ?? null
1066
- const version = credentials?.appVersion ?? null
1067
-
1068
- return authority.issue({
1069
- /**
1070
- * Who the token is for
1071
- */
1072
- userId,
1073
- /**
1074
- * What custom authentication wants remembered
1075
- */
1076
- authCookie: cookie,
1077
- /**
1078
- * Bit flags the servers pass along
1079
- */
1080
- flags: packet.flags ?? 0,
1081
- /**
1082
- * Custom unless the client said otherwise
1083
- */
1084
- customAuthProvider:
1085
- credentials?.authentication?.type ?? CustomAuthenticationType.CUSTOM,
1086
- ...(application === null ? {} : { applicationId: application }),
1087
- ...(version === null ? {} : { applicationVersion: version }),
1088
- })
1089
- }
1090
-
1091
- /**
1092
- * A token as the value a client is sent
1093
- * @param token what to seal
1094
- */
1095
- private sealed(token: AuthToken): PhotonValue {
1096
- const authority = this.services.authority
1097
- if (authority === null) {
1098
- throw new Error(ErrorMessage.AUTH_TOKEN_TYPE_NOT_SUPPORTED)
1099
- }
1100
-
1101
- return PhotonValue.string(authority.seal(token, false))
1102
- }
1103
391
  }