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.
- package/LICENSE +21 -0
- package/README.md +140 -0
- package/dist/debug.d.ts +29 -0
- package/dist/debug.d.ts.map +1 -0
- package/dist/debug.js +56 -0
- package/dist/debug.js.map +1 -0
- package/dist/index.d.ts +62 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +57 -0
- package/dist/index.js.map +1 -0
- package/dist/protocol/encoding.d.ts +80 -0
- package/dist/protocol/encoding.d.ts.map +1 -0
- package/dist/protocol/encoding.js +992 -0
- package/dist/protocol/encoding.js.map +1 -0
- package/dist/protocol/messages.d.ts +271 -0
- package/dist/protocol/messages.d.ts.map +1 -0
- package/dist/protocol/messages.js +114 -0
- package/dist/protocol/messages.js.map +1 -0
- package/dist/rollback/engine.d.ts +261 -0
- package/dist/rollback/engine.d.ts.map +1 -0
- package/dist/rollback/engine.js +543 -0
- package/dist/rollback/engine.js.map +1 -0
- package/dist/rollback/input-buffer.d.ts +225 -0
- package/dist/rollback/input-buffer.d.ts.map +1 -0
- package/dist/rollback/input-buffer.js +483 -0
- package/dist/rollback/input-buffer.js.map +1 -0
- package/dist/rollback/snapshot-buffer.d.ts +119 -0
- package/dist/rollback/snapshot-buffer.d.ts.map +1 -0
- package/dist/rollback/snapshot-buffer.js +256 -0
- package/dist/rollback/snapshot-buffer.js.map +1 -0
- package/dist/session/desync-manager.d.ts +106 -0
- package/dist/session/desync-manager.d.ts.map +1 -0
- package/dist/session/desync-manager.js +136 -0
- package/dist/session/desync-manager.js.map +1 -0
- package/dist/session/lag-monitor.d.ts +69 -0
- package/dist/session/lag-monitor.d.ts.map +1 -0
- package/dist/session/lag-monitor.js +74 -0
- package/dist/session/lag-monitor.js.map +1 -0
- package/dist/session/message-builders.d.ts +86 -0
- package/dist/session/message-builders.d.ts.map +1 -0
- package/dist/session/message-builders.js +199 -0
- package/dist/session/message-builders.js.map +1 -0
- package/dist/session/message-router.d.ts +61 -0
- package/dist/session/message-router.d.ts.map +1 -0
- package/dist/session/message-router.js +105 -0
- package/dist/session/message-router.js.map +1 -0
- package/dist/session/player-manager.d.ts +100 -0
- package/dist/session/player-manager.d.ts.map +1 -0
- package/dist/session/player-manager.js +160 -0
- package/dist/session/player-manager.js.map +1 -0
- package/dist/session/session.d.ts +379 -0
- package/dist/session/session.d.ts.map +1 -0
- package/dist/session/session.js +1294 -0
- package/dist/session/session.js.map +1 -0
- package/dist/session/topology.d.ts +66 -0
- package/dist/session/topology.d.ts.map +1 -0
- package/dist/session/topology.js +72 -0
- package/dist/session/topology.js.map +1 -0
- package/dist/transport/adapter.d.ts +99 -0
- package/dist/transport/adapter.d.ts.map +1 -0
- package/dist/transport/adapter.js +8 -0
- package/dist/transport/adapter.js.map +1 -0
- package/dist/transport/local.d.ts +192 -0
- package/dist/transport/local.d.ts.map +1 -0
- package/dist/transport/local.js +435 -0
- package/dist/transport/local.js.map +1 -0
- package/dist/transport/transforming.d.ts +177 -0
- package/dist/transport/transforming.d.ts.map +1 -0
- package/dist/transport/transforming.js +407 -0
- package/dist/transport/transforming.js.map +1 -0
- package/dist/transport/webrtc.d.ts +285 -0
- package/dist/transport/webrtc.d.ts.map +1 -0
- package/dist/transport/webrtc.js +734 -0
- package/dist/transport/webrtc.js.map +1 -0
- package/dist/types.d.ts +394 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +256 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/rate-limiter.d.ts +59 -0
- package/dist/utils/rate-limiter.d.ts.map +1 -0
- package/dist/utils/rate-limiter.js +93 -0
- package/dist/utils/rate-limiter.js.map +1 -0
- package/package.json +61 -0
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Factory functions for creating protocol messages.
|
|
3
|
+
*
|
|
4
|
+
* Centralizes message construction to reduce duplication in Session.ts.
|
|
5
|
+
*/
|
|
6
|
+
import type { Message } from "../protocol/messages.js";
|
|
7
|
+
import type { PauseReason, PlayerId, PlayerRole, PlayerTimeline, Tick } from "../types.js";
|
|
8
|
+
/**
|
|
9
|
+
* Create a JoinRequest message.
|
|
10
|
+
*/
|
|
11
|
+
export declare function createJoinRequest(playerId: PlayerId, role: PlayerRole): Message;
|
|
12
|
+
/**
|
|
13
|
+
* Create a PlayerLeft message.
|
|
14
|
+
*/
|
|
15
|
+
export declare function createPlayerLeft(playerId: PlayerId, leaveTick: Tick): Message;
|
|
16
|
+
/**
|
|
17
|
+
* Create a Sync message for state synchronization.
|
|
18
|
+
*/
|
|
19
|
+
export declare function createSync(tick: Tick, state: Uint8Array, hash: number, playerTimeline: PlayerTimeline): Message;
|
|
20
|
+
/**
|
|
21
|
+
* Create a Pause message.
|
|
22
|
+
*/
|
|
23
|
+
export declare function createPause(playerId: PlayerId, pauseTick: Tick, reason: PauseReason): Message;
|
|
24
|
+
/**
|
|
25
|
+
* Create a Resume message.
|
|
26
|
+
*/
|
|
27
|
+
export declare function createResume(playerId: PlayerId, resumeTick: Tick): Message;
|
|
28
|
+
/**
|
|
29
|
+
* Create a ResumeCountdown message.
|
|
30
|
+
*/
|
|
31
|
+
export declare function createResumeCountdown(secondsRemaining: number): Message;
|
|
32
|
+
/**
|
|
33
|
+
* Create a SyncRequest message.
|
|
34
|
+
*/
|
|
35
|
+
export declare function createSyncRequest(playerId: PlayerId, desyncTick: Tick, localHash: number): Message;
|
|
36
|
+
/**
|
|
37
|
+
* Create a DisconnectReport message.
|
|
38
|
+
*/
|
|
39
|
+
export declare function createDisconnectReport(disconnectedPeerId: PlayerId): Message;
|
|
40
|
+
/**
|
|
41
|
+
* Create a LagReport message.
|
|
42
|
+
*/
|
|
43
|
+
export declare function createLagReport(laggyPlayerId: PlayerId, ticksBehind: number): Message;
|
|
44
|
+
/**
|
|
45
|
+
* Create a JoinReject message.
|
|
46
|
+
*/
|
|
47
|
+
export declare function createJoinReject(playerId: PlayerId, reason: string): Message;
|
|
48
|
+
/**
|
|
49
|
+
* Create a JoinAccept message.
|
|
50
|
+
*/
|
|
51
|
+
export declare function createJoinAccept(playerId: PlayerId, roomId: string, config: {
|
|
52
|
+
tickRate: number;
|
|
53
|
+
maxPlayers: number;
|
|
54
|
+
}, players: PlayerId[]): Message;
|
|
55
|
+
/**
|
|
56
|
+
* Create a PlayerJoined message.
|
|
57
|
+
*/
|
|
58
|
+
export declare function createPlayerJoined(playerId: PlayerId, role: PlayerRole, joinTick: Tick): Message;
|
|
59
|
+
/**
|
|
60
|
+
* Create a Ping message.
|
|
61
|
+
*/
|
|
62
|
+
export declare function createPing(timestamp: number): Message;
|
|
63
|
+
/**
|
|
64
|
+
* Create a Pong message.
|
|
65
|
+
*/
|
|
66
|
+
export declare function createPong(timestamp: number): Message;
|
|
67
|
+
/**
|
|
68
|
+
* Create an Input message.
|
|
69
|
+
*/
|
|
70
|
+
export declare function createInput(playerId: PlayerId, inputs: Array<{
|
|
71
|
+
tick: Tick;
|
|
72
|
+
input: Uint8Array;
|
|
73
|
+
}>): Message;
|
|
74
|
+
/**
|
|
75
|
+
* Create a Hash message.
|
|
76
|
+
*/
|
|
77
|
+
export declare function createHash(playerId: PlayerId, tick: Tick, hash: number): Message;
|
|
78
|
+
/**
|
|
79
|
+
* Create a StateSync message for late-joining players.
|
|
80
|
+
*/
|
|
81
|
+
export declare function createStateSync(tick: Tick, state: Uint8Array, hash: number, playerTimeline: PlayerTimeline): Message;
|
|
82
|
+
/**
|
|
83
|
+
* Create a DropPlayer message.
|
|
84
|
+
*/
|
|
85
|
+
export declare function createDropPlayer(playerId: PlayerId, metadata?: Uint8Array): Message;
|
|
86
|
+
//# sourceMappingURL=message-builders.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"message-builders.d.ts","sourceRoot":"","sources":["../../src/session/message-builders.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAEvD,OAAO,K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M,EACd,MAAM,EAAE;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAAE,EAChD,OAAO,EAAE,QAAQ,EAAE,GACjB,OAAO,CAQT;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CACjC,QAAQ,EAAE,QAAQ,EAClB,IAAI,EAAE,UAAU,EAChB,QAAQ,EAAE,IAAI,GACZ,OAAO,CAOT;AAED;;GAEG;AACH,wBAAgB,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAKrD;AAED;;GAEG;AACH,wBAAgB,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAKrD;AAED;;GAEG;AACH,wBAAgB,WAAW,CAC1B,QAAQ,EAAE,QAAQ,EAClB,MAAM,EAAE,KAAK,CAAC;IAAE,IAAI,EAAE,IAAI,CAAC;IAAC,KAAK,EAAE,UAAU,CAAA;CAAE,CAAC,GAC9C,OAAO,CAMT;AAED;;GAEG;AACH,wBAAgB,UAAU,CACzB,QAAQ,EAAE,QAAQ,EAClB,IAAI,EAAE,IAAI,EACV,IAAI,EAAE,MAAM,GACV,OAAO,CAOT;AAED;;GAEG;AACH,wBAAgB,eAAe,CAC9B,IAAI,EAAE,IAAI,EACV,KAAK,EAAE,UAAU,EACjB,IAAI,EAAE,MAAM,EACZ,cAAc,EAAE,cAAc,GAC5B,OAAO,CAQT;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAC/B,QAAQ,EAAE,QAAQ,EAClB,QAAQ,CAAC,EAAE,UAAU,GACnB,OAAO,CAYT"}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Factory functions for creating protocol messages.
|
|
3
|
+
*
|
|
4
|
+
* Centralizes message construction to reduce duplication in Session.ts.
|
|
5
|
+
*/
|
|
6
|
+
import { MessageType } from "../protocol/messages.js";
|
|
7
|
+
/**
|
|
8
|
+
* Create a JoinRequest message.
|
|
9
|
+
*/
|
|
10
|
+
export function createJoinRequest(playerId, role) {
|
|
11
|
+
return {
|
|
12
|
+
type: MessageType.JoinRequest,
|
|
13
|
+
playerId,
|
|
14
|
+
role,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Create a PlayerLeft message.
|
|
19
|
+
*/
|
|
20
|
+
export function createPlayerLeft(playerId, leaveTick) {
|
|
21
|
+
return {
|
|
22
|
+
type: MessageType.PlayerLeft,
|
|
23
|
+
playerId,
|
|
24
|
+
leaveTick,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Create a Sync message for state synchronization.
|
|
29
|
+
*/
|
|
30
|
+
export function createSync(tick, state, hash, playerTimeline) {
|
|
31
|
+
return {
|
|
32
|
+
type: MessageType.Sync,
|
|
33
|
+
tick,
|
|
34
|
+
state,
|
|
35
|
+
hash,
|
|
36
|
+
playerTimeline,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Create a Pause message.
|
|
41
|
+
*/
|
|
42
|
+
export function createPause(playerId, pauseTick, reason) {
|
|
43
|
+
return {
|
|
44
|
+
type: MessageType.Pause,
|
|
45
|
+
playerId,
|
|
46
|
+
pauseTick,
|
|
47
|
+
reason,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Create a Resume message.
|
|
52
|
+
*/
|
|
53
|
+
export function createResume(playerId, resumeTick) {
|
|
54
|
+
return {
|
|
55
|
+
type: MessageType.Resume,
|
|
56
|
+
playerId,
|
|
57
|
+
resumeTick,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Create a ResumeCountdown message.
|
|
62
|
+
*/
|
|
63
|
+
export function createResumeCountdown(secondsRemaining) {
|
|
64
|
+
return {
|
|
65
|
+
type: MessageType.ResumeCountdown,
|
|
66
|
+
secondsRemaining,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Create a SyncRequest message.
|
|
71
|
+
*/
|
|
72
|
+
export function createSyncRequest(playerId, desyncTick, localHash) {
|
|
73
|
+
return {
|
|
74
|
+
type: MessageType.SyncRequest,
|
|
75
|
+
playerId,
|
|
76
|
+
desyncTick,
|
|
77
|
+
localHash,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Create a DisconnectReport message.
|
|
82
|
+
*/
|
|
83
|
+
export function createDisconnectReport(disconnectedPeerId) {
|
|
84
|
+
return {
|
|
85
|
+
type: MessageType.DisconnectReport,
|
|
86
|
+
disconnectedPeerId,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Create a LagReport message.
|
|
91
|
+
*/
|
|
92
|
+
export function createLagReport(laggyPlayerId, ticksBehind) {
|
|
93
|
+
return {
|
|
94
|
+
type: MessageType.LagReport,
|
|
95
|
+
laggyPlayerId,
|
|
96
|
+
ticksBehind,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Create a JoinReject message.
|
|
101
|
+
*/
|
|
102
|
+
export function createJoinReject(playerId, reason) {
|
|
103
|
+
return {
|
|
104
|
+
type: MessageType.JoinReject,
|
|
105
|
+
playerId,
|
|
106
|
+
reason,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Create a JoinAccept message.
|
|
111
|
+
*/
|
|
112
|
+
export function createJoinAccept(playerId, roomId, config, players) {
|
|
113
|
+
return {
|
|
114
|
+
type: MessageType.JoinAccept,
|
|
115
|
+
playerId,
|
|
116
|
+
roomId,
|
|
117
|
+
config,
|
|
118
|
+
players,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Create a PlayerJoined message.
|
|
123
|
+
*/
|
|
124
|
+
export function createPlayerJoined(playerId, role, joinTick) {
|
|
125
|
+
return {
|
|
126
|
+
type: MessageType.PlayerJoined,
|
|
127
|
+
playerId,
|
|
128
|
+
role,
|
|
129
|
+
joinTick,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Create a Ping message.
|
|
134
|
+
*/
|
|
135
|
+
export function createPing(timestamp) {
|
|
136
|
+
return {
|
|
137
|
+
type: MessageType.Ping,
|
|
138
|
+
timestamp,
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Create a Pong message.
|
|
143
|
+
*/
|
|
144
|
+
export function createPong(timestamp) {
|
|
145
|
+
return {
|
|
146
|
+
type: MessageType.Pong,
|
|
147
|
+
timestamp,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Create an Input message.
|
|
152
|
+
*/
|
|
153
|
+
export function createInput(playerId, inputs) {
|
|
154
|
+
return {
|
|
155
|
+
type: MessageType.Input,
|
|
156
|
+
playerId,
|
|
157
|
+
inputs,
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Create a Hash message.
|
|
162
|
+
*/
|
|
163
|
+
export function createHash(playerId, tick, hash) {
|
|
164
|
+
return {
|
|
165
|
+
type: MessageType.Hash,
|
|
166
|
+
playerId,
|
|
167
|
+
tick,
|
|
168
|
+
hash,
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Create a StateSync message for late-joining players.
|
|
173
|
+
*/
|
|
174
|
+
export function createStateSync(tick, state, hash, playerTimeline) {
|
|
175
|
+
return {
|
|
176
|
+
type: MessageType.StateSync,
|
|
177
|
+
tick,
|
|
178
|
+
state,
|
|
179
|
+
hash,
|
|
180
|
+
playerTimeline,
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Create a DropPlayer message.
|
|
185
|
+
*/
|
|
186
|
+
export function createDropPlayer(playerId, metadata) {
|
|
187
|
+
if (metadata !== undefined) {
|
|
188
|
+
return {
|
|
189
|
+
type: MessageType.DropPlayer,
|
|
190
|
+
playerId,
|
|
191
|
+
metadata,
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
return {
|
|
195
|
+
type: MessageType.DropPlayer,
|
|
196
|
+
playerId,
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
//# sourceMappingURL=message-builders.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"message-builders.js","sourceRoot":"","sources":["../../src/session/message-builders.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAStD;;GAEG;AACH,MAAM,UAAU,iBAAiB,CAChC,QAAkB,EAClB,IAAgB;IAEhB,OAAO;QACN,IAAI,EAAE,WAAW,CAAC,WAAW;QAC7B,QAAQ;QACR,IAAI;KACJ,CAAC;AACH,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,gBAAgB,CAAC,QAAkB,EAAE,SAAe;IACnE,OAAO;QACN,IAAI,EAAE,WAAW,CAAC,UAAU;QAC5B,QAAQ;QACR,SAAS;KACT,CAAC;AACH,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,UAAU,CACzB,IAAU,EACV,KAAiB,EACjB,IAAY,EACZ,cAA8B;IAE9B,OAAO;QACN,IAAI,EAAE,WAAW,CAAC,IAAI;QACtB,IAAI;QACJ,KAAK;QACL,IAAI;QACJ,cAAc;KACd,CAAC;AACH,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,WAAW,CAC1B,QAAkB,EAClB,SAAe,EACf,MAAmB;IAEnB,OAAO;QACN,IAAI,EAAE,WAAW,CAAC,KAAK;QACvB,QAAQ;QACR,SAAS;QACT,MAAM;KACN,CAAC;AACH,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,YAAY,CAAC,QAAkB,EAAE,UAAgB;IAChE,OAAO;QACN,IAAI,EAAE,WAAW,CAAC,MAAM;QACxB,QAAQ;QACR,UAAU;KACV,CAAC;AACH,CAAC;AAED;;GAEG;AACH,MAAM,UA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ACtB,SAAS;KACT,CAAC;AACH,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,UAAU,CAAC,SAAiB;IAC3C,OAAO;QACN,IAAI,EAAE,WAAW,CAAC,IAAI;QACtB,SAAS;KACT,CAAC;AACH,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,WAAW,CAC1B,QAAkB,EAClB,MAAgD;IAEhD,OAAO;QACN,IAAI,EAAE,WAAW,CAAC,KAAK;QACvB,QAAQ;QACR,MAAM;KACN,CAAC;AACH,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,UAAU,CACzB,QAAkB,EAClB,IAAU,EACV,IAAY;IAEZ,OAAO;QACN,IAAI,EAAE,WAAW,CAAC,IAAI;QACtB,QAAQ;QACR,IAAI;QACJ,IAAI;KACJ,CAAC;AACH,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,eAAe,CAC9B,IAAU,EACV,KAAiB,EACjB,IAAY,EACZ,cAA8B;IAE9B,OAAO;QACN,IAAI,EAAE,WAAW,CAAC,SAAS;QAC3B,IAAI;QACJ,KAAK;QACL,IAAI;QACJ,cAAc;KACd,CAAC;AACH,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,gBAAgB,CAC/B,QAAkB,EAClB,QAAqB;IAErB,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;QAC5B,OAAO;YACN,IAAI,EAAE,WAAW,CAAC,UAAU;YAC5B,QAAQ;YACR,QAAQ;SACR,CAAC;IACH,CAAC;IACD,OAAO;QACN,IAAI,EAAE,WAAW,CAAC,UAAU;QAC5B,QAAQ;KACR,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Message routing for session communication.
|
|
3
|
+
*
|
|
4
|
+
* Decodes incoming messages and routes them to the appropriate handlers.
|
|
5
|
+
*/
|
|
6
|
+
import { type DisconnectReportMessage, type DropPlayerMessage, type HashMessage, type InputMessage, type JoinAcceptMessage, type JoinRejectMessage, type JoinRequestMessage, type LagReportMessage, type Message, type PauseMessage, type PingMessage, type PlayerJoinedMessage, type PlayerLeftMessage, type PongMessage, type ResumeCountdownMessage, type ResumeMessage, type StateSyncMessage, type SyncMessage, type SyncRequestMessage } from "../protocol/messages.js";
|
|
7
|
+
/**
|
|
8
|
+
* Handler callbacks for each message type.
|
|
9
|
+
*
|
|
10
|
+
* Each handler receives the decoded message and the peer ID of the sender.
|
|
11
|
+
*/
|
|
12
|
+
export interface MessageHandlers {
|
|
13
|
+
onInput?: (msg: InputMessage, peerId: string) => void;
|
|
14
|
+
onHash?: (msg: HashMessage, peerId: string) => void;
|
|
15
|
+
onSync?: (msg: SyncMessage | StateSyncMessage, peerId: string) => void;
|
|
16
|
+
onSyncRequest?: (msg: SyncRequestMessage, peerId: string) => void;
|
|
17
|
+
onJoinRequest?: (msg: JoinRequestMessage, peerId: string) => void;
|
|
18
|
+
onJoinAccept?: (msg: JoinAcceptMessage, peerId: string) => void;
|
|
19
|
+
onJoinReject?: (msg: JoinRejectMessage, peerId: string) => void;
|
|
20
|
+
onPlayerJoined?: (msg: PlayerJoinedMessage, peerId: string) => void;
|
|
21
|
+
onPlayerLeft?: (msg: PlayerLeftMessage, peerId: string) => void;
|
|
22
|
+
onPause?: (msg: PauseMessage, peerId: string) => void;
|
|
23
|
+
onResume?: (msg: ResumeMessage, peerId: string) => void;
|
|
24
|
+
onPing?: (msg: PingMessage, peerId: string) => void;
|
|
25
|
+
onPong?: (msg: PongMessage, peerId: string) => void;
|
|
26
|
+
onDisconnectReport?: (msg: DisconnectReportMessage, peerId: string) => void;
|
|
27
|
+
onLagReport?: (msg: LagReportMessage, peerId: string) => void;
|
|
28
|
+
onResumeCountdown?: (msg: ResumeCountdownMessage, peerId: string) => void;
|
|
29
|
+
onDropPlayer?: (msg: DropPlayerMessage, peerId: string) => void;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Error handler callback.
|
|
33
|
+
*/
|
|
34
|
+
export type DecodeErrorHandler = (error: Error, peerId: string, dataLength: number) => void;
|
|
35
|
+
/**
|
|
36
|
+
* Routes decoded messages to their appropriate handlers.
|
|
37
|
+
*
|
|
38
|
+
* Centralizes message dispatch logic and error handling for decode failures.
|
|
39
|
+
*/
|
|
40
|
+
export declare class MessageRouter {
|
|
41
|
+
private readonly handlers;
|
|
42
|
+
private readonly onDecodeError;
|
|
43
|
+
constructor(handlers: MessageHandlers, onDecodeError: DecodeErrorHandler);
|
|
44
|
+
/**
|
|
45
|
+
* Route a raw message from a peer.
|
|
46
|
+
*
|
|
47
|
+
* Decodes the message and dispatches to the appropriate handler.
|
|
48
|
+
*
|
|
49
|
+
* @param peerId - The peer ID of the sender
|
|
50
|
+
* @param data - The raw message bytes
|
|
51
|
+
*/
|
|
52
|
+
route(peerId: string, data: Uint8Array): void;
|
|
53
|
+
/**
|
|
54
|
+
* Dispatch a decoded message to the appropriate handler.
|
|
55
|
+
*
|
|
56
|
+
* @param message - The decoded message
|
|
57
|
+
* @param peerId - The peer ID of the sender
|
|
58
|
+
*/
|
|
59
|
+
dispatch(message: Message, peerId: string): void;
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=message-router.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"message-router.d.ts","sourceRoot":"","sources":["../../src/session/message-router.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EACN,KAAK,uBAAuB,EAC5B,KAAK,iBAAiB,EACtB,KAAK,WAAW,EAChB,KAAK,YAAY,EACjB,KAAK,iBAAiB,EACtB,KAAK,iBAAiB,EACtB,KAAK,kBAAkB,EACvB,KAAK,gBAAgB,EACrB,KAAK,OAAO,EAEZ,KAAK,YAAY,EACjB,KAAK,WAAW,EAChB,KAAK,mBAAmB,EACxB,KAAK,iBAAiB,EACtB,KAAK,WAAW,EAChB,KAAK,sBAAsB,EAC3B,KAAK,aAAa,EAClB,KAAK,gBAAgB,EACrB,KAAK,WAAW,EAChB,KAAK,kBAAkB,EACvB,MAA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AAE,MAAM,KAAK,IAAI,CAAC;IACpD,kBAAkB,CAAC,EAAE,CAAC,GAAG,EAAE,uBAAuB,EAAE,MAAM,EAAE,MAAM,KAAK,IAAI,CAAC;IAC5E,WAAW,CAAC,EAAE,CAAC,GAAG,EAAE,gBAAgB,EAAE,MAAM,EAAE,MAAM,KAAK,IAAI,CAAC;IAC9D,iBAAiB,CAAC,EAAE,CAAC,GAAG,EAAE,sBAAsB,EAAE,MAAM,EAAE,MAAM,KAAK,IAAI,CAAC;IAC1E,YAAY,CAAC,EAAE,CAAC,GAAG,EAAE,iBAAiB,EAAE,MAAM,EAAE,MAAM,KAAK,IAAI,CAAC;CAChE;AAED;;GAEG;AACH,MAAM,MAAM,kBAAkB,GAAG,CAChC,KAAK,EAAE,KAAK,EACZ,MAAM,EAAE,MAAM,EACd,UAAU,EAAE,MAAM,KACd,IAAI,CAAC;AAEV;;;;GAIG;AACH,qBAAa,aAAa;IACzB,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAkB;IAC3C,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAqB;gBAEvC,QAAQ,EAAE,eAAe,EAAE,aAAa,EAAE,kBAAkB;IAKxE;;;;;;;OAOG;IACH,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU,GAAG,IAAI;IAgB7C;;;;;OAKG;IACH,QAAQ,CAAC,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI;CA4EhD"}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Message routing for session communication.
|
|
3
|
+
*
|
|
4
|
+
* Decodes incoming messages and routes them to the appropriate handlers.
|
|
5
|
+
*/
|
|
6
|
+
import { decodeMessage } from "../protocol/encoding.js";
|
|
7
|
+
import { MessageType, } from "../protocol/messages.js";
|
|
8
|
+
/**
|
|
9
|
+
* Routes decoded messages to their appropriate handlers.
|
|
10
|
+
*
|
|
11
|
+
* Centralizes message dispatch logic and error handling for decode failures.
|
|
12
|
+
*/
|
|
13
|
+
export class MessageRouter {
|
|
14
|
+
handlers;
|
|
15
|
+
onDecodeError;
|
|
16
|
+
constructor(handlers, onDecodeError) {
|
|
17
|
+
this.handlers = handlers;
|
|
18
|
+
this.onDecodeError = onDecodeError;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Route a raw message from a peer.
|
|
22
|
+
*
|
|
23
|
+
* Decodes the message and dispatches to the appropriate handler.
|
|
24
|
+
*
|
|
25
|
+
* @param peerId - The peer ID of the sender
|
|
26
|
+
* @param data - The raw message bytes
|
|
27
|
+
*/
|
|
28
|
+
route(peerId, data) {
|
|
29
|
+
let message;
|
|
30
|
+
try {
|
|
31
|
+
message = decodeMessage(data);
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
this.onDecodeError(error instanceof Error ? error : new Error(String(error)), peerId, data.length);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
this.dispatch(message, peerId);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Dispatch a decoded message to the appropriate handler.
|
|
41
|
+
*
|
|
42
|
+
* @param message - The decoded message
|
|
43
|
+
* @param peerId - The peer ID of the sender
|
|
44
|
+
*/
|
|
45
|
+
dispatch(message, peerId) {
|
|
46
|
+
switch (message.type) {
|
|
47
|
+
case MessageType.Input:
|
|
48
|
+
this.handlers.onInput?.(message, peerId);
|
|
49
|
+
break;
|
|
50
|
+
case MessageType.Hash:
|
|
51
|
+
this.handlers.onHash?.(message, peerId);
|
|
52
|
+
break;
|
|
53
|
+
case MessageType.Sync:
|
|
54
|
+
case MessageType.StateSync:
|
|
55
|
+
this.handlers.onSync?.(message, peerId);
|
|
56
|
+
break;
|
|
57
|
+
case MessageType.SyncRequest:
|
|
58
|
+
this.handlers.onSyncRequest?.(message, peerId);
|
|
59
|
+
break;
|
|
60
|
+
case MessageType.JoinRequest:
|
|
61
|
+
this.handlers.onJoinRequest?.(message, peerId);
|
|
62
|
+
break;
|
|
63
|
+
case MessageType.JoinAccept:
|
|
64
|
+
this.handlers.onJoinAccept?.(message, peerId);
|
|
65
|
+
break;
|
|
66
|
+
case MessageType.JoinReject:
|
|
67
|
+
this.handlers.onJoinReject?.(message, peerId);
|
|
68
|
+
break;
|
|
69
|
+
case MessageType.PlayerJoined:
|
|
70
|
+
this.handlers.onPlayerJoined?.(message, peerId);
|
|
71
|
+
break;
|
|
72
|
+
case MessageType.PlayerLeft:
|
|
73
|
+
this.handlers.onPlayerLeft?.(message, peerId);
|
|
74
|
+
break;
|
|
75
|
+
case MessageType.Pause:
|
|
76
|
+
this.handlers.onPause?.(message, peerId);
|
|
77
|
+
break;
|
|
78
|
+
case MessageType.Resume:
|
|
79
|
+
this.handlers.onResume?.(message, peerId);
|
|
80
|
+
break;
|
|
81
|
+
case MessageType.Ping:
|
|
82
|
+
this.handlers.onPing?.(message, peerId);
|
|
83
|
+
break;
|
|
84
|
+
case MessageType.Pong:
|
|
85
|
+
this.handlers.onPong?.(message, peerId);
|
|
86
|
+
break;
|
|
87
|
+
case MessageType.DisconnectReport:
|
|
88
|
+
this.handlers.onDisconnectReport?.(message, peerId);
|
|
89
|
+
break;
|
|
90
|
+
case MessageType.LagReport:
|
|
91
|
+
this.handlers.onLagReport?.(message, peerId);
|
|
92
|
+
break;
|
|
93
|
+
case MessageType.ResumeCountdown:
|
|
94
|
+
this.handlers.onResumeCountdown?.(message, peerId);
|
|
95
|
+
break;
|
|
96
|
+
case MessageType.DropPlayer:
|
|
97
|
+
this.handlers.onDropPlayer?.(message, peerId);
|
|
98
|
+
break;
|
|
99
|
+
// InputAck is not currently handled by Session
|
|
100
|
+
case MessageType.InputAck:
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
//# sourceMappingURL=message-router.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"message-router.js","sourceRoot":"","sources":["../../src/session/message-router.ts"],"names":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,IAAI;gBACpB,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;gBACxC,MAAM;YAEP,KAAK,WAAW,CAAC,gBAAgB;gBAChC,IAAI,CAAC,QAAQ,CAAC,kBAAkB,EAAE,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;gBACpD,MAAM;YAEP,KAAK,WAAW,CAAC,SAAS;gBACzB,IAAI,CAAC,QAAQ,CAAC,WAAW,EAAE,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;gBAC7C,MAAM;YAEP,KAAK,WAAW,CAAC,eAAe;gBAC/B,IAAI,CAAC,QAAQ,CAAC,iBAAiB,EAAE,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;gBACnD,MAAM;YAEP,KAAK,WAAW,CAAC,UAAU;gBAC1B,IAAI,CAAC,QAAQ,CAAC,YAAY,EAAE,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;gBAC9C,MAAM;YAEP,+CAA+C;YAC/C,KAAK,WAAW,CAAC,QAAQ;gBACxB,MAAM;QACR,CAAC;IACF,CAAC;CACD"}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Player state management for sessions.
|
|
3
|
+
*/
|
|
4
|
+
import { type PlayerId, type PlayerInfo, type Tick } from "../types.js";
|
|
5
|
+
/**
|
|
6
|
+
* Manages player state within a session.
|
|
7
|
+
*
|
|
8
|
+
* Centralizes player tracking, connection state, and lifecycle operations.
|
|
9
|
+
*/
|
|
10
|
+
export declare class PlayerManager implements Iterable<PlayerInfo> {
|
|
11
|
+
private readonly players;
|
|
12
|
+
/**
|
|
13
|
+
* Add a new player to the session.
|
|
14
|
+
*
|
|
15
|
+
* @param info - The player info to add
|
|
16
|
+
*/
|
|
17
|
+
addPlayer(info: PlayerInfo): void;
|
|
18
|
+
/**
|
|
19
|
+
* Remove a player from the session.
|
|
20
|
+
*
|
|
21
|
+
* @param playerId - The player ID to remove
|
|
22
|
+
* @returns The removed player info, or undefined if not found
|
|
23
|
+
*/
|
|
24
|
+
removePlayer(playerId: PlayerId): PlayerInfo | undefined;
|
|
25
|
+
/**
|
|
26
|
+
* Get a player by ID.
|
|
27
|
+
*
|
|
28
|
+
* @param playerId - The player ID to look up
|
|
29
|
+
* @returns The player info, or undefined if not found
|
|
30
|
+
*/
|
|
31
|
+
getPlayer(playerId: PlayerId): PlayerInfo | undefined;
|
|
32
|
+
/**
|
|
33
|
+
* Check if a player exists.
|
|
34
|
+
*
|
|
35
|
+
* @param playerId - The player ID to check
|
|
36
|
+
*/
|
|
37
|
+
hasPlayer(playerId: PlayerId): boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Mark a player as disconnected.
|
|
40
|
+
*
|
|
41
|
+
* @param playerId - The player ID to mark
|
|
42
|
+
* @param leaveTick - The tick when they disconnected (optional)
|
|
43
|
+
* @returns The updated player info, or undefined if not found
|
|
44
|
+
*/
|
|
45
|
+
markDisconnected(playerId: PlayerId, leaveTick?: Tick): PlayerInfo | undefined;
|
|
46
|
+
/**
|
|
47
|
+
* Mark a player as connected.
|
|
48
|
+
*
|
|
49
|
+
* @param playerId - The player ID to mark
|
|
50
|
+
* @returns The updated player info, or undefined if not found
|
|
51
|
+
*/
|
|
52
|
+
markConnected(playerId: PlayerId): PlayerInfo | undefined;
|
|
53
|
+
/**
|
|
54
|
+
* Get all players that are currently active (connected and role is Player).
|
|
55
|
+
*
|
|
56
|
+
* @returns Array of active player infos
|
|
57
|
+
*/
|
|
58
|
+
getActivePlayers(): PlayerInfo[];
|
|
59
|
+
/**
|
|
60
|
+
* Get all connected players (any role).
|
|
61
|
+
*
|
|
62
|
+
* @returns Array of connected player infos
|
|
63
|
+
*/
|
|
64
|
+
getConnectedPlayers(): PlayerInfo[];
|
|
65
|
+
/**
|
|
66
|
+
* Get the count of active players.
|
|
67
|
+
*/
|
|
68
|
+
getActivePlayerCount(): number;
|
|
69
|
+
/**
|
|
70
|
+
* Find the host player.
|
|
71
|
+
*
|
|
72
|
+
* @returns The host player info, or undefined if no host
|
|
73
|
+
*/
|
|
74
|
+
findHost(): PlayerInfo | undefined;
|
|
75
|
+
/**
|
|
76
|
+
* Get all player IDs.
|
|
77
|
+
*/
|
|
78
|
+
getPlayerIds(): PlayerId[];
|
|
79
|
+
/**
|
|
80
|
+
* Get all player infos as an array.
|
|
81
|
+
*/
|
|
82
|
+
getAllPlayers(): PlayerInfo[];
|
|
83
|
+
/**
|
|
84
|
+
* Clear all players.
|
|
85
|
+
*/
|
|
86
|
+
clear(): void;
|
|
87
|
+
/**
|
|
88
|
+
* Number of players in the session.
|
|
89
|
+
*/
|
|
90
|
+
get size(): number;
|
|
91
|
+
/**
|
|
92
|
+
* Iterate over all players.
|
|
93
|
+
*/
|
|
94
|
+
[Symbol.iterator](): Iterator<PlayerInfo>;
|
|
95
|
+
/**
|
|
96
|
+
* Get players as a readonly map (for compatibility with existing code).
|
|
97
|
+
*/
|
|
98
|
+
asReadonlyMap(): ReadonlyMap<PlayerId, PlayerInfo>;
|
|
99
|
+
}
|
|
100
|
+
//# sourceMappingURL=player-manager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"player-manager.d.ts","sourceRoot":"","sources":["../../src/session/player-manager.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAEN,KAAK,QAAQ,EACb,KAAK,UAAU,EAEf,KAAK,IAAI,EACT,MAAM,aAAa,CAAC;AAErB;;;;GAIG;AACH,qBAAa,aAAc,YAAW,QAAQ,CAAC,UAAU,CAAC;IACzD,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAwC;IAEhE;;;;OAIG;IACH,SAAS,CAAC,IAAI,EAAE,UAAU,GAAG,IAAI;IAIjC;;;;;OAKG;IACH,YAAY,CAAC,QAAQ,EAAE,QAAQ,GAAG,UAAU,GAAG,SAAS;IAQxD;;;;;OAKG;IACH,SAAS,CAAC,QAAQ,EAAE,QAAQ,GAAG,UAAU,GAAG,S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