@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,234 @@
1
+ import { EventCaching } from '../../balancing/code/caching/index.ts'
2
+ import { CustomEvent } from '../../balancing/packet/event/custom/index.ts'
3
+ import { CacheSliceChangedEvent } from '../../balancing/packet/event/slice/index.ts'
4
+ import type { RaiseEventRequest } from '../../balancing/packet/raise/request/index.ts'
5
+ import type { Actor } from '../actor/index.ts'
6
+ import { ActorCache, type CacheSlice, RoomCache } from '../cache/index.ts'
7
+ import { Fault } from '../fault/index.ts'
8
+ import type { GamePeer } from '../peer/index.ts'
9
+ import type { Seats } from '../seats/index.ts'
10
+ import type { GameConfiguration } from '../settings/index.ts'
11
+ import type { Venue } from '../venue/index.ts'
12
+
13
+ /**
14
+ * The events one room keeps for later joiners
15
+ * @class
16
+ */
17
+ export class History {
18
+ /**
19
+ * Events kept for the whole room
20
+ */
21
+ private readonly roomCache: RoomCache
22
+
23
+ /**
24
+ * Events actors keep about themselves
25
+ */
26
+ private readonly actorCache: ActorCache
27
+
28
+ /**
29
+ * Who hears about a slice change
30
+ */
31
+ private readonly seats: Seats
32
+
33
+ /**
34
+ * The room it belongs to
35
+ */
36
+ private readonly venue: Venue
37
+
38
+ /**
39
+ * @param configuration cache limits
40
+ * @param seats who hears about a slice change
41
+ * @param venue the room it belongs to
42
+ */
43
+ public constructor(
44
+ configuration: GameConfiguration,
45
+ seats: Seats,
46
+ venue: Venue,
47
+ ) {
48
+ this.roomCache = new RoomCache(
49
+ configuration.eventCacheLimit,
50
+ configuration.sliceLimit,
51
+ )
52
+ this.actorCache = new ActorCache(configuration.actorEventCacheLimit)
53
+ this.seats = seats
54
+ this.venue = venue
55
+ }
56
+
57
+ /**
58
+ * The refusal for content an actor cache can't hold
59
+ * @param operation name of the cache operation
60
+ */
61
+ private static unsuited(operation: string): Fault {
62
+ return Fault.invalid(
63
+ "Cache operation '{0}' requires a Hashtable as event data.",
64
+ operation,
65
+ )
66
+ }
67
+
68
+ /**
69
+ * Whether a cache passed its limit
70
+ */
71
+ public get discarded(): boolean {
72
+ return this.roomCache.discarded || this.actorCache.discarded
73
+ }
74
+
75
+ /**
76
+ * How many events the room cache holds
77
+ */
78
+ public get size(): number {
79
+ return this.roomCache.size
80
+ }
81
+
82
+ /**
83
+ * Index new room events go to
84
+ */
85
+ public get current(): number {
86
+ return this.roomCache.current
87
+ }
88
+
89
+ /**
90
+ * Whether a room cache slice exists
91
+ * @param index which slice
92
+ */
93
+ public has(index: number): boolean {
94
+ return this.roomCache.has(index)
95
+ }
96
+
97
+ /**
98
+ * Room cache slices that hold events
99
+ */
100
+ public filled(): readonly CacheSlice[] {
101
+ return this.roomCache.filled()
102
+ }
103
+
104
+ /**
105
+ * Puts saved slices back
106
+ * @param slices slices as they were saved, or null
107
+ * @param current index new events go to
108
+ */
109
+ public load(slices: readonly CacheSlice[] | null, current: number): void {
110
+ this.roomCache.load(slices, current)
111
+ }
112
+
113
+ /**
114
+ * Sends a joiner every cached event
115
+ * @param peer who got in
116
+ * @param from first room cache slice to send
117
+ */
118
+ public replay(peer: GamePeer, from: number): void {
119
+ for (const event of this.actorCache.replay()) {
120
+ peer.send(CustomEvent.of(event.code, event.actor, event.data))
121
+ }
122
+
123
+ for (const slice of this.roomCache.replay(from)) {
124
+ if (slice.index !== 0) {
125
+ peer.send(CacheSliceChangedEvent.of(slice.index, 0))
126
+ }
127
+
128
+ for (const event of slice.events) {
129
+ peer.send(CustomEvent.of(event.code, event.actor, event.data))
130
+ }
131
+ }
132
+ }
133
+
134
+ /**
135
+ * Drops every event of an actor
136
+ * @param actor its number
137
+ */
138
+ public forget(actor: number): void {
139
+ this.actorCache.forget(actor)
140
+ this.roomCache.forget(actor)
141
+ }
142
+
143
+ /**
144
+ * Drops the room events a removal request picks
145
+ * @param packet what the client sent
146
+ */
147
+ public remove(packet: RaiseEventRequest): void {
148
+ this.roomCache.remove(packet.eventCode, packet.actors, packet.data)
149
+ }
150
+
151
+ /**
152
+ * Drops the room events of actors that aren't connected
153
+ */
154
+ public prune(): void {
155
+ this.roomCache.prune(new Set<number>(this.seats.numbers))
156
+ }
157
+
158
+ /**
159
+ * Puts a raised event in a cache, or says why not
160
+ * @param sender who raised it
161
+ * @param packet what the client sent
162
+ */
163
+ public keep(sender: Actor, packet: RaiseEventRequest): Fault | null {
164
+ const code = packet.eventCode
165
+ const data = packet.data
166
+
167
+ switch (packet.caching) {
168
+ case EventCaching.MERGE_CACHE:
169
+ if (!ActorCache.suits(data)) return History.unsuited('MergeCache')
170
+ this.actorCache.merge(sender.number, code, data)
171
+ return null
172
+ case EventCaching.REPLACE_CACHE:
173
+ if (!ActorCache.suits(data)) return History.unsuited('ReplaceCache')
174
+ this.actorCache.replace(sender.number, code, data)
175
+ return null
176
+ case EventCaching.REMOVE_CACHE:
177
+ if (!ActorCache.suits(data)) return History.unsuited('RemoveCache')
178
+ this.actorCache.remove(sender.number, code)
179
+ return null
180
+ case EventCaching.ADD_TO_ROOM_CACHE:
181
+ this.roomCache.add(sender.number, code, data)
182
+ return null
183
+ case EventCaching.ADD_TO_ROOM_CACHE_GLOBAL:
184
+ this.roomCache.add(0, code, data)
185
+ return null
186
+ default:
187
+ return Fault.invalid("Unknown cache operation '{0}'.", packet.caching)
188
+ }
189
+ }
190
+
191
+ /**
192
+ * Moves or purges cache slices, or says why not
193
+ * @param sender who asked
194
+ * @param packet what the client sent
195
+ */
196
+ public slice(sender: Actor, packet: RaiseEventRequest): Fault | null {
197
+ const index = packet.cacheSliceIndex
198
+ const before = this.roomCache.current
199
+ let fault: Fault | null
200
+
201
+ switch (packet.caching) {
202
+ case EventCaching.SLICE_INCREASE_INDEX:
203
+ fault = this.roomCache.increase()
204
+ break
205
+ case EventCaching.SLICE_SET_INDEX:
206
+ fault =
207
+ index === null
208
+ ? Fault.invalid('SliceSetIndex: Missing paramter CacheSliceIndex.')
209
+ : this.roomCache.move(index)
210
+ break
211
+ case EventCaching.SLICE_PURGE_INDEX:
212
+ return index === null
213
+ ? Fault.invalid('SlicePurgeIndex: Missing paramter CacheSliceIndex.')
214
+ : this.roomCache.purge(index)
215
+ case EventCaching.SLICE_PURGE_UP_TO_INDEX:
216
+ return index === null
217
+ ? Fault.invalid(
218
+ 'SlicePurgeUpToIndex: Missing paramter CacheSliceIndex.',
219
+ )
220
+ : this.roomCache.purgeBelow(index)
221
+ default:
222
+ return Fault.invalid('Unknown cache operation={0}.', packet.caching)
223
+ }
224
+
225
+ if (fault !== null || this.roomCache.current === before) return fault
226
+
227
+ this.venue.tell(
228
+ this.seats.active,
229
+ CacheSliceChangedEvent.of(this.roomCache.current, sender.number),
230
+ )
231
+
232
+ return null
233
+ }
234
+ }
@@ -31,7 +31,8 @@ import type { EntrySetup } from '../entry/index.ts'
31
31
  import { Fault } from '../fault/index.ts'
