UNPKG

tetris-fumen

Version:
232 lines (231 loc) 8.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Quiz = void 0; var defines_1 = require("./defines"); var Operation; (function (Operation) { Operation["Direct"] = "direct"; Operation["Swap"] = "swap"; Operation["Stock"] = "stock"; })(Operation || (Operation = {})); var Quiz = /** @class */ (function () { function Quiz(quiz) { this.quiz = Quiz.verify(quiz); } Object.defineProperty(Quiz.prototype, "next", { get: function () { var index = this.quiz.indexOf(')') + 1; var name = this.quiz[index]; if (name === undefined || name === ';') { return ''; } return name; }, enumerable: false, configurable: true }); Quiz.isQuizComment = function (comment) { return comment.startsWith('#Q='); }; Quiz.create = function (first, second) { var create = function (hold, other) { var parse = function (s) { return s ? s : ''; }; return new Quiz("#Q=[".concat(parse(hold), "](").concat(parse(other[0]), ")").concat(parse(other.substring(1)))); }; return second !== undefined ? create(first, second) : create(undefined, first); }; Quiz.trim = function (quiz) { return quiz.trim().replace(/\s+/g, ''); }; Object.defineProperty(Quiz.prototype, "least", { get: function () { var index = this.quiz.indexOf(')'); return this.quiz.substr(index + 1); }, enumerable: false, configurable: true }); Object.defineProperty(Quiz.prototype, "current", { get: function () { var index = this.quiz.indexOf('(') + 1; var name = this.quiz[index]; if (name === ')') { return ''; } return name; }, enumerable: false, configurable: true }); Object.defineProperty(Quiz.prototype, "hold", { get: function () { var index = this.quiz.indexOf('[') + 1; var name = this.quiz[index]; if (name === ']') { return ''; } return name; }, enumerable: false, configurable: true }); Object.defineProperty(Quiz.prototype, "leastAfterNext2", { get: function () { var index = this.quiz.indexOf(')'); if (this.quiz[index + 1] === ';') { return this.quiz.substr(index + 1); } return this.quiz.substr(index + 2); }, enumerable: false, configurable: true }); Quiz.prototype.getOperation = function (used) { var usedName = (0, defines_1.parsePieceName)(used); var current = this.current; if (usedName === current) { return Operation.Direct; } var hold = this.hold; if (usedName === hold) { return Operation.Swap; } // 次のミノを利用できる if (hold === '') { if (usedName === this.next) { return Operation.Stock; } } else { if (current === '' && usedName === this.next) { return Operation.Direct; } } throw new Error("Unexpected hold piece in quiz: ".concat(this.quiz)); }; Object.defineProperty(Quiz.prototype, "leastInActiveBag", { get: function () { var separateIndex = this.quiz.indexOf(';'); var quiz = 0 <= separateIndex ? this.quiz.substring(0, separateIndex) : this.quiz; var index = quiz.indexOf(')'); if (quiz[index + 1] === ';') { return quiz.substr(index + 1); } return quiz.substr(index + 2); }, enumerable: false, configurable: true }); Quiz.verify = function (quiz) { var replaced = this.trim(quiz); if (replaced.length === 0 || quiz === '#Q=[]()' || !quiz.startsWith('#Q=')) { return quiz; } if (!replaced.match(/^#Q=\[[TIOSZJL]?]\([TIOSZJL]?\)[TIOSZJL]*;?.*$/i)) { throw new Error("Current piece doesn't exist, however next pieces exist: ".concat(quiz)); } return replaced; }; Quiz.prototype.direct = function () { if (this.current === '') { var least = this.leastAfterNext2; return new Quiz("#Q=[".concat(this.hold, "](").concat(least[0], ")").concat(least.substr(1))); } return new Quiz("#Q=[".concat(this.hold, "](").concat(this.next, ")").concat(this.leastAfterNext2)); }; Quiz.prototype.swap = function () { if (this.hold === '') { throw new Error("Cannot find hold piece: ".concat(this.quiz)); } var next = this.next; return new Quiz("#Q=[".concat(this.current, "](").concat(next, ")").concat(this.leastAfterNext2)); }; Quiz.prototype.stock = function () { if (this.hold !== '' || this.next === '') { throw new Error("Cannot stock: ".concat(this.quiz)); } var least = this.leastAfterNext2; var head = least[0] !== undefined ? least[0] : ''; if (1 < least.length) { return new Quiz("#Q=[".concat(this.current, "](").concat(head, ")").concat(least.substr(1))); } return new Quiz("#Q=[".concat(this.current, "](").concat(head, ")")); }; Quiz.prototype.operate = function (operation) { switch (operation) { case Operation.Direct: return this.direct(); case Operation.Swap: return this.swap(); case Operation.Stock: return this.stock(); } throw new Error('Unexpected operation'); }; Quiz.prototype.format = function () { var quiz = this.nextIfEnd(); if (quiz.quiz === '#Q=[]()') { return new Quiz(''); } var current = quiz.current; var hold = quiz.hold; if (current === '' && hold !== '') { return new Quiz("#Q=[](".concat(hold, ")").concat(quiz.least)); } if (current === '') { var least = quiz.least; var head = least[0]; if (head === undefined) { return new Quiz(''); } if (head === ';') { return new Quiz(least.substr(1)); } return new Quiz("#Q=[](".concat(head, ")").concat(least.substr(1))); } return quiz; }; Quiz.prototype.getHoldPiece = function () { if (!this.canOperate()) { return defines_1.Piece.Empty; } var name = this.hold; if (name === undefined || name === '' || name === ';') { return defines_1.Piece.Empty; } return (0, defines_1.parsePiece)(name); }; Quiz.prototype.getNextPieces = function (max) { if (!this.canOperate()) { return max !== undefined ? Array.from({ length: max }).map(function () { return defines_1.Piece.Empty; }) : []; } var names = (this.current + this.next + this.leastInActiveBag).substr(0, max); if (max !== undefined && names.length < max) { names += ' '.repeat(max - names.length); } return names.split('').map(function (name) { if (name === undefined || name === ' ' || name === ';') { return defines_1.Piece.Empty; } return (0, defines_1.parsePiece)(name); }); }; Quiz.prototype.toString = function () { return this.quiz; }; Quiz.prototype.canOperate = function () { var quiz = this.quiz; if (quiz.startsWith('#Q=[]();')) { quiz = this.quiz.substr(8); } return quiz.startsWith('#Q=') && quiz !== '#Q=[]()'; }; Quiz.prototype.nextIfEnd = function () { if (this.quiz.startsWith('#Q=[]();')) { return new Quiz(this.quiz.substr(8)); } return this; }; return Quiz; }()); exports.Quiz = Quiz;