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.mjs CHANGED
@@ -499,6 +499,7 @@ var OcgcoreDuel = class {
499
499
  this.duelPtr = duelPtr;
500
500
  this.returnPtr = 0;
501
501
  this.returnSize = 512;
502
+ this.ended = false;
502
503
  }
503
504
  startDuel(options) {
504
505
  if (!this.returnPtr) {
@@ -508,6 +509,8 @@ var OcgcoreDuel = class {
508
509
  this.ocgcoreWrapper.ocgcoreModule._start_duel(this.duelPtr, optionValue);
509
510
  }
510
511
  endDuel() {
512
+ if (this.ended) return;
513
+ this.ended = true;
511
514
  this.ocgcoreWrapper.ocgcoreModule._end_duel(this.duelPtr);
512
515
  if (this.returnPtr) {
513
516
  this.ocgcoreWrapper.free(this.returnPtr);
@@ -628,6 +631,10 @@ var OcgcoreDuel = class {
628
631
  this.ocgcoreWrapper.ocgcoreModule._set_responsei(this.duelPtr, value);
629
632
  }
630
633
  setResponse(response) {
634
+ if (typeof response === "number") {
635
+ this.setResponseInt(response);
636
+ return;
637
+ }
631
638
  if (response.length > this.returnSize) {
632
639
  this.ocgcoreWrapper.free(this.returnPtr);
633
640
  this.returnPtr = this.ocgcoreWrapper.malloc(response.length);
@@ -2291,31 +2298,29 @@ async function safeReadDir2(fs, dirPath) {
2291
2298
  }
2292
2299
  }
2293
2300
  async function collectFsDbPaths(fs, pathMod, baseDir) {
2294
- const results = [];
2295
- const baseDb = joinPath3(pathMod, baseDir, "cards.cdb");
2296
- try {
2297
- const stats = await fs.promises.stat(baseDb);
2298
- if (stats.isFile()) {
2299
- results.push(baseDb);
2300
- }
2301
- } catch {
2302
- }
2303
- const expansionsDir = joinPath3(pathMod, baseDir, "expansions");
2304
- const entries = await safeReadDir2(fs, expansionsDir);
2305
- for (const entry of entries) {
2306
- if (!entry.toLowerCase().endsWith(".cdb")) {
2307
- continue;
2308
- }
2309
- const fullPath = joinPath3(pathMod, expansionsDir, entry);
2310
- try {
2311
- const stats = await fs.promises.stat(fullPath);
2312
- if (stats.isFile()) {
2313
- results.push(fullPath);
2301
+ const collectCdbFiles = async (dirPath) => {
2302
+ const paths = [];
2303
+ const entries = await safeReadDir2(fs, dirPath);
2304
+ for (const entry of entries) {
2305
+ if (!entry.toLowerCase().endsWith(".cdb")) {
2306
+ continue;
2307
+ }
2308
+ const fullPath = joinPath3(pathMod, dirPath, entry);
2309
+ try {
2310
+ const stats = await fs.promises.stat(fullPath);
2311
+ if (stats.isFile()) {
2312
+ paths.push(fullPath);
2313
+ }
2314
+ } catch {
2315
+ continue;
2314
2316
  }
2315
- } catch {
2316
- continue;
2317
2317
  }
2318
- }
2318
+ return paths;
2319
+ };
2320
+ const results = [];
2321
+ results.push(...await collectCdbFiles(baseDir));
2322
+ const expansionsDir = joinPath3(pathMod, baseDir, "expansions");
2323
+ results.push(...await collectCdbFiles(expansionsDir));
2319
2324
  return results;
2320
2325
  }
2321
2326
  function isRootCdbEntry(entryName) {
@@ -2372,14 +2377,6 @@ function normalizeYrp(input) {
2372
2377
  }
2373
2378
  return new YGOProYrp().fromYrp(input);
2374
2379
  }
2375
- function createReplayDuel(wrapper, yrp) {
2376
- const header = yrp.header;
2377
- const seedSequence = header?.seedSequence ?? [];
2378
- if (seedSequence.length > 0) {
2379
- return wrapper.createDuelV2(seedSequence);
2380
- }
2381
- return wrapper.createDuel(header?.seed ?? 0);
2382
- }
2383
2380
  function loadDeck(duel, deck, owner, player) {
2384
2381
  if (!deck) return;
2385
2382
  for (const code of deck.main ?? []) {
@@ -2423,87 +2420,97 @@ function loadTagDeck(duel, deck, owner) {
2423
2420
  function setRegistryValue(duel, key, value) {
2424
2421
  duel.setRegistryValue(key, value);
2425
2422
  }
2426
- function* playYrpStep(ocgcoreWrapper, yrpInput) {
2423
+ function createDuelFromYrp(wrapper, yrpInput) {
2427
2424
  const yrp = normalizeYrp(yrpInput);
2428
- const responses = yrp.responses.slice();
2429
- const duel = createReplayDuel(ocgcoreWrapper, yrp);
2430
- let ended = false;
2431
- const endDuel = () => {
2432
- if (ended) return;
2433
- duel.endDuel();
2434
- ended = true;
2435
- };
2436
- try {
2437
- setRegistryValue(duel, "duel_mode", yrp.isTag ? "tag" : "single");
2438
- setRegistryValue(duel, "start_lp", String(yrp.startLp));
2439
- setRegistryValue(duel, "start_hand", String(yrp.startHand));
2440
- setRegistryValue(duel, "draw_count", String(yrp.drawCount));
2441
- const playerNames = yrp.isTag ? [
2442
- yrp.hostName,
2443
- yrp.tagHostName ?? "",
2444
- yrp.tagClientName ?? "",
2445
- yrp.clientName
2446
- ] : [yrp.hostName, yrp.clientName];
2447
- for (let i = 0; i < playerNames.length; i++) {
2448
- setRegistryValue(duel, `player_name_${i}`, playerNames[i] ?? "");
2425
+ const header = yrp.header;
2426
+ const seedSequence = header?.seedSequence ?? [];
2427
+ const duel = seedSequence.length > 0 ? wrapper.createDuelV2(seedSequence) : wrapper.createDuel(header?.seed ?? 0);
2428
+ setRegistryValue(duel, "duel_mode", yrp.isTag ? "tag" : "single");
2429
+ setRegistryValue(duel, "start_lp", String(yrp.startLp));
2430
+ setRegistryValue(duel, "start_hand", String(yrp.startHand));
2431
+ setRegistryValue(duel, "draw_count", String(yrp.drawCount));
2432
+ const playerNames = yrp.isTag ? [
2433
+ yrp.hostName,
2434
+ yrp.tagHostName ?? "",
2435
+ yrp.tagClientName ?? "",
2436
+ yrp.clientName
2437
+ ] : [yrp.hostName, yrp.clientName];
2438
+ for (let i = 0; i < playerNames.length; i++) {
2439
+ setRegistryValue(duel, `player_name_${i}`, playerNames[i] ?? "");
2440
+ }
2441
+ setRegistryValue(duel, "player_type_0", "0");
2442
+ setRegistryValue(duel, "player_type_1", "1");
2443
+ duel.setPlayerInfo({
2444
+ player: 0,
2445
+ lp: yrp.startLp,
2446
+ startHand: yrp.startHand,
2447
+ drawCount: yrp.drawCount
2448
+ });
2449
+ duel.setPlayerInfo({
2450
+ player: 1,
2451
+ lp: yrp.startLp,
2452
+ startHand: yrp.startHand,
2453
+ drawCount: yrp.drawCount
2454
+ });
2455
+ duel.preloadScript("./script/patches/entry.lua");
2456
+ duel.preloadScript("./script/special.lua");
2457
+ duel.preloadScript("./script/init.lua");
2458
+ if (yrp.isSingleMode && yrp.singleScript) {
2459
+ duel.preloadScript(`./single/${yrp.singleScript}`);
2460
+ } else if (yrp.isTag) {
2461
+ loadDeck(duel, yrp.hostDeck, 0, 0);
2462
+ loadTagDeck(duel, yrp.tagHostDeck, 0);
2463
+ loadDeck(duel, yrp.clientDeck, 1, 1);
2464
+ loadTagDeck(duel, yrp.tagClientDeck, 1);
2465
+ } else {
2466
+ loadDeck(duel, yrp.hostDeck, 0, 0);
2467
+ loadDeck(duel, yrp.clientDeck, 1, 1);
2468
+ }
2469
+ duel.startDuel(yrp.opt >>> 0);
2470
+ return { yrp, duel };
2471
+ }
2472
+ function consumeResponseFromOcgcoreProcess(duel, result, responses) {
2473
+ if (result.raw.length > 0 && result.raw[0] === OcgcoreCommonConstants.MSG_RETRY) {
2474
+ throw new Error("Got MSG_RETRY");
2475
+ }
2476
+ if (result.status === 0) {
2477
+ return false;
2478
+ }
2479
+ if (result.status === 1) {
2480
+ if (result.raw.length === 0) {
2481
+ return false;
2449
2482
  }
2450
- setRegistryValue(duel, "player_type_0", "0");
2451
- setRegistryValue(duel, "player_type_1", "1");
2452
- duel.setPlayerInfo({
2453
- player: 0,
2454
- lp: yrp.startLp,
2455
- startHand: yrp.startHand,
2456
- drawCount: yrp.drawCount
2457
- });
2458
- duel.setPlayerInfo({
2459
- player: 1,
2460
- lp: yrp.startLp,
2461
- startHand: yrp.startHand,
2462
- drawCount: yrp.drawCount
2463
- });
2464
- duel.preloadScript("./script/patches/entry.lua");
2465
- duel.preloadScript("./script/special.lua");
2466
- duel.preloadScript("./script/init.lua");
2467
- if (yrp.isSingleMode && yrp.singleScript) {
2468
- duel.preloadScript(`./single/${yrp.singleScript}`);
2469
- } else if (yrp.isTag) {
2470
- loadDeck(duel, yrp.hostDeck, 0, 0);
2471
- loadTagDeck(duel, yrp.tagHostDeck, 0);
2472
- loadDeck(duel, yrp.clientDeck, 1, 1);
2473
- loadTagDeck(duel, yrp.tagClientDeck, 1);
2474
- } else {
2475
- loadDeck(duel, yrp.hostDeck, 0, 0);
2476
- loadDeck(duel, yrp.clientDeck, 1, 1);
2483
+ const response = responses.shift();
2484
+ if (!response) {
2485
+ return true;
2477
2486
  }
2478
- duel.startDuel(yrp.opt >>> 0);
2479
- while (true) {
2480
- const result = duel.process();
2481
- yield {
2482
- duel,
2483
- result,
2484
- responses
2485
- };
2486
- if (result.raw.length > 0 && result.raw[0] === OcgcoreCommonConstants.MSG_RETRY) {
2487
- throw new Error("Got MSG_RETRY");
2488
- }
2489
- if (result.status === 0) {
2490
- continue;
2491
- }
2492
- if (result.status === 1) {
2493
- if (result.raw.length === 0) {
2494
- continue;
2495
- }
2496
- const response = responses.shift();
2497
- if (!response) {
2498
- break;
2499
- }
2500
- duel.setResponse(response);
2501
- continue;
2502
- }
2487
+ duel.setResponse(response);
2488
+ return false;
2489
+ }
2490
+ return true;
2491
+ }
2492
+ function* processYrpDuelStep(duel, yrp) {
2493
+ const responses = yrp.responses.slice();
2494
+ while (true) {
2495
+ const result = duel.process();
2496
+ yield {
2497
+ duel,
2498
+ result,
2499
+ responses
2500
+ };
2501
+ if (consumeResponseFromOcgcoreProcess(duel, result, responses)) {
2503
2502
  break;
2504
2503
  }
2504
+ }
2505
+ }
2506
+ function* playYrpStep(ocgcoreWrapper, yrpInput) {
2507
+ const { yrp, duel } = createDuelFromYrp(ocgcoreWrapper, yrpInput);
2508
+ try {
2509
+ for (const stepResult of processYrpDuelStep(duel, yrp)) {
2510
+ yield stepResult;
2511
+ }
2505
2512
  } finally {
2506
- endDuel();
2513
+ duel.endDuel();
2507
2514
  }
2508
2515
  }
2509
2516
  var playYrp = (ocgcoreWrapper, yrpInput) => {
@@ -2585,6 +2592,8 @@ export {
2585
2592
  SqljsCardReader,
2586
2593
  ZipReader,
2587
2594
  ZipScriptReader,
2595
+ consumeResponseFromOcgcoreProcess,
2596
+ createDuelFromYrp,
2588
2597
  createOcgcoreWrapper,
2589
2598
  createSqljsCardReader,
2590
2599
  normalizeStartDuelOptions,
@@ -2595,6 +2604,7 @@ export {
2595
2604
  parseRegistryKeys,
2596
2605
  playYrp,
2597
2606
  playYrpStep,
2607
+ processYrpDuelStep,
2598
2608
  testCard
2599
2609
  };
2600
2610
  //# sourceMappingURL=index.mjs.map