@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
@@ -0,0 +1,446 @@
1
+ import { ActorProperty } from '../../balancing/code/actor/index.ts'
2
+ import { ErrorCode } from '../../balancing/code/error/index.ts'
3
+ import { GameProperty } from '../../balancing/code/game/index.ts'
4
+ import { ErrorMessage } from '../../balancing/code/message/index.ts'
5
+ import { RoomOptionFlag } from '../../balancing/code/room/index.ts'
6
+ import { PropertiesChangedEvent } from '../../balancing/packet/event/properties/index.ts'
7
+ import type { GetPropertiesRequest } from '../../balancing/packet/properties/get/request/index.ts'
8
+ import { GetPropertiesResponse } from '../../balancing/packet/properties/get/response/index.ts'
9
+ import type { SetPropertiesRequest } from '../../balancing/packet/properties/set/request/index.ts'
10
+ import { SetPropertiesResponse } from '../../balancing/packet/properties/set/response/index.ts'
11
+ import { PhotonHashtable } from '../../binary/hashtable/index.ts'
12
+ import { PhotonValue } from '../../binary/value/index.ts'
13
+ import { JsonKeys } from '../../message/json/keys/index.ts'
14
+ import type { Actor } from '../actor/index.ts'
15
+ import { EntrySetup } from '../entry/index.ts'
16
+ import { Fault } from '../fault/index.ts'
17
+ import type { GamePeer } from '../peer/index.ts'
18
+ import { RoomProperties } from '../properties/index.ts'
19
+ import type { GameChanges } from '../report/index.ts'
20
+ import type { Seats } from '../seats/index.ts'
21
+ import type { RoomServices } from '../services/index.ts'
22
+ import { JoinStage } from '../stage/index.ts'
23
+ import type { Throttle } from '../throttle/index.ts'
24
+ import type { Venue } from '../venue/index.ts'
25
+ import { WellKnownProperties } from '../wellknown/index.ts'
26
+
27
+ /**
28
+ * Reads and writes the properties of one room and its actors
29
+ * @class
30
+ */
31
+ export class PropertyEditor {
32
+ /**
33
+ * Shared empty audience
34
+ */
35
+ private static readonly NOBODY: readonly Actor[] = []
36
+
37
+ /**
38
+ * Properties and options of the room
39
+ */
40
+ private readonly properties: RoomProperties
41
+
42
+ /**
43
+ * Who sits in the room
44
+ */
45
+ private readonly seats: Seats
46
+
47
+ /**
48
+ * Counts forwarded operations
49
+ */
50
+ private readonly throttle: Throttle
51
+
52
+ /**
53
+ * What every room shares
54
+ */
55
+ private readonly services: RoomServices
56
+
57
+ /**
58
+ * The room it belongs to
59
+ */
60
+ private readonly venue: Venue
61
+
62
+ /**
63
+ * @param properties properties and options of the room
64
+ * @param seats who sits in the room
65
+ * @param throttle counts forwarded operations
66
+ * @param services what every room shares
67
+ * @param venue the room it belongs to
68
+ */
69
+ public constructor(
70
+ properties: RoomProperties,
71
+ seats: Seats,
72
+ throttle: Throttle,
73
+ services: RoomServices,
74
+ venue: Venue,
75
+ ) {
76
+ this.properties = properties
77
+ this.seats = seats
78
+ this.throttle = throttle
79
+ this.services = services
80
+ this.venue = venue
81
+ }
82
+
83
+ /**
84
+ * Property keys as a bag knows them
85
+ * @param keys keys as the client sent them
86
+ * @param json whether the client speaks JSON
87
+ * @param codes property codes a JSON client sends as text
88
+ */
89
+ private static keys(
90
+ keys: readonly PhotonValue[],
91
+ json: boolean,
92
+ codes: readonly number[],
93
+ ): readonly PhotonValue[] {
94
+ if (!json) return keys
95
+
96
+ return keys.map<PhotonValue>((key: PhotonValue): PhotonValue => {
97
+ const code = Number(key.text() ?? '')
98
+ return codes.includes(code) ? PhotonValue.byte(code) : key
99
+ })
100
+ }
101
+
102
+ /**
103
+ * Answers with properties of the room, its actors, or both
104
+ * @param peer who's asking
105
+ * @param packet what the client sent
106
+ */
107
+ public inspect(peer: GamePeer, packet: GetPropertiesRequest): void {
108
+ const bag = this.properties.bag
109
+ const kinds = packet.kinds ?? 0
110
+ const publish = this.properties.has(RoomOptionFlag.PUBLISH_USER_ID)
111
+ let game: PhotonHashtable | null = null
112
+ let actors: PhotonHashtable | null = null
113
+
114
+ if ((kinds & 0x01) !== 0) {
115
+ const keys = packet.gameProperties
116
+ game =
117
+ keys === null
118
+ ? bag.all()
119
+ : bag.pick(PropertyEditor.keys(keys, peer.json, JsonKeys.ROOM))
120
+ }
121
+
122
+ if ((kinds & 0x02) !== 0) {
123
+ const keys = packet.actorProperties
124
+ const wanted =
125
+ packet.actors === null ? null : new Set<number>(packet.actors)
126
+ actors = PhotonHashtable.of()
127
+
128
+ for (const actor of this.seats.active) {
129
+ if (wanted !== null && !wanted.has(actor.number)) continue
130
+
131
+ const table =
132
+ keys === null
133
+ ? actor.properties.all()
134
+ : actor.properties.pick(
135
+ PropertyEditor.keys(keys, peer.json, JsonKeys.ACTOR),
136
+ )
137
+ if (keys === null && publish) {
138
+ table.set(
139
+ RoomProperties.key(ActorProperty.USER_ID),
140
+ PhotonValue.string(actor.userId),
141
+ )
142
+ }
143
+
144
+ actors.set(PhotonValue.int(actor.number), PhotonValue.hashtable(table))
145
+ }
146
+ }
147
+
148
+ peer.send(GetPropertiesResponse.success(actors, game).to(packet))
149
+ }
150
+
151
+ /**
152
+ * Changes properties of the room or of one actor and says why it failed, or null
153
+ * @param peer who's changing them
154
+ * @param packet what the client sent
155
+ */
156
+ public change(peer: GamePeer, packet: SetPropertiesRequest): Fault | null {
157
+ return packet.actorNumber > 0
158
+ ? this.changeActor(peer, packet)
159
+ : this.changeRoom(peer, packet)
160
+ }
161
+
162
+ /**
163
+ * Who hears about a property change
164
+ * @param sender who made it
165
+ * @param packet what the client sent
166
+ * @param swapping whether expected values came along
167
+ */
168
+ private audience(
169
+ sender: Actor,
170
+ packet: SetPropertiesRequest,
171
+ swapping: boolean,
172
+ ): readonly Actor[] {
173
+ if (!packet.broadcast && !swapping) return PropertyEditor.NOBODY
174
+ if (
175
+ swapping ||
176
+ this.properties.has(RoomOptionFlag.BROADCAST_PROPS_CHANGE_TO_ALL)
177
+ ) {
178
+ return [...this.seats.active]
179
+ }
180
+
181
+ return this.seats.active.filter((actor: Actor): boolean => actor !== sender)
182
+ }
183
+
184
+ /**
185
+ * The actor behind a property request, or why there's none
186
+ * @param peer who's changing properties
187
+ * @param packet what the client sent
188
+ */
189
+ private setter(peer: GamePeer, packet: SetPropertiesRequest): Actor | Fault {
190
+ const sender = this.seats.playing(peer)
191
+ if (sender === null) {
192
+ return Fault.invalid(ErrorMessage.PEER_NOT_JOINED_TO_ROOM)
193
+ }
194
+
195
+ return this.throttle.throttled(packet.webFlags)
196
+ ? Fault.of(
197
+ ErrorCode.HTTP_LIMIT_REACHED,
198
+ ErrorMessage.HTTP_FORWARDED_OPERATIONS_LIMIT_REACHED,
199
+ )
200
+ : sender
201
+ }
202
+
203
+ /**
204
+ * Changes the properties of one actor and says why it failed, or null
205
+ * @param peer who's changing them
206
+ * @param packet what the client sent
207
+ */
208
+ private changeActor(
209
+ peer: GamePeer,
210
+ packet: SetPropertiesRequest,
211
+ ): Fault | null {
212
+ const sender = this.setter(peer, packet)
213
+ if (sender instanceof Fault) return sender
214
+
215
+ const target = this.seats.numbered(packet.actorNumber)
216
+ if (target === null) {
217
+ return Fault.invalid(ErrorMessage.ACTOR_NOT_FOUND, packet.actorNumber)
218
+ }
219
+
220
+ const joining =
221
+ target.peer !== null && target.peer.stage !== JoinStage.COMPLETE
222
+ if (packet.broadcast && joining) {
223
+ return Fault.invalid(
224
+ ErrorMessage.ACTOR_JOINING_NOT_COMPLETE,
225
+ packet.actorNumber,
226
+ )
227
+ }
228
+
229
+ const expected = EntrySetup.promoted(
230
+ packet.expectedValues,
231
+ peer.json,
232
+ JsonKeys.ACTOR,
233
+ )
234
+ const properties = EntrySetup.stripped(
235
+ EntrySetup.promoted(packet.properties, peer.json, JsonKeys.ACTOR),
236
+ )
237
+ const limit = this.services.configuration.propertiesCountLimit
238
+ if (target.properties.grown(properties) > limit) {
239
+ return Fault.of(ErrorCode.OPERATION_INVALID, 'Too many properties')
240
+ }
241
+
242
+ const swapping = expected !== null && expected.size > 0
243
+ const unmet = target.properties.swap(properties, swapping ? expected : null)
244
+ if (unmet !== null) return Fault.of(ErrorCode.OPERATION_INVALID, unmet)
245
+
246
+ peer.send(SetPropertiesResponse.success().to(packet))
247
+ const audience = this.properties.quiet
248
+ ? PropertyEditor.NOBODY
249
+ : this.audience(sender, packet, swapping)
250
+ if (audience.length === 0) return null
251
+
252
+ this.venue.tell(
253
+ audience,
254
+ PropertiesChangedEvent.of(sender.number, target.number, properties).via(
255
+ packet.travel,
256
+ ),
257
+ )
258
+
259
+ return null
260
+ }
261
+
262
+ /**
263
+ * Changes the properties of the room and says why it failed, or null
264
+ * @param peer who's changing them
265
+ * @param packet what the client sent
266
+ */
267
+ private changeRoom(
268
+ peer: GamePeer,
269
+ packet: SetPropertiesRequest,
270
+ ): Fault | null {
271
+ const known = WellKnownProperties.read(
272
+ EntrySetup.promoted(packet.properties, peer.json, JsonKeys.ROOM),
273
+ )
274
+ if (known.fault !== null) {
275
+ return Fault.of(ErrorCode.OPERATION_INVALID, known.fault)
276
+ }
277
+
278
+ const sender = this.setter(peer, packet)
279
+ if (sender instanceof Fault) return sender
280
+
281
+ const bag = this.properties.bag
282
+ const limit = this.services.configuration.propertiesCountLimit
283
+ if (bag.grown(known.properties) > limit) {
284
+ return Fault.of(ErrorCode.OPERATION_INVALID, 'Too many properties')
285
+ }
286
+
287
+ const wanted = WellKnownProperties.read(
288
+ EntrySetup.promoted(packet.expectedValues, peer.json, JsonKeys.ROOM),
289
+ )
290
+ const expected =
291
+ wanted.fault === null ? wanted.properties : packet.expectedValues
292
+ const swapping = expected !== null && expected.size > 0
293
+ const before = bag.all()
294
+ const seats = [...this.seats.expected]
295
+ const properties = this.properties
296
+ const state = [
297
+ properties.maxPlayers,
298
+ properties.isOpen,
299
+ properties.isVisible,
300
+ ] as const
301
+
302
+ const fault = this.revise(known, swapping ? expected : null)
303
+ if (fault !== null) {
304
+ bag.restore(before)
305
+ this.seats.expected = seats
306
+ return fault
307
+ }
308
+
309
+ peer.send(SetPropertiesResponse.success().to(packet))
310
+ this.venue.tell(
311
+ this.audience(sender, packet, swapping),
312
+ PropertiesChangedEvent.of(sender.number, 0, known.properties).via(
313
+ packet.travel,
314
+ ),
315
+ )
316
+ this.publish(known, state)
317
+
318
+ return null
319
+ }
320
+
321
+ /**
322
+ * Writes checked room properties and says why it failed, or null
323
+ * @param known the properties after their type check
324
+ * @param expected values the room must hold first, or null
325
+ */
326
+ private revise(
327
+ known: WellKnownProperties,
328
+ expected: PhotonHashtable | null,
329
+ ): Fault | null {
330
+ const longest = this.services.configuration.maxEmptyRoomTtl
331
+ const stay = known.number(GameProperty.EMPTY_ROOM_TTL)
332
+ if (stay !== null && stay > longest) {
333
+ return Fault.invalid(
334
+ ErrorMessage.SET_PROPERTIES_MAX_TTL_EXCEEDED,
335
+ stay,
336
+ longest,
337
+ )
338
+ }
339
+
340
+ const leader = known.number(GameProperty.MASTER_CLIENT_ID)
341
+ if (leader !== null && !this.seats.numbers.includes(leader)) {
342
+ return Fault.invalid(
343
+ "MasterClientId value '{0}' is invalid. No actor with such number",
344
+ leader,
345
+ )
346
+ }
347
+
348
+ if (known.has(GameProperty.EXPECTED_USERS) && expected === null) {
349
+ return Fault.invalid('ExpectedUsers may be updated only in CAS mode')
350
+ }
351
+
352
+ const limit = known.number(GameProperty.MAX_PLAYERS) ?? 0
353
+ if (limit !== 0 && limit < this.seats.taken) {
354
+ return Fault.invalid(
355
+ 'new MaxPlayer can not be less then current amount of players',
356
+ )
357
+ }
358
+
359
+ const unmet = this.properties.bag.swap(known.properties, expected)
360
+ if (unmet !== null) return Fault.of(ErrorCode.OPERATION_INVALID, unmet)
361
+
362
+ if (leader !== null) this.seats.master = leader
363
+ this.properties.list(known.properties)
364
+
365
+ return known.has(GameProperty.EXPECTED_USERS)
366
+ ? this.reseat(known, expected)
367
+ : null
368
+ }
369
+
370
+ /**
371
+ * Swaps the list of kept seats and says why it failed, or null
372
+ * @param known the properties after their type check
373
+ * @param expected values the room held first, or null
374
+ */
375
+ private reseat(
376
+ known: WellKnownProperties,
377
+ expected: PhotonHashtable | null,
378
+ ): Fault | null {
379
+ const key = RoomProperties.key(GameProperty.EXPECTED_USERS)
380
+ const old = WellKnownProperties.users(expected?.get(key) ?? null) ?? []
381
+ const users =
382
+ WellKnownProperties.users(known.value(GameProperty.EXPECTED_USERS)) ?? []
383
+ if (users.includes('')) {
384
+ return Fault.invalid(ErrorMessage.SLOT_CAN_NOT_HAVE_EMPTY_NAME)
385
+ }
386
+
387
+ const kept = this.seats.expected.filter(
388
+ (user: string): boolean => !old.includes(user) || users.includes(user),
389
+ )
390
+ const seats = [...new Set<string>([...kept, ...users])]
391
+ const limit = this.properties.maxPlayers
392
+ const taken = this.seats.taken + this.seats.pending(seats)
393
+ if (limit !== 0 && taken > limit) {
394
+ return Fault.invalid('Not enough places to reserve all requested slots')
395
+ }
396
+
397
+ this.seats.expected = seats
398
+ return null
399
+ }
400
+
401
+ /**
402
+ * Tells the Master about changed room properties if it cares
403
+ * @param known the properties that were written
404
+ * @param state player limit, open and visible as they were before
405
+ */
406
+ private publish(
407
+ known: WellKnownProperties,
408
+ state: readonly [number, boolean, boolean],
409
+ ): void {
410
+ const herald = this.services.herald
411
+ const properties = this.properties
412
+ const refiltered = known.has(GameProperty.PROPS_LISTED_IN_LOBBY)
413
+ const reseated = known.has(GameProperty.EXPECTED_USERS)
414
+ const limited =
415
+ known.has(GameProperty.MAX_PLAYERS) && properties.maxPlayers !== state[0]
416
+ const opened =
417
+ known.has(GameProperty.IS_OPEN) && properties.isOpen !== state[1]
418
+ const shown =
419
+ known.has(GameProperty.IS_VISIBLE) && properties.isVisible !== state[2]
420
+ const listed = properties.listed(
421
+ refiltered ? properties.bag.all() : known.properties,
422
+ )
423
+ if (
424
+ !limited &&
425
+ !opened &&
426
+ !shown &&
427
+ !refiltered &&
428
+ !reseated &&
429
+ listed.size === 0
430
+ ) {
431
+ return
432
+ }
433
+
434
+ const filter = properties.filter
435
+ const changes: GameChanges = {
436
+ maxPlayers: limited ? properties.maxPlayers : null,
437
+ isOpen: opened ? properties.isOpen : null,
438
+ isVisible: shown ? properties.isVisible : null,
439
+ expectedUsers: reseated ? [...this.seats.expected] : null,
440
+ propertyFilter:
441
+ refiltered && filter !== null ? herald.filter(filter) : null,
442
+ properties: listed.size === 0 ? null : herald.properties(listed),
443
+ }
444
+ this.venue.report(changes)
445
+ }
446
+ }
@@ -82,16 +82,17 @@ export class GameGate extends InboundHandlerAdapter {
82
82
  * @param context the connection
83
83
  * @param message whatever the message layer handed up
84
84
  */
85
- public override channelRead(
86
- context: HandlerContext,
87
- message: unknown,
88
- ): void | Promise<void> {
89
- if (!(message instanceof Envelope)) return context.fireChannelRead(message)
85
+ public override channelRead(context: HandlerContext, message: unknown): void {
86
+ if (!(message instanceof Envelope)) {
87
+ context.fireChannelRead(message)
88
+ return
89
+ }
90
90
 
91
91
  const envelope = message as Envelope
92
92
  const request = envelope.message
93
93
  if (!(request instanceof OperationRequest)) {
94
- return context.fireChannelRead(message)
94
+ context.fireChannelRead(message)
95
+ return
95
96
  }
96
97
 
97
98
  this.peer.operated = this.clock.now()
@@ -101,7 +102,7 @@ export class GameGate extends InboundHandlerAdapter {
101
102
  const fault =
102
103
  refusal === null ? this.uncallable(request) : Fault.denied(refusal)
103
104
  if (fault !== null) {
104
- context.writeAndFlushSync(
105
+ context.writeAndFlush(
105
106
  travel
106
107
  .reply()
107
108
  .wrap<OperationResponse>(
@@ -111,7 +112,7 @@ export class GameGate extends InboundHandlerAdapter {
111
112
  return
112
113
  }
113
114
 
114
- return context.fireChannelRead(
115
+ context.fireChannelRead(
115
116
  this.garbled(request)
116
117
  ? travel.wrap<OperationRequest>(OperationRequest.of(request.code))
117
118
  : message,
@@ -208,6 +208,11 @@ export class GameHandler {
208
208
  return
209
209
  }
210
210
 
211
+ if (this.crowded()) {
212
+ peer.send(setup.refuse(GameHandler.full()))
213
+ return
214
+ }
215
+
211
216
  const room = this.services.rooms.create(
212
217
  this.application(peer),
213
218
  this.version(peer),
@@ -246,10 +251,16 @@ export class GameHandler {
246
251
 
247
252
  const rooms = this.services.rooms
248
253
  const loads = this.services.configuration.hooks?.asyncJoin ?? false
254
+ const creates = setup.mode !== JoinMode.DEFAULT || loads
255
+ const found = rooms.find(application, version, setup.roomName)
256
+ if (found === null && creates && this.crowded()) {
257
+ peer.send(setup.refuse(GameHandler.full()))
258
+ return
259
+ }
260
+
249
261
  const room =
250
- setup.mode === JoinMode.DEFAULT && !loads
251
- ? rooms.find(application, version, setup.roomName)
252
- : rooms.obtain(application, version, setup.roomName)
262
+ found ??
263
+ (creates ? rooms.obtain(application, version, setup.roomName) : null)
253
264
  if (room === null) {
254
265
  peer.send(
255
266
  setup.refuse(
@@ -419,6 +430,22 @@ export class GameHandler {
419
430
  }
420
431
  }
421
432
 
433
+ /**
434
+ * Whether the server holds as many rooms as it may
435
+ */
436
+ private crowded(): boolean {
437
+ return (
438
+ this.services.rooms.size >= this.services.configuration.roomsCountLimit
439
+ )
440
+ }
441
+
442
+ /**
443
+ * Why a room can't be made on a full server
444
+ */
445
+ private static full(): Fault {
446
+ return Fault.of(ErrorCode.SERVER_FULL, 'Server is full')
447
+ }
448
+
422
449
  /**
423
450
  * The peer of a connection
424
451
  * @param session the connection