cardation
Version:
fundation of card games, card model
135 lines (134 loc) • 4.46 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.short2char_map = exports.short2entity_map = exports.getGraph = void 0;
exports.parseCardFromId = parseCardFromId;
const Heart_1 = require("../suit/Heart");
const Diamond_1 = require("../suit/Diamond");
const Spade_1 = require("../suit/Spade");
const Club_1 = require("../suit/Club");
const CardFactory_1 = require("../../tool/CardFactory");
const CardError_1 = require("../../error/CardError");
const JokerCard_1 = require("../card/JokerCard");
const RedJokerCard_1 = require("../card/RedJokerCard");
const MarkerCard_1 = require("../card/MarkerCard");
/**
* Get the colored graph of a card.
* @param card A SuitCard instance
* @returns Colored string which can show in console
*/
const getGraph = (card) => {
const line1 = '┌─────┐';
const line5 = '\n└─────┘';
let line2, line3, line4;
const colorise = (str) => str;
const rank = card.getRank();
let score = rank + '';
if (card instanceof MarkerCard_1.default) {
/* eslint-disable no-useless-assignment */
line2 = '\n│ |';
line4 = '\n│ |';
line3 = '\n│ M │';
}
if (card instanceof JokerCard_1.default) {
line2 = '\n│ |';
line4 = '\n│ |';
if (card instanceof RedJokerCard_1.default) {
// colorise = chalk.red
}
else {
// colorise = (str: string)=>str
}
const coloredNumber = colorise('Joker');
line3 = `\n│${coloredNumber}│`;
}
else {
if (rank === 1) {
score = 'A';
}
else if (rank === 10) {
// score = 'T'
// score = '+'
}
else if (rank === 11) {
score = 'J';
}
else if (rank === 12) {
score = 'Q';
}
else if (rank === 13) {
score = 'K';
}
const suit = card.getCardSuit().getShortName();
if (suit === 'h' || suit === 'd') {
// colorise = chalk.red
}
const coloredNumber = colorise(score);
const coloredSuit = colorise(short2char_map[card.getCardSuit().getShortName()]);
line2 = `\n│${coloredSuit} │`;
line3 = `\n│ ${coloredNumber} │`;
line4 = `\n│ ${coloredSuit}│`;
if (rank == 10) {
line3 = `\n│ ${coloredNumber} │`;
}
}
return `${line1}${line2}${line3}${line4}${line5}`;
};
exports.getGraph = getGraph;
const short2entity_map = {
h: new Heart_1.default(),
d: new Diamond_1.default(),
s: new Spade_1.default(),
c: new Club_1.default(),
};
exports.short2entity_map = short2entity_map;
const short2char_map = {
h: '♥',
d: '♦',
s: '♠',
c: '♣',
};
exports.short2char_map = short2char_map;
/**
* Parse a card from a string id.
* @param id the id of a card
* @returns the card parsed from the id
*/
function parseCardFromId(id) {
const id_regexp = /^[a-zA-Z]\d{1,3}\.\d{1,4}$/;
if (!id_regexp.test(id)) {
throw new CardError_1.default('[utils][parseCardFromId]: the format of id should match /^[a-zA-Z]\\d{1,3}\\.\\d{1,4}$/!');
}
const info_array = id.split('.');
const [firstPart] = info_array;
const type = firstPart.slice(0, 1);
const rank = +firstPart.slice(1);
const point = +info_array[1];
// if (suit.length !== 1) {
// throw new CardError(`[utils][CardId]: suit should be a single letter!`)
// }
// if (typeof rank != "number") {
// throw new CardError(`[utils][CardId]: rank should be a number!`)
// }
// if (typeof point != "number") {
// throw new CardError(`[utils][CardId]: point should be a number!`)
// }
if (type == 'b') {
return CardFactory_1.default.createBlackCard(point);
}
if (type == 'j') {
if (rank === 31) {
return CardFactory_1.default.createBlackJokerCard(rank, point);
}
return CardFactory_1.default.createRedJokerCard(rank, point);
}
// if (['h', 'd', 's', 'c'].includes(type)) {
// }
const suit = short2entity_map[type];
if (rank === 1) {
return CardFactory_1.default.createAceCard(suit, point);
}
if (rank < 11) {
return CardFactory_1.default.createNumberCard(suit, rank, point);
}
return CardFactory_1.default.createFaceCard(suit, rank, point);
}