@konoui/mjimage 0.0.25 → 0.0.27

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.
@@ -1,5 +1,5 @@
1
1
  import { TYPE, Round, Wind } from "../core/constants";
2
- import { Tile, BlockPon, BlockChi, BlockShoKan, BlockAnKan, BlockDaiKan, Type, Block } from "../core/parser";
2
+ import { Tile, BlockPon, BlockChi, BlockShoKan, BlockAnKan, BlockDaiKan, Type, Block, SerializedBlock } from "../core/parser";
3
3
  export type TupleOfSize<T, N extends number, R extends unknown[] = []> = R["length"] extends N ? R : TupleOfSize<T, N, [T, ...R]>;
4
4
  export interface HandData {
5
5
  [TYPE.M]: TupleOfSize<number, 10>;
@@ -7,7 +7,7 @@ export interface HandData {
7
7
  [TYPE.P]: TupleOfSize<number, 10>;
8
8
  [TYPE.Z]: TupleOfSize<number, 8>;
9
9
  [TYPE.BACK]: [string, number];
10
- called: (BlockChi | BlockPon | BlockAnKan | BlockDaiKan | BlockShoKan)[];
10
+ called: readonly (BlockChi | BlockPon | BlockAnKan | BlockDaiKan | BlockShoKan)[];
11
11
  tsumo: Tile | null;
12
12
  reached: boolean;
13
13
  }
@@ -17,15 +17,15 @@ export declare class Hand {
17
17
  private init;
18
18
  get hands(): Tile[];
19
19
  toString(): string;
20
- get called(): (BlockChi | BlockPon | BlockAnKan | BlockDaiKan | BlockShoKan)[];
20
+ get called(): readonly (BlockChi | BlockPon | BlockAnKan | BlockDaiKan | BlockShoKan)[];
21
21
  get reached(): boolean;
22
22
  get drawn(): Tile | null;
23
23
  get menzen(): boolean;
24
24
  getArrayLen(t: Type): 2 | 8 | 10;
25
25
  sum(t: Type): number;
26
26
  get(t: Type, n: number): number;
27
- inc(tiles: Tile[]): Tile[];
28
- dec(tiles: Tile[]): Tile[];
27
+ inc(tiles: readonly Tile[]): Tile[];
28
+ dec(tiles: readonly Tile[]): Tile[];
29
29
  draw(t: Tile): void;
30
30
  discard(t: Tile): void;
31
31
  reach(): void;
@@ -56,7 +56,18 @@ export declare class BlockCalculator {
56
56
  private commonAll;
57
57
  private commonByType;
58
58
  }
59
- export interface BoardParams {
59
+ export declare const NZ: readonly number[];
60
+ export declare const N19: readonly number[];
61
+ export declare const deserializeWinResult: (ret: SerializedWinResult) => WinResult;
62
+ type SerializedBoardContext = Omit<BoardContext, "doraMarkers" | "blindDoraMarkers"> & {
63
+ doraMarkers: string[];
64
+ blindDoraMarkers?: string[];
65
+ };
66
+ export type SerializedWinResult = Omit<WinResult, "hand" | "boardContext"> & {
67
+ hand: SerializedBlock[];
68
+ boardContext: SerializedBoardContext;
69
+ };
70
+ export interface BoardContext {
60
71
  doraMarkers: Tile[];
61
72
  blindDoraMarkers?: Tile[];
62
73
  round: Round;
@@ -85,7 +96,7 @@ export interface WinResult {
85
96
  }[];
86
97
  point: number;
87
98
  hand: Block[];
88
- params: BoardParams;
99
+ boardContext: BoardContext;
89
100
  }
90
101
  export declare class DoubleCalculator {
91
102
  hand: Hand;
@@ -104,9 +115,9 @@ export declare class DoubleCalculator {
104
115
  finalWallWin: boolean;
105
116
  finalDiscardWin: boolean;
106
117
  oneShotWin: boolean;
107
- orig: BoardParams;
118
+ orig: BoardContext;
108
119
  };
109
- constructor(hand: Hand, params: BoardParams);
120
+ constructor(hand: Hand, params: BoardContext);
110
121
  calc(hands: Block[][]): WinResult | false;
111
122
  calcPatterns(hands: Block[][]): {
112
123
  points: {
@@ -261,3 +272,4 @@ export declare class DoubleCalculator {
261
272
  dK13(h: Block[]): never[];
262
273
  calcFu(h: Block[]): number;
263
274
  }
275
+ export {};
@@ -1,4 +1,15 @@
1
1
  "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
2
13
  var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
3
14
  if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
4
15
  if (ar || !(i in from)) {
@@ -9,8 +20,9 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
9
20
  return to.concat(ar || Array.prototype.slice.call(from));
10
21
  };
11
22
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.DoubleCalculator = exports.BlockCalculator = exports.ShantenCalculator = exports.Hand = void 0;
23
+ exports.DoubleCalculator = exports.deserializeWinResult = exports.N19 = exports.NZ = exports.BlockCalculator = exports.ShantenCalculator = exports.Hand = void 0;
13
24
  var constants_1 = require("../core/constants");
25
+ var helper_1 = require("./helper");
14
26
  var parser_1 = require("../core/parser");
15
27
  var Hand = (function () {
16
28
  function Hand(input, allowBackBlock) {
@@ -33,7 +45,7 @@ var Hand = (function () {
33
45
  for (var _i = 0, blocks_1 = blocks; _i < blocks_1.length; _i++) {
34
46
  var b = blocks_1[_i];
35
47
  if (b.isCalled()) {
36
- this.data.called.push(b);
48
+ this.data.called = __spreadArray(__spreadArray([], this.called, true), [b], false);
37
49
  continue;
38
50
  }
39
51
  else if (b.is(constants_1.BLOCK.TSUMO)) {
@@ -43,15 +55,15 @@ var Hand = (function () {
43
55
  continue;
44
56
  }
45
57
  else if (b.is(constants_1.BLOCK.HAND)) {
46
- this.inc(__spreadArray([], b.tiles, true));
58
+ this.inc(b.tiles);
47
59
  continue;
48
60
  }
49
61
  else if (input.split("").every(function (v) { return v === constants_1.TYPE.BACK; })) {
50
- this.inc(__spreadArray([], b.tiles, true));
62
+ this.inc(b.tiles);
51
63
  continue;
52
64
  }
53
65
  else if (allowBackBlock) {
54
- this.inc(__spreadArray([], b.tiles, true));
66
+ this.inc(b.tiles);
55
67
  continue;
56
68
  }
57
69
  throw new Error("unexpected block ".concat(b.type, " ").concat(b.toString()));
@@ -89,18 +101,21 @@ var Hand = (function () {
89
101
  configurable: true
90
102
  });
91
103
  Hand.prototype.toString = function () {
92
- var c = "";
104
+ var called = "";
93
105
  for (var _i = 0, _a = this.called; _i < _a.length; _i++) {
94
106
  var b_1 = _a[_i];
95
- c = "".concat(c, ",").concat(b_1.toString());
107
+ called = "".concat(called, ",").concat(b_1.toString());
96
108
  }
97
- var tiles = this.hands;
109
+ var tsumo = "";
110
+ if (this.drawn)
111
+ tsumo = ",".concat(this.drawn.toString());
112
+ var tiles = this.hands.filter(function (v) { return !v.has(constants_1.OPERATOR.TSUMO); });
98
113
  var b = new parser_1.BlockHand(tiles).toString();
99
- return "".concat(b).concat(c);
114
+ return "".concat(b).concat(tsumo).concat(called);
100
115
  };
101
116
  Object.defineProperty(Hand.prototype, "called", {
102
117
  get: function () {
103
- return this.data.called.concat();
118
+ return this.data.called;
104
119
  },
105
120
  enumerable: false,
106
121
  configurable: true
@@ -215,15 +230,17 @@ var Hand = (function () {
215
230
  if (toRemove.length != b.tiles.length - 1)
216
231
  throw new Error("removal: ".concat(toRemove, " block: ").concat(b));
217
232
  this.dec(toRemove);
218
- this.data.called.push(b);
233
+ this.data.called = __spreadArray(__spreadArray([], this.called, true), [b], false);
219
234
  this.data.tsumo = null;
220
235
  return;
221
236
  };
222
237
  Hand.prototype.kan = function (b) {
223
238
  if (b instanceof parser_1.BlockAnKan) {
224
239
  var t = b.tiles[0];
240
+ if ((0, parser_1.isNum0)(t))
241
+ t = new parser_1.Tile(t.t, 5);
225
242
  this.dec([t, t, t, t]);
226
- this.data.called.push(b);
243
+ this.data.called = __spreadArray(__spreadArray([], this.called, true), [b], false);
227
244
  this.data.tsumo = null;
228
245
  return;
229
246
  }
@@ -232,12 +249,12 @@ var Hand = (function () {
232
249
  if (idx == -1)
233
250
  throw new Error("unable to find ".concat(b.tiles[0]));
234
251
  var t = b.tiles[0];
235
- if ((0, parser_1.isNum0)(t)) {
252
+ if ((0, parser_1.isNum0)(t))
236
253
  t = new parser_1.Tile(t.t, 5);
237
- }
238
- this.data.called.splice(idx, 1);
239
254
  this.dec([t]);
240
- this.data.called.push(b);
255
+ this.data.called = __spreadArray(__spreadArray(__spreadArray([], this.called.slice(0, idx), true), this.called.slice(idx + 1), true), [
256
+ b,
257
+ ], false);
241
258
  this.data.tsumo = null;
242
259
  return;
243
260
  }
@@ -245,9 +262,7 @@ var Hand = (function () {
245
262
  };
246
263
  Hand.prototype.clone = function () {
247
264
  var c = new Hand(this.toString());
248
- c.data.called = this.called.map(function (b) { return b.clone(); });
249
265
  c.data.reached = this.data.reached;
250
- c.data.tsumo = this.data.tsumo == null ? null : this.data.tsumo;
251
266
  return c;
252
267
  };
253
268
  return Hand;
@@ -291,7 +306,7 @@ var ShantenCalculator = (function () {
291
306
  var t = _a[_i];
292
307
  if (t == constants_1.TYPE.BACK)
293
308
  continue;
294
- var nn = t == constants_1.TYPE.Z ? [1, 2, 3, 4, 5, 6, 7] : [1, 9];
309
+ var nn = t == constants_1.TYPE.Z ? exports.NZ : exports.N19;
295
310
  for (var _b = 0, nn_1 = nn; _b < nn_1.length; _b++) {
296
311
  var n = nn_1[_b];
297
312
  if (this.hand.get(t, n) >= 1)
@@ -529,7 +544,7 @@ var BlockCalculator = (function () {
529
544
  var t = _a[_i];
530
545
  if (t == constants_1.TYPE.BACK)
531
546
  continue;
532
- var nn = t == constants_1.TYPE.Z ? [1, 2, 3, 4, 5, 6, 7] : [1, 9];
547
+ var nn = t == constants_1.TYPE.Z ? exports.NZ : exports.N19;
533
548
  for (var _b = 0, nn_2 = nn; _b < nn_2.length; _b++) {
534
549
  var n = nn_2[_b];
535
550
  if (this.hand.get(t, n) == 1)
@@ -626,7 +641,7 @@ var BlockCalculator = (function () {
626
641
  this.commonByType(constants_1.TYPE.S),
627
642
  handleZ(),
628
643
  handleBack(),
629
- [this.hand.called],
644
+ [this.hand.called.concat()],
630
645
  ].sort(function (a, b) { return b.length - a.length; });
631
646
  var ret = vvv[0].concat();
632
647
  for (var i = 0; i < ret.length; i++) {
@@ -686,6 +701,14 @@ var BlockCalculator = (function () {
686
701
  return BlockCalculator;
687
702
  }());
688
703
  exports.BlockCalculator = BlockCalculator;
704
+ exports.NZ = [1, 2, 3, 4, 5, 6, 7];
705
+ exports.N19 = [1, 9];
706
+ var deserializeWinResult = function (ret) {
707
+ var _a;
708
+ var bc = ret.boardContext;
709
+ return __assign(__assign({}, ret), { hand: ret.hand.map(parser_1.Block.from), boardContext: __assign(__assign({}, bc), { doraMarkers: bc.doraMarkers.map(parser_1.Tile.from), blindDoraMarkers: (_a = bc.blindDoraMarkers) === null || _a === void 0 ? void 0 : _a.map(parser_1.Tile.from) }) });
710
+ };
711
+ exports.deserializeWinResult = deserializeWinResult;
689
712
  var DoubleCalculator = (function () {
690
713
  function DoubleCalculator(hand, params) {
691
714
  var _a, _b, _c, _d, _e, _f, _g;
@@ -766,13 +789,8 @@ var DoubleCalculator = (function () {
766
789
  return b.tiles.some(function (t) { return t.has(constants_1.OPERATOR.TSUMO); });
767
790
  });
768
791
  var myWind = this.cfg.orig.myWind;
769
- var isParent = myWind == "1w";
770
- var deltas = {
771
- "1w": 0,
772
- "2w": 0,
773
- "3w": 0,
774
- "4w": 0,
775
- };
792
+ var isParent = myWind == constants_1.WIND.EAST;
793
+ var deltas = (0, helper_1.createWindMap)(0);
776
794
  if (!isTsumo) {
777
795
  var deadPoint = this.cfg.sticks.dead * 300;
778
796
  if (this.cfg.orig.ronWind == null)
@@ -796,7 +814,7 @@ var DoubleCalculator = (function () {
796
814
  var key = _a[_i];
797
815
  if (key == myWind)
798
816
  continue;
799
- var coefficient = key == "1w" ? 2 : 1;
817
+ var coefficient = key == constants_1.WIND.EAST ? 2 : 1;
800
818
  var point = ceil(base * coefficient) + deadPoint;
801
819
  deltas[key] -= point;
802
820
  deltas[myWind] += point;
@@ -811,7 +829,7 @@ var DoubleCalculator = (function () {
811
829
  points: patterns[idx].points,
812
830
  point: deltas[myWind],
813
831
  hand: patterns[idx].hand,
814
- params: this.cfg.orig,
832
+ boardContext: this.cfg.orig,
815
833
  };
816
834
  return v;
817
835
  };
@@ -867,18 +885,19 @@ var DoubleCalculator = (function () {
867
885
  DoubleCalculator.prototype.dC1 = function (h) {
868
886
  if (this.minus() != 0)
869
887
  return [];
888
+ var yaku = "平和";
870
889
  var fu = this.calcFu(h);
871
890
  if (fu == 20)
872
- return [{ name: "平和", double: 1 }];
891
+ return [{ name: yaku, double: 1 }];
873
892
  if (!h.some(function (b) { return b.tiles.some(function (t) { return t.has(constants_1.OPERATOR.TSUMO); }); })) {
874
893
  if (fu == 30)
875
- return [{ name: "平和", double: 1 }];
894
+ return [{ name: yaku, double: 1 }];
876
895
  }
877
896
  return [];
878
897
  };
879
898
  DoubleCalculator.prototype.dD1 = function (h) {
880
899
  var cond = h.some(function (block) {
881
- return block.tiles.some(function (t) { return t.t == constants_1.TYPE.Z || [1, 9].includes(t.n); });
900
+ return block.tiles.some(function (t) { return t.t == constants_1.TYPE.Z || exports.N19.includes(t.n); });
882
901
  });
883
902
  return cond ? [] : [{ name: "断么九", double: 1 }];
884
903
  };
@@ -1063,7 +1082,7 @@ var DoubleCalculator = (function () {
1063
1082
  };
1064
1083
  DoubleCalculator.prototype.dH2 = function (h) {
1065
1084
  var cond = h.every(function (b) {
1066
- var values = b.tiles[0].t == constants_1.TYPE.Z ? [1, 2, 3, 4, 5, 6, 7] : [1, 9];
1085
+ var values = b.tiles[0].t == constants_1.TYPE.Z ? exports.NZ : exports.N19;
1067
1086
  return b.tiles.every(function (t) { return values.includes(t.n); });
1068
1087
  });
1069
1088
  return cond ? [{ name: "混老頭", double: 2 }] : [];
@@ -1076,7 +1095,7 @@ var DoubleCalculator = (function () {
1076
1095
  if (!h.some(function (b) { return b.tiles[0].t == constants_1.TYPE.Z; }))
1077
1096
  return [];
1078
1097
  var cond = h.every(function (block) {
1079
- var values = block.tiles[0].t == constants_1.TYPE.Z ? [1, 2, 3, 4, 5, 6, 7] : [1, 9];
1098
+ var values = block.tiles[0].t == constants_1.TYPE.Z ? exports.NZ : exports.N19;
1080
1099
  return block.tiles.some(function (t) { return values.includes(t.n); });
1081
1100
  });
1082
1101
  return cond ? [{ name: "混全帯么九", double: 2 - this.minus() }] : [];
@@ -1139,7 +1158,7 @@ var DoubleCalculator = (function () {
1139
1158
  if (h.some(function (b) { return b.tiles[0].t == constants_1.TYPE.Z; }))
1140
1159
  return [];
1141
1160
  var cond = h.every(function (b) {
1142
- return b.tiles.some(function (t) { return [1, 9].includes(t.n); });
1161
+ return b.tiles.some(function (t) { return exports.N19.includes(t.n); });
1143
1162
  });
1144
1163
  return cond ? [{ name: "純全帯么九色", double: 3 - this.minus() }] : [];
1145
1164
  };
@@ -1212,12 +1231,12 @@ var DoubleCalculator = (function () {
1212
1231
  return cond ? [{ name: "大三元", double: 13 }] : [];
1213
1232
  };
1214
1233
  DoubleCalculator.prototype.dE13 = function (h) {
1215
- var cond = h.every(function (b) { return b.tiles.every(function (t) { return t.t == constants_1.TYPE.Z; }); });
1234
+ var cond = h.every(function (b) { return b.tiles[0].t == constants_1.TYPE.Z; });
1216
1235
  return cond ? [{ name: "字一色", double: 13 }] : [];
1217
1236
  };
1218
1237
  DoubleCalculator.prototype.dF13 = function (h) {
1219
1238
  var cond = h.every(function (b) {
1220
- return b.tiles.every(function (t) { return t.t != constants_1.TYPE.Z && [1, 9].includes(t.n); });
1239
+ return b.tiles.every(function (t) { return t.t != constants_1.TYPE.Z && exports.N19.includes(t.n); });
1221
1240
  });
1222
1241
  return cond ? [{ name: "清老頭", double: 13 }] : [];
1223
1242
  };
@@ -1282,7 +1301,7 @@ var DoubleCalculator = (function () {
1282
1301
  return base * 2;
1283
1302
  else if (tile.t == constants_1.TYPE.Z && [myWind, round].includes(tile.n))
1284
1303
  return base * 2;
1285
- else if ([1, 9].includes(tile.n))
1304
+ else if (exports.N19.includes(tile.n))
1286
1305
  return base * 2;
1287
1306
  else
1288
1307
  return base;
@@ -1374,7 +1393,7 @@ var minTile = function (b) {
1374
1393
  return __spreadArray([], b.tiles, true).sort(parser_1.tileSortFunc)[0];
1375
1394
  };
1376
1395
  var toDora = function (doraMarker) {
1377
- var n = doraMarker.isNum() && doraMarker.n == 0 ? 5 : doraMarker.n;
1396
+ var n = (0, parser_1.isNum0)(doraMarker) ? 5 : doraMarker.n;
1378
1397
  var t = doraMarker.t;
1379
1398
  return new parser_1.Tile(t, (n % 9) + 1);
1380
1399
  };
@@ -1,5 +1,10 @@
1
1
  import { Tile } from "../core";
2
2
  import { Hand } from "./calc";
3
+ export interface SerializedCandidate {
4
+ tile: string;
5
+ candidates: string[];
6
+ shanten: number;
7
+ }
3
8
  export interface Candidate {
4
9
  tile: Tile;
5
10
  candidates: Tile[];
@@ -0,0 +1,6 @@
1
+ export declare function createWindMap<T>(initial: T, clone?: boolean): {
2
+ "1w": T;
3
+ "2w": T;
4
+ "3w": T;
5
+ "4w": T;
6
+ };
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createWindMap = createWindMap;
4
+ var constants_1 = require("../core/constants");
5
+ function createWindMap(initial, clone) {
6
+ var _a;
7
+ if (clone === void 0) { clone = false; }
8
+ var m = (_a = {},
9
+ _a[constants_1.WIND.EAST] = initial,
10
+ _a[constants_1.WIND.SOUTH] = initial,
11
+ _a[constants_1.WIND.WEST] = initial,
12
+ _a[constants_1.WIND.NORTH] = initial,
13
+ _a);
14
+ if (clone) {
15
+ for (var _i = 0, _b = Object.values(constants_1.WIND); _i < _b.length; _i++) {
16
+ var w = _b[_i];
17
+ m[w] = structuredClone(initial);
18
+ }
19
+ }
20
+ return m;
21
+ }
@@ -1,2 +1,3 @@
1
1
  export * from "./calc";
2
2
  export * from "./efficiency";
3
+ export * from "./helper";
@@ -16,3 +16,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./calc"), exports);
18
18
  __exportStar(require("./efficiency"), exports);
19
+ __exportStar(require("./helper"), exports);
@@ -1,5 +1,5 @@
1
1
  import { Wind, Round } from "../core/";
2
- import { BoardParams, Hand, WinResult, Candidate } from "../calculator";
2
+ import { BoardContext, Hand, WinResult, Candidate } from "../calculator";
3
3
  import { BlockAnKan, BlockChi, BlockDaiKan, BlockPon, BlockShoKan, Tile } from "../core/parser";
4
4
  import { PlayerEvent, EventHandler, DistributeEvent } from "./events";
5
5
  import { Wall, WallProps, River, PlaceManager, ScoreManager, Counter } from ".";
@@ -96,7 +96,7 @@ export declare class Controller {
96
96
  constructor(players: PlayerProps[], params?: {
97
97
  debug?: boolean;
98
98
  });
99
- boardParams(w: Wind): BoardParams;
99
+ boardParams(w: Wind): BoardContext;
100
100
  hand(w: Wind): ActorHand;
101
101
  get placeManager(): PlaceManager;
102
102
  get scoreManager(): ScoreManager;
@@ -137,7 +137,7 @@ export declare class Controller {
137
137
  export declare class ActorHand extends Hand {
138
138
  isBackHand(): boolean;
139
139
  clone(): ActorHand;
140
- dec(tiles: Tile[]): Tile[];
140
+ dec(tiles: readonly Tile[]): Tile[];
141
141
  }
142
142
  export declare abstract class BaseActor {
143
143
  id: string;