@lizardon386/game-sdk 0.0.0-stage → 0.2.0
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 +14 -2
- package/dist/contract/client.d.ts +2 -0
- package/dist/contract/client.d.ts.map +1 -0
- package/dist/contract/client.js +2 -0
- package/dist/contract/client.js.map +1 -0
- package/dist/contract/module-api.d.ts +104 -0
- package/dist/contract/module-api.d.ts.map +1 -0
- package/dist/contract/module-api.js +22 -0
- package/dist/contract/module-api.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/kit/client/chat.d.ts +46 -0
- package/dist/kit/client/chat.d.ts.map +1 -0
- package/dist/kit/client/chat.js +183 -0
- package/dist/kit/client/chat.js.map +1 -0
- package/dist/kit/client/feed.d.ts +39 -0
- package/dist/kit/client/feed.d.ts.map +1 -0
- package/dist/kit/client/feed.js +157 -0
- package/dist/kit/client/feed.js.map +1 -0
- package/dist/kit/client/index.d.ts +6 -0
- package/dist/kit/client/index.d.ts.map +1 -0
- package/dist/kit/client/index.js +6 -0
- package/dist/kit/client/index.js.map +1 -0
- package/dist/kit/client/scoreboard.d.ts +68 -0
- package/dist/kit/client/scoreboard.d.ts.map +1 -0
- package/dist/kit/client/scoreboard.js +282 -0
- package/dist/kit/client/scoreboard.js.map +1 -0
- package/dist/kit/palette.d.ts +9 -0
- package/dist/kit/palette.d.ts.map +1 -0
- package/dist/kit/palette.js +38 -0
- package/dist/kit/palette.js.map +1 -0
- package/dist/kit/server/feed.d.ts +41 -0
- package/dist/kit/server/feed.d.ts.map +1 -0
- package/dist/kit/server/feed.js +210 -0
- package/dist/kit/server/feed.js.map +1 -0
- package/dist/kit/server/index.d.ts +3 -0
- package/dist/kit/server/index.d.ts.map +1 -0
- package/dist/kit/server/index.js +3 -0
- package/dist/kit/server/index.js.map +1 -0
- package/dist/kit/shared.d.ts +26 -0
- package/dist/kit/shared.d.ts.map +1 -0
- package/dist/kit/shared.js +8 -0
- package/dist/kit/shared.js.map +1 -0
- package/dist/node/pack.d.ts +2 -0
- package/dist/node/pack.d.ts.map +1 -0
- package/dist/node/pack.js +2 -0
- package/dist/node/pack.js.map +1 -0
- package/package.json +60 -4
- package/src/contract/client.ts +1 -0
- package/src/index.ts +30 -0
- package/src/kit/client/chat.ts +199 -0
- package/src/kit/client/feed.ts +170 -0
- package/src/kit/client/index.ts +33 -0
- package/src/kit/client/scoreboard.ts +333 -0
- package/src/kit/palette.ts +44 -0
- package/src/kit/server/feed.ts +221 -0
- package/src/kit/server/index.ts +2 -0
- package/src/kit/shared.ts +28 -0
- package/src/node/pack.ts +1 -0
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import type { Effect, ViewerId } from '@lizardon386/core-abi';
|
|
2
|
+
import { FEED_EVENT, FeedError, type FeedEntry, type FeedVisibility } from '../shared.js';
|
|
3
|
+
|
|
4
|
+
export interface FeedSeat {
|
|
5
|
+
seatId: string;
|
|
6
|
+
team: string | number | null;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface FeedLogOptions {
|
|
10
|
+
max?: number;
|
|
11
|
+
maxText?: number;
|
|
12
|
+
now?: () => number;
|
|
13
|
+
/** team 可见性用;返回当前所有座位及其队伍。 */
|
|
14
|
+
seatTeams?: () => ReadonlyArray<FeedSeat>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface FeedEmitInput {
|
|
18
|
+
type: string;
|
|
19
|
+
text: string;
|
|
20
|
+
actor?: string;
|
|
21
|
+
target?: string;
|
|
22
|
+
data?: Record<string, unknown>;
|
|
23
|
+
visibility?: FeedVisibility;
|
|
24
|
+
at?: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
interface StoredEntry extends FeedEntry {
|
|
28
|
+
visibility: FeedVisibility;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const MAX_TYPE_LENGTH = 64;
|
|
32
|
+
const MAX_DATA_JSON = 2_000;
|
|
33
|
+
|
|
34
|
+
function clamp(value: number | undefined, min: number, max: number, fallback: number): number {
|
|
35
|
+
if (value === undefined || !Number.isFinite(value)) return fallback;
|
|
36
|
+
return Math.min(max, Math.max(min, Math.trunc(value)));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function validateVisibility(value: FeedVisibility | undefined): FeedVisibility {
|
|
40
|
+
if (value === undefined || value === 'all') return 'all';
|
|
41
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
42
|
+
throw new FeedError('事件可见性必须是 all、{ seatId } 或 { team }');
|
|
43
|
+
}
|
|
44
|
+
const keys = Object.keys(value);
|
|
45
|
+
if (keys.length !== 1) throw new FeedError('事件可见性只能包含 seatId 或 team');
|
|
46
|
+
if (keys[0] === 'seatId') {
|
|
47
|
+
const seatId = (value as { seatId?: unknown }).seatId;
|
|
48
|
+
if (typeof seatId !== 'string' || seatId.length === 0) throw new FeedError('事件 seatId 不合法');
|
|
49
|
+
return { seatId };
|
|
50
|
+
}
|
|
51
|
+
if (keys[0] === 'team') {
|
|
52
|
+
const team = (value as { team?: unknown }).team;
|
|
53
|
+
if (typeof team !== 'string' && (typeof team !== 'number' || !Number.isFinite(team))) {
|
|
54
|
+
throw new FeedError('事件 team 不合法');
|
|
55
|
+
}
|
|
56
|
+
return { team };
|
|
57
|
+
}
|
|
58
|
+
throw new FeedError('事件可见性只能包含 seatId 或 team');
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function publicEntry(entry: StoredEntry): FeedEntry {
|
|
62
|
+
const copy: FeedEntry = {
|
|
63
|
+
seq: entry.seq,
|
|
64
|
+
at: entry.at,
|
|
65
|
+
type: entry.type,
|
|
66
|
+
text: entry.text,
|
|
67
|
+
};
|
|
68
|
+
if (entry.actor !== undefined) copy.actor = entry.actor;
|
|
69
|
+
if (entry.target !== undefined) copy.target = entry.target;
|
|
70
|
+
if (entry.data !== undefined) copy.data = entry.data;
|
|
71
|
+
return copy;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function validateEntry(value: unknown): StoredEntry | null {
|
|
75
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) return null;
|
|
76
|
+
const raw = value as Record<string, unknown>;
|
|
77
|
+
if (!Number.isSafeInteger(raw.seq) || (raw.seq as number) < 1) return null;
|
|
78
|
+
if (typeof raw.at !== 'number' || !Number.isFinite(raw.at)) return null;
|
|
79
|
+
if (typeof raw.type !== 'string' || raw.type.trim().length === 0 || raw.type.length > MAX_TYPE_LENGTH) return null;
|
|
80
|
+
if (typeof raw.text !== 'string' || raw.text.length === 0) return null;
|
|
81
|
+
if (raw.actor !== undefined && typeof raw.actor !== 'string') return null;
|
|
82
|
+
if (raw.target !== undefined && typeof raw.target !== 'string') return null;
|
|
83
|
+
if (raw.data !== undefined && (typeof raw.data !== 'object' || raw.data === null || Array.isArray(raw.data))) return null;
|
|
84
|
+
let visibility: FeedVisibility;
|
|
85
|
+
try {
|
|
86
|
+
visibility = validateVisibility(raw.visibility as FeedVisibility | undefined);
|
|
87
|
+
} catch {
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
const entry: StoredEntry = {
|
|
91
|
+
seq: raw.seq as number,
|
|
92
|
+
at: raw.at,
|
|
93
|
+
type: raw.type.trim(),
|
|
94
|
+
text: raw.text,
|
|
95
|
+
visibility,
|
|
96
|
+
};
|
|
97
|
+
if (raw.actor !== undefined) entry.actor = raw.actor;
|
|
98
|
+
if (raw.target !== undefined) entry.target = raw.target;
|
|
99
|
+
if (raw.data !== undefined) entry.data = raw.data as Record<string, unknown>;
|
|
100
|
+
return entry;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** 服务端事件流水:生成序号/时间、按可见性发 Effect、保留最近 N 条。 */
|
|
104
|
+
export class FeedLog {
|
|
105
|
+
private readonly max: number;
|
|
106
|
+
private readonly maxText: number;
|
|
107
|
+
private readonly now: () => number;
|
|
108
|
+
private readonly seatTeams: (() => ReadonlyArray<FeedSeat>) | null;
|
|
109
|
+
private entries: StoredEntry[] = [];
|
|
110
|
+
private nextSeq = 1;
|
|
111
|
+
private pending: Effect[] = [];
|
|
112
|
+
|
|
113
|
+
constructor(options: FeedLogOptions = {}) {
|
|
114
|
+
this.max = clamp(options.max, 1, 500, 100);
|
|
115
|
+
this.maxText = clamp(options.maxText, 1, 2_000, 200);
|
|
116
|
+
this.now = options.now ?? (() => Date.now());
|
|
117
|
+
this.seatTeams = options.seatTeams ?? null;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
emit(input: FeedEmitInput): FeedEntry {
|
|
121
|
+
const type = typeof input.type === 'string' ? input.type.trim() : '';
|
|
122
|
+
const text = typeof input.text === 'string' ? input.text.trim() : '';
|
|
123
|
+
if (!type || type.length > MAX_TYPE_LENGTH) throw new FeedError('事件 type 为空或过长');
|
|
124
|
+
if (!text) throw new FeedError('事件 text 不能为空');
|
|
125
|
+
const visibility = validateVisibility(input.visibility);
|
|
126
|
+
|
|
127
|
+
let recipients: string[] = [];
|
|
128
|
+
if (typeof visibility === 'object' && 'team' in visibility) {
|
|
129
|
+
if (!this.seatTeams) throw new FeedError('team 可见性需要 seatTeams 选项');
|
|
130
|
+
recipients = [...new Set(
|
|
131
|
+
this.seatTeams()
|
|
132
|
+
.filter((seat) => seat.team === visibility.team)
|
|
133
|
+
.map((seat) => seat.seatId),
|
|
134
|
+
)];
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const at = typeof input.at === 'number' && Number.isFinite(input.at) ? input.at : this.now();
|
|
138
|
+
const entry: StoredEntry = {
|
|
139
|
+
seq: this.nextSeq,
|
|
140
|
+
at,
|
|
141
|
+
type,
|
|
142
|
+
text: text.slice(0, this.maxText),
|
|
143
|
+
visibility,
|
|
144
|
+
};
|
|
145
|
+
if (input.actor) entry.actor = input.actor;
|
|
146
|
+
if (input.target) entry.target = input.target;
|
|
147
|
+
if (input.data) {
|
|
148
|
+
const json = JSON.stringify(input.data);
|
|
149
|
+
if (json.length > MAX_DATA_JSON) throw new FeedError('事件 data 过大');
|
|
150
|
+
entry.data = input.data;
|
|
151
|
+
}
|
|
152
|
+
this.nextSeq += 1;
|
|
153
|
+
this.entries.push(entry);
|
|
154
|
+
if (this.entries.length > this.max) this.entries.splice(0, this.entries.length - this.max);
|
|
155
|
+
|
|
156
|
+
const payload = publicEntry(entry);
|
|
157
|
+
if (visibility === 'all') {
|
|
158
|
+
this.pending.push({ kind: 'broadcast', name: FEED_EVENT, payload });
|
|
159
|
+
} else if ('seatId' in visibility) {
|
|
160
|
+
this.pending.push({ kind: 'toSeat', seatId: visibility.seatId, name: FEED_EVENT, payload });
|
|
161
|
+
} else {
|
|
162
|
+
for (const seatId of recipients) {
|
|
163
|
+
this.pending.push({ kind: 'toSeat', seatId, name: FEED_EVENT, payload });
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return payload;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** 按视角裁剪历史;viewer 为 null 时只返回全员事件。 */
|
|
170
|
+
viewFor(viewer: ViewerId): FeedEntry[] {
|
|
171
|
+
const viewerTeam = viewer !== null && this.seatTeams
|
|
172
|
+
? this.seatTeams().find((seat) => seat.seatId === viewer)?.team ?? null
|
|
173
|
+
: null;
|
|
174
|
+
return this.entries
|
|
175
|
+
.filter((entry) => {
|
|
176
|
+
if (entry.visibility === 'all') return true;
|
|
177
|
+
if (viewer === null) return false;
|
|
178
|
+
if ('seatId' in entry.visibility) return entry.visibility.seatId === viewer;
|
|
179
|
+
return viewerTeam !== null && entry.visibility.team === viewerTeam;
|
|
180
|
+
})
|
|
181
|
+
.map(publicEntry);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
drainEffects(): Effect[] {
|
|
185
|
+
const effects = this.pending;
|
|
186
|
+
this.pending = [];
|
|
187
|
+
return effects;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
clear(): void {
|
|
191
|
+
this.entries = [];
|
|
192
|
+
this.pending = [];
|
|
193
|
+
this.nextSeq = 1;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
snapshot(): unknown {
|
|
197
|
+
return {
|
|
198
|
+
version: 1,
|
|
199
|
+
nextSeq: this.nextSeq,
|
|
200
|
+
entries: this.entries.map((entry) => ({ ...entry, data: entry.data ? { ...entry.data } : undefined })),
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
restore(value: unknown): void {
|
|
205
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) throw new FeedError('事件流快照不合法');
|
|
206
|
+
const raw = value as { version?: unknown; nextSeq?: unknown; entries?: unknown };
|
|
207
|
+
if (raw.version !== 1 || !Array.isArray(raw.entries)) throw new FeedError('事件流快照版本不支持');
|
|
208
|
+
const entries: StoredEntry[] = [];
|
|
209
|
+
for (const item of raw.entries) {
|
|
210
|
+
const entry = validateEntry(item);
|
|
211
|
+
if (!entry) throw new FeedError('事件流快照含坏记录');
|
|
212
|
+
entries.push(entry);
|
|
213
|
+
}
|
|
214
|
+
entries.sort((a, b) => a.seq - b.seq);
|
|
215
|
+
this.entries = entries.slice(-this.max);
|
|
216
|
+
const maxSeq = this.entries.reduce((max, entry) => Math.max(max, entry.seq), 0);
|
|
217
|
+
const savedNext = typeof raw.nextSeq === 'number' && Number.isSafeInteger(raw.nextSeq) ? raw.nextSeq : 1;
|
|
218
|
+
this.nextSeq = Math.max(savedNext, maxSeq + 1, 1);
|
|
219
|
+
this.pending = [];
|
|
220
|
+
}
|
|
221
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/** 游戏事件流的一条公开记录:玩法文本由游戏填,平台只搬运。 */
|
|
2
|
+
export interface FeedEntry {
|
|
3
|
+
seq: number;
|
|
4
|
+
at: number;
|
|
5
|
+
type: string;
|
|
6
|
+
text: string;
|
|
7
|
+
actor?: string;
|
|
8
|
+
target?: string;
|
|
9
|
+
data?: Record<string, unknown>;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** 事件对谁可见;team 需要 FeedLog 提供 seatTeams。 */
|
|
13
|
+
export type FeedVisibility = 'all' | { seatId: string } | { team: string | number };
|
|
14
|
+
|
|
15
|
+
export const FEED_EVENT = 'bp.feed';
|
|
16
|
+
|
|
17
|
+
export class FeedError extends Error {
|
|
18
|
+
constructor(message: string) {
|
|
19
|
+
super(message);
|
|
20
|
+
this.name = 'FeedError';
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface SeatLike {
|
|
25
|
+
seatId: string;
|
|
26
|
+
slot?: number;
|
|
27
|
+
team?: string | number | null;
|
|
28
|
+
}
|
package/src/node/pack.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@lizardon386/pack-format';
|