koishipro-core.js 1.1.2 → 1.1.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/dist/index.cjs CHANGED
@@ -60,6 +60,8 @@ __export(index_exports, {
60
60
  SqljsCardReader: () => SqljsCardReader,
61
61
  ZipReader: () => ZipReader,
62
62
  ZipScriptReader: () => ZipScriptReader,
63
+ consumeResponseFromOcgcoreProcess: () => consumeResponseFromOcgcoreProcess,
64
+ createDuelFromYrp: () => createDuelFromYrp,
63
65
  createOcgcoreWrapper: () => createOcgcoreWrapper,
64
66
  createSqljsCardReader: () => createSqljsCardReader,
65
67
  normalizeStartDuelOptions: () => normalizeStartDuelOptions,
@@ -70,6 +72,7 @@ __export(index_exports, {
70
72
  parseRegistryKeys: () => parseRegistryKeys,
71
73
  playYrp: () => playYrp,
72
74
  playYrpStep: () => playYrpStep,
75
+ processYrpDuelStep: () => processYrpDuelStep,
73
76
  testCard: () => testCard
74
77
  });
75
78
  module.exports = __toCommonJS(index_exports);
@@ -562,6 +565,7 @@ var OcgcoreDuel = class {
562
565
  this.duelPtr = duelPtr;
563
566
  this.returnPtr = 0;
564
567
  this.returnSize = 512;
568
+ this.ended = false;
565
569
  }
566
570
  startDuel(options) {
567
571
  if (!this.returnPtr) {
@@ -571,6 +575,8 @@ var OcgcoreDuel = class {
571
575
  this.ocgcoreWrapper.ocgcoreModule._start_duel(this.duelPtr, optionValue);
572
576
  }
573
577
  endDuel() {
578
+ if (this.ended) return;
579
+ this.ended = true;
574
580
  this.ocgcoreWrapper.ocgcoreModule._end_duel(this.duelPtr);
575
581
  if (this.returnPtr) {
576
582
  this.ocgcoreWrapper.free(this.returnPtr);
@@ -691,6 +697,10 @@ var OcgcoreDuel = class {
691
697
  this.ocgcoreWrapper.ocgcoreModule._set_responsei(this.duelPtr, value);
692
698
  }
693
699
  setResponse(response) {
700
+ if (typeof response === "number") {
701
+ this.setResponseInt(response);
702
+ return;
703
+ }
694
704
  if (response.length > this.returnSize) {
695
705
  this.ocgcoreWrapper.free(this.returnPtr);
696
706
  this.returnPtr = this.ocgcoreWrapper.malloc(response.length);
@@ -2376,31 +2386,29 @@ async function safeReadDir2(fs, dirPath) {
2376
2386
  }
2377
2387
  }
2378
2388
  async function collectFsDbPaths(fs, pathMod, baseDir) {
2379
- const results = [];
2380
- const baseDb = joinPath3(pathMod, baseDir, "cards.cdb");
2381
- try {
2382
- const stats = await fs.promises.stat(baseDb);
2383
- if (stats.isFile()) {
2384
- results.push(baseDb);
2385
- }
2386
- } catch {
2387
- }
2388
- const expansionsDir = joinPath3(pathMod, baseDir, "expansions");
2389
- const entries = await safeReadDir2(fs, expansionsDir);
2390
- for (const entry of entries) {
2391
- if (!entry.toLowerCase().endsWith(".cdb")) {
2392
- continue;
2393
- }
2394
- const fullPath = joinPath3(pathMod, expansionsDir, entry);
2395
- try {
2396
- const stats = await fs.promises.stat(fullPath);
2397
- if (stats.isFile()) {
2398
- results.push(fullPath);
2389
+ const collectCdbFiles = async (dirPath) => {
2390
+ const paths = [];
2391
+ const entries = await safeReadDir2(fs, dirPath);
2392
+ for (const entry of entries) {
2393
+ if (!entry.toLowerCase().endsWith(".cdb")) {
2394
+ continue;
2395
+ }
2396
+ const fullPath = joinPath3(pathMod, dirPath, entry);
2397
+ try {
2398
+ const stats = await fs.promises.stat(fullPath);
2399
+ if (stats.isFile()) {
2400
+ paths.push(fullPath);
2401
+ }
2402
+ } catch {
2403
+ continue;
2399
2404
  }
2400
- } catch {
2401
- continue;
2402
2405
  }
2403
- }
2406
+ return paths;
2407
+ };
2408
+ const results = [];
2409
+ results.push(...await collectCdbFiles(baseDir));
2410
+ const expansionsDir = joinPath3(pathMod, baseDir, "expansions");
2411
+ results.push(...await collectCdbFiles(expansionsDir));
2404
2412
  return results;
2405
2413
  }
2406
2414
  function isRootCdbEntry(entryName) {
@@ -2457,14 +2465,6 @@ function normalizeYrp(input) {
2457
2465
  }
2458
2466
  return new import_ygopro_yrp_encode.YGOProYrp().fromYrp(input);
2459
2467
  }
2460
- function createReplayDuel(wrapper, yrp) {
2461
- const header = yrp.header;
2462
- const seedSequence = header?.seedSequence ?? [];
2463
- if (seedSequence.length > 0) {
2464
- return wrapper.createDuelV2(seedSequence);
2465
- }
2466
- return wrapper.createDuel(header?.seed ?? 0);
2467
- }
2468
2468
  function loadDeck(duel, deck, owner, player) {
2469
2469
  if (!deck) return;
2470
2470
  for (const code of deck.main ?? []) {
@@ -2508,87 +2508,97 @@ function loadTagDeck(duel, deck, owner) {
2508
2508
  function setRegistryValue(duel, key, value) {
2509
2509
  duel.setRegistryValue(key, value);
2510
2510
  }
2511
- function* playYrpStep(ocgcoreWrapper, yrpInput) {
2511
+ function createDuelFromYrp(wrapper, yrpInput) {
2512
2512
  const yrp = normalizeYrp(yrpInput);
2513
- const responses = yrp.responses.slice();
2514
- const duel = createReplayDuel(ocgcoreWrapper, yrp);
2515
- let ended = false;
2516
- const endDuel = () => {
2517
- if (ended) return;
2518
- duel.endDuel();
2519
- ended = true;
2520
- };
2521
- try {
2522
- setRegistryValue(duel, "duel_mode", yrp.isTag ? "tag" : "single");
2523
- setRegistryValue(duel, "start_lp", String(yrp.startLp));
2524
- setRegistryValue(duel, "start_hand", String(yrp.startHand));
2525
- setRegistryValue(duel, "draw_count", String(yrp.drawCount));
2526
- const playerNames = yrp.isTag ? [
2527
- yrp.hostName,
2528
- yrp.tagHostName ?? "",
2529
- yrp.tagClientName ?? "",
2530
- yrp.clientName
2531
- ] : [yrp.hostName, yrp.clientName];
2532
- for (let i = 0; i < playerNames.length; i++) {
2533
- setRegistryValue(duel, `player_name_${i}`, playerNames[i] ?? "");
2513
+ const header = yrp.header;
2514
+ const seedSequence = header?.seedSequence ?? [];
2515
+ const duel = seedSequence.length > 0 ? wrapper.createDuelV2(seedSequence) : wrapper.createDuel(header?.seed ?? 0);
2516
+ setRegistryValue(duel, "duel_mode", yrp.isTag ? "tag" : "single");
2517
+ setRegistryValue(duel, "start_lp", String(yrp.startLp));
2518
+ setRegistryValue(duel, "start_hand", String(yrp.startHand));
2519
+ setRegistryValue(duel, "draw_count", String(yrp.drawCount));
2520
+ const playerNames = yrp.isTag ? [
2521
+ yrp.hostName,
2522
+ yrp.tagHostName ?? "",
2523
+ yrp.tagClientName ?? "",
2524
+ yrp.clientName
2525
+ ] : [yrp.hostName, yrp.clientName];
2526
+ for (let i = 0; i < playerNames.length; i++) {
2527
+ setRegistryValue(duel, `player_name_${i}`, playerNames[i] ?? "");
2528
+ }
2529
+ setRegistryValue(duel, "player_type_0", "0");
2530
+ setRegistryValue(duel, "player_type_1", "1");
2531
+ duel.setPlayerInfo({
2532
+ player: 0,
2533
+ lp: yrp.startLp,
2534
+ startHand: yrp.startHand,
2535
+ drawCount: yrp.drawCount
2536
+ });
2537
+ duel.setPlayerInfo({
2538
+ player: 1,
2539
+ lp: yrp.startLp,
2540
+ startHand: yrp.startHand,
2541
+ drawCount: yrp.drawCount
2542
+ });
2543
+ duel.preloadScript("./script/patches/entry.lua");
2544
+ duel.preloadScript("./script/special.lua");
2545
+ duel.preloadScript("./script/init.lua");
2546
+ if (yrp.isSingleMode && yrp.singleScript) {
2547
+ duel.preloadScript(`./single/${yrp.singleScript}`);
2548
+ } else if (yrp.isTag) {
2549
+ loadDeck(duel, yrp.hostDeck, 0, 0);
2550
+ loadTagDeck(duel, yrp.tagHostDeck, 0);
2551
+ loadDeck(duel, yrp.clientDeck, 1, 1);
2552
+ loadTagDeck(duel, yrp.tagClientDeck, 1);
2553
+ } else {
2554
+ loadDeck(duel, yrp.hostDeck, 0, 0);
2555
+ loadDeck(duel, yrp.clientDeck, 1, 1);
2556
+ }
2557
+ duel.startDuel(yrp.opt >>> 0);
2558
+ return { yrp, duel };
2559
+ }
2560
+ function consumeResponseFromOcgcoreProcess(duel, result, responses) {
2561
+ if (result.raw.length > 0 && result.raw[0] === OcgcoreCommonConstants.MSG_RETRY) {
2562
+ throw new Error("Got MSG_RETRY");
2563
+ }
2564
+ if (result.status === 0) {
2565
+ return false;
2566
+ }
2567
+ if (result.status === 1) {
2568
+ if (result.raw.length === 0) {
2569
+ return false;
2534
2570
  }
2535
- setRegistryValue(duel, "player_type_0", "0");
2536
- setRegistryValue(duel, "player_type_1", "1");
2537
- duel.setPlayerInfo({
2538
- player: 0,
2539
- lp: yrp.startLp,
2540
- startHand: yrp.startHand,
2541
- drawCount: yrp.drawCount
2542
- });
2543
- duel.setPlayerInfo({
2544
- player: 1,
2545
- lp: yrp.startLp,
2546
- startHand: yrp.startHand,
2547
- drawCount: yrp.drawCount
2548
- });
2549
- duel.preloadScript("./script/patches/entry.lua");
2550
- duel.preloadScript("./script/special.lua");
2551
- duel.preloadScript("./script/init.lua");
2552
- if (yrp.isSingleMode && yrp.singleScript) {
2553
- duel.preloadScript(`./single/${yrp.singleScript}`);
2554
- } else if (yrp.isTag) {
2555
- loadDeck(duel, yrp.hostDeck, 0, 0);
2556
- loadTagDeck(duel, yrp.tagHostDeck, 0);
2557
- loadDeck(duel, yrp.clientDeck, 1, 1);
2558
- loadTagDeck(duel, yrp.tagClientDeck, 1);
2559
- } else {
2560
- loadDeck(duel, yrp.hostDeck, 0, 0);
2561
- loadDeck(duel, yrp.clientDeck, 1, 1);
2571
+ const response = responses.shift();
2572
+ if (!response) {
2573
+ return true;
2562
2574
  }
2563
- duel.startDuel(yrp.opt >>> 0);
2564
- while (true) {
2565
- const result = duel.process();
2566
- yield {
2567
- duel,
2568
- result,
2569
- responses
2570
- };
2571
- if (result.raw.length > 0 && result.raw[0] === OcgcoreCommonConstants.MSG_RETRY) {
2572
- throw new Error("Got MSG_RETRY");
2573
- }
2574
- if (result.status === 0) {
2575
- continue;
2576
- }
2577
- if (result.status === 1) {
2578
- if (result.raw.length === 0) {
2579
- continue;
2580
- }
2581
- const response = responses.shift();
2582
- if (!response) {
2583
- break;
2584
- }
2585
- duel.setResponse(response);
2586
- continue;
2587
- }
2575
+ duel.setResponse(response);
2576
+ return false;
2577
+ }
2578
+ return true;
2579
+ }
2580
+ function* processYrpDuelStep(duel, yrp) {
2581
+ const responses = yrp.responses.slice();
2582
+ while (true) {
2583
+ const result = duel.process();
2584
+ yield {
2585
+ duel,
2586
+ result,
2587
+ responses
2588
+ };
2589
+ if (consumeResponseFromOcgcoreProcess(duel, result, responses)) {
2588
2590
  break;
2589
2591
  }
2592
+ }
2593
+ }
2594
+ function* playYrpStep(ocgcoreWrapper, yrpInput) {
2595
+ const { yrp, duel } = createDuelFromYrp(ocgcoreWrapper, yrpInput);
2596
+ try {
2597
+ for (const stepResult of processYrpDuelStep(duel, yrp)) {
2598
+ yield stepResult;
2599
+ }
2590
2600
  } finally {
2591
- endDuel();
2601
+ duel.endDuel();
2592
2602
  }
2593
2603
  }
2594
2604
  var playYrp = (ocgcoreWrapper, yrpInput) => {
@@ -2671,6 +2681,8 @@ if (typeof globalThis !== "undefined" && !globalThis.Buffer) {
2671
2681
  SqljsCardReader,
2672
2682
  ZipReader,
2673
2683
  ZipScriptReader,
2684
+ consumeResponseFromOcgcoreProcess,
2685
+ createDuelFromYrp,
2674
2686
  createOcgcoreWrapper,
2675
2687
  createSqljsCardReader,
2676
2688
  normalizeStartDuelOptions,
@@ -2681,6 +2693,7 @@ if (typeof globalThis !== "undefined" && !globalThis.Buffer) {
2681
2693
  parseRegistryKeys,
2682
2694
  playYrp,
2683
2695
  playYrpStep,
2696
+ processYrpDuelStep,
2684
2697
  testCard
2685
2698
  });
2686
2699
  //# sourceMappingURL=index.cjs.map