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

263 lines
10 KiB
JavaScript
Raw Normal View History

2021-01-18 16:11:27 +01:00
/* -------------------------------------------- */
const NB_POKER_CARD = 54;
const IDX2CARDFAMILY = ['c', 'd', 'h', 's'];
/* -------------------------------------------- */
2021-01-18 22:05:02 +01:00
export class SoSCardDeck {
2021-01-18 16:11:27 +01:00
/* -------------------------------------------- */
2021-01-19 22:54:53 +01:00
initCardDeck(actor, savedDeck = undefined ) {
2021-01-18 22:05:02 +01:00
this.data = {};
2021-01-19 22:54:53 +01:00
2021-01-18 22:05:02 +01:00
this.data.deck = [];
this.data.discard = [];
this.data.cardState = [];
this.data.cardEdge = [];
2021-01-19 22:54:53 +01:00
2021-01-31 17:23:14 +01:00
if ( savedDeck.deck && savedDeck.deck.length > 0 ) {
2021-01-19 22:54:53 +01:00
this.data.deck = duplicate(savedDeck.deck);
2021-01-31 17:23:14 +01:00
}
if ( savedDeck.discard && savedDeck.discard.length > 0 ) {
2021-01-19 22:54:53 +01:00
this.data.discard = duplicate(savedDeck.discard);
2021-01-31 17:23:14 +01:00
}
if ( savedDeck.cardEdge && savedDeck.cardEdge.length > 0 ) {
2021-01-19 22:54:53 +01:00
this.data.cardEdge = duplicate(savedDeck.cardEdge);
2021-01-31 17:23:14 +01:00
}
2021-01-19 22:54:53 +01:00
this.data.actor = actor;
if ( this.data.deck.length == 0 && this.data.discard.length == 0) {
this.shuffleDeck();
}
2021-01-18 16:11:27 +01:00
}
/* -------------------------------------------- */
shuffleDeck() {
this.cleanCardList();
// Randomize deck
2021-01-18 22:05:02 +01:00
while (this.data.deck.length != NB_POKER_CARD) {
2021-01-18 16:11:27 +01:00
let idx = new Roll("1d54").roll().total;
if (!this.data.cardState[idx - 1]) {
if (idx == 53) { // Red Joker
2021-01-18 22:05:02 +01:00
this.data.deck.push( { cardName: 'jr' } );
2021-01-18 16:11:27 +01:00
} else if (idx == 54) { // Black Joker
2021-01-18 22:05:02 +01:00
this.data.deck.push({ cardName: 'jb' });
2021-01-18 16:11:27 +01:00
} else {
let familyIdx = idx % 4;
2021-01-18 22:05:02 +01:00
let cardIdx = String( (idx % 13) + 1);
cardIdx = (cardIdx.length < 2) ? "0"+cardIdx: cardIdx;
let cardName = IDX2CARDFAMILY[familyIdx] + cardIdx;
this.data.deck.push( { cardName: cardName } );
2021-01-18 16:11:27 +01:00
}
this.data.cardState[idx - 1] = true;
}
2021-01-18 22:05:02 +01:00
}
}
/* -------------------------------------------- */
2021-01-31 17:39:37 +01:00
resetDeck() {
let newdeck = duplicate(this.data.deck).concat( duplicate (this.data.discard));
this.data.discard = []; // Reinit discard pile
this.data.deck = [];
let decklen = newdeck.len;
let cardState = [];
for (let i = 0; i <decklen; i++) {
cardState[i] = false;
}
// Randomize deck
while (this.data.deck.length != decklen) {
let idx = new Roll("1d"+decklen).roll().total;
if (!cardState[idx - 1]) {
this.data.deck.push( newdeck[idx-1] );
}
cardState[idx - 1] = true;
}
}
/* -------------------------------------------- */
drawEdge( edgeNumber = 1 ) {
2021-01-21 17:16:01 +01:00
for (let i=0; i<edgeNumber; i++) {
this.data.cardEdge.push( this.data.deck.pop() );
console.log("DRAW EDGE", this.data.cardEdge);
}
2021-01-18 16:11:27 +01:00
}
/* -------------------------------------------- */
cleanCardList() {
2021-01-18 22:05:02 +01:00
this.data.discard = []; // Reinit discard pile
this.data.deck = [];
2021-01-21 17:16:01 +01:00
this.data.cardEdge = [];
2021-01-18 16:11:27 +01:00
for (let i = 0; i < NB_POKER_CARD; i++) {
2021-01-18 22:05:02 +01:00
this.data.cardState[i] = false;
2021-01-18 16:11:27 +01:00
}
}
2021-01-18 22:05:02 +01:00
/* -------------------------------------------- */
2021-01-24 23:18:50 +01:00
getCardSuit( cardName ) {
if ( cardName[0] == 'c') return 'club';
if ( cardName[0] == 'd') return 'diamond';
if ( cardName[0] == 'h') return 'hearth';
if ( cardName[0] == 's') return 'spade';
if ( cardName[0] == 'j') return 'joker';
}
/* -------------------------------------------- */
drawFromDeck() {
2021-01-18 22:05:02 +01:00
let card = this.data.deck.pop();
2021-01-24 23:18:50 +01:00
this.data.discard.push( card );
return card;
}
/* -------------------------------------------- */
getFromEdge( cardName) {
let card = this.data.cardEdge.find( card => card.cardName == cardName); // Get the card
let newEdge = this.data.cardEdge.filter(card => card.cardName != cardName); // Remove used card
this.data.cardEdge = newEdge;
return card;
}
/* -------------------------------------------- */
getCardValue( cardName ) {
console.log(cardName);
2021-01-25 11:16:45 +01:00
if (cardName[0] == 'j' ) return 0; // Joker case
2021-01-24 23:18:50 +01:00
let parsed = cardName.match( /\w(\d\d)/i );
let value = Number( parsed[1] );
if ( value > 10 ) value -= 10;
return value;
}
/* -------------------------------------------- */
isCardFace(cardName) {
2021-01-25 11:16:45 +01:00
if (cardName[0] == 'j' ) return false; // Joker case
2021-01-24 23:18:50 +01:00
let parsed = cardName.match( /\w(\d\d)/i );
let value = Number( parsed[1] );
return (value > 10) ? true : false;
2021-01-18 22:05:02 +01:00
}
2021-01-21 17:16:01 +01:00
2021-01-25 11:16:45 +01:00
/* -------------------------------------------- */
setJoker( flipData ) {
console.log("THIS IS A JOKER !!!!");
flipData.cardSlot[0].total = 0;
flipData.cardSlot[0].card1Path = `systems/foundryvtt-shadows-over-sol/img/cards/${flipData.cardSlot[0].card1.cardName}.webp`;
flipData.isJoker = true;
}
/* -------------------------------------------- */
isJoker( cardName) {
return cardName[0] == 'j';
}
2021-01-24 23:18:50 +01:00
/* -------------------------------------------- */
async doFlipFromDeckOrEdge( flipData ) {
flipData.cardSlot = [ { total: 0}];
flipData.isTrump = false;
flipData.isJoker = false;
flipData.fullTrump = false;
// Select card origin
if ( flipData.cardOrigin == "Deck") {
flipData.cardSlot[0].card1 = this.drawFromDeck();
} else {
flipData.cardSlot[0].card1 = this.getFromEdge( flipData.edgeName );
}
let cardsuit = this.getCardSuit(flipData.cardSlot[0].card1.cardName);
if ( cardsuit == 'joker' ) {
2021-01-25 11:16:45 +01:00
this.setJoker( flipData );
2021-01-24 23:18:50 +01:00
} else {
console.log("First card : ", flipData.cardSlot[0].card1);
// Face check for first card
flipData.cardSlot[0].value1 = this.getCardValue(flipData.cardSlot[0].card1.cardName);
flipData.cardSlot[0].isFace1 = this.isCardFace(flipData.cardSlot[0].card1.cardName);
flipData.cardSlot[0].card1Path = `systems/foundryvtt-shadows-over-sol/img/cards/${flipData.cardSlot[0].card1.cardName}.webp`;
flipData.cardSlot[0].card2 = false;
if ( flipData.cardSlot[0].isFace1 ) {
flipData.cardSlot[0].card2 = this.drawFromDeck();
2021-01-25 11:16:45 +01:00
flipData.isJoker = this.isJoker(flipData.cardSlot[0].card2.cardName);
2021-01-24 23:18:50 +01:00
flipData.cardSlot[0].value2 = this.getCardValue(flipData.cardSlot[0].card2.cardName);
flipData.cardSlot[0].isFace2 = this.isCardFace(flipData.cardSlot[0].card2.cardName);
flipData.cardSlot[0].card2Path = `systems/foundryvtt-shadows-over-sol/img/cards/${flipData.cardSlot[0].card2.cardName}.webp`;
} else {
flipData.cardSlot[0].value2 = 0; // Safe init
}
flipData.cardSlot[0].total = flipData.cardSlot[0].value1 + flipData.cardSlot[0].value2;
// Trump check
flipData.cardSlot[0].cardsuit = cardsuit;
2021-01-25 11:16:45 +01:00
if ( ! flipData.isJoker && cardsuit == flipData.stat.cardsuit ) {
2021-01-24 23:18:50 +01:00
// This is a trump !
flipData.cardSlot[1] = { total: 0 };
flipData.isTrump = true;
flipData.cardSlot[1].card1 = this.drawFromDeck();
2021-01-25 11:16:45 +01:00
flipData.isJoker = this.isJoker(flipData.cardSlot[1].card1.cardName);
2021-01-24 23:18:50 +01:00
flipData.cardSlot[1].card1Path = `systems/foundryvtt-shadows-over-sol/img/cards/${flipData.cardSlot[1].card1.cardName}.webp`;
flipData.cardSlot[1].cardsuit = this.getCardSuit(flipData.cardSlot[1].card1.cardName);
flipData.cardSlot[1].value1 = this.getCardValue(flipData.cardSlot[1].card1.cardName);
flipData.cardSlot[1].isFace1 = this.isCardFace(flipData.cardSlot[1].card1.cardName);
2021-01-25 11:16:45 +01:00
if ( !flipData.isJoker && flipData.cardSlot[1].isFace1 ) {
2021-01-24 23:18:50 +01:00
flipData.cardSlot[1].card2 = this.drawFromDeck();
2021-01-25 11:16:45 +01:00
flipData.isJoker = this.isJoker(flipData.cardSlot[1].card2.cardName);
2021-01-24 23:18:50 +01:00
flipData.cardSlot[1].value2 = this.getCardValue(flipData.cardSlot[1].card2.cardName);
flipData.cardSlot[1].isFace2 = this.isCardFace(flipData.cardSlot[1].card2.cardName);
flipData.cardSlot[1].card2Path = `systems/foundryvtt-shadows-over-sol/img/cards/${flipData.cardSlot[1].card2.cardName}.webp`;
} else {
flipData.cardSlot[1].value2 = 0; // Safe init
}
if ( flipData.cardSlot[1].cardsuit == cardsuit ) {
flipData.fullTrump = true;
}
flipData.cardSlot[1].total = flipData.cardSlot[1].value1 + flipData.cardSlot[1].value2;
}
}
// Card Total
flipData.cardTotal = flipData.cardSlot[0].total;
if ( flipData.fullTrump ) {
flipData.cardTotal = flipData.cardSlot[0].total + flipData.cardSlot[1].total;
} else if (flipData.isTrump) {
flipData.cardTotal = (flipData.cardSlot[0].total > flipData.cardSlot[1].total) ? flipData.cardSlot[0].total : flipData.cardSlot[1].total;
}
// Compute final result and compare
if ( flipData.mode == 'stat' ) {
flipData.baseScore = flipData.stat.value;
} else if (flipData.mode == 'skill') {
flipData.baseScore = Math.floor(flipData.stat.value/2) + flipData.skill.data.value;
}
flipData.finalScore = flipData.baseScore + flipData.cardTotal + Number(flipData.modifier);
flipData.magnitude = flipData.finalScore - flipData.tn;
flipData.result = (flipData.magnitude >= 0) ? "Success": "Failure";
console.log(flipData);
this.data.actor.saveDeck();
flipData.alias = this.data.actor.name;
let html = await renderTemplate('systems/foundryvtt-shadows-over-sol/templates/chat-flip.html', flipData);
ChatMessage.create( { content: html });
}
2021-01-21 17:16:01 +01:00
/* -------------------------------------------- */
getDeckHTML( ) {
2021-01-24 23:18:50 +01:00
return "<a class='view-deck'><img class='flip-card deck-card' src='systems/foundryvtt-shadows-over-sol/img/cards/card_back.webp' /></a>";
2021-01-21 17:16:01 +01:00
}
/* -------------------------------------------- */
getEdgeHTML( ) {
let html = "";
for (let edge of this.data.cardEdge) {
2021-01-24 23:18:50 +01:00
html += `<a class='view-edge'><img class='flip-card edge-card' data-edge-card='${edge.cardName}' src='systems/foundryvtt-shadows-over-sol/img/cards/${edge.cardName}.webp' /></a>`
2021-01-21 17:16:01 +01:00
}
return html;
}
2021-01-18 22:05:02 +01:00
2021-01-21 17:16:01 +01:00
/* -------------------------------------------- */
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;
}
2021-01-18 16:11:27 +01:00
}