koishipro-core.js 1.2.1 → 1.2.3

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
@@ -1,17 +1,8 @@
1
1
  var __defProp = Object.defineProperty;
2
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
2
  var __export = (target, all) => {
4
3
  for (var name in all)
5
4
  __defProp(target, name, { get: all[name], enumerable: true });
6
5
  };
7
- var __decorateClass = (decorators, target, key, kind) => {
8
- var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
9
- for (var i = decorators.length - 1, decorator; i >= 0; i--)
10
- if (decorator = decorators[i])
11
- result = (kind ? decorator(target, key, result) : decorator(result)) || result;
12
- if (kind && result) __defProp(target, key, result);
13
- return result;
14
- };
15
6
 
16
7
  // index.ts
17
8
  import { Buffer as Buffer2 } from "buffer";
@@ -145,7 +136,10 @@ function normalizeStartDuelOptions(options) {
145
136
  }
146
137
 
147
138
  // src/ocgcore-duel.ts
148
- import { YGOProMessages } from "ygopro-msg-encode";
139
+ import {
140
+ YGOProMessages,
141
+ YGOProMsgRetry
142
+ } from "ygopro-msg-encode";
149
143
  var OcgcoreDuel = class {
150
144
  constructor(ocgcoreWrapper, duelPtr) {
151
145
  this.ocgcoreWrapper = ocgcoreWrapper;
@@ -212,6 +206,22 @@ var OcgcoreDuel = class {
212
206
  message: parsedMessage
213
207
  };
214
208
  }
209
+ *advance(advancor) {
210
+ while (true) {
211
+ const res = this.process();
212
+ yield res;
213
+ if (res.status === 2 || res.message instanceof YGOProMsgRetry) {
214
+ break;
215
+ }
216
+ if (res.status === 1 && res.raw.length > 0) {
217
+ const response = advancor?.();
218
+ if (!response) {
219
+ break;
220
+ }
221
+ this.setResponse(response);
222
+ }
223
+ }
224
+ }
215
225
  newCard(card) {
216
226
  this.ocgcoreWrapper.ocgcoreModule._new_card(
217
227
  this.duelPtr,
@@ -383,59 +393,8 @@ var OcgcoreDuel = class {
383
393
  }
384
394
  };
385
395
 
386
- // src/structs/card-data.ts
387
- import {
388
- BinaryField,
389
- fillBinaryFields,
390
- toBinaryFields
391
- } from "ygopro-msg-encode";
392
- var _CardDataStruct = class _CardDataStruct {
393
- static fromBytes(data) {
394
- const obj = new _CardDataStruct();
395
- fillBinaryFields(obj, data, _CardDataStruct);
396
- return obj;
397
- }
398
- toBytes() {
399
- return toBinaryFields(this, _CardDataStruct);
400
- }
401
- };
402
- __decorateClass([
403
- BinaryField("u32", 0)
404
- ], _CardDataStruct.prototype, "code", 2);
405
- __decorateClass([
406
- BinaryField("u32", 4)
407
- ], _CardDataStruct.prototype, "alias", 2);
408
- __decorateClass([
409
- BinaryField("u16", 8, 16)
410
- ], _CardDataStruct.prototype, "setcode", 2);
411
- __decorateClass([
412
- BinaryField("u32", 40)
413
- ], _CardDataStruct.prototype, "type", 2);
414
- __decorateClass([
415
- BinaryField("u32", 44)
416
- ], _CardDataStruct.prototype, "level", 2);
417
- __decorateClass([
418
- BinaryField("u32", 48)
419
- ], _CardDataStruct.prototype, "attribute", 2);
420
- __decorateClass([
421
- BinaryField("u32", 52)
422
- ], _CardDataStruct.prototype, "race", 2);
423
- __decorateClass([
424
- BinaryField("i32", 56)
425
- ], _CardDataStruct.prototype, "attack", 2);
426
- __decorateClass([
427
- BinaryField("i32", 60)
428
- ], _CardDataStruct.prototype, "defense", 2);
429
- __decorateClass([
430
- BinaryField("u32", 64)
431
- ], _CardDataStruct.prototype, "lscale", 2);
432
- __decorateClass([
433
- BinaryField("u32", 68)
434
- ], _CardDataStruct.prototype, "rscale", 2);
435
- __decorateClass([
436
- BinaryField("u32", 72)
437
- ], _CardDataStruct.prototype, "linkMarker", 2);
438
- var CardDataStruct = _CardDataStruct;
396
+ // src/ocgcore-wrapper.ts
397
+ import { CardData } from "ygopro-msg-encode";
439
398
 
440
399
  // src/types/ocgcore-enums.ts
441
400
  var OcgcoreMessageType = /* @__PURE__ */ ((OcgcoreMessageType2) => {
@@ -513,34 +472,7 @@ var OcgcoreWrapper = class {
513
472
  if (!data) {
514
473
  return 0;
515
474
  }
516
- let buf;
517
- if (data instanceof CardDataStruct) {
518
- buf = data.toBytes();
519
- } else {
520
- const cardData = new CardDataStruct();
521
- cardData.code = data.code;
522
- cardData.alias = data.alias;
523
- const targetSetcode = new Uint16Array(16);
524
- targetSetcode.fill(0);
525
- if (data.setcode instanceof Uint16Array && data.setcode.length === 16) {
526
- targetSetcode.set(data.setcode);
527
- } else {
528
- for (let i = 0; i < 16 && i < data.setcode.length; i++) {
529
- targetSetcode[i] = data.setcode[i];
530
- }
531
- }
532
- cardData.setcode = targetSetcode;
533
- cardData.type = data.type;
534
- cardData.level = data.level;
535
- cardData.attribute = data.attribute;
536
- cardData.race = data.race;
537
- cardData.attack = data.attack;
538
- cardData.defense = data.defense;
539
- cardData.lscale = data.lscale;
540
- cardData.rscale = data.rscale;
541
- cardData.linkMarker = data.linkMarker;
542
- buf = cardData.toBytes();
543
- }
475
+ const buf = new CardData().fromPartial(data).toPayload();
544
476
  this.heapU8.set(buf, cardDataPtr);
545
477
  return 0;
546
478
  }, "iii");
@@ -1005,15 +937,13 @@ async function DirScriptReaderEx(...baseDirs) {
1005
937
  }
1006
938
 
1007
939
  // src/card-reader/sqljs-card-reader.ts
1008
- function toUint16ArrayFromSetcode(value) {
940
+ function toNumberArrayFromSetcode(value) {
1009
941
  let raw = typeof value === "bigint" ? value : BigInt(value >>> 0);
1010
- const list = new Uint16Array(16);
1011
- let idx = 0;
1012
- while (raw !== 0n && idx < 16) {
942
+ const list = [];
943
+ while (raw !== 0n && list.length < 16) {
1013
944
  const chunk = raw & 0xffffn;
1014
945
  if (chunk !== 0n) {
1015
- list[idx] = Number(chunk);
1016
- idx++;
946
+ list.push(Number(chunk));
1017
947
  }
1018
948
  raw >>= 16n;
1019
949
  }
@@ -1035,7 +965,7 @@ function mapRowToCardData(row) {
1035
965
  return {
1036
966
  code: row.id,
1037
967
  alias: row.alias ?? 0,
1038
- setcode: toUint16ArrayFromSetcode(row.setcode ?? 0),
968
+ setcode: toNumberArrayFromSetcode(row.setcode ?? 0),
1039
969
  type,
1040
970
  level,
1041
971
  attribute: (row.attribute ?? 0) >>> 0,
@@ -1196,6 +1126,7 @@ async function DirCardReader(sqljs, ...baseDirs) {
1196
1126
 
1197
1127
  // src/play-yrp.ts
1198
1128
  import { YGOProYrp } from "ygopro-yrp-encode";
1129
+ import { YGOProMsgRetry as YGOProMsgRetry2 } from "ygopro-msg-encode";
1199
1130
  var { LOCATION_DECK, LOCATION_EXTRA, POS_FACEDOWN_DEFENSE } = OcgcoreScriptConstants;
1200
1131
  function normalizeYrp(input) {
1201
1132
  if (input instanceof YGOProYrp) {
@@ -1295,45 +1226,19 @@ function createDuelFromYrp(wrapper, yrpInput) {
1295
1226
  duel.startDuel(yrp.opt >>> 0);
1296
1227
  return { yrp, duel };
1297
1228
  }
1298
- function consumeResponseFromOcgcoreProcess(duel, result, responses) {
1299
- if (result.raw.length > 0 && result.raw[0] === OcgcoreCommonConstants.MSG_RETRY) {
1300
- throw new Error("Got MSG_RETRY");
1301
- }
1302
- if (result.status === 0) {
1303
- return false;
1304
- }
1305
- if (result.status === 1) {
1306
- if (result.raw.length === 0) {
1307
- return false;
1308
- }
1309
- const response = responses.shift();
1310
- if (!response) {
1311
- return true;
1312
- }
1313
- duel.setResponse(response);
1314
- return false;
1315
- }
1316
- return true;
1317
- }
1318
- function* processYrpDuelStep(duel, yrp) {
1319
- const responses = yrp.responses.slice();
1320
- while (true) {
1321
- const result = duel.process();
1322
- yield {
1323
- duel,
1324
- result,
1325
- responses
1326
- };
1327
- if (consumeResponseFromOcgcoreProcess(duel, result, responses)) {
1328
- break;
1329
- }
1330
- }
1331
- }
1332
1229
  function* playYrpStep(ocgcoreWrapper, yrpInput) {
1333
1230
  const { yrp, duel } = createDuelFromYrp(ocgcoreWrapper, yrpInput);
1231
+ const responses = yrp.responses.slice();
1334
1232
  try {
1335
- for (const stepResult of processYrpDuelStep(duel, yrp)) {
1336
- yield stepResult;
1233
+ for (const result of duel.advance(() => responses.shift())) {
1234
+ yield {
1235
+ duel,
1236
+ result,
1237
+ responses
1238
+ };
1239
+ if (result.message instanceof YGOProMsgRetry2) {
1240
+ throw new Error("Got MSG_RETRY");
1241
+ }
1337
1242
  }
1338
1243
  } finally {
1339
1244
  duel.endDuel();
@@ -1395,7 +1300,6 @@ if (typeof globalThis !== "undefined" && !globalThis.Buffer) {
1395
1300
  globalThis.Buffer = Buffer2;
1396
1301
  }
1397
1302
  export {
1398
- CardDataStruct,
1399
1303
  DirCardReader,
1400
1304
  DirReader,
1401
1305
  DirScriptReader,
@@ -1417,7 +1321,6 @@ export {
1417
1321
  ZipReader,
1418
1322
  ZipScriptReader,
1419
1323
  vendor_exports as _OcgcoreConstants,
1420
- consumeResponseFromOcgcoreProcess,
1421
1324
  createDuelFromYrp,
1422
1325
  createOcgcoreWrapper,
1423
1326
  createSqljsCardReader,
@@ -1429,7 +1332,6 @@ export {
1429
1332
  parseRegistryKeys,
1430
1333
  playYrp,
1431
1334
  playYrpStep,
1432
- processYrpDuelStep,
1433
1335
  testCard
1434
1336
  };
1435
1337
  //# sourceMappingURL=index.mjs.map