32
32
  import type { GamePeer } from '../peer/index.ts'
33
33
  import type { GameChanges } from '../report/index.ts'
34
- import { Room, type RoomServices } from '../room/index.ts'
34
+ import { Room } from '../room/index.ts'
35
+ import type { RoomServices } from '../services/index.ts'
35
36
  import type { RoomSnapshot } from '../snapshot/index.ts'
36
37
  import { JoinStage } from '../stage/index.ts'
37
38
  import { WellKnownProperties } from '../wellknown/index.ts'
@@ -222,7 +223,7 @@ export class HookedRoom extends Room {
222
223
  */
223
224
  public override leave(peer: GamePeer, packet: LeaveRequest): void {
224
225
  this.run((): void => {
225
- const actor = this.playing(peer)
226
+ const actor = this.seats.playing(peer)
226
227
  const cookie = ((packet.webFlags ?? 0) & WebFlag.SEND_AUTH_COOKIE) !== 0
227
228
 
228
229
  if (actor !== null) {
@@ -235,7 +236,7 @@ export class HookedRoom extends Room {
235
236
  this.parted(
236
237
  leaver,
237
238
  LeaveReason.LEAVE_REQUEST,
238
- packet.isInactive && this.playerTtl !== 0,
239
+ packet.isInactive && this.properties.playerTtl !== 0,
239
240
  )
240
241
  }
241
242
 
@@ -250,7 +251,7 @@ export class HookedRoom extends Room {
250
251
  public override disconnected(peer: GamePeer): void {
251
252
  this.run((): void => {
252
253
  if (peer.room === this) {
253
- const actor = this.playing(peer)
254
+ const actor = this.seats.playing(peer)
254
255
  const leaver: HookActor = {
255
256
  number: actor?.number ?? -1,
256
257
  userId: peer.userId,
@@ -260,7 +261,7 @@ export class HookedRoom extends Room {
260
261
  this.parted(
261
262
  leaver,
262
263
  peer.reason ?? LeaveReasons.closing(peer.channel),
263
- peer.stage === JoinStage.COMPLETE && this.playerTtl !== 0,
264
+ peer.stage === JoinStage.COMPLETE && this.properties.playerTtl !== 0,
264
265
  )
265
266
  }
266
267
 
@@ -396,9 +397,9 @@ export class HookedRoom extends Room {
396
397
  * Takes out an actor whose time away ran out and tells the web service
397
398
  * @param actor who stayed away
398
399
  */
399
- protected override expire(actor: Actor): void {
400
+ public override expire(actor: Actor): void {
400
401
  this.run((): void => {
401
- if (!this.closed && this.inactive.includes(actor)) {
402
+ if (this.lifecycle.waiting(actor)) {
402
403
  const leaver: HookActor = {
403
404
  number: actor.number,
404
405
  userId: actor.userId,
@@ -450,7 +451,7 @@ export class HookedRoom extends Room {
450
451
  peer: GamePeer,
451
452
  packet: SetPropertiesRequest,
452
453
  ): Fault | null {
453
- const sender = this.playing(peer)
454
+ const sender = this.seats.playing(peer)
454
455
  const fault = super.altered(peer, packet)
455
456
  const flags = packet.webFlags ?? 0
456
457
  const url = this.routes.properties
@@ -571,7 +572,7 @@ export class HookedRoom extends Room {
571
572
  this.hooks.failed(message)
572
573
 
573
574
  if (!this.hooks.hasErrorInfo) return
574
- this.tell(this.active, ErrorInfoEvent.of(message))
575
+ this.tell(this.seats.active, ErrorInfoEvent.of(message))
575
576
  }
576
577
 
577
578
  /**
@@ -598,7 +599,7 @@ export class HookedRoom extends Room {
598
599
  const wanted = (flags & WebFlag.SEND_STATE) !== 0
599
600
 
600
601
  return wanted && this.routes.persistent
601
- ? GameStates.write(this.snapshot())
602
+ ? GameStates.write(this.archive.capture())
602
603
  : null
603
604
  }
604
605
 
@@ -655,7 +656,7 @@ export class HookedRoom extends Room {
655
656
  * @param peer who asked
656
657
  */
657
658
  private joined(peer: GamePeer): void {
658
- const actor = this.playing(peer)
659
+ const actor = this.seats.playing(peer)
659
660
  const url = this.routes.join
660
661
  if (actor === null || peer.stage !== JoinStage.COMPLETE || url === '') {
661
662
  return
@@ -805,10 +806,10 @@ export class HookedRoom extends Room {
805
806
  return
806
807
  }
807
808
 
808
- this.restore(snapshot)
809
+ this.archive.restore(snapshot)
809
810
  this.report(this.describe())
810
811
  this.open(peer, setup, false)
811
- this.vacate()
812
+ this.lifecycle.rearm()
812
813
  }
813
814
 
814
815
  /**
@@ -844,13 +845,13 @@ export class HookedRoom extends Room {
844
845
  const url = this.routes.close
845
846
  if (url === '') return null
846
847
 
847
- const kept = this.routes.persistent ? this.inactive.length : 0
848
+ const kept = this.routes.persistent ? this.seats.inactive.length : 0
848
849
 
849
850
  return HookRequest.close(
850
851
  url,
851
852
  this.game,
852
853
  kept,
853
- kept > 0 ? GameStates.write(this.snapshot()) : null,
854
+ kept > 0 ? GameStates.write(this.archive.capture()) : null,
854
855
  )
855
856
  }
856
857
 
@@ -1,29 +1,37 @@
1
1
  export { Actor } from './actor/index.ts'
2
+ export { Admission } from './admission/index.ts'
2
3
  export { TokenAuthenticator } from './authenticator/index.ts'
3
4
  export type { Authenticator, Renewal } from './authenticator/index.ts'
4
5
  export { PropertyBag } from './bag/index.ts'
6
+ export { Broadcaster } from './broadcast/index.ts'
7
+ export type { Broadcast } from './broadcast/index.ts'
5
8
  export { ActorCache, RoomCache } from './cache/index.ts'
6
9
  export type { CachedEvent, CacheSlice } from './cache/index.ts'
7
10
  export { StampClock, WallAlarm, WallClock } from './clock/index.ts'
8
11
  export type { Alarm, Clock } from './clock/index.ts'
9
12
  export { PropertyComparer } from './compare/index.ts'
13
+ export { PropertyEditor } from './editor/index.ts'
10
14
  export { EntrySetup } from './entry/index.ts'
11
15
  export type { EntryRequest, EntryResponse } from './entry/index.ts'
12
16
  export { Fault } from './fault/index.ts'
13
17
  export { GameGate } from './gate/index.ts'
14
18
  export { InterestGroups } from './groups/index.ts'
15
19
  export { CallGuard } from './guard/index.ts'
20
+ export { History } from './history/index.ts'
16
21
  export { GameHandler } from './handler/index.ts'
17
22
  export { HookedRoom } from './hooked/index.ts'
18
23
  export type { Reentry } from './hooked/index.ts'
24
+ export { Lifecycle } from './lifecycle/index.ts'
19
25
  export type { GameServices } from './handler/index.ts'
20
26
  export { GamePeer } from './peer/index.ts'
27
+ export { RoomProperties } from './properties/index.ts'
21
28
  export { Herald } from './report/index.ts'
22
29
  export type { GameChanges, GameSummary } from './report/index.ts'
23
30
  export { Room } from './room/index.ts'
24
- export type { RoomServices } from './room/index.ts'
25
31
  export { Rooms } from './rooms/index.ts'
32
+ export { Seats } from './seats/index.ts'
26
33
  export { GameServer } from './server/index.ts'
34
+ export type { RoomServices } from './services/index.ts'
27
35
  export { GameConfiguration } from './settings/index.ts'
28
36
  export { SharedFrames } from './shared/index.ts'
29
37
  export type { GameSettings } from './settings/index.ts'
@@ -34,7 +42,10 @@ export type {
34
42
  WebRpcResult,
35
43
  WebRpcService,
36
44
  } from '../rpc/service/index.ts'
45
+ export { RoomArchive } from './snapshot/index.ts'
37
46
  export type { ActorSnapshot, RoomSnapshot } from './snapshot/index.ts'
47
+ export { Throttle } from './throttle/index.ts'
48
+ export type { Venue } from './venue/index.ts'
38
49
  export { JoinStage } from './stage/index.ts'
39
50
  export type { JoinStageId } from './stage/index.ts'
40
51
  export { TouchHandler } from './touch/index.ts'
@@ -0,0 +1,193 @@
1
+ import type { Actor } from '../actor/index.ts'
2
+ import type { Alarm, Clock } from '../clock/index.ts'
3
+ import type { GamePeer } from '../peer/index.ts'
4
+ import type { RoomProperties } from '../properties/index.ts'
5
+ import type { Seats } from '../seats/index.ts'
6
+ import type { Venue } from '../venue/index.ts'
7
+
8
+ /**
9
+ * How long a room and its empty seats stay around
10
+ * @class
11
+ */
12
+ export class Lifecycle {
13
+ /**
14
+ * A player time to live that never runs out
15
+ */
16
+ private static readonly FOREVER: number = 2 ** 31 - 1
17
+
18
+ /**
19
+ * Connections that keep the room alive
20
+ */
21
+ private readonly references: Set<GamePeer>
22
+
23
+ /**
24
+ * Who sits in the room
25
+ */
26
+ private readonly seats: Seats
27
+
28
+ /**
29
+ * Where the waits come from
30
+ */
31
+ private readonly properties: RoomProperties
32
+
33
+ /**
34
+ * Tells the time and runs the waits
35
+ */
36
+ private readonly clock: Clock
37
+
38
+ /**
39
+ * The room it looks after
40
+ */
41
+ private readonly venue: Venue
42
+
43
+ /**
44
+ * Whether the room is gone for good
45
+ */
46
+ private ended: boolean
47
+
48
+ /**
49
+ * Closes the empty room once its time is up, or null
50
+ */
51
+ private removal: Alarm | null
52
+
53
+ /**
54
+ * @param seats who sits in the room
55
+ * @param properties where the waits come from
56
+ * @param clock tells the time and runs the waits
57
+ * @param venue the room it looks after
58
+ */
59
+ public constructor(
60
+ seats: Seats,
61
+ properties: RoomProperties,
62
+ clock: Clock,
63
+ venue: Venue,
64
+ ) {
65
+ this.references = new Set<GamePeer>()
66
+ this.seats = seats
67
+ this.properties = properties
68
+ this.clock = clock
69
+ this.venue = venue
70
+ this.ended = false
71
+ this.removal = null
72
+ }
73
+
74
+ /**
75
+ * Whether the room is gone for good
76
+ */
77
+ public get closed(): boolean {
78
+ return this.ended
79
+ }
80
+
81
+ /**
82
+ * Notes a connection that keeps the room alive
83
+ * @param peer the connection
84
+ */
85
+ public hold(peer: GamePeer): void {
86
+ this.references.add(peer)
87
+ }
88
+
89
+ /**
90
+ * Forgets a connection and says whether it held the room
91
+ * @param peer the connection
92
+ */
93
+ public forget(peer: GamePeer): boolean {
94
+ return this.references.delete(peer)
95
+ }
96
+
97
+ /**
98
+ * Closes the room now or later if nobody holds it
99
+ */
100
+ public linger(): void {
101
+ if (this.references.size > 0 || this.ended) return
102
+
103
+ const wait = this.properties.emptyRoomTtl
104
+ if (wait <= 0) {
105
+ this.venue.close()
106
+ return
107
+ }
108
+
109
+ this.removal?.cancel()
110
+ this.removal = this.clock.after(wait, (): void => {
111
+ this.removal = null
112
+ if (this.references.size > 0 || this.seats.active.length > 0) return
113
+ this.venue.close()
114
+ })
115
+ }
116
+
117
+ /**
118
+ * Calls off the wait of the empty room
119
+ */
120
+ public occupied(): void {
121
+ this.removal?.cancel()
122
+ this.removal = null
123
+ }
124
+
125
+ /**
126
+ * Marks the room gone and stops every wait
127
+ */
128
+ public stop(): void {
129
+ this.ended = true
130
+ this.occupied()
131
+
132
+ for (const actor of this.seats.inactive) {
133
+ actor.expiry?.cancel()
134
+ actor.expiry = null
135
+ }
136
+ }
137
+
138
+ /**
139
+ * Starts the wait of an actor that went away
140
+ * @param actor who went away
141
+ */
142
+ public away(actor: Actor): void {
143
+ const ttl = this.properties.playerTtl
144
+ if (ttl <= 0 || ttl >= Lifecycle.FOREVER) return
145
+
146
+ actor.expiry = this.clock.after(ttl, (): void => {
147
+ this.venue.expire(actor)
148
+ })
149
+ }
150
+
151
+ /**
152
+ * Starts the wait of every empty seat of a saved room
153
+ */
154
+ public rearm(): void {
155
+ const ttl = this.properties.playerTtl
156
+ const now = this.clock.now()
157
+ if (ttl <= 0 || ttl >= Lifecycle.FOREVER) return
158
+
159
+ for (const actor of [...this.seats.inactive]) {
160
+ const left = actor.deactivatedAt ?? now
161
+ actor.deactivatedAt = left
162
+
163
+ if (left + ttl <= now) {
164
+ this.venue.expire(actor)
165
+ continue
166
+ }
167
+
168
+ actor.expiry = this.clock.after(left + ttl - now, (): void => {
169
+ this.venue.expire(actor)
170
+ })
171
+ }
172
+ }
173
+
174
+ /**
175
+ * Whether a seat's time away has run out
176
+ * @param actor the seat
177
+ */
178
+ public expired(actor: Actor): boolean {
179
+ const ttl = this.properties.playerTtl
180
+ const since = actor.deactivatedAt
181
+ if (ttl <= 0 || ttl >= Lifecycle.FOREVER || since === null) return false
182
+
183
+ return this.clock.now() - since > ttl
184
+ }
185
+
186
+ /**
187
+ * Whether an actor still waits in an open room
188
+ * @param actor who stayed away
189
+ */
190
+ public waiting(actor: Actor): boolean {
191
+ return !this.ended && this.seats.inactive.includes(actor)
192
+ }
193
+ }
@@ -142,16 +142,7 @@ export class GamePeer {
142
142
  */
143
143
  public send(packet: unknown): void {
144
144
  if (!this.channel.isActive) return
145
- this.channel.writeAndFlushSync(packet)
146
- }
147
-
148
- /**
149
- * Writes a whole frame straight to the socket
150
- * @param frame bytes as the client reads them
151
- */
152
- public pass(frame: Uint8Array): void {
153
- if (!this.channel.isActive) return
154
- this.channel.writeAndFlushEncodedSync(frame)
145
+ this.channel.writeAndFlush(packet)
155
146
  }
156
147
 
157
148
  /**