@konoui/mjimage 0.0.1

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.
Files changed (102) hide show
  1. package/README.md +83 -0
  2. package/dist/index.html +17763 -0
  3. package/dist/mjs/index.d.ts +8 -0
  4. package/dist/mjs/index.js +24 -0
  5. package/dist/mjs/lib/constants.d.ts +50 -0
  6. package/dist/mjs/lib/constants.js +48 -0
  7. package/dist/mjs/lib/context.d.ts +12 -0
  8. package/dist/mjs/lib/context.js +33 -0
  9. package/dist/mjs/lib/image.d.ts +39 -0
  10. package/dist/mjs/lib/image.js +303 -0
  11. package/dist/mjs/lib/lexer.d.ts +13 -0
  12. package/dist/mjs/lib/lexer.js +46 -0
  13. package/dist/mjs/lib/mjimage.d.ts +11 -0
  14. package/dist/mjs/lib/mjimage.js +90 -0
  15. package/dist/mjs/lib/parser.d.ts +30 -0
  16. package/dist/mjs/lib/parser.js +245 -0
  17. package/dist/mjs/lib/table-parser.d.ts +220 -0
  18. package/dist/mjs/lib/table-parser.js +114 -0
  19. package/dist/mjs/lib/table.d.ts +10 -0
  20. package/dist/mjs/lib/table.js +242 -0
  21. package/dist/mjs/svg/_0.svg +1 -0
  22. package/dist/mjs/svg/m0.svg +1 -0
  23. package/dist/mjs/svg/m1.svg +1 -0
  24. package/dist/mjs/svg/m2.svg +1 -0
  25. package/dist/mjs/svg/m3.svg +1 -0
  26. package/dist/mjs/svg/m4.svg +1 -0
  27. package/dist/mjs/svg/m5.svg +1 -0
  28. package/dist/mjs/svg/m6.svg +1 -0
  29. package/dist/mjs/svg/m7.svg +1 -0
  30. package/dist/mjs/svg/m8.svg +1 -0
  31. package/dist/mjs/svg/m9.svg +1 -0
  32. package/dist/mjs/svg/p0.svg +1 -0
  33. package/dist/mjs/svg/p1.svg +1 -0
  34. package/dist/mjs/svg/p2.svg +1 -0
  35. package/dist/mjs/svg/p3.svg +1 -0
  36. package/dist/mjs/svg/p4.svg +1 -0
  37. package/dist/mjs/svg/p5.svg +1 -0
  38. package/dist/mjs/svg/p6.svg +1 -0
  39. package/dist/mjs/svg/p7.svg +1 -0
  40. package/dist/mjs/svg/p8.svg +1 -0
  41. package/dist/mjs/svg/p9.svg +1 -0
  42. package/dist/mjs/svg/s0.svg +1 -0
  43. package/dist/mjs/svg/s1.svg +1 -0
  44. package/dist/mjs/svg/s2.svg +1 -0
  45. package/dist/mjs/svg/s3.svg +1 -0
  46. package/dist/mjs/svg/s4.svg +1 -0
  47. package/dist/mjs/svg/s5.svg +1 -0
  48. package/dist/mjs/svg/s6.svg +1 -0
  49. package/dist/mjs/svg/s7.svg +1 -0
  50. package/dist/mjs/svg/s8.svg +1 -0
  51. package/dist/mjs/svg/s9.svg +1 -0
  52. package/dist/mjs/svg/stick100.svg +1 -0
  53. package/dist/mjs/svg/stick1000.svg +1 -0
  54. package/dist/mjs/svg/z1.svg +1 -0
  55. package/dist/mjs/svg/z2.svg +1 -0
  56. package/dist/mjs/svg/z3.svg +1 -0
  57. package/dist/mjs/svg/z4.svg +1 -0
  58. package/dist/mjs/svg/z5.svg +1 -0
  59. package/dist/mjs/svg/z6.svg +1 -0
  60. package/dist/mjs/svg/z7.svg +1 -0
  61. package/dist/svg/_0.svg +1 -0
  62. package/dist/svg/m0.svg +1 -0
  63. package/dist/svg/m1.svg +1 -0
  64. package/dist/svg/m2.svg +1 -0
  65. package/dist/svg/m3.svg +1 -0
  66. package/dist/svg/m4.svg +1 -0
  67. package/dist/svg/m5.svg +1 -0
  68. package/dist/svg/m6.svg +1 -0
  69. package/dist/svg/m7.svg +1 -0
  70. package/dist/svg/m8.svg +1 -0
  71. package/dist/svg/m9.svg +1 -0
  72. package/dist/svg/p0.svg +1 -0
  73. package/dist/svg/p1.svg +1 -0
  74. package/dist/svg/p2.svg +1 -0
  75. package/dist/svg/p3.svg +1 -0
  76. package/dist/svg/p4.svg +1 -0
  77. package/dist/svg/p5.svg +1 -0
  78. package/dist/svg/p6.svg +1 -0
  79. package/dist/svg/p7.svg +1 -0
  80. package/dist/svg/p8.svg +1 -0
  81. package/dist/svg/p9.svg +1 -0
  82. package/dist/svg/s0.svg +1 -0
  83. package/dist/svg/s1.svg +1 -0
  84. package/dist/svg/s2.svg +1 -0
  85. package/dist/svg/s3.svg +1 -0
  86. package/dist/svg/s4.svg +1 -0
  87. package/dist/svg/s5.svg +1 -0
  88. package/dist/svg/s6.svg +1 -0
  89. package/dist/svg/s7.svg +1 -0
  90. package/dist/svg/s8.svg +1 -0
  91. package/dist/svg/s9.svg +1 -0
  92. package/dist/svg/stick100.svg +1 -0
  93. package/dist/svg/stick1000.svg +1 -0
  94. package/dist/svg/z1.svg +1 -0
  95. package/dist/svg/z2.svg +1 -0
  96. package/dist/svg/z3.svg +1 -0
  97. package/dist/svg/z4.svg +1 -0
  98. package/dist/svg/z5.svg +1 -0
  99. package/dist/svg/z6.svg +1 -0
  100. package/dist/svg/z7.svg +1 -0
  101. package/dist/tiles.1417fc37.svg +1 -0
  102. package/package.json +71 -0
