rollback-netcode 0.0.4

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 (83) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +140 -0
  3. package/dist/debug.d.ts +29 -0
  4. package/dist/debug.d.ts.map +1 -0
  5. package/dist/debug.js +56 -0
  6. package/dist/debug.js.map +1 -0
  7. package/dist/index.d.ts +62 -0
  8. package/dist/index.d.ts.map +1 -0
  9. package/dist/index.js +57 -0
  10. package/dist/index.js.map +1 -0
  11. package/dist/protocol/encoding.d.ts +80 -0
  12. package/dist/protocol/encoding.d.ts.map +1 -0
  13. package/dist/protocol/encoding.js +992 -0
  14. package/dist/protocol/encoding.js.map +1 -0
  15. package/dist/protocol/messages.d.ts +271 -0
  16. package/dist/protocol/messages.d.ts.map +1 -0
  17. package/dist/protocol/messages.js +114 -0
  18. package/dist/protocol/messages.js.map +1 -0
  19. package/dist/rollback/engine.d.ts +261 -0
  20. package/dist/rollback/engine.d.ts.map +1 -0
  21. package/dist/rollback/engine.js +543 -0
  22. package/dist/rollback/engine.js.map +1 -0
  23. package/dist/rollback/input-buffer.d.ts +225 -0
  24. package/dist/rollback/input-buffer.d.ts.map +1 -0
  25. package/dist/rollback/input-buffer.js +483 -0
  26. package/dist/rollback/input-buffer.js.map +1 -0
  27. package/dist/rollback/snapshot-buffer.d.ts +119 -0
  28. package/dist/rollback/snapshot-buffer.d.ts.map +1 -0
  29. package/dist/rollback/snapshot-buffer.js +256 -0
  30. package/dist/rollback/snapshot-buffer.js.map +1 -0
  31. package/dist/session/desync-manager.d.ts +106 -0
  32. package/dist/session/desync-manager.d.ts.map +1 -0
  33. package/dist/session/desync-manager.js +136 -0
  34. package/dist/session/desync-manager.js.map +1 -0
  35. package/dist/session/lag-monitor.d.ts +69 -0
  36. package/dist/session/lag-monitor.d.ts.map +1 -0
  37. package/dist/session/lag-monitor.js +74 -0
  38. package/dist/session/lag-monitor.js.map +1 -0
  39. package/dist/session/message-builders.d.ts +86 -0
  40. package/dist/session/message-builders.d.ts.map +1 -0
  41. package/dist/session/message-builders.js +199 -0
  42. package/dist/session/message-builders.js.map +1 -0
  43. package/dist/session/message-router.d.ts +61 -0
  44. package/dist/session/message-router.d.ts.map +1 -0
  45. package/dist/session/message-router.js +105 -0
  46. package/dist/session/message-router.js.map +1 -0
  47. package/dist/session/player-manager.d.ts +100 -0
  48. package/dist/session/player-manager.d.ts.map +1 -0
  49. package/dist/session/player-manager.js +160 -0
  50. package/dist/session/player-manager.js.map +1 -0
  51. package/dist/session/session.d.ts +379 -0
  52. package/dist/session/session.d.ts.map +1 -0
  53. package/dist/session/session.js +1294 -0
  54. package/dist/session/session.js.map +1 -0
  55. package/dist/session/topology.d.ts +66 -0
  56. package/dist/session/topology.d.ts.map +1 -0
  57. package/dist/session/topology.js +72 -0
  58. package/dist/session/topology.js.map +1 -0
  59. package/dist/transport/adapter.d.ts +99 -0
  60. package/dist/transport/adapter.d.ts.map +1 -0
  61. package/dist/transport/adapter.js +8 -0
  62. package/dist/transport/adapter.js.map +1 -0
  63. package/dist/transport/local.d.ts +192 -0
  64. package/dist/transport/local.d.ts.map +1 -0
  65. package/dist/transport/local.js +435 -0
  66. package/dist/transport/local.js.map +1 -0
  67. package/dist/transport/transforming.d.ts +177 -0
  68. package/dist/transport/transforming.d.ts.map +1 -0
  69. package/dist/transport/transforming.js +407 -0
  70. package/dist/transport/transforming.js.map +1 -0
  71. package/dist/transport/webrtc.d.ts +285 -0
  72. package/dist/transport/webrtc.d.ts.map +1 -0
  73. package/dist/transport/webrtc.js +734 -0
  74. package/dist/transport/webrtc.js.map +1 -0
  75. package/dist/types.d.ts +394 -0
  76. package/dist/types.d.ts.map +1 -0
  77. package/dist/types.js +256 -0
  78. package/dist/types.js.map +1 -0
  79. package/dist/utils/rate-limiter.d.ts +59 -0
  80. package/dist/utils/rate-limiter.d.ts.map +1 -0
  81. package/dist/utils/rate-limiter.js +93 -0
  82. package/dist/utils/rate-limiter.js.map +1 -0
  83. package/package.json +61 -0
