connectbase-client 6.26.0 → 6.27.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/CHANGELOG.md +26 -0
- package/dist/cli.js +15 -3
- package/dist/connect-base.umd.js +5 -5
- package/dist/index.d.mts +670 -2
- package/dist/index.d.ts +670 -2
- package/dist/index.js +735 -4
- package/dist/index.mjs +731 -4
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -29,11 +29,15 @@ __export(index_exports, {
|
|
|
29
29
|
AuthError: () => AuthError,
|
|
30
30
|
ConnectBase: () => ConnectBase,
|
|
31
31
|
EndpointAPI: () => EndpointAPI,
|
|
32
|
+
GAME_MESSAGE: () => GAME_MESSAGE,
|
|
32
33
|
GameAPI: () => GameAPI,
|
|
33
34
|
GameConfigAPI: () => GameConfigAPI,
|
|
34
35
|
GameError: () => GameError,
|
|
36
|
+
GameHubAPI: () => GameHubAPI,
|
|
35
37
|
GameRoom: () => GameRoom,
|
|
36
38
|
GameRoomTransport: () => GameRoomTransport,
|
|
39
|
+
GameScope: () => GameScope,
|
|
40
|
+
GamesAPI: () => GamesAPI,
|
|
37
41
|
NativeAPI: () => NativeAPI,
|
|
38
42
|
OrganizationsAPI: () => OrganizationsAPI,
|
|
39
43
|
RealtimeConnectionError: () => RealtimeConnectionError,
|
|
@@ -6066,6 +6070,622 @@ var GameConfigAPI = class {
|
|
|
6066
6070
|
}
|
|
6067
6071
|
};
|
|
6068
6072
|
|
|
6073
|
+
// src/api/game-units.ts
|
|
6074
|
+
var GAME_MESSAGE = {
|
|
6075
|
+
ready: "cb:game:ready",
|
|
6076
|
+
token: "cb:game:token",
|
|
6077
|
+
refresh: "cb:game:token:refresh"
|
|
6078
|
+
};
|
|
6079
|
+
var DEFAULT_REFRESH_BEFORE_MS = 12e4;
|
|
6080
|
+
var MIN_REFRESH_DELAY_MS = 5e3;
|
|
6081
|
+
function decodeJwtPayload2(token) {
|
|
6082
|
+
const part = token.split(".")[1];
|
|
6083
|
+
if (!part) return null;
|
|
6084
|
+
try {
|
|
6085
|
+
const b64 = part.replace(/-/g, "+").replace(/_/g, "/");
|
|
6086
|
+
const padded = b64 + "=".repeat((4 - b64.length % 4) % 4);
|
|
6087
|
+
const json = typeof atob === "function" ? decodeURIComponent(
|
|
6088
|
+
Array.from(atob(padded)).map((c) => `%${c.charCodeAt(0).toString(16).padStart(2, "0")}`).join("")
|
|
6089
|
+
) : "";
|
|
6090
|
+
const parsed = JSON.parse(json);
|
|
6091
|
+
return parsed && typeof parsed === "object" ? parsed : null;
|
|
6092
|
+
} catch {
|
|
6093
|
+
return null;
|
|
6094
|
+
}
|
|
6095
|
+
}
|
|
6096
|
+
function normalizeGameTokenGrant(raw) {
|
|
6097
|
+
let g = raw;
|
|
6098
|
+
if (g && typeof g === "object" && !g.token && !g.access_token && g.result && typeof g.result === "object") {
|
|
6099
|
+
g = g.result;
|
|
6100
|
+
}
|
|
6101
|
+
const token = g?.token ?? g?.access_token;
|
|
6102
|
+
if (!g || typeof token !== "string" || token === "") {
|
|
6103
|
+
throw new Error(
|
|
6104
|
+
"\uAC8C\uC784 \uD1A0\uD070 \uBB36\uC74C\uC5D0 token(\uB610\uB294 access_token) \uC774 \uC5C6\uC2B5\uB2C8\uB2E4. getToken \uC774 \uBC1C\uAE09 \uC751\uB2F5\uC744 \uB3CC\uB824\uC8FC\uB294\uC9C0 \uD655\uC778\uD558\uC138\uC694."
|
|
6105
|
+
);
|
|
6106
|
+
}
|
|
6107
|
+
let expiresAt = g.expiresAt;
|
|
6108
|
+
if (expiresAt === void 0 && typeof g.expires_in === "number") {
|
|
6109
|
+
expiresAt = Date.now() + g.expires_in * 1e3;
|
|
6110
|
+
}
|
|
6111
|
+
return {
|
|
6112
|
+
token,
|
|
6113
|
+
playerId: g.playerId ?? g.player_id,
|
|
6114
|
+
playerIdMode: g.playerIdMode ?? g.player_id_mode,
|
|
6115
|
+
expiresAt,
|
|
6116
|
+
gameId: g.game_id
|
|
6117
|
+
};
|
|
6118
|
+
}
|
|
6119
|
+
function encodeSeg(v) {
|
|
6120
|
+
return encodeURIComponent(v);
|
|
6121
|
+
}
|
|
6122
|
+
function isExactOrigin(origin) {
|
|
6123
|
+
if (typeof origin !== "string" || origin === "" || origin.includes("*"))
|
|
6124
|
+
return false;
|
|
6125
|
+
try {
|
|
6126
|
+
const u = new URL(origin);
|
|
6127
|
+
return (u.protocol === "https:" || u.protocol === "http:") && u.origin === origin;
|
|
6128
|
+
} catch {
|
|
6129
|
+
return false;
|
|
6130
|
+
}
|
|
6131
|
+
}
|
|
6132
|
+
function unrefTimer(t) {
|
|
6133
|
+
t.unref?.();
|
|
6134
|
+
}
|
|
6135
|
+
var GameScope = class {
|
|
6136
|
+
constructor(deps, gameId, options) {
|
|
6137
|
+
this.refreshTimer = null;
|
|
6138
|
+
this.rooms = /* @__PURE__ */ new Set();
|
|
6139
|
+
this.disposers = [];
|
|
6140
|
+
this.disposed = false;
|
|
6141
|
+
if (!gameId) throw new Error("cb.game.forGame: gameId \uAC00 \uD544\uC694\uD569\uB2C8\uB2E4.");
|
|
6142
|
+
if (!options?.token)
|
|
6143
|
+
throw new Error("cb.game.forGame: \uAC8C\uC784 \uD1A0\uD070(token) \uC774 \uD544\uC694\uD569\uB2C8\uB2E4.");
|
|
6144
|
+
const appId = options.appId ?? deps.defaultAppId;
|
|
6145
|
+
if (!appId)
|
|
6146
|
+
throw new Error(
|
|
6147
|
+
"cb.game.forGame: appId \uAC00 \uD544\uC694\uD569\uB2C8\uB2E4. new ConnectBase({ appId }) \uB098 options.appId \uB85C \uB118\uAE30\uC138\uC694."
|
|
6148
|
+
);
|
|
6149
|
+
this.deps = deps;
|
|
6150
|
+
this.options = options;
|
|
6151
|
+
this.gameId = gameId;
|
|
6152
|
+
this.appId = appId;
|
|
6153
|
+
this.token = options.token;
|
|
6154
|
+
this._playerId = "";
|
|
6155
|
+
this._playerIdMode = void 0;
|
|
6156
|
+
this._expiresAt = 0;
|
|
6157
|
+
this.applyGrant({
|
|
6158
|
+
token: options.token,
|
|
6159
|
+
playerId: options.playerId,
|
|
6160
|
+
playerIdMode: options.playerIdMode,
|
|
6161
|
+
expiresAt: options.expiresAt
|
|
6162
|
+
});
|
|
6163
|
+
const lb = (key) => `/v1/game/${encodeSeg(this.appId)}/leaderboards/${encodeSeg(key)}`;
|
|
6164
|
+
this.leaderboard = {
|
|
6165
|
+
submit: (key, score, opts) => this.request("POST", `${lb(key)}/scores`, {
|
|
6166
|
+
member: this.requirePlayerId(),
|
|
6167
|
+
score,
|
|
6168
|
+
mode: opts?.mode ?? "set"
|
|
6169
|
+
}),
|
|
6170
|
+
top: (key, n = 10) => this.request("GET", `${lb(key)}/top?n=${n}`),
|
|
6171
|
+
rank: (key) => this.request(
|
|
6172
|
+
"GET",
|
|
6173
|
+
`${lb(key)}/members/${encodeSeg(this.requirePlayerId())}`
|
|
6174
|
+
),
|
|
6175
|
+
around: (key, above = 5, below = 5) => this.request(
|
|
6176
|
+
"GET",
|
|
6177
|
+
`${lb(key)}/around/${encodeSeg(this.requirePlayerId())}?above=${above}&below=${below}`
|
|
6178
|
+
),
|
|
6179
|
+
removeMe: (key) => this.request(
|
|
6180
|
+
"DELETE",
|
|
6181
|
+
`${lb(key)}/members/${encodeSeg(this.requirePlayerId())}`
|
|
6182
|
+
)
|
|
6183
|
+
};
|
|
6184
|
+
const mq = (key) => `/v1/game/${encodeSeg(this.appId)}/matchqueue/${encodeSeg(key)}`;
|
|
6185
|
+
this.matchqueue = {
|
|
6186
|
+
enqueue: (queueKey, attributes, ttlSec, ticketId) => this.request("POST", `${mq(queueKey)}/tickets`, {
|
|
6187
|
+
ticket_id: ticketId ?? this.requirePlayerId(),
|
|
6188
|
+
attributes,
|
|
6189
|
+
ttl_sec: ttlSec ?? 0
|
|
6190
|
+
}),
|
|
6191
|
+
list: (queueKey) => this.request("GET", mq(queueKey)),
|
|
6192
|
+
cancel: (queueKey, ticketId) => this.request(
|
|
6193
|
+
"DELETE",
|
|
6194
|
+
`${mq(queueKey)}/tickets/${encodeSeg(ticketId ?? this.requirePlayerId())}`
|
|
6195
|
+
)
|
|
6196
|
+
};
|
|
6197
|
+
const ent = (name) => `/v1/game/${encodeSeg(this.appId)}/entities/${encodeSeg(name)}`;
|
|
6198
|
+
this.entities = {
|
|
6199
|
+
list: (name, opts) => {
|
|
6200
|
+
const q = new URLSearchParams();
|
|
6201
|
+
if (opts?.limit) q.set("limit", String(opts.limit));
|
|
6202
|
+
if (opts?.cursor) q.set("cursor", opts.cursor);
|
|
6203
|
+
const qs = q.toString();
|
|
6204
|
+
return this.request(
|
|
6205
|
+
"GET",
|
|
6206
|
+
qs ? `${ent(name)}?${qs}` : ent(name)
|
|
6207
|
+
);
|
|
6208
|
+
},
|
|
6209
|
+
get: (name, id) => this.request("GET", `${ent(name)}/${encodeSeg(id)}`)
|
|
6210
|
+
};
|
|
6211
|
+
const kv = (key) => key === void 0 ? `/v1/game/${encodeSeg(this.appId)}/kv` : `/v1/game/${encodeSeg(this.appId)}/kv/${encodeSeg(key)}`;
|
|
6212
|
+
this.kv = {
|
|
6213
|
+
list: () => this.request("GET", kv()),
|
|
6214
|
+
get: (key) => this.request("GET", kv(key)),
|
|
6215
|
+
set: (key, value, opts) => {
|
|
6216
|
+
const body = { value };
|
|
6217
|
+
if (opts?.expectedRevision !== void 0)
|
|
6218
|
+
body.expected_revision = opts.expectedRevision;
|
|
6219
|
+
return this.request("PUT", kv(key), body);
|
|
6220
|
+
},
|
|
6221
|
+
delete: (key) => this.request("DELETE", kv(key))
|
|
6222
|
+
};
|
|
6223
|
+
}
|
|
6224
|
+
/** 이 게임에서 내 식별자(리더보드 member, 매치큐 ticket id). */
|
|
6225
|
+
get playerId() {
|
|
6226
|
+
return this._playerId;
|
|
6227
|
+
}
|
|
6228
|
+
/** `playerId` 의 방식(`anonymous` / `member`). 발급 응답이나 옵션으로 받지 못했으면 undefined. */
|
|
6229
|
+
get playerIdMode() {
|
|
6230
|
+
return this._playerIdMode;
|
|
6231
|
+
}
|
|
6232
|
+
/** 토큰 만료 시각(ms). 모르면 0. */
|
|
6233
|
+
get expiresAt() {
|
|
6234
|
+
return this._expiresAt;
|
|
6235
|
+
}
|
|
6236
|
+
/** 지금 쓰는 게임 토큰. 다른 곳에 저장하지 마세요(메모리에만 두는 것이 원칙). */
|
|
6237
|
+
getToken() {
|
|
6238
|
+
return this.token;
|
|
6239
|
+
}
|
|
6240
|
+
/**
|
|
6241
|
+
* 게임 룸(WebSocket) 클라이언트. 토큰은 이 게임 토큰으로 고정한다. 토큰이 바뀌면 다음 연결,
|
|
6242
|
+
* 재연결부터 새 토큰을 쓴다.
|
|
6243
|
+
*/
|
|
6244
|
+
createClient(config) {
|
|
6245
|
+
const room = this.deps.createRoom({
|
|
6246
|
+
...config,
|
|
6247
|
+
appId: this.appId,
|
|
6248
|
+
accessToken: this.token
|
|
6249
|
+
});
|
|
6250
|
+
this.rooms.add(room);
|
|
6251
|
+
return room;
|
|
6252
|
+
}
|
|
6253
|
+
/** 토큰을 바꾼다(갱신). 문자열 또는 발급 응답 모양을 받는다. */
|
|
6254
|
+
setToken(grant) {
|
|
6255
|
+
const n = typeof grant === "string" ? { token: grant } : normalizeGameTokenGrant(grant);
|
|
6256
|
+
if (n.gameId && n.gameId !== this.gameId) {
|
|
6257
|
+
throw new Error(
|
|
6258
|
+
`\uB2E4\uB978 \uAC8C\uC784(${n.gameId}) \uC758 \uD1A0\uD070\uC785\uB2C8\uB2E4. \uC774 \uD074\uB77C\uC774\uC5B8\uD2B8\uB294 ${this.gameId} \uC804\uC6A9\uC785\uB2C8\uB2E4.`
|
|
6259
|
+
);
|
|
6260
|
+
}
|
|
6261
|
+
this.applyGrant(n);
|
|
6262
|
+
for (const room of this.rooms) room.setAccessToken(this.token);
|
|
6263
|
+
}
|
|
6264
|
+
/** 갱신 타이머를 멈추고 붙은 리스너(fromHost)를 뗀다. */
|
|
6265
|
+
dispose() {
|
|
6266
|
+
this.disposed = true;
|
|
6267
|
+
if (this.refreshTimer) clearTimeout(this.refreshTimer);
|
|
6268
|
+
this.refreshTimer = null;
|
|
6269
|
+
for (const d of this.disposers.splice(0)) d();
|
|
6270
|
+
}
|
|
6271
|
+
/** @internal fromHost 가 리스너 정리를 맡긴다. */
|
|
6272
|
+
_onDispose(fn) {
|
|
6273
|
+
this.disposers.push(fn);
|
|
6274
|
+
}
|
|
6275
|
+
applyGrant(g) {
|
|
6276
|
+
this.token = g.token;
|
|
6277
|
+
if (g.playerIdMode) this._playerIdMode = g.playerIdMode;
|
|
6278
|
+
const claims = decodeJwtPayload2(g.token);
|
|
6279
|
+
const fromClaims = this._playerIdMode === "member" ? "" : typeof claims?.gpid === "string" && claims.gpid || (typeof claims?.sub === "string" && claims.sub.startsWith("gp_") ? claims.sub : "");
|
|
6280
|
+
this._playerId = g.playerId || this._playerId || fromClaims || "";
|
|
6281
|
+
if (g.expiresAt !== void 0) {
|
|
6282
|
+
this._expiresAt = g.expiresAt;
|
|
6283
|
+
} else if (typeof claims?.exp === "number") {
|
|
6284
|
+
this._expiresAt = claims.exp * 1e3;
|
|
6285
|
+
}
|
|
6286
|
+
this.scheduleRefresh();
|
|
6287
|
+
}
|
|
6288
|
+
scheduleRefresh() {
|
|
6289
|
+
if (this.refreshTimer) clearTimeout(this.refreshTimer);
|
|
6290
|
+
this.refreshTimer = null;
|
|
6291
|
+
const cb = this.options.onTokenExpiring;
|
|
6292
|
+
if (!cb || !this._expiresAt || this.disposed) return;
|
|
6293
|
+
const before = this.options.refreshBeforeMs ?? DEFAULT_REFRESH_BEFORE_MS;
|
|
6294
|
+
const delay = Math.max(
|
|
6295
|
+
MIN_REFRESH_DELAY_MS,
|
|
6296
|
+
this._expiresAt - before - Date.now()
|
|
6297
|
+
);
|
|
6298
|
+
this.refreshTimer = setTimeout(async () => {
|
|
6299
|
+
this.refreshTimer = null;
|
|
6300
|
+
try {
|
|
6301
|
+
const next = await cb({
|
|
6302
|
+
gameId: this.gameId,
|
|
6303
|
+
expiresAt: this._expiresAt
|
|
6304
|
+
});
|
|
6305
|
+
if (next) this.setToken(next);
|
|
6306
|
+
} catch {
|
|
6307
|
+
}
|
|
6308
|
+
}, delay);
|
|
6309
|
+
unrefTimer(this.refreshTimer);
|
|
6310
|
+
}
|
|
6311
|
+
requirePlayerId() {
|
|
6312
|
+
if (!this._playerId) {
|
|
6313
|
+
throw new Error(
|
|
6314
|
+
this._playerIdMode === "member" ? "member \uBC29\uC2DD \uAC8C\uC784\uC740 playerId(\uBC1C\uAE09 \uC751\uB2F5\uC758 player_id = \uD68C\uC6D0 id) \uB97C \uB118\uACA8\uC57C \uD569\uB2C8\uB2E4. forGame(gameId, { token, playerId, playerIdMode }) \uB85C \uB118\uAE30\uC138\uC694." : "\uC774 \uAC8C\uC784\uC758 playerId \uB97C \uBAA8\uB985\uB2C8\uB2E4. forGame(gameId, { token, playerId }) \uB85C \uB118\uAE30\uC138\uC694."
|
|
6315
|
+
);
|
|
6316
|
+
}
|
|
6317
|
+
return this._playerId;
|
|
6318
|
+
}
|
|
6319
|
+
async request(method, path, body) {
|
|
6320
|
+
const headers = {
|
|
6321
|
+
Authorization: `Bearer ${this.token}`
|
|
6322
|
+
};
|
|
6323
|
+
if (this.deps.publicKey) headers["X-Public-Key"] = this.deps.publicKey;
|
|
6324
|
+
const init = { method, headers };
|
|
6325
|
+
if (body !== void 0) {
|
|
6326
|
+
headers["Content-Type"] = "application/json";
|
|
6327
|
+
init.body = JSON.stringify(body);
|
|
6328
|
+
}
|
|
6329
|
+
const response = await fetch(`${this.deps.gameServerUrl}${path}`, init);
|
|
6330
|
+
if (!response.ok) {
|
|
6331
|
+
let code = "GAME_REQUEST_FAILED";
|
|
6332
|
+
let message = `${method} ${path} failed: ${response.status}`;
|
|
6333
|
+
let details;
|
|
6334
|
+
try {
|
|
6335
|
+
const data = await response.json();
|
|
6336
|
+
details = data;
|
|
6337
|
+
if (typeof data.code === "string") code = data.code;
|
|
6338
|
+
else if (typeof data.error === "string") code = data.error;
|
|
6339
|
+
if (typeof data.message === "string") message = data.message;
|
|
6340
|
+
else if (typeof data.reason === "string") message = data.reason;
|
|
6341
|
+
else if (typeof data.error === "string") message = data.error;
|
|
6342
|
+
} catch {
|
|
6343
|
+
}
|
|
6344
|
+
throw new ApiError(response.status, message, code, details);
|
|
6345
|
+
}
|
|
6346
|
+
if (response.status === 204) return void 0;
|
|
6347
|
+
const ct = response.headers.get("content-type") || "";
|
|
6348
|
+
if (!ct.includes("application/json")) return void 0;
|
|
6349
|
+
return await response.json();
|
|
6350
|
+
}
|
|
6351
|
+
};
|
|
6352
|
+
var GameHubAPI = class {
|
|
6353
|
+
/**
|
|
6354
|
+
* iframe 게임에 게임 토큰을 넘긴다.
|
|
6355
|
+
*
|
|
6356
|
+
* 게임이 `cb:game:ready` 나 `cb:game:token:refresh` 를 보내면 `getToken()` 으로 토큰을 받아
|
|
6357
|
+
* `{type:'cb:game:token', gameId, token, playerId, playerIdMode, expiresAt}` 를 **게임 origin 으로만**
|
|
6358
|
+
* 보낸다. 만료 2분 전에는 게임이 묻지 않아도 새 토큰을 보낸다. 다른 origin 이나 다른 창에서 온
|
|
6359
|
+
* 메시지는 무시한다.
|
|
6360
|
+
*
|
|
6361
|
+
* @example
|
|
6362
|
+
* ```ts
|
|
6363
|
+
* const handle = cb.game.hub.mount(iframeEl, {
|
|
6364
|
+
* gameId: 'space-shooter',
|
|
6365
|
+
* origin: 'https://abc.web.connectbase.world',
|
|
6366
|
+
* getToken: async () => (await cb.functions.invoke('issue-game-token', { gameId: 'space-shooter' })).result,
|
|
6367
|
+
* })
|
|
6368
|
+
* // 화면을 떠날 때
|
|
6369
|
+
* handle.destroy()
|
|
6370
|
+
* ```
|
|
6371
|
+
*/
|
|
6372
|
+
mount(iframe, options) {
|
|
6373
|
+
if (!options?.gameId)
|
|
6374
|
+
throw new Error("cb.game.hub.mount: gameId \uAC00 \uD544\uC694\uD569\uB2C8\uB2E4.");
|
|
6375
|
+
if (!isExactOrigin(options.origin)) {
|
|
6376
|
+
throw new Error(
|
|
6377
|
+
"cb.game.hub.mount: origin \uC740 'https://host[:port]' \uCC98\uB7FC \uC815\uD655\uD55C origin \uC774\uC5B4\uC57C \uD569\uB2C8\uB2E4('*', \uACBD\uB85C \uBD88\uAC00)."
|
|
6378
|
+
);
|
|
6379
|
+
}
|
|
6380
|
+
if (typeof options.getToken !== "function")
|
|
6381
|
+
throw new Error("cb.game.hub.mount: getToken \uD568\uC218\uAC00 \uD544\uC694\uD569\uB2C8\uB2E4.");
|
|
6382
|
+
const doc = iframe.ownerDocument;
|
|
6383
|
+
const host = doc?.defaultView ?? (typeof window !== "undefined" ? window : void 0);
|
|
6384
|
+
if (!host)
|
|
6385
|
+
throw new Error("cb.game.hub.mount: \uBE0C\uB77C\uC6B0\uC800 \uCC3D\uC5D0\uC11C\uB9CC \uC4F8 \uC218 \uC788\uC2B5\uB2C8\uB2E4.");
|
|
6386
|
+
const refreshBefore = options.refreshBeforeMs ?? DEFAULT_REFRESH_BEFORE_MS;
|
|
6387
|
+
let timer = null;
|
|
6388
|
+
let inflight = null;
|
|
6389
|
+
let destroyed = false;
|
|
6390
|
+
const send = () => {
|
|
6391
|
+
if (destroyed) return Promise.resolve();
|
|
6392
|
+
if (inflight) return inflight;
|
|
6393
|
+
inflight = (async () => {
|
|
6394
|
+
try {
|
|
6395
|
+
const g = normalizeGameTokenGrant(await options.getToken());
|
|
6396
|
+
if (g.gameId && g.gameId !== options.gameId) {
|
|
6397
|
+
throw new Error(
|
|
6398
|
+
`getToken \uC774 \uB2E4\uB978 \uAC8C\uC784(${g.gameId}) \uC758 \uD1A0\uD070\uC744 \uB3CC\uB824\uC92C\uC2B5\uB2C8\uB2E4.`
|
|
6399
|
+
);
|
|
6400
|
+
}
|
|
6401
|
+
const expiresAt = g.expiresAt ?? (() => {
|
|
6402
|
+
const exp = decodeJwtPayload2(g.token)?.exp;
|
|
6403
|
+
return typeof exp === "number" ? exp * 1e3 : void 0;
|
|
6404
|
+
})();
|
|
6405
|
+
const target = iframe.contentWindow;
|
|
6406
|
+
if (destroyed || !target) return;
|
|
6407
|
+
target.postMessage(
|
|
6408
|
+
{
|
|
6409
|
+
type: GAME_MESSAGE.token,
|
|
6410
|
+
gameId: options.gameId,
|
|
6411
|
+
token: g.token,
|
|
6412
|
+
playerId: g.playerId,
|
|
6413
|
+
playerIdMode: g.playerIdMode,
|
|
6414
|
+
expiresAt
|
|
6415
|
+
},
|
|
6416
|
+
options.origin
|
|
6417
|
+
);
|
|
6418
|
+
if (timer) clearTimeout(timer);
|
|
6419
|
+
timer = null;
|
|
6420
|
+
if (expiresAt) {
|
|
6421
|
+
timer = setTimeout(
|
|
6422
|
+
() => {
|
|
6423
|
+
void send();
|
|
6424
|
+
},
|
|
6425
|
+
Math.max(
|
|
6426
|
+
MIN_REFRESH_DELAY_MS,
|
|
6427
|
+
expiresAt - refreshBefore - Date.now()
|
|
6428
|
+
)
|
|
6429
|
+
);
|
|
6430
|
+
unrefTimer(timer);
|
|
6431
|
+
}
|
|
6432
|
+
} catch (err) {
|
|
6433
|
+
options.onError?.(err);
|
|
6434
|
+
} finally {
|
|
6435
|
+
inflight = null;
|
|
6436
|
+
}
|
|
6437
|
+
})();
|
|
6438
|
+
return inflight;
|
|
6439
|
+
};
|
|
6440
|
+
const onMessage = (e) => {
|
|
6441
|
+
if (e.origin !== options.origin) return;
|
|
6442
|
+
if (!iframe.contentWindow || e.source !== iframe.contentWindow) return;
|
|
6443
|
+
const data = e.data;
|
|
6444
|
+
if (!data || typeof data !== "object") return;
|
|
6445
|
+
if (data.gameId !== void 0 && data.gameId !== options.gameId) return;
|
|
6446
|
+
if (data.type === GAME_MESSAGE.ready || data.type === GAME_MESSAGE.refresh) {
|
|
6447
|
+
void send();
|
|
6448
|
+
}
|
|
6449
|
+
};
|
|
6450
|
+
host.addEventListener("message", onMessage);
|
|
6451
|
+
return {
|
|
6452
|
+
refresh: () => send(),
|
|
6453
|
+
destroy: () => {
|
|
6454
|
+
destroyed = true;
|
|
6455
|
+
host.removeEventListener("message", onMessage);
|
|
6456
|
+
if (timer) clearTimeout(timer);
|
|
6457
|
+
timer = null;
|
|
6458
|
+
}
|
|
6459
|
+
};
|
|
6460
|
+
}
|
|
6461
|
+
};
|
|
6462
|
+
function receiveGameTokenFromHost(options, makeScope) {
|
|
6463
|
+
const allowed = options?.allowedParentOrigins ?? [];
|
|
6464
|
+
if (!Array.isArray(allowed) || allowed.length === 0) {
|
|
6465
|
+
return Promise.reject(
|
|
6466
|
+
new Error(
|
|
6467
|
+
"cb.game.fromHost: allowedParentOrigins \uAC00 \uD544\uC694\uD569\uB2C8\uB2E4. \uD5C8\uBE0C origin \uC744 \uC815\uD655\uD788 \uC801\uC73C\uC138\uC694."
|
|
6468
|
+
)
|
|
6469
|
+
);
|
|
6470
|
+
}
|
|
6471
|
+
for (const o of allowed) {
|
|
6472
|
+
if (!isExactOrigin(o)) {
|
|
6473
|
+
return Promise.reject(
|
|
6474
|
+
new Error(
|
|
6475
|
+
`cb.game.fromHost: '${o}' \uB294 \uC815\uD655\uD55C origin \uC774 \uC544\uB2D9\uB2C8\uB2E4('*', \uACBD\uB85C \uBD88\uAC00).`
|
|
6476
|
+
)
|
|
6477
|
+
);
|
|
6478
|
+
}
|
|
6479
|
+
}
|
|
6480
|
+
if (typeof window === "undefined") {
|
|
6481
|
+
return Promise.reject(
|
|
6482
|
+
new Error("cb.game.fromHost: \uBE0C\uB77C\uC6B0\uC800 iframe \uC548\uC5D0\uC11C\uB9CC \uC4F8 \uC218 \uC788\uC2B5\uB2C8\uB2E4.")
|
|
6483
|
+
);
|
|
6484
|
+
}
|
|
6485
|
+
const self = window;
|
|
6486
|
+
const parent = self.parent;
|
|
6487
|
+
if (!parent || parent === self) {
|
|
6488
|
+
return Promise.reject(
|
|
6489
|
+
new Error("cb.game.fromHost: iframe \uC548\uC774 \uC544\uB2D9\uB2C8\uB2E4(\uBD80\uBAA8 \uCC3D\uC774 \uC5C6\uC2B5\uB2C8\uB2E4).")
|
|
6490
|
+
);
|
|
6491
|
+
}
|
|
6492
|
+
return new Promise((resolve, reject) => {
|
|
6493
|
+
let scope = null;
|
|
6494
|
+
let hostOrigin = "";
|
|
6495
|
+
let settled = false;
|
|
6496
|
+
let readyTimer;
|
|
6497
|
+
let timeout;
|
|
6498
|
+
const sendToParent = (type, origins) => {
|
|
6499
|
+
for (const o of origins) {
|
|
6500
|
+
try {
|
|
6501
|
+
parent.postMessage({ type, gameId: options.gameId }, o);
|
|
6502
|
+
} catch {
|
|
6503
|
+
}
|
|
6504
|
+
}
|
|
6505
|
+
};
|
|
6506
|
+
const cleanup = () => {
|
|
6507
|
+
self.removeEventListener("message", onMessage);
|
|
6508
|
+
clearInterval(readyTimer);
|
|
6509
|
+
clearTimeout(timeout);
|
|
6510
|
+
};
|
|
6511
|
+
const onMessage = (e) => {
|
|
6512
|
+
if (e.source !== parent) return;
|
|
6513
|
+
if (!allowed.includes(e.origin)) return;
|
|
6514
|
+
if (hostOrigin && e.origin !== hostOrigin) return;
|
|
6515
|
+
const d = e.data;
|
|
6516
|
+
if (!d || typeof d !== "object" || d.type !== GAME_MESSAGE.token) return;
|
|
6517
|
+
if (typeof d.token !== "string" || d.token === "") return;
|
|
6518
|
+
if (typeof d.gameId !== "string" || d.gameId === "") return;
|
|
6519
|
+
if (options.gameId && d.gameId !== options.gameId) return;
|
|
6520
|
+
const grant = {
|
|
6521
|
+
token: d.token,
|
|
6522
|
+
playerId: typeof d.playerId === "string" ? d.playerId : void 0,
|
|
6523
|
+
playerIdMode: d.playerIdMode === "anonymous" || d.playerIdMode === "member" ? d.playerIdMode : void 0,
|
|
6524
|
+
expiresAt: typeof d.expiresAt === "number" ? d.expiresAt : void 0
|
|
6525
|
+
};
|
|
6526
|
+
if (scope) {
|
|
6527
|
+
if (d.gameId !== scope.gameId) return;
|
|
6528
|
+
scope.setToken(grant);
|
|
6529
|
+
return;
|
|
6530
|
+
}
|
|
6531
|
+
hostOrigin = e.origin;
|
|
6532
|
+
try {
|
|
6533
|
+
scope = makeScope(d.gameId, {
|
|
6534
|
+
token: grant.token,
|
|
6535
|
+
playerId: grant.playerId,
|
|
6536
|
+
playerIdMode: grant.playerIdMode,
|
|
6537
|
+
expiresAt: grant.expiresAt,
|
|
6538
|
+
onTokenExpiring: () => {
|
|
6539
|
+
sendToParent(GAME_MESSAGE.refresh, [hostOrigin]);
|
|
6540
|
+
return void 0;
|
|
6541
|
+
}
|
|
6542
|
+
});
|
|
6543
|
+
} catch (err) {
|
|
6544
|
+
settled = true;
|
|
6545
|
+
cleanup();
|
|
6546
|
+
reject(err);
|
|
6547
|
+
return;
|
|
6548
|
+
}
|
|
6549
|
+
settled = true;
|
|
6550
|
+
clearInterval(readyTimer);
|
|
6551
|
+
clearTimeout(timeout);
|
|
6552
|
+
scope._onDispose(() => self.removeEventListener("message", onMessage));
|
|
6553
|
+
resolve(scope);
|
|
6554
|
+
};
|
|
6555
|
+
self.addEventListener("message", onMessage);
|
|
6556
|
+
sendToParent(GAME_MESSAGE.ready, allowed);
|
|
6557
|
+
readyTimer = setInterval(() => {
|
|
6558
|
+
if (!settled) sendToParent(GAME_MESSAGE.ready, allowed);
|
|
6559
|
+
}, 1e3);
|
|
6560
|
+
timeout = setTimeout(() => {
|
|
6561
|
+
if (settled) return;
|
|
6562
|
+
settled = true;
|
|
6563
|
+
cleanup();
|
|
6564
|
+
reject(
|
|
6565
|
+
new Error(
|
|
6566
|
+
"cb.game.fromHost: \uD5C8\uBE0C\uC5D0\uC11C \uAC8C\uC784 \uD1A0\uD070\uC744 \uBC1B\uC9C0 \uBABB\uD588\uC2B5\uB2C8\uB2E4. \uD5C8\uBE0C\uAC00 cb.game.hub.mount \uB97C \uBD88\uB800\uB294\uC9C0, origin \uC774 \uB9DE\uB294\uC9C0 \uD655\uC778\uD558\uC138\uC694."
|
|
6567
|
+
)
|
|
6568
|
+
);
|
|
6569
|
+
}, options.timeoutMs ?? 15e3);
|
|
6570
|
+
});
|
|
6571
|
+
}
|
|
6572
|
+
function requireServerCredential(http, method) {
|
|
6573
|
+
if (!http.hasJWT() && !http.hasSecretKey()) {
|
|
6574
|
+
throw new Error(
|
|
6575
|
+
`${method} \uB294 \uC2DC\uD06C\uB9BF \uD0A4(cb_sk_) \uB610\uB294 \uD568\uC218\uC758 ctx.cbAdmin(management_scopes: ["game:manage"]) \uC774 \uD544\uC694\uD569\uB2C8\uB2E4. Public Key(cb_pk_) \uB2E8\uB3C5 SDK \uC778\uC2A4\uD134\uC2A4\uB85C\uB294 \uD638\uCD9C\uD560 \uC218 \uC5C6\uC2B5\uB2C8\uB2E4 \u2014 \uD5C8\uBE0C \uC11C\uBC84\uB098 \uD568\uC218\uC5D0\uC11C \uC4F0\uC138\uC694.`
|
|
6576
|
+
);
|
|
6577
|
+
}
|
|
6578
|
+
}
|
|
6579
|
+
async function issueGameToken(http, options) {
|
|
6580
|
+
requireServerCredential(http, "cb.game.issueToken()");
|
|
6581
|
+
if (!options?.accessToken)
|
|
6582
|
+
throw new Error(
|
|
6583
|
+
"cb.game.issueToken: accessToken(\uD68C\uC6D0 \uD1A0\uD070) \uC774 \uD544\uC694\uD569\uB2C8\uB2E4."
|
|
6584
|
+
);
|
|
6585
|
+
if (!options.gameId)
|
|
6586
|
+
throw new Error("cb.game.issueToken: gameId \uAC00 \uD544\uC694\uD569\uB2C8\uB2E4.");
|
|
6587
|
+
const body = {
|
|
6588
|
+
access_token: options.accessToken,
|
|
6589
|
+
game_id: options.gameId
|
|
6590
|
+
};
|
|
6591
|
+
if (options.ttlSeconds !== void 0) body.ttl_seconds = options.ttlSeconds;
|
|
6592
|
+
return http.post("/v1/public/game/tokens", body);
|
|
6593
|
+
}
|
|
6594
|
+
var GamesAPI = class {
|
|
6595
|
+
constructor(http) {
|
|
6596
|
+
this.http = http;
|
|
6597
|
+
this.creatorTokens = {
|
|
6598
|
+
list: async (gameKey) => {
|
|
6599
|
+
this.guard("cb.games.creatorTokens.list()");
|
|
6600
|
+
const res = await this.http.get(
|
|
6601
|
+
`${this.path(gameKey)}/creator-tokens`
|
|
6602
|
+
);
|
|
6603
|
+
return res?.tokens ?? [];
|
|
6604
|
+
},
|
|
6605
|
+
issue: async (gameKey, request) => {
|
|
6606
|
+
this.guard("cb.games.creatorTokens.issue()");
|
|
6607
|
+
return this.http.post(
|
|
6608
|
+
`${this.path(gameKey)}/creator-tokens`,
|
|
6609
|
+
request ?? {}
|
|
6610
|
+
);
|
|
6611
|
+
},
|
|
6612
|
+
revoke: async (gameKey, tokenId) => {
|
|
6613
|
+
this.guard("cb.games.creatorTokens.revoke()");
|
|
6614
|
+
await this.http.delete(
|
|
6615
|
+
`${this.path(gameKey)}/creator-tokens/${encodeSeg(tokenId)}`
|
|
6616
|
+
);
|
|
6617
|
+
}
|
|
6618
|
+
};
|
|
6619
|
+
}
|
|
6620
|
+
/** 게임 목록(삭제 중 포함, 최근 순). */
|
|
6621
|
+
async list() {
|
|
6622
|
+
this.guard("cb.games.list()");
|
|
6623
|
+
const res = await this.http.get("/v1/public/games");
|
|
6624
|
+
return res?.games ?? [];
|
|
6625
|
+
}
|
|
6626
|
+
/** 게임 하나. 없으면 404 `game_not_found`. */
|
|
6627
|
+
async get(gameKey) {
|
|
6628
|
+
this.guard("cb.games.get()");
|
|
6629
|
+
return this.http.get(this.path(gameKey));
|
|
6630
|
+
}
|
|
6631
|
+
/** 게임 등록. game_key = Lua 스크립트 이름. */
|
|
6632
|
+
async create(request) {
|
|
6633
|
+
this.guard("cb.games.create()");
|
|
6634
|
+
return this.http.post("/v1/public/games", request);
|
|
6635
|
+
}
|
|
6636
|
+
/** 게임 수정. 스토리지를 바꾸거나 떼면 그 게임의 제작자 토큰이 모두 회수된다. */
|
|
6637
|
+
async update(gameKey, request) {
|
|
6638
|
+
this.guard("cb.games.update()");
|
|
6639
|
+
return this.http.patch(this.path(gameKey), request);
|
|
6640
|
+
}
|
|
6641
|
+
/** 강제 비활성화. 게임 토큰, 연결, 룸, 사이트, 제작자 토큰이 한 번에 막힌다. */
|
|
6642
|
+
async disable(gameKey, reason) {
|
|
6643
|
+
this.guard("cb.games.disable()");
|
|
6644
|
+
return this.http.post(
|
|
6645
|
+
`${this.path(gameKey)}/disable`,
|
|
6646
|
+
reason === void 0 ? {} : { reason }
|
|
6647
|
+
);
|
|
6648
|
+
}
|
|
6649
|
+
/** 다시 켜기. 데이터와 스크립트는 그대로 돌아온다. */
|
|
6650
|
+
async enable(gameKey) {
|
|
6651
|
+
this.guard("cb.games.enable()");
|
|
6652
|
+
return this.http.post(`${this.path(gameKey)}/enable`, {});
|
|
6653
|
+
}
|
|
6654
|
+
/**
|
|
6655
|
+
* 회원 id 와 player id 를 서로 찾는다(최대 100개). 허브 화면에 닉네임을 보여 줄 때 쓴다.
|
|
6656
|
+
*
|
|
6657
|
+
* @example
|
|
6658
|
+
* const { members } = await ctx.cbAdmin.games.resolvePlayers('space-shooter', { playerIds: top.entries.map(e => e.member) })
|
|
6659
|
+
*/
|
|
6660
|
+
async resolvePlayers(gameKey, ids) {
|
|
6661
|
+
this.guard("cb.games.resolvePlayers()");
|
|
6662
|
+
const body = "memberIds" in ids ? { member_ids: ids.memberIds } : { player_ids: ids.playerIds };
|
|
6663
|
+
return this.http.post(
|
|
6664
|
+
`${this.path(gameKey)}/players/resolve`,
|
|
6665
|
+
body
|
|
6666
|
+
);
|
|
6667
|
+
}
|
|
6668
|
+
/**
|
|
6669
|
+
* 스크립트 버전 활성화(제작자 초안 승인). 스크립트 이름은 game_key 와 같아야 한다.
|
|
6670
|
+
* version 을 생략하면 최신 버전.
|
|
6671
|
+
*/
|
|
6672
|
+
async activateScript(gameKey, options) {
|
|
6673
|
+
this.guard("cb.games.activateScript()");
|
|
6674
|
+
const name = options?.name ?? gameKey;
|
|
6675
|
+
return this.http.post(
|
|
6676
|
+
`${this.path(gameKey)}/scripts/${encodeSeg(name)}/activate`,
|
|
6677
|
+
options?.version === void 0 ? {} : { version: options.version }
|
|
6678
|
+
);
|
|
6679
|
+
}
|
|
6680
|
+
path(gameKey) {
|
|
6681
|
+
if (!gameKey) throw new Error("cb.games: gameKey \uAC00 \uD544\uC694\uD569\uB2C8\uB2E4.");
|
|
6682
|
+
return `/v1/public/games/${encodeSeg(gameKey)}`;
|
|
6683
|
+
}
|
|
6684
|
+
guard(method) {
|
|
6685
|
+
requireServerCredential(this.http, method);
|
|
6686
|
+
}
|
|
6687
|
+
};
|
|
6688
|
+
|
|
6069
6689
|
// src/api/game.ts
|
|
6070
6690
|
function parseGameError(msg) {
|
|
6071
6691
|
const payload = msg.data ?? msg;
|
|
@@ -6271,6 +6891,12 @@ var GameRoom = class {
|
|
|
6271
6891
|
this.ws.addEventListener("message", onMessage);
|
|
6272
6892
|
});
|
|
6273
6893
|
}
|
|
6894
|
+
/**
|
|
6895
|
+
* 다음 연결과 재연결부터 쓸 액세스 토큰을 바꾼다(게임 토큰 갱신용). 이미 열린 연결은 그대로 둔다.
|
|
6896
|
+
*/
|
|
6897
|
+
setAccessToken(token) {
|
|
6898
|
+
this.config.accessToken = token;
|
|
6899
|
+
}
|
|
6274
6900
|
/**
|
|
6275
6901
|
* 연결 해제
|
|
6276
6902
|
*/
|
|
@@ -6773,6 +7399,69 @@ var GameAPI = class {
|
|
|
6773
7399
|
this.gameServerUrl = gameServerUrl || getDefaultGameServerUrl().replace(/^ws/, "http");
|
|
6774
7400
|
this.appId = appId;
|
|
6775
7401
|
this.config = new GameConfigAPI(http, appId);
|
|
7402
|
+
this.hub = new GameHubAPI();
|
|
7403
|
+
}
|
|
7404
|
+
/**
|
|
7405
|
+
* 게임 하나로 범위를 고정한 클라이언트를 만든다(앱 안의 게임 단위).
|
|
7406
|
+
*
|
|
7407
|
+
* 모든 요청에 `Authorization: Bearer <게임 토큰>` 을 고정해서 보내고, 리더보드 member 와
|
|
7408
|
+
* 매치큐 ticket id 는 `playerId` 로 자동으로 채운다. SDK 로그인 토큰으로 바꿔 끼우지 않는다.
|
|
7409
|
+
*
|
|
7410
|
+
* @example
|
|
7411
|
+
* ```ts
|
|
7412
|
+
* const game = cb.game.forGame('space-shooter', { token, playerId, expiresAt,
|
|
7413
|
+
* onTokenExpiring: () => fetchNewTokenFromHub() })
|
|
7414
|
+
* await game.leaderboard.submit('ranking', 120, { mode: 'incr' })
|
|
7415
|
+
* const room = game.createClient({ clientId: game.playerId })
|
|
7416
|
+
* ```
|
|
7417
|
+
*/
|
|
7418
|
+
forGame(gameId, options) {
|
|
7419
|
+
return new GameScope(
|
|
7420
|
+
{
|
|
7421
|
+
gameServerUrl: this.gameServerUrl,
|
|
7422
|
+
publicKey: this.http.getPublicKey(),
|
|
7423
|
+
defaultAppId: this.appId,
|
|
7424
|
+
createRoom: (cfg) => this.createClient(cfg)
|
|
7425
|
+
},
|
|
7426
|
+
gameId,
|
|
7427
|
+
options
|
|
7428
|
+
);
|
|
7429
|
+
}
|
|
7430
|
+
/**
|
|
7431
|
+
* iframe 안 게임이 허브(부모 창)에서 게임 토큰을 받아 게임 범위 클라이언트를 만든다.
|
|
7432
|
+
*
|
|
7433
|
+
* `allowedParentOrigins` 에 없는 origin 이나 부모가 아닌 창에서 온 메시지는 받지 않는다. 토큰은
|
|
7434
|
+
* 메모리에만 두고, 만료 2분 전에 허브에 새 토큰을 요청한다.
|
|
7435
|
+
*
|
|
7436
|
+
* @example
|
|
7437
|
+
* ```ts
|
|
7438
|
+
* const game = await cb.game.fromHost({ allowedParentOrigins: ['https://hub.example.com'] })
|
|
7439
|
+
* await game.kv.set('save', { stage: 3 })
|
|
7440
|
+
* ```
|
|
7441
|
+
*/
|
|
7442
|
+
fromHost(options) {
|
|
7443
|
+
return receiveGameTokenFromHost(
|
|
7444
|
+
options,
|
|
7445
|
+
(gameId, opts) => this.forGame(gameId, opts)
|
|
7446
|
+
);
|
|
7447
|
+
}
|
|
7448
|
+
/**
|
|
7449
|
+
* 회원 토큰을 게임 하나로 축소한 게임 토큰을 발급한다(서버 전용).
|
|
7450
|
+
*
|
|
7451
|
+
* 시크릿 키 SDK 또는 함수의 `ctx.cbAdmin`(management_scopes `game:manage`) 에서만 부른다.
|
|
7452
|
+
* 응답의 `app_member_id` 는 게임에 넘기지 않는다.
|
|
7453
|
+
*
|
|
7454
|
+
* @example
|
|
7455
|
+
* ```ts
|
|
7456
|
+
* export default async (payload, ctx) => {
|
|
7457
|
+
* const accessToken = await ctx.cb.auth.getAccessToken()
|
|
7458
|
+
* const t = await ctx.cbAdmin.game.issueToken({ accessToken, gameId: payload.gameId, ttlSeconds: 3600 })
|
|
7459
|
+
* return { access_token: t.access_token, player_id: t.player_id, player_id_mode: t.player_id_mode, expires_in: t.expires_in, game_id: t.game_id }
|
|
7460
|
+
* }
|
|
7461
|
+
* ```
|
|
7462
|
+
*/
|
|
7463
|
+
issueToken(options) {
|
|
7464
|
+
return issueGameToken(this.http, options);
|
|
6776
7465
|
}
|
|
6777
7466
|
/**
|
|
6778
7467
|
* 게임 룸 클라이언트 생성.
|
|
@@ -6789,7 +7478,10 @@ var GameAPI = class {
|
|
|
6789
7478
|
// config.appId 를 무조건 덮어써서 호출별 지정이 불가능했다.
|
|
6790
7479
|
appId: config.appId ?? this.appId,
|
|
6791
7480
|
publicKey: this.http.getPublicKey(),
|
|
6792
|
-
|
|
7481
|
+
// 호출자가 준 토큰이 먼저다. 게임 하나로 축소한 멤버 토큰(POST /v1/public/game/tokens)을
|
|
7482
|
+
// 이 룸 연결에만 쓰려면 여기로 넘긴다. 예전엔 타입에 있는 이 값을 SDK 로그인 토큰이
|
|
7483
|
+
// 조용히 덮어써서 축소 토큰을 SDK 로 쓸 수 없었다.
|
|
7484
|
+
accessToken: config.accessToken ?? this.http.getAccessToken()
|
|
6793
7485
|
});
|
|
6794
7486
|
}
|
|
6795
7487
|
/**
|
|
@@ -7089,6 +7781,9 @@ var GameAPI = class {
|
|
|
7089
7781
|
}
|
|
7090
7782
|
/**
|
|
7091
7783
|
* 매칭 큐에서 ticket 제거 (예: 사용자가 매칭 취소).
|
|
7784
|
+
*
|
|
7785
|
+
* 자기 티켓만 취소된다 (등록 때 서버가 박은 `attributes.member_id` 로 가림). 남의 티켓은 403
|
|
7786
|
+
* `forbidden_member` 이다.
|
|
7092
7787
|
*/
|
|
7093
7788
|
async cancelMatch(appId, queueKey, ticketId) {
|
|
7094
7789
|
await this.gameFetch(
|
|
@@ -7153,6 +7848,10 @@ var GameAPI = class {
|
|
|
7153
7848
|
}
|
|
7154
7849
|
/**
|
|
7155
7850
|
* leaderboard 전체 reset (시즌 종료 시).
|
|
7851
|
+
*
|
|
7852
|
+
* 앱 소유자 동작이다. 회원 토큰과 공개키로는 403 `forbidden` 이다 (회원 하나가 앱 전체 랭킹을
|
|
7853
|
+
* 지울 수 있었기 때문). 서버 Lua 의 `leaderboard.reset` 이나 콘솔에서 하고, 새 시즌은 key
|
|
7854
|
+
* 접미사(`ranks:2026q3`)로 시작한다.
|
|
7156
7855
|
*/
|
|
7157
7856
|
async resetLeaderboard(appId, leaderboardKey) {
|
|
7158
7857
|
await this.gameFetch(
|
|
@@ -7163,7 +7862,8 @@ var GameAPI = class {
|
|
|
7163
7862
|
);
|
|
7164
7863
|
}
|
|
7165
7864
|
/**
|
|
7166
|
-
*
|
|
7865
|
+
* 자기 기록 제거 (탈퇴 등). 회원은 자기 member id 만 지울 수 있고, 남의 기록은 403
|
|
7866
|
+
* `forbidden_member` 이다. 남의 기록은 서버 Lua 의 `leaderboard.remove` 로 지운다.
|
|
7167
7867
|
*/
|
|
7168
7868
|
async removeFromLeaderboard(appId, leaderboardKey, member) {
|
|
7169
7869
|
await this.gameFetch(
|
|
@@ -9792,11 +10492,37 @@ var PaymentAPI = class {
|
|
|
9792
10492
|
params.set("limit", String(options.limit));
|
|
9793
10493
|
if (options?.offset !== void 0)
|
|
9794
10494
|
params.set("offset", String(options.offset));
|
|
10495
|
+
if (options?.gameKey) params.set("game_key", options.gameKey);
|
|
9795
10496
|
const qs = params.toString();
|
|
9796
10497
|
return this.http.get(
|
|
9797
10498
|
`/v1/apps/${appId}/payments${qs ? `?${qs}` : ""}`
|
|
9798
10499
|
);
|
|
9799
10500
|
}
|
|
10501
|
+
/**
|
|
10502
|
+
* 결제 합계를 게임(game_key)별로 묶어 돌려준다(통화별, 테스트 결제 제외).
|
|
10503
|
+
*
|
|
10504
|
+
* 서버사이드 전용 — service_role(ctx.cbAdmin, management_scopes=["payment:read"]) 또는 콘솔 JWT.
|
|
10505
|
+
* 대상은 `approved_at` 이 기간 안이고 상태가 done, partial_canceled, canceled 인 결제다.
|
|
10506
|
+
*
|
|
10507
|
+
* @example
|
|
10508
|
+
* ```typescript
|
|
10509
|
+
* const { groups } = await ctx.cbAdmin.payment.summary(ctx.appId, { groupBy: 'game_key', from: '2026-11-01', to: '2026-11-30' })
|
|
10510
|
+
* ```
|
|
10511
|
+
*/
|
|
10512
|
+
async summary(appId, options) {
|
|
10513
|
+
if (this.http.hasPublicKey() && !this.http.hasJWT()) {
|
|
10514
|
+
throw new Error(
|
|
10515
|
+
'cb.payment.summary() \uB294 \uCF58\uC194 JWT \uB610\uB294 service_role(ctx.cbAdmin, management_scopes: ["payment:read"]) \uC778\uC99D\uC774 \uD544\uC694\uD569\uB2C8\uB2E4. Public Key(cb_pk_) \uB2E8\uB3C5 SDK \uC778\uC2A4\uD134\uC2A4\uB85C\uB294 \uD638\uCD9C\uD560 \uC218 \uC5C6\uC2B5\uB2C8\uB2E4 \u2014 Functions \uD658\uACBD\uC5D0\uC11C \uC0AC\uC6A9\uD558\uC138\uC694.'
|
|
10516
|
+
);
|
|
10517
|
+
}
|
|
10518
|
+
const params = new URLSearchParams();
|
|
10519
|
+
params.set("group_by", options?.groupBy ?? "game_key");
|
|
10520
|
+
if (options?.from) params.set("from", options.from);
|
|
10521
|
+
if (options?.to) params.set("to", options.to);
|
|
10522
|
+
return this.http.get(
|
|
10523
|
+
`/v1/apps/${appId}/payments/summary?${params.toString()}`
|
|
10524
|
+
);
|
|
10525
|
+
}
|
|
9800
10526
|
};
|
|
9801
10527
|
|
|
9802
10528
|
// src/api/public-key.ts
|
|
@@ -13886,8 +14612,8 @@ var SupportAPI = class {
|
|
|
13886
14612
|
}
|
|
13887
14613
|
};
|
|
13888
14614
|
function detectSdkVersion() {
|
|
13889
|
-
if ("6.
|
|
13890
|
-
return "6.
|
|
14615
|
+
if ("6.27.0") {
|
|
14616
|
+
return "6.27.0";
|
|
13891
14617
|
}
|
|
13892
14618
|
return "unknown";
|
|
13893
14619
|
}
|
|
@@ -16405,6 +17131,7 @@ var ConnectBase = class {
|
|
|
16405
17131
|
config.gameUrl || env("CB_GAME_URL") || DEFAULT_GAME_URL,
|
|
16406
17132
|
config.appId
|
|
16407
17133
|
);
|
|
17134
|
+
this.games = new GamesAPI(this.http);
|
|
16408
17135
|
this.ads = new AdsAPI(this.http);
|
|
16409
17136
|
this.native = new NativeAPI();
|
|
16410
17137
|
this.knowledge = new KnowledgeAPI(this.http);
|
|
@@ -16486,11 +17213,15 @@ var index_default = ConnectBase;
|
|
|
16486
17213
|
AuthError,
|
|
16487
17214
|
ConnectBase,
|
|
16488
17215
|
EndpointAPI,
|
|
17216
|
+
GAME_MESSAGE,
|
|
16489
17217
|
GameAPI,
|
|
16490
17218
|
GameConfigAPI,
|
|
16491
17219
|
GameError,
|
|
17220
|
+
GameHubAPI,
|
|
16492
17221
|
GameRoom,
|
|
16493
17222
|
GameRoomTransport,
|
|
17223
|
+
GameScope,
|
|
17224
|
+
GamesAPI,
|
|
16494
17225
|
NativeAPI,
|
|
16495
17226
|
OrganizationsAPI,
|
|
16496
17227
|
RealtimeConnectionError,
|