@@ -0,0 +1,90 @@
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
+ };
13
+ var __importDefault = (this && this.__importDefault) || function (mod) {
14
+ return (mod && mod.__esModule) ? mod : { "default": mod };
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.mjimage = void 0;
18
+ var assert_1 = __importDefault(require("assert"));
19
+ var parser_1 = require("./parser");
20
+ var image_1 = require("./image");
21
+ var context_1 = require("./context");
22
+ var table_1 = require("./table");
23
+ var constants_1 = require("./constants");
24
+ var svg_js_1 = require("@svgdotjs/svg.js");
25
+ var tiles_svg_1 = __importDefault(require("./tiles.svg"));
26
+ var defaultQuerySelector = ".mjimage";
27
+ var defaultScale = 1.6;
28
+ var defaultSvgSprite = false;
29
+ var tableRegex = /^\s*table/;
30
+ var minPaiHeight = Math.min(constants_1.TILE_CONTEXT.WIDTH, constants_1.TILE_CONTEXT.HEIGHT);
31
+ var calculateScale = function (scale, textHeight) {
32
+ return (textHeight / minPaiHeight) * scale;
33
+ };
34
+ var mjimage = (function () {
35
+ function mjimage() {
36
+ }
37
+ mjimage.svgURL = function () {
38
+ return tiles_svg_1.default;
39
+ };
40
+ mjimage.initialize = function (props) {
41
+ var _a, _b, _c, _d;
42
+ if (props === void 0) { props = {}; }
43
+ console.debug("initializing....");
44
+ var querySelector = (_a = props.querySelector) !== null && _a !== void 0 ? _a : defaultQuerySelector;
45
+ var handScale = (_b = props.scale) !== null && _b !== void 0 ? _b : defaultScale;
46
+ var tableScale = (_c = props.tableScale) !== null && _c !== void 0 ? _c : handScale;
47
+ var svgSprite = (_d = props.svgSprite) !== null && _d !== void 0 ? _d : defaultSvgSprite;
48
+ if (typeof querySelector === "string")
49
+ querySelector = [querySelector];
50
+ var ctx = document.createElement("canvas").getContext("2d");
51
+ (0, assert_1.default)(ctx != null);
52
+ querySelector.forEach(function (qs) {
53
+ console.debug("try to find", qs);
54
+ var targets = document.querySelectorAll(qs);
55
+ for (var i = 0; i < targets.length; i++) {
56
+ var target = targets[i];
57
+ var input = target.textContent || "";
58
+ if (input == "") {
59
+ console.debug("skip due to not input");
60
+ continue;
61
+ }
62
+ console.debug("found", input);
63
+ target.textContent = "";
64
+ var style = window.getComputedStyle(target, null);
65
+ var fontSize = parseFloat(style.getPropertyValue("font-size"));
66
+ var textHeight = fontSize;
67
+ var svg = (0, svg_js_1.SVG)();
68
+ try {
69
+ if (tableRegex.test(input)) {
70
+ var scale = calculateScale(tableScale, textHeight);
71
+ var fontCtx = (0, context_1.getTableFontContext)(ctx, scale);
72
+ (0, table_1.drawTable)(svg, input, __assign(__assign({}, props), { svgSprite: svgSprite, scale: scale }), fontCtx);
73
+ }
74
+ else {
75
+ var scale = calculateScale(handScale, textHeight);
76
+ var blocks = new parser_1.Parser(input).parse();
77
+ (0, image_1.drawBlocks)(svg, blocks, __assign(__assign({}, props), { svgSprite: svgSprite, scale: scale }));
78
+ }
79
+ svg.addTo(target);
80
+ }
81
+ catch (e) {
82
+ target.textContent = input;
83
+ console.error("encounter unexpected error:", e);
84
+ }
85
+ }
86
+ });
87
+ };
88
+ return mjimage;
89
+ }());
90
+ exports.mjimage = mjimage;
@@ -0,0 +1,30 @@
1
+ import { BLOCK, OPERATOR, KIND } from "./constants";
2
+ export declare const tileSortFunc: (i: Tile, j: Tile) => number;
3
+ type Kind = (typeof KIND)[keyof typeof KIND];
4
+ export declare function isKind(v: string): [Kind, boolean];
5
+ type Operator = (typeof OPERATOR)[keyof typeof OPERATOR];
6
+ export declare class Tile {
7
+ k: Kind;
8
+ n: number;
9
+ op?: Operator | undefined;
10
+ constructor(k: Kind, n: number, op?: Operator | undefined);
11
+ toString(): string;
12
+ equals(t: Tile, ignoreRed?: boolean): boolean;
13
+ }
14
+ type BLOCK = (typeof BLOCK)[keyof typeof BLOCK];
15
+ export declare class Block {
16
+ tiles: Tile[];
17
+ type: BLOCK;
18
+ constructor(tiles: Tile[], type: BLOCK);
19
+ toString(): string;
20
+ }
21
+ export declare class Parser {
22
+ readonly input: string;
23
+ readonly maxInputLength = 128;
24
+ constructor(input: string);
25
+ parse(): Block[];
26
+ parseInput(): Tile[];
27
+ private makeBlocks;
28
+ validate(input: string): void;
29
+ }
30
+ export {};
@@ -0,0 +1,245 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Parser = exports.Block = exports.Tile = exports.isKind = exports.tileSortFunc = void 0;
4
+ var lexer_1 = require("./lexer");
5
+ var constants_1 = require("./constants");
6
+ var tileSortFunc = function (i, j) {
7
+ var _a;
8
+ if (i.k == j.k) {
9
+ if (i.n == 0)
10
+ return 5 - j.n;
11
+ if (j.n == 0)
12
+ return i.n - 5;
13
+ return i.n - j.n;
14
+ }
15
+ var lookup = (_a = {},
16
+ _a[constants_1.KIND.M] = 1,
17
+ _a[constants_1.KIND.P] = 2,
18
+ _a[constants_1.KIND.S] = 3,
19
+ _a[constants_1.KIND.Z] = 4,
20
+ _a[constants_1.KIND.BACK] = 5,
21
+ _a[constants_1.KIND.SEPARATOR] = 6,
22
+ _a);
23
+ return lookup[i.k] - lookup[j.k];
24
+ };
25
+ exports.tileSortFunc = tileSortFunc;
26
+ function isKind(v) {
27
+ for (var _i = 0, _a = Object.values(constants_1.KIND); _i < _a.length; _i++) {
28
+ var k = _a[_i];
29
+ if (k == v) {
30
+ return [v, true];
31
+ }
32
+ }
33
+ return [constants_1.KIND.BACK, false];
34
+ }
35
+ exports.isKind = isKind;
36
+ var Tile = (function () {
37
+ function Tile(k, n, op) {
38
+ this.k = k;
39
+ this.n = n;
40
+ this.op = op;
41
+ }
42
+ Tile.prototype.toString = function () {
43
+ if (this.k === constants_1.KIND.BACK || this.k === constants_1.KIND.SEPARATOR)
44
+ return this.k;
45
+ var op = this.op != null ? this.op : "";
46
+ return "".concat(op).concat(this.n).concat(this.k);
47
+ };
48
+ Tile.prototype.equals = function (t, ignoreRed) {
49
+ if (ignoreRed === void 0) { ignoreRed = false; }
50
+ var ok = this.n === t.n;
51
+ if (ignoreRed)
52
+ ok || (ok = (this.n == 5 && t.n == 0) || (this.n == 0 && t.n == 5));
53
+ return this.k === t.k && ok;
54
+ };
55
+ return Tile;
56
+ }());
57
+ exports.Tile = Tile;
58
+ var Block = (function () {
59
+ function Block(tiles, type) {
60
+ this.tiles = tiles;
61
+ this.type = type;
62
+ if (type == constants_1.BLOCK.CHI) {
63
+ tiles.sort(function (a, b) {
64
+ if (a.op == constants_1.OPERATOR.HORIZONTAL)
65
+ return -1;
66
+ if (b.op == constants_1.OPERATOR.HORIZONTAL)
67
+ return 1;
68
+ return (0, exports.tileSortFunc)(a, b);
69
+ });
70
+ return;
71
+ }
72
+ if (type != constants_1.BLOCK.DISCARD) {
73
+ tiles.sort(exports.tileSortFunc);
74
+ }
75
+ }
76
+ Block.prototype.toString = function () {
77
+ var result = "";
78
+ for (var _i = 0, _a = this.tiles; _i < _a.length; _i++) {
79
+ var t = _a[_i];
80
+ result += t.toString();
81
+ }
82
+ return result;
83
+ };
84
+ return Block;
85
+ }());
86
+ exports.Block = Block;
87
+ var Parser = (function () {
88
+ function Parser(input) {
89
+ this.input = input;
90
+ this.maxInputLength = 128;
91
+ this.input = input.replace(/\s/g, "");
92
+ }
93
+ Parser.prototype.parse = function () {
94
+ var parsed = this.parseInput();
95
+ return this.makeBlocks(parsed);
96
+ };
97
+ Parser.prototype.parseInput = function () {
98
+ var l = new lexer_1.Lexer(this.input);
99
+ var res = [];
100
+ var cluster = [];
101
+ this.validate(this.input);
102
+ for (;;) {
103
+ l.skipWhitespace();
104
+ var char = l.char;
105
+ if (char === l.eof)
106
+ break;
107
+ var _a = isKindAlias(char, cluster), k = _a[0], isKind_1 = _a[1];
108
+ if (isKind_1) {
109
+ if (k == constants_1.KIND.BACK) {
110
+ res.push(new Tile(k, 0));
111
+ l.readChar();
112
+ continue;
113
+ }
114
+ if (k == constants_1.KIND.SEPARATOR) {
115
+ res.push(new Tile(k, -1));
116
+ l.readChar();
117
+ continue;
118
+ }
119
+ res.push.apply(res, makeTiles(cluster, k));
120
+ cluster = [];
121
+ l.readChar();
122
+ continue;
123
+ }
124
+ else {
125
+ var _b = isOperator(l), t = _b[0], isOp = _b[1];
126
+ if (isOp) {
127
+ l.readChar();
128
+ cluster.push(t);
129
+ l.readChar();
130
+ continue;
131
+ }
132
+ var _c = isNumber(char), n = _c[0], isNum = _c[1];
133
+ if (!isNum)
134
+ throw new Error("encounter unexpected number: ".concat(n, " ").concat(char));
135
+ cluster.push(new Tile(constants_1.KIND.BACK, n));
136
+ }
137
+ l.readChar();
138
+ }
139
+ if (cluster.length > 0)
140
+ throw new Error("remaining values ".concat(cluster.toString()));
141
+ return res;
142
+ };
143
+ Parser.prototype.makeBlocks = function (tiles) {
144
+ var cluster = [];
145
+ var res = [];
146
+ if (tiles.length == 0)
147
+ return res;
148
+ for (var _i = 0, tiles_1 = tiles; _i < tiles_1.length; _i++) {
149
+ var t = tiles_1[_i];
150
+ if (t.k === constants_1.KIND.SEPARATOR) {
151
+ var type_1 = detectBlockType(cluster);
152
+ var b_1 = new Block(cluster, type_1);
153
+ res.push(b_1);
154
+ cluster = [];
155
+ continue;
156
+ }
157
+ cluster.push(t);
158
+ }
159
+ var type = detectBlockType(cluster);
160
+ var b = new Block(cluster, type);
161
+ res.push(b);
162
+ cluster = [];
163
+ return res;
164
+ };
165
+ Parser.prototype.validate = function (input) {
166
+ if (input.length == 0)
167
+ return;
168
+ if (input.length > this.maxInputLength)
169
+ throw new Error("exceeded maximum input length");
170
+ var lastChar = input.charAt(input.length - 1);
171
+ var _a = isKindAlias(lastChar, [new Tile(constants_1.KIND.BACK, 1)]), _ = _a[0], isKind = _a[1];
172
+ if (!isKind)
173
+ throw new Error("last character(".concat(lastChar, ") is not kind value"));
174
+ };
175
+ return Parser;
176
+ }());
177
+ exports.Parser = Parser;
178
+ function detectBlockType(tiles) {
179
+ if (tiles.length === 0)
180
+ return constants_1.BLOCK.UNKNOWN;
181
+ if (tiles.length === 1) {
182
+ if (tiles[0].op === constants_1.OPERATOR.DORA)
183
+ return constants_1.BLOCK.DORA;
184
+ if (tiles[0].op === constants_1.OPERATOR.TSUMO)
185
+ return constants_1.BLOCK.TSUMO;
186
+ return constants_1.BLOCK.HAND;
187
+ }
188
+ var sameAll = tiles.filter(function (v) { return v.equals(tiles[0], true); }).length == tiles.length;
189
+ var numOfHorizontals = tiles.filter(function (v) { return v.op == constants_1.OPERATOR.HORIZONTAL; }).length;
190
+ var numOfTsumoDoraTiles = tiles.filter(function (v) { return v.op == constants_1.OPERATOR.TSUMO || v.op == constants_1.OPERATOR.DORA; }).length;
191
+ var numOfBackTiles = tiles.filter(function (v) { return v.k == constants_1.KIND.BACK; }).length;
192
+ if (numOfTsumoDoraTiles > 0)
193
+ return constants_1.BLOCK.UNKNOWN;
194
+ if (numOfHorizontals == 0 && numOfBackTiles == 0)
195
+ return constants_1.BLOCK.HAND;
196
+ if (tiles.length === 3) {
197
+ return sameAll ? constants_1.BLOCK.PON : constants_1.BLOCK.CHI;
198
+ }
199
+ if (tiles.length == 4 && numOfBackTiles == 2)
200
+ return constants_1.BLOCK.AN_KAN;
201
+ if (tiles.length == 4 && sameAll) {
202
+ if (numOfHorizontals == 1)
203
+ return constants_1.BLOCK.DAI_KAN;
204
+ if (numOfHorizontals == 2)
205
+ return constants_1.BLOCK.SHO_KAN;
206
+ }
207
+ if (numOfHorizontals == 1)
208
+ return constants_1.BLOCK.DISCARD;
209
+ return constants_1.BLOCK.UNKNOWN;
210
+ }
211
+ function makeTiles(cluster, k) {
212
+ return cluster.map(function (v) {
213
+ return new Tile(k, v.n, v.op);
214
+ });
215
+ }
216
+ function isKindAlias(s, cluster) {
217
+ var _a = isKind(s), k = _a[0], ok = _a[1];
218
+ if (ok)
219
+ return [k, true];
220
+ var isAlias = s === "w" || s === "d";
221
+ if (isAlias && cluster.length > 0) {
222
+ for (var i = 0; i < cluster.length; i++) {
223
+ if (s === "d")
224
+ cluster[i].n += 4;
225
+ }
226
+ return [constants_1.KIND.Z, true];
227
+ }
228
+ return [constants_1.KIND.BACK, false];
229
+ }
230
+ function isNumber(v) {
231
+ var valid = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
232
+ return [Number(v), valid.includes(v)];
233
+ }
234
+ function isOperator(l) {
235
+ for (var _i = 0, _a = Object.values(constants_1.OPERATOR); _i < _a.length; _i++) {
236
+ var op = _a[_i];
237
+ if (op == l.char) {
238
+ var _b = isNumber(l.peekChar()), n = _b[0], ok = _b[1];
239
+ if (!ok)
240
+ return [new Tile(constants_1.KIND.BACK, 0), false];
241
+ return [new Tile(constants_1.KIND.BACK, n, op), true];
242
+ }
243
+ }
244
+ return [new Tile(constants_1.KIND.BACK, 0), false];
245
+ }
@@ -0,0 +1,220 @@
1
+ import { z } from "zod";
2
+ import { Tile, Block } from "./parser";
3
+ import { WIND_MAP, ROUND_MAP } from "./constants";
4
+ declare const tableInputSchema: z.ZodObject<{
5
+ "1w": z.ZodDefault<z.ZodOptional<z.ZodObject<{
6
+ discard: z.ZodDefault<z.ZodOptional<z.ZodString>>;
7
+ hand: z.ZodDefault<z.ZodOptional<z.ZodString>>;
8
+ score: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
9
+ }, "strict", z.ZodTypeAny, {
10
+ discard: string;
11
+ hand: string;
12
+ score: number;
13
+ }, {
14
+ discard?: string | undefined;
15
+ hand?: string | undefined;
16
+ score?: number | undefined;
17
+ }>>>;
18
+ "2w": z.ZodDefault<z.ZodOptional<z.ZodObject<{
19
+ discard: z.ZodDefault<z.ZodOptional<z.ZodString>>;
20
+ hand: z.ZodDefault<z.ZodOptional<z.ZodString>>;
21
+ score: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
22
+ }, "strict", z.ZodTypeAny, {
23
+ discard: string;
24
+ hand: string;
25
+ score: number;
26
+ }, {
27
+ discard?: string | undefined;
28
+ hand?: string | undefined;
29
+ score?: number | undefined;
30
+ }>>>;
31
+ "3w": z.ZodDefault<z.ZodOptional<z.ZodObject<{
32
+ discard: z.ZodDefault<z.ZodOptional<z.ZodString>>;
33
+ hand: z.ZodDefault<z.ZodOptional<z.ZodString>>;
34
+ score: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
35
+ }, "strict", z.ZodTypeAny, {
36
+ discard: string;
37
+ hand: string;
38
+ score: number;
39
+ }, {
40
+ discard?: string | undefined;
41
+ hand?: string | undefined;
42
+ score?: number | undefined;
43
+ }>>>;
44
+ "4w": z.ZodDefault<z.ZodOptional<z.ZodObject<{
45
+ discard: z.ZodDefault<z.ZodOptional<z.ZodString>>;
46
+ hand: z.ZodDefault<z.ZodOptional<z.ZodString>>;
47
+ score: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
48
+ }, "strict", z.ZodTypeAny, {
49
+ discard: string;
50
+ hand: string;
51
+ score: number;
52
+ }, {
53
+ discard?: string | undefined;
54
+ hand?: string | undefined;
55
+ score?: number | undefined;
56
+ }>>>;
57
+ board: z.ZodObject<{
58
+ round: z.ZodDefault<z.ZodOptional<z.ZodUnion<readonly [z.ZodLiteral<"1w1" | "1w2" | "1w3" | "1w4" | "2w1" | "2w2" | "2w3" | "2w4">, z.ZodLiteral<"1w1" | "1w2" | "1w3" | "1w4" | "2w1" | "2w2" | "2w3" | "2w4">, ...z.ZodLiteral<"1w1" | "1w2" | "1w3" | "1w4" | "2w1" | "2w2" | "2w3" | "2w4">[]]>>>;
59
+ sticks: z.ZodDefault<z.ZodOptional<z.ZodObject<{
60
+ reach: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
61
+ dead: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
62
+ }, "strip", z.ZodTypeAny, {
63
+ reach: number;
64
+ dead: number;
65
+ }, {
66
+ reach?: number | undefined;
67
+ dead?: number | undefined;
68
+ }>>>;
69
+ doras: z.ZodDefault<z.ZodOptional<z.ZodArray<z.ZodString, "many">>>;
70
+ front: z.ZodDefault<z.ZodOptional<z.ZodUnion<readonly [z.ZodLiteral<"1w" | "2w" | "3w" | "4w">, z.ZodLiteral<"1w" | "2w" | "3w" | "4w">, ...z.ZodLiteral<"1w" | "2w" | "3w" | "4w">[]]>>>;
71
+ }, "strict", z.ZodTypeAny, {
72
+ round: "1w1" | "1w2" | "1w3" | "1w4" | "2w1" | "2w2" | "2w3" | "2w4";
73
+ sticks: {
74
+ reach: number;
75
+ dead: number;
76
+ };
77
+ doras: string[];
78
+ front: "1w" | "2w" | "3w" | "4w";
79
+ }, {
80
+ round?: "1w1" | "1w2" | "1w3" | "1w4" | "2w1" | "2w2" | "2w3" | "2w4" | undefined;
81
+ sticks?: {
82
+ reach?: number | undefined;
83
+ dead?: number | undefined;
84
+ } | undefined;
85
+ doras?: string[] | undefined;
86
+ front?: "1w" | "2w" | "3w" | "4w" | undefined;
87
+ }>;
88
+ }, "strict", z.ZodTypeAny, {
89
+ "1w": {
90
+ discard: string;
91
+ hand: string;
92
+ score: number;
93
+ };
94
+ "2w": {
95
+ discard: string;
96
+ hand: string;
97
+ score: number;
98
+ };
99
+ "3w": {
100
+ discard: string;
101
+ hand: string;
102
+ score: number;
103
+ };
104
+ "4w": {
105
+ discard: string;
106
+ hand: string;
107
+ score: number;
108
+ };
109
+ board: {
110
+ round: "1w1" | "1w2" | "1w3" | "1w4" | "2w1" | "2w2" | "2w3" | "2w4";
111
+ sticks: {
112
+ reach: number;
113
+ dead: number;
114
+ };
115
+ doras: string[];
116
+ front: "1w" | "2w" | "3w" | "4w";
117
+ };
118
+ }, {
119
+ board: {
120
+ round?: "1w1" | "1w2" | "1w3" | "1w4" | "2w1" | "2w2" | "2w3" | "2w4" | undefined;
121
+ sticks?: {
122
+ reach?: number | undefined;
123
+ dead?: number | undefined;
124
+ } | undefined;
125
+ doras?: string[] | undefined;
126
+ front?: "1w" | "2w" | "3w" | "4w" | undefined;
127
+ };
128
+ "1w"?: {
129
+ discard?: string | undefined;
130
+ hand?: string | undefined;
131
+ score?: number | undefined;
132
+ } | undefined;
133
+ "2w"?: {
134
+ discard?: string | undefined;
135
+ hand?: string | undefined;
136
+ score?: number | undefined;
137
+ } | undefined;
138
+ "3w"?: {
139
+ discard?: string | undefined;
140
+ hand?: string | undefined;
141
+ score?: number | undefined;
142
+ } | undefined;
143
+ "4w"?: {
144
+ discard?: string | undefined;
145
+ hand?: string | undefined;
146
+ score?: number | undefined;
147
+ } | undefined;
148
+ }>;
149
+ export type TableInput = z.infer<typeof tableInputSchema>;
150
+ export interface DiscardsInput {
151
+ front: Tile[];
152
+ right: Tile[];
153
+ opposite: Tile[];
154
+ left: Tile[];
155
+ }
156
+ export interface HandsInput {
157
+ front: Block[];
158
+ right: Block[];
159
+ opposite: Block[];
160
+ left: Block[];
161
+ }
162
+ export interface ScoreBoardInput {
163
+ doras: Tile[];
164
+ round: BoardRound;
165
+ sticks: {
166
+ reach: number;
167
+ dead: number;
168
+ };
169
+ scores: {
170
+ front: number;
171
+ right: number;
172
+ opposite: number;
173
+ left: number;
174
+ };
175
+ frontPlace: BoardWind;
176
+ }
177
+ type BoardRound = (typeof ROUND_MAP)[keyof typeof ROUND_MAP];
178
+ type BoardWind = (typeof WIND_MAP)[keyof typeof WIND_MAP];
179
+ export declare const parse: (s: string) => {
180
+ discards: DiscardsInput;
181
+ hands: HandsInput;
182
+ scoreBoard: ScoreBoardInput;
183
+ };
184
+ export declare const parseTableInput: (s: string) => {
185
+ "1w": {
186
+ discard: string;
187
+ hand: string;
188
+ score: number;
189
+ };
190
+ "2w": {
191
+ discard: string;
192
+ hand: string;
193
+ score: number;
194
+ };
195
+ "3w": {
196
+ discard: string;
197
+ hand: string;
198
+ score: number;
199
+ };
200
+ "4w": {
201
+ discard: string;
202
+ hand: string;
203
+ score: number;
204
+ };
205
+ board: {
206
+ round: "1w1" | "1w2" | "1w3" | "1w4" | "2w1" | "2w2" | "2w3" | "2w4";
207
+ sticks: {
208
+ reach: number;
209
+ dead: number;
210
+ };
211
+ doras: string[];
212
+ front: "1w" | "2w" | "3w" | "4w";
213
+ };
214
+ };
215
+ export declare const convertInput: (i: TableInput) => {
216
+ discards: DiscardsInput;
217
+ hands: HandsInput;
218
+ scoreBoard: ScoreBoardInput;
219
+ };
220
+ export {};
@@ -0,0 +1,114 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.convertInput = exports.parseTableInput = exports.parse = void 0;
4
+ var js_yaml_1 = require("js-yaml");
5
+ var zod_1 = require("zod");
6
+ var parser_1 = require("./parser");
7
+ var constants_1 = require("./constants");
8
+ var windInputSchema = zod_1.z
9
+ .object({
10
+ discard: zod_1.z.string().max(50).optional().default(""),
11
+ hand: zod_1.z.string().max(25).optional().default(""),
12
+ score: zod_1.z.number().optional().default(25000),
13
+ })
14
+ .strict()
15
+ .optional()
16
+ .default({ discard: "", hand: "", score: 25000 });
17
+ var windInputsSchema = zod_1.z
18
+ .object({
19
+ "1w": windInputSchema,
20
+ "2w": windInputSchema,
21
+ "3w": windInputSchema,
22
+ "4w": windInputSchema,
23
+ })
24
+ .strict();
25
+ function unionOfLiterals(constants) {
26
+ var literals = constants.map(function (x) { return zod_1.z.literal(x); });
27
+ return zod_1.z.union(literals);
28
+ }
29
+ var boardInputSchema = zod_1.z
30
+ .object({
31
+ round: unionOfLiterals(Object.keys(constants_1.ROUND_MAP))
32
+ .optional()
33
+ .default("1w1"),
34
+ sticks: zod_1.z
35
+ .object({
36
+ reach: zod_1.z.number().max(9).gte(0).optional().default(0),
37
+ dead: zod_1.z.number().max(9).gte(0).optional().default(0),
38
+ })
39
+ .optional()
40
+ .default({ reach: 0, dead: 0 }),
41
+ doras: zod_1.z.array(zod_1.z.string()).max(4).optional().default(["3w"]),
42
+ front: unionOfLiterals(Object.keys(constants_1.WIND_MAP))
43
+ .optional()
44
+ .default("1w"),
45
+ })
46
+ .strict();
47
+ var tableInputSchema = windInputsSchema.extend({
48
+ board: boardInputSchema,
49
+ });
50
+ var parse = function (s) {
51
+ var d = (0, exports.parseTableInput)(s);
52
+ return (0, exports.convertInput)(d);
53
+ };
54
+ exports.parse = parse;
55
+ var parseTableInput = function (s) {
56
+ var rawInput = (0, js_yaml_1.load)(s);
57
+ var ret = tableInputSchema.safeParse(rawInput.table);
58
+ if (!ret.success) {
59
+ throw ret.error;
60
+ }
61
+ return ret.data;
62
+ };
63
+ exports.parseTableInput = parseTableInput;
64
+ var convertInput = function (i) {
65
+ var frontPlace = i.board.front;
66
+ var m = createPlaceMap(frontPlace);
67
+ var f = function (w) {
68
+ return i[w].discard.replace(/\r?\n/g, "");
69
+ };
70
+ var discards = {
71
+ front: new parser_1.Parser(f(m.front)).parseInput(),
72
+ right: new parser_1.Parser(f(m.right)).parseInput(),
73
+ opposite: new parser_1.Parser(f(m.opposite)).parseInput(),
74
+ left: new parser_1.Parser(f(m.left)).parseInput(),
75
+ };
76
+ var hands = {
77
+ front: new parser_1.Parser(i[m.front].hand).parse(),
78
+ right: new parser_1.Parser(i[m.right].hand).parse(),
79
+ opposite: new parser_1.Parser(i[m.opposite].hand).parse(),
80
+ left: new parser_1.Parser(i[m.left].hand).parse(),
81
+ };
82
+ var scoreBoard = {
83
+ round: constants_1.ROUND_MAP[i.board.round],
84
+ frontPlace: constants_1.WIND_MAP[frontPlace],
85
+ sticks: i.board.sticks,
86
+ doras: i.board.doras.map(function (v) {
87
+ return new parser_1.Parser(v).parseInput()[0];
88
+ }),
89
+ scores: {
90
+ front: i[m.front].score,
91
+ right: i[m.right].score,
92
+ opposite: i[m.opposite].score,
93
+ left: i[m.left].score,
94
+ },
95
+ };
96
+ return { discards: discards, hands: hands, scoreBoard: scoreBoard };
97
+ };
98
+ exports.convertInput = convertInput;
99
+ var createPlaceMap = function (front) {
100
+ var f = function (start, v) {
101
+ var ret = "".concat(v, "w");
102
+ if (v > 4)
103
+ ret = "".concat(v - start, "w");
104
+ return ret;
105
+ };
106
+ var start = Number(front[0]);
107
+ var m = {
108
+ front: front,
109
+ right: f(start, start + 1),
110
+ opposite: f(start, start + 2),
111
+ left: f(start, start + 3),
112
+ };
113
+ return m;
114
+ };