foundryvtt-shadows-over-sol/module/sos-card-deck.js

105 lines
3.3 KiB
JavaScript

/* -------------------------------------------- */
const NB_POKER_CARD = 54;
const IDX2CARDFAMILY = ['c', 'd', 'h', 's'];
/* -------------------------------------------- */
export class SoSCardDeck {
/* -------------------------------------------- */
initCardDeck(actor, savedDeck = undefined ) {
this.data = {};
this.data.deck = [];
this.data.discard = [];
this.data.cardState = [];
this.data.cardEdge = [];
if ( savedDeck.deck && savedDeck.deck.length > 0 ) {}
this.data.deck = duplicate(savedDeck.deck);
if ( savedDeck.discard && savedDeck.discard.length > 0 ) {}
this.data.discard = duplicate(savedDeck.discard);
if ( savedDeck.cardEdge && savedDeck.cardEdge.length > 0 ) {}
this.data.cardEdge = duplicate(savedDeck.cardEdge);
this.data.actor = actor;
if ( this.data.deck.length == 0 && this.data.discard.length == 0) {
this.shuffleDeck();
}
}
/* -------------------------------------------- */
shuffleDeck() {
this.cleanCardList();
// Randomize deck
while (this.data.deck.length != NB_POKER_CARD) {
let idx = new Roll("1d54").roll().total;
if (!this.data.cardState[idx - 1]) {
if (idx == 53) { // Red Joker
this.data.deck.push( { cardName: 'jr' } );
} else if (idx == 54) { // Black Joker
this.data.deck.push({ cardName: 'jb' });
} else {
let familyIdx = idx % 4;
let cardIdx = String( (idx % 13) + 1);
cardIdx = (cardIdx.length < 2) ? "0"+cardIdx: cardIdx;
let cardName = IDX2CARDFAMILY[familyIdx] + cardIdx;
this.data.deck.push( { cardName: cardName } );
}
this.data.cardState[idx - 1] = true;
}
}
}
/* -------------------------------------------- */
drawEdge( edgeNumber ) {
for (let i=0; i<edgeNumber; i++) {
this.data.cardEdge.push( this.data.deck.pop() );
console.log("DRAW EDGE", this.data.cardEdge);
}
}
/* -------------------------------------------- */
cleanCardList() {
this.data.discard = []; // Reinit discard pile
this.data.deck = [];
this.data.cardEdge = [];
for (let i = 0; i < NB_POKER_CARD; i++) {
this.data.cardState[i] = false;
}
}
/* -------------------------------------------- */
doFlipFromDeck( ) {
let card = this.data.deck.pop();
this.data.discard.push( card );
console.log("CARD IS : ", card, this.data.deck.length );
}
/* -------------------------------------------- */
getDeckHTML( ) {
return "<img class='view-deck flip-card' src='systems/foundryvtt-shadows-over-sol/img/cards/card_back.webp' />";
}
/* -------------------------------------------- */
getEdgeHTML( ) {
let html = "";
for (let edge of this.data.cardEdge) {
html += `<img class='view-discard flip-card' src='systems/foundryvtt-shadows-over-sol/img/cards/${edge.cardName}.webp' />`
}
return html;
}
/* -------------------------------------------- */
getDiscardTopHTML( ) {
let html = "";
console.log( "DISCARD: ", this.data.discard );
if ( this.data.discard.length > 0) {
let card = this.data.discard[this.data.discard.length-1];
html = `<img class='view-discard flip-card' src='systems/foundryvtt-shadows-over-sol/img/cards/${card.cardName}.webp' />`;
}
return html;
}
}