@@ -0,0 +1,225 @@
1
+ /**
2
+ * Per-player input tracking with support for dynamic join/leave.
3
+ *
4
+ * Tracks received inputs, confirmed ticks, and used (predicted) inputs
5
+ * to enable misprediction detection during rollback.
6
+ */
7
+ import type { PlayerId, Tick } from "../types.js";
8
+ /**
9
+ * Buffer for tracking inputs from all players.
10
+ *
11
+ * Supports:
12
+ * - Dynamic player join/leave
13
+ * - Out-of-order input reception
14
+ * - Misprediction detection
15
+ * - Confirmed tick tracking
16
+ */
17
+ export declare class InputBuffer {
18
+ private readonly players;
19
+ /** Index of players by their join tick for O(1) lookup */
20
+ private readonly joinsByTick;
21
+ /** Index of players by their leave tick for O(1) lookup */
22
+ private readonly leavesByTick;
23
+ /**
24
+ * Add a player to the buffer.
25
+ *
26
+ * @param playerId - The player's ID
27
+ * @param joinTick - The tick at which the player joins
28
+ */
29
+ addPlayer(playerId: PlayerId, joinTick: Tick): void;
30
+ /**
31
+ * Mark a player as having left.
32
+ *
33
+ * @param playerId - The player's ID
34
+ * @param leaveTick - The tick at which the player leaves
35
+ */
36
+ removePlayer(playerId: PlayerId, leaveTick: Tick): void;
37
+ /**
38
+ * Set the confirmed tick for all active players to a specific value.
39
+ * Used after a state sync to indicate that all inputs up to that tick
40
+ * are implicitly confirmed by the synced state.
41
+ *
42
+ * @param tick - The tick to set as confirmed for all active players
43
+ */
44
+ setConfirmedTickForSync(tick: Tick): void;
45
+ /**
46
+ * Check if a player is active at a given tick.
47
+ *
48
+ * @param playerId - The player's ID
49
+ * @param tick - The tick to check
50
+ * @returns true if the player is active at that tick
51
+ */
52
+ isPlayerActive(playerId: PlayerId, tick: Tick): boolean;
53
+ /**
54
+ * Get all players that are active at a given tick.
55
+ *
56
+ * @param tick - The tick to check
57
+ * @returns Array of active player IDs
58
+ */
59
+ getActivePlayers(tick: Tick): PlayerId[];
60
+ /**
61
+ * Get all player IDs (active or not).
62
+ *
63
+ * @returns Array of all player IDs
64
+ */
65
+ getAllPlayers(): PlayerId[];
66
+ /**
67
+ * Receive an input from a player.
68
+ * The input is copied to prevent external mutation.
69
+ *
70
+ * @param playerId - The player's ID
71
+ * @param tick - The tick the input is for
72
+ * @param input - The input data
73
+ */
74
+ receiveInput(playerId: PlayerId, tick: Tick, input: Uint8Array): void;
75
+ /**
76
+ * Update the confirmed tick for a player.
77
+ * Confirmed tick is the highest tick where all ticks from joinTick
78
+ * to that tick have received inputs.
79
+ */
80
+ private updateConfirmedTick;
81
+ /**
82
+ * Get the received input for a player at a specific tick.
83
+ *
84
+ * @param playerId - The player's ID
85
+ * @param tick - The tick to get input for
86
+ * @returns The input, or undefined if not received
87
+ */
88
+ getInput(playerId: PlayerId, tick: Tick): Uint8Array | undefined;
89
+ /**
90
+ * Get the confirmed tick for a player.
91
+ * This is the highest consecutive tick for which we have received input.
92
+ *
93
+ * @param playerId - The player's ID
94
+ * @returns The confirmed tick, or undefined if player not found
95
+ */
96
+ getConfirmedTick(playerId: PlayerId): Tick | undefined;
97
+ /**
98
+ * Get the join tick for a player.
99
+ *
100
+ * @param playerId - The player's ID
101
+ * @returns The join tick, or undefined if player not found
102
+ */
103
+ getJoinTick(playerId: PlayerId): Tick | undefined;
104
+ /**
105
+ * Get the leave tick for a player.
106
+ *
107
+ * @param playerId - The player's ID
108
+ * @returns The leave tick, or null/undefined if player hasn't left or not found
109
+ */
110
+ getLeaveTick(playerId: PlayerId): Tick | null | undefined;
111
+ /**
112
+ * Record the input that was actually used for a player at a tick.
113
+ * This may be the real input or a predicted input.
114
+ *
115
+ * @param playerId - The player's ID
116
+ * @param tick - The tick
117
+ * @param input - The input that was used
118
+ */
119
+ recordUsedInput(playerId: PlayerId, tick: Tick, input: Uint8Array): void;
120
+ /**
121
+ * Get the input that was used for a player at a tick.
122
+ *
123
+ * @param playerId - The player's ID
124
+ * @param tick - The tick
125
+ * @returns The used input, or undefined if not recorded
126
+ */
127
+ getUsedInput(playerId: PlayerId, tick: Tick): Uint8Array | undefined;
128
+ /**
129
+ * Find the first tick where a misprediction occurred for a player.
130
+ * A misprediction is when we used a predicted input that differs
131
+ * from the actual received input.
132
+ *
133
+ * @param playerId - The player's ID
134
+ * @param fromTick - Start searching from this tick
135
+ * @returns The tick of the first misprediction, or undefined if none found
136
+ */
137
+ findMisprediction(playerId: PlayerId, fromTick: Tick): Tick | undefined;
138
+ /**
139
+ * Check if there are any mispredictions for a player in a tick range.
140
+ *
141
+ * @param playerId - The player's ID
142
+ * @param fromTick - Start of range (inclusive)
143
+ * @param toTick - End of range (inclusive)
144
+ * @returns true if any misprediction found
145
+ */
146
+ hasMisprediction(playerId: PlayerId, fromTick: Tick, toTick: Tick): boolean;
147
+ /**
148
+ * Get the last confirmed input for a player.
149
+ * Useful for input prediction.
150
+ *
151
+ * @param playerId - The player's ID
152
+ * @returns The last confirmed input, or undefined if none
153
+ */
154
+ getLastConfirmedInput(playerId: PlayerId): Uint8Array | undefined;
155
+ /**
156
+ * Remove all data before a given tick.
157
+ * Used to clean up old inputs that are no longer needed.
158
+ *
159
+ * @param tick - Remove all data for ticks < this value
160
+ */
161
+ pruneBeforeTick(tick: Tick): void;
162
+ /**
163
+ * Clear used inputs for a player from a given tick onwards.
164
+ * Called during rollback to allow re-recording inputs.
165
+ *
166
+ * @param playerId - The player's ID
167
+ * @param fromTick - Clear from this tick onwards
168
+ */
169
+ clearUsedInputsFrom(playerId: PlayerId, fromTick: Tick): void;
170
+ /**
171
+ * Clear all used inputs from a given tick onwards for all players.
172
+ *
173
+ * @param fromTick - Clear from this tick onwards
174
+ */
175
+ clearAllUsedInputsFrom(fromTick: Tick): void;
176
+ /**
177
+ * Get the minimum confirmed tick across all active players at a given tick.
178
+ *
179
+ * @param tick - The reference tick for determining active players
180
+ * @returns The minimum confirmed tick, or undefined if no active players
181
+ */
182
+ getMinConfirmedTick(tick: Tick): Tick | undefined;
183
+ /**
184
+ * Check if we have all inputs for a given tick from all active players.
185
+ *
186
+ * @param tick - The tick to check
187
+ * @returns true if all inputs are available
188
+ */
189
+ hasAllInputsForTick(tick: Tick): boolean;
190
+ /**
191
+ * Clear all data for a player.
192
+ *
193
+ * @param playerId - The player's ID
194
+ */
195
+ clearPlayer(playerId: PlayerId): void;
196
+ /**
197
+ * Clear all players and data.
198
+ */
199
+ clear(): void;
200
+ /**
201
+ * Get all players that join at a specific tick.
202
+ * O(1) lookup using tick-indexed map.
203
+ *
204
+ * @param tick - The tick to check
205
+ * @returns Array of player IDs joining at this tick
206
+ */
207
+ getPlayersJoiningAtTick(tick: Tick): PlayerId[];
208
+ /**
209
+ * Get all players that leave at a specific tick.
210
+ * O(1) lookup using tick-indexed map.
211
+ *
212
+ * @param tick - The tick to check
213
+ * @returns Array of player IDs leaving at this tick
214
+ */
215
+ getPlayersLeavingAtTick(tick: Tick): PlayerId[];
216
+ /**
217
+ * Add a player to a tick index.
218
+ */
219
+ private addToTickIndex;
220
+ /**
221
+ * Remove a player from a tick index.
222
+ */
223
+ private removeFromTickIndex;
224
+ }
225
+ //# sourceMappingURL=input-buffer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"input-buffer.d.ts","sourceRoot":"","sources":["../../src/rollback/input-buffer.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAkClD;;;;;;;;GAQG;AACH,qBAAa,WAAW;IACvB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA8C;IAEtE,0DAA0D;IAC1D,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAuC;IAEnE,2DAA2D;IAC3D,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAuC;IAEpE;;;;;OAKG;IACH,SAAS,CAAC,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,IAAI,GAAG,IAAI;IAsCnD;;;;;OAKG;IACH,YAAY,CAAC,QAAQ,EAAE,QAAQ,EAAE,SAAS,EAAE,IAAI,GAAG,IAAI;IAevD;;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