koishipro-core.js 1.2.3 → 1.2.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/README.md CHANGED
@@ -162,6 +162,26 @@ Represents a single duel instance with full lifecycle management.
162
162
  - `endDuel(): void`
163
163
  End the duel and clean up resources.
164
164
 
165
+ **Advance Helpers (Advancors):**
166
+ - `advance(advancor?: Advancor): Generator<OcgcoreProcessResult>`
167
+ Advances the duel processing loop. It repeatedly calls `process()`, and when a response is required, it invokes your advancor. It stops when the duel ends, a retry is requested, or your advancor returns `undefined`.
168
+
169
+ **Example (Advance + Advancor)**
170
+ ```ts
171
+ import { createOcgcoreWrapper, SlientAdvancor } from 'koishipro-core.js';
172
+
173
+ const wrapper = await createOcgcoreWrapper();
174
+ // ...setScriptReader / setCardReader / create duel / start duel...
175
+
176
+ const duel = wrapper.createDuel(1234);
177
+
178
+ for (const result of duel.advance(SlientAdvancor())) {
179
+ if (result.message) {
180
+ console.log(result.message);
181
+ }
182
+ }
183
+ ```
184
+
165
185
  **Card Management:**
166
186
  - `newCard(card: OcgcoreNewCardParams): void`
167
187
  Add a card to the duel.
@@ -267,6 +287,61 @@ for (const { duel, result } of playYrpStep(wrapper, yrpBytes)) {
267
287
  }
