koishipro-core.js 1.0.6 → 1.0.8

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
@@ -19,24 +19,35 @@ npm install koishipro-core.js
19
19
 
20
20
  ## Quick Start
21
21
  ```ts
22
- import { createOcgcoreWrapper, ZipReader, createSqljsCardReader } from 'koishipro-core.js';
22
+ import {
23
+ createOcgcoreWrapper,
24
+ ZipReader,
25
+ DirReader,
26
+ createSqljsCardReader,
27
+ } from 'koishipro-core.js';
23
28
  import initSqlJs from 'sql.js';
24
29
 
25
30
  const wrapper = await createOcgcoreWrapper();
26
31
 
27
- // Provide scripts via zip
32
+ // Provide scripts via zip + local directory fallback (Node only)
28
33
  const zipBytes = await fetch('/script.zip').then((r) => r.arrayBuffer());
29
- wrapper.setScriptReader(await ZipReader(new Uint8Array(zipBytes)));
34
+ wrapper
35
+ .setScriptReader(await ZipReader(new Uint8Array(zipBytes)), true)
36
+ .setScriptReader(DirReader('./ygopro-scripts'));
30
37
 
31
38
  // Provide cards via sql.js
32
39
  const SQL = await initSqlJs();
33
40
  const db = new SQL.Database(await fetch('/cards.cdb').then((r) => r.arrayBuffer()));
34
41
  wrapper.setCardReader(createSqljsCardReader(db));
35
42
 
36
- // Optional: log messages from ocgcore
37
- wrapper.setMessageHandler((_duel, message, type) => {
38
- console.log(type, message);
39
- });
43
+ // Optional: log messages from ocgcore (multiple handlers allowed)
44
+ wrapper
45
+ .setMessageHandler((_duel, message, type) => {
46
+ console.log(type, message);
47
+ }, true)
48
+ .setMessageHandler((_duel, message, type) => {
49
+ if (type === 1) console.error(message);
50
+ });
40
51
 
41
52
  const duel = wrapper.createDuel(1234);
42
53
  duel.setPlayerInfo({ playerId: 0, lp: 8000, startCount: 5, drawCount: 1 });
@@ -74,15 +85,18 @@ console.log('message count:', messages.length);
74
85
  - `createOcgcoreWrapper(options?)`
75
86
  Load the ocgcore WASM module and return an `OcgcoreWrapper`.
76
87
  - `OcgcoreWrapper`
77
- Manages the WASM module, script/card/message handlers, and duel creation.
88
+ Manages the WASM module, script/card/message handlers, and duel creation.
89
+ Script readers and handlers can be registered multiple times (fallback/fan-out).
78
90
  - `OcgcoreDuel`
79
91
  Duel lifecycle and core engine calls (`startDuel`, `process`, `setResponse`, etc.).
80
92
 
81
93
  ### Script Readers
82
- - `MapReader(map)`
83
- Resolve Lua scripts from a `Map<string, string | Uint8Array>`.
84
- - `ZipReader(zipBytes)`
85
- Load all `.lua` files from a zip and expose them via `MapReader`.
94
+ - `MapReader(...maps)`
95
+ Resolve Lua scripts from one or more `Map<string, string | Uint8Array>` with fallback.
96
+ - `ZipReader(...zipBytes)`
97
+ Load all `.lua` files from one or more zips and expose them via `MapReader` (fallback order).
98
+ - `DirReader(...dirs)`
99
+ Node-only directory reader with the same resolution rules (fallback order).
86
100
 
87
101
  ### Replay
88
102
  - `playYrp(wrapper, yrpOrBytes)`
package/dist/index.cjs CHANGED
@@ -54,7 +54,8 @@ __export(index_exports, {
54
54
  parseFieldInfo: () => parseFieldInfo,
55
55
  parseRegistryDump: () => parseRegistryDump,
56
56
  parseRegistryKeys: () => parseRegistryKeys,
57
- playYrp: () => playYrp
57
+ playYrp: () => playYrp,
58
+ testCard: () => testCard
58
59
  });
59
60
  module.exports = __toCommonJS(index_exports);
60
61
  var import_buffer = require("buffer");
@@ -1093,7 +1094,7 @@ var OcgcoreWrapper = class {
1093
1094
  }
1094
1095
  this.ocgcoreModule._get_log_message(duelPtr, this.logBufferPtr);
1095
1096
  const message = this.getUTF8String(this.logBufferPtr);
1096
- const type = messageType === 1 ? "ScriptError" /* ScriptError */ : messageType === 2 ? "DebugMessage" /* DebugMessage */ : messageType;
1097
+ const type = messageType === 2 ? "DebugMessage" /* DebugMessage */ : "ScriptError" /* ScriptError */;
1097
1098
  const duel = this.getOrCreateDuel(duelPtr);
1098
1099
  for (const handler of this.messageHandlers) {
1099
1100
  try {
@@ -2534,6 +2535,49 @@ var playYrp = (ocgcoreWrapper, yrpInput) => {
2534
2535
  return messages;
2535
2536
  };
2536
2537
 
2538
+ // src/test-card.ts
2539
+ var { LOCATION_DECK: LOCATION_DECK2, POS_FACEUP_ATTACK } = OcgcoreScriptConstants;
2540
+ function testCard(ocgcoreWrapper, ...ids) {
2541
+ const logs = [];
2542
+ const duel = ocgcoreWrapper.createDuelV2(
2543
+ Array.from(
2544
+ { length: 8 },
2545
+ () => Math.floor(Math.random() * 4294967295) >>> 0
2546
+ )
2547
+ );
2548
+ ocgcoreWrapper.setMessageHandler((_duel, message, type) => {
2549
+ if (duel.duelPtr !== _duel.duelPtr) return;
2550
+ logs.push({ type, message });
2551
+ });
2552
+ duel.preloadScript("./script/special.lua");
2553
+ duel.preloadScript("./script/init.lua");
2554
+ duel.setPlayerInfo({ playerId: 0, lp: 8e3, startCount: 5, drawCount: 1 });
2555
+ duel.setPlayerInfo({ playerId: 1, lp: 8e3, startCount: 5, drawCount: 1 });
2556
+ for (const code of ids) {
2557
+ duel.newCard({
2558
+ code,
2559
+ owner: 0,
2560
+ playerId: 0,
2561
+ location: LOCATION_DECK2,
2562
+ sequence: 0,
2563
+ position: POS_FACEUP_ATTACK
2564
+ });
2565
+ }
2566
+ duel.startDuel(0);
2567
+ while (true) {
2568
+ const { status, raw } = duel.process();
2569
+ if (raw.length > 0 && raw[0] === OcgcoreCommonConstants.MSG_RETRY) {
2570
+ break;
2571
+ }
2572
+ if (status === 0) {
2573
+ continue;
2574
+ }
2575
+ break;
2576
+ }
2577
+ duel.endDuel();
2578
+ return logs;
2579
+ }
2580
+
2537
2581
  // index.ts
2538
2582
  if (typeof globalThis !== "undefined" && !globalThis.Buffer) {
2539
2583
  globalThis.Buffer = import_buffer.Buffer;
@@ -2565,6 +2609,7 @@ if (typeof globalThis !== "undefined" && !globalThis.Buffer) {
2565
2609
  parseFieldInfo,
2566
2610
  parseRegistryDump,
2567
2611
  parseRegistryKeys,
2568
- playYrp
2612
+ playYrp,
2613
+ testCard
2569
2614
  });
2570
2615
  //# sourceMappingURL=index.cjs.map