@typed-tabletop-simulator/lib
Version:
Library with some helping modules for working with Tabletop Simulator
80 lines (79 loc) • 2.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.combineDecks = exports.createDeck = exports.setCurrentDeckId = void 0;
const baseObject_1 = require("./baseObject");
// The latest generated deck id. Will be incremented by 1 each time a new deck is created.
let currentDeckId = 1337;
// The list of alread created decks ids based on the URL to the front image.
const createdDeckIds = new Map();
/**
* Sets the starting deck id that will be used.
* This is useful if multiple processes create content for the same TTS save, since otherwise their deck ids would clash.
*/
const setCurrentDeckId = (id) => (currentDeckId = id);
exports.setCurrentDeckId = setCurrentDeckId;
/**
* Creates a new deck with the given properties.
*
* By default the `BackIsHidden` property is set to `true`.
*/
const createDeck = (properties) => {
const deckId = getDeckId(properties);
const customDeck = {
[deckId]: {
Type: properties.type ?? 0 /* CardType.RectangleRounded */,
FaceURL: properties.front,
BackURL: properties.back,
NumWidth: properties.width,
NumHeight: properties.height,
BackIsHidden: true,
UniqueBack: properties.uniqueBack ?? false,
},
};
const createCard = (card) => {
return {
...(0, baseObject_1.createBaseObject)(card, "Card" /* ObjectName.Card */),
CardID: getCardId(deckId, card),
CustomDeck: customDeck,
};
};
const cards = properties.cards.map((c) => createCard(c)).reverse();
const cardIds = cards.map((c) => c.CardID);
return {
...(0, baseObject_1.createBaseObject)(properties, "DeckCustom" /* ObjectName.DeckCustom */),
DeckIDs: cardIds,
CustomDeck: customDeck,
ContainedObjects: cards,
};
};
exports.createDeck = createDeck;
/**
* Combines the given decks into a single deck. The result deck has the properties (name, tags, etc.) of the first deck in the list.
* Only the cards of the other decks will be added to the first deck.
*
* *NOTE*: This operation mutates the first deck!
*/
const combineDecks = (decks) => {
const baseDeck = decks[0];
for (let i = 1; i < decks.length; i++) {
const deck = decks[i];
baseDeck.ContainedObjects.push(...deck.ContainedObjects);
baseDeck.DeckIDs.push(...deck.DeckIDs);
baseDeck.CustomDeck = { ...baseDeck.CustomDeck, ...deck.CustomDeck };
}
return baseDeck;
};
exports.combineDecks = combineDecks;
const getDeckId = (deck) => {
const existing = createdDeckIds.get(deck.front);
if (existing) {
return existing;
}
createdDeckIds.set(deck.front, currentDeckId);
currentDeckId++;
return currentDeckId - 1;
};
const getCardId = (deckId, card) => {
const index = `${card.index}`.padStart(2, "0");
return Number(`${deckId}${index}`);
};