268
288
  ```
269
289
 
290
+
291
+ ### Advancors
292
+ Advancors are small response producers. You pass them into `duel.advance(...)` and they auto-generate response bytes for the current message. If multiple advancors are combined, the first one that returns a response wins.
293
+
294
+ **Type**
295
+ ```ts
296
+ import { YGOProMsgResponseBase } from 'ygopro-msg-encode';
297
+
298
+ export type Advancor<T extends YGOProMsgResponseBase = YGOProMsgResponseBase> =
299
+ (message: T) => Uint8Array | null | undefined;
300
+ ```
301
+
302
+ #### `SlientAdvancor()`
303
+ Calls `defaultResponse()` for any message. In practice, this auto-answers optional effect prompts with “do not activate” and is ideal for fast-forwarding.
304
+
305
+ #### `NoEffectAdvancor()`
306
+ Only responds to `SelectChain` when there are **no** chains available, allowing the duel to continue. It does not auto-decline effect prompts. Use this when you want to handle effect prompts yourself.
307
+
308
+ #### `SummonPlaceAdvancor(placeAndPosition?)`
309
+ Auto-selects summon placement (`SelectPlace`) and position (`SelectPosition`). You can pass a partial filter to constrain player/location/sequence/position.
310
+
311
+ #### `SelectCardAdvancor(...filters)`
312
+ Selects cards by matching filters (e.g., code, location, controller). Supports several message types like `SelectCard`, `SelectUnselectCard`, `SelectSum`, `SelectTribute`.
313
+
314
+ #### `StaticAdvancor(items)`
315
+ Returns a fixed sequence of responses you provide. Each call consumes one item.
316
+
317
+ #### `CombinedAdvancor(...advancors)`
318
+ Runs advancors in order and returns the first non-`undefined` response. This is the same combiner used by `advance(...)` internally.
319
+
320
+ #### `MapAdvancor(...handlers)`
321
+ Dispatches by message class. Each handler maps a message type to an advancor function.
322
+
323
+ #### `MapAdvancorHandler(msgClass, cb)`
324
+ Helper for building `MapAdvancor` handler objects.
325
+
326
+ #### `LimitAdvancor(advancor, limit)`
327
+ Wraps an advancor and only allows it to return a response `limit` times.
328
+
329
+ #### `OnceAdvancor(advancor)`
330
+ Shorthand for `LimitAdvancor(advancor, 1)`.
331
+
332
+ #### `PlayerViewAdvancor(player, advancor)`
333
+ Runs the inner advancor only when `responsePlayer()` matches the specified player.
334
+
335
+ #### Composition
336
+ You can combine advancors to form a pipeline:
337
+ ```ts
338
+ duel.advance(
339
+ SlientAdvancor(),
340
+ SummonPlaceAdvancor(),
341
+ SelectCardAdvancor({ code: 28985331 }),
342
+ );
343
+ ```
344
+
270
345
  ### Card Reader
271
346
 
272
347
  #### `SqljsCardReader(...dbs: Database[]): CardReader`
package/dist/index.cjs CHANGED
@@ -29,6 +29,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
29
29
  // index.ts
30
30
  var index_exports = {};
31
31
  __export(index_exports, {
32
+ CombinedAdvancor: () => CombinedAdvancor,
32
33
  DirCardReader: () => DirCardReader,
33
34
  DirReader: () => DirReader,
34
35
  DirScriptReader: () => DirScriptReader,
@@ -36,17 +37,26 @@ __export(index_exports, {
36
37
  LEN_EMPTY: () => LEN_EMPTY,
37
38
  LEN_FAIL: () => LEN_FAIL,
38
39
  LEN_HEADER: () => LEN_HEADER,
40
+ LimitAdvancor: () => LimitAdvancor,
39
41
  MESSAGE_BUFFER_SIZE: () => MESSAGE_BUFFER_SIZE,
42
+ MapAdvancor: () => MapAdvancor,
43
+ MapAdvancorHandler: () => MapAdvancorHandler,
40
44
  MapReader: () => MapReader,
41
45
  MapScriptReader: () => MapScriptReader,
46
+ NoEffectAdvancor: () => NoEffectAdvancor,
42
47
  OcgcoreDuel: () => OcgcoreDuel,
43
48
  OcgcoreDuelOptionFlag: () => OcgcoreDuelOptionFlag,
44
49
  OcgcoreDuelRule: () => OcgcoreDuelRule,
45
50
  OcgcoreMessageType: () => OcgcoreMessageType,
46
51
  OcgcoreWrapper: () => OcgcoreWrapper,
52
+ OnceAdvancor: () => OnceAdvancor,
53
+ PlayerViewAdvancor: () => PlayerViewAdvancor,
47
54
  QUERY_BUFFER_SIZE: () => QUERY_BUFFER_SIZE,
48
55
  REGISTRY_BUFFER_SIZE: () => REGISTRY_BUFFER_SIZE,
56
+ SlientAdvancor: () => SlientAdvancor,
49
57
  SqljsCardReader: () => SqljsCardReader,
58
+ StaticAdvancor: () => StaticAdvancor,
59
+ SummonPlaceAdvancor: () => SummonPlaceAdvancor,
50
60
  ZipReader: () => ZipReader,
51
61
  ZipScriptReader: () => ZipScriptReader,
52
62
  _OcgcoreConstants: () => vendor_exports,
@@ -267,7 +277,7 @@ var OcgcoreDuel = class {
267
277
  break;
268
278
  }
269
279
  if (res.status === 1 && res.raw.length > 0) {
270
- const response = advancor?.();
280
+ const response = advancor?.(res.message);
271
281
  if (!response) {
272
282
  break;
273
283
  }
@@ -1374,12 +1384,125 @@ function testCard(ocgcoreWrapper, ...ids) {
1374
1384
  return logs;
1375
1385
  }
1376
1386
 
1387
+ // src/advancors/slient-advancor.ts
1388
+ var SlientAdvancor = () => {
1389
+ return (msg) => {
1390
+ return msg.defaultResponse();
1391
+ };
1392
+ };
1393
+
1394
+ // src/advancors/static-advancor.ts
1395
+ var import_nfkit = require("nfkit");
1396
+ var StaticAdvancor = (items) => {
1397
+ const _items = (0, import_nfkit.makeArray)(items).slice();
1398
+ return () => _items.shift();
1399
+ };
1400
+
1401
+ // src/advancors/no-effect-advancor.ts
1402
+ var import_ygopro_msg_encode6 = require("ygopro-msg-encode");
1403
+
1404
+ // src/advancors/map-advancor.ts
1405
+ var MapAdvancorHandler = (msgClass, cb) => ({
1406
+ msgClass,
1407
+ cb
1408
+ });
1409
+ var MapAdvancor = (...handlers) => {
1410
+ const handlerMap = /* @__PURE__ */ new Map();
1411
+ for (const handleObj of handlers) {
1412
+ handlerMap.set(handleObj.msgClass, handleObj.cb);
1413
+ }
1414
+ return (msg) => {
1415
+ const cb = handlerMap.get(
1416
+ msg.constructor
1417
+ );
1418
+ if (cb != null) {
1419
+ const res = cb(msg);
1420
+ if (res != null) {
1421
+ return res;
1422
+ }
1423
+ }
1424
+ return void 0;
1425
+ };
1426
+ };
1427
+
1428
+ // src/advancors/no-effect-advancor.ts
1429
+ var NoEffectAdvancor = () => MapAdvancor(
1430
+ MapAdvancorHandler(import_ygopro_msg_encode6.YGOProMsgSelectChain, (msg) => {
1431
+ if (msg.chains.length) {
1432
+ return;
1433
+ }
1434
+ return msg.prepareResponse(void 0);
1435
+ })
1436
+ );
1437
+
1438
+ // src/advancors/combined-advancor.ts
1439
+ var CombinedAdvancor = (...advancors) => {
1440
+ return (msg) => {
1441
+ for (const advancor of advancors) {
1442
+ const res = advancor(msg);
1443
+ if (res != null) {
1444
+ return res;
1445
+ }
1446
+ }
1447
+ return void 0;
1448
+ };
1449
+ };
1450
+
1451
+ // src/advancors/limit-advancor.ts
1452
+ var LimitAdvancor = (a, limit = 1) => {
1453
+ let called = 0;
1454
+ return (msg) => {
1455
+ if (called >= limit) {
1456
+ return void 0;
1457
+ }
1458
+ const res = a(msg);
1459
+ if (res !== void 0) {
1460
+ called++;
1461
+ }
1462
+ return res;
1463
+ };
1464
+ };
1465
+ var OnceAdvancor = (a) => LimitAdvancor(a, 1);
1466
+
1467
+ // src/advancors/player-view-advancor.ts
1468
+ var PlayerViewAdvancor = (player, a) => (msg) => {
1469
+ if (msg.responsePlayer() === player) {
1470
+ return a(msg);
1471
+ }
1472
+ return void 0;
1473
+ };
1474
+
1475
+ // src/advancors/summon-place-advancor.ts
1476
+ var import_ygopro_msg_encode7 = require("ygopro-msg-encode");
1477
+ var SummonPlaceAdvancor = (placeAndPosition = {}) => MapAdvancor(
1478
+ MapAdvancorHandler(import_ygopro_msg_encode7.YGOProMsgSelectPlace, (msg) => {
1479
+ const places = msg.getSelectablePlaces().filter(
1480
+ (p) => (placeAndPosition.player == null || p.player === placeAndPosition.player) && (placeAndPosition.location == null || p.location === placeAndPosition.location) && (placeAndPosition.sequence == null || p.sequence === placeAndPosition.sequence)
1481
+ ).slice(0, msg.count);
1482
+ return msg.prepareResponse(places);
1483
+ }),
1484
+ MapAdvancorHandler(import_ygopro_msg_encode7.YGOProMsgSelectPosition, (msg) => {
1485
+ if (placeAndPosition.position) {
1486
+ return msg.prepareResponse(placeAndPosition.position);
1487
+ }
1488
+ const possiblePositions = msg.positions;
1489
+ for (let i = 0; i < 8; i++) {
1490
+ const value = 1 << i;
1491
+ if ((possiblePositions & value) !== 0) {
1492
+ return msg.prepareResponse(value);
1493
+ }
1494
+ }
1495
+ return void 0;
1496
+ })
1497
+ );
1498
+
1377
1499
  // index.ts
1378
1500
  if (typeof globalThis !== "undefined" && !globalThis.Buffer) {
1379
1501
  globalThis.Buffer = import_buffer.Buffer;
1380
1502
  }
1381
1503
  // Annotate the CommonJS export names for ESM import in node:
1382
1504
  0 && (module.exports = {
1505
+ CombinedAdvancor,
1383
1506
  DirCardReader,
1384
1507
  DirReader,
1385
1508
  DirScriptReader,
@@ -1387,17 +1510,26 @@ if (typeof globalThis !== "undefined" && !globalThis.Buffer) {
1387
1510
  LEN_EMPTY,
1388
1511
  LEN_FAIL,
1389
1512
  LEN_HEADER,
1513
+ LimitAdvancor,
1390
1514
  MESSAGE_BUFFER_SIZE,
1515
+ MapAdvancor,
1516
+ MapAdvancorHandler,
1391
1517
  MapReader,
1392
1518
  MapScriptReader,
1519
+ NoEffectAdvancor,
1393
1520
  OcgcoreDuel,
1394
1521
  OcgcoreDuelOptionFlag,
1395
1522
  OcgcoreDuelRule,
1396
1523
  OcgcoreMessageType,
1397
1524
  OcgcoreWrapper,
1525
+ OnceAdvancor,
1526
+ PlayerViewAdvancor,
1398
1527
  QUERY_BUFFER_SIZE,
1399
1528
  REGISTRY_BUFFER_SIZE,
1529
+ SlientAdvancor,
1400
1530
  SqljsCardReader,
1531
+ StaticAdvancor,
1532
+ SummonPlaceAdvancor,
1401
1533
  ZipReader,
1402
1534
  ZipScriptReader,
1403
1535
  _OcgcoreConstants,