mirror of
https://github.com/tcgdex/cards-database.git
synced 2025-07-29 19:19:50 +00:00
@ -5,6 +5,7 @@ import { CardResume, Card as CardSingle } from '../../../meta/definitions/api'
|
||||
import { getSet, setToSetSimple } from './setUtil'
|
||||
import translate from './translationUtil'
|
||||
import { DB_PATH, cardIsLegal, fetchRemoteFile, getDataFolder, getLastEdit, smartGlob } from './util'
|
||||
import { objectRemap, objectSize } from '@dzeio/object-util'
|
||||
|
||||
export async function getCardPictures(cardId: string, card: Card, lang: SupportedLanguages): Promise<string | undefined> {
|
||||
try {
|
||||
@ -41,6 +42,21 @@ export async function cardToCardSingle(localId: string, card: Card, lang: Suppor
|
||||
throw new Error(`Card (${localId}) dont exist in (${lang})`)
|
||||
}
|
||||
|
||||
const set = card.set
|
||||
|
||||
const pullRates: CardSingle['pullRates'] = {}
|
||||
|
||||
if (set.pullRates) {
|
||||
for (const type of Object.keys(set.pullRates)) {
|
||||
const value = set.pullRates[type]
|
||||
pullRates[type] = {
|
||||
rate: value.rate,
|
||||
slots: await Promise.all(value.slots.map(async (it) => it[card.rarity] ? await getPullRate(set, card, it[card.rarity], lang) : 0)),
|
||||
total_slots: await Promise.all(value.slots.map(async (it) => it[card.rarity] ? await getPullRate(set, card, it[card.rarity] * (value.rate / 100), lang) : 0)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
category: translate('category', card.category, lang) as any,
|
||||
id: `${card.set.id}-${localId}`,
|
||||
@ -59,6 +75,8 @@ export async function cardToCardSingle(localId: string, card: Card, lang: Suppor
|
||||
wPromo: typeof card.variants?.wPromo === 'boolean' ? card.variants.wPromo : false
|
||||
},
|
||||
|
||||
boosters: card.boosters,
|
||||
pullRates: objectSize(pullRates) > 0 ? pullRates : undefined,
|
||||
|
||||
dexId: card.dexId,
|
||||
hp: card.hp,
|
||||
@ -126,6 +144,23 @@ export async function getCard(set: Set, id: string, lang: SupportedLanguages): P
|
||||
}
|
||||
}
|
||||
|
||||
function arrayIntersect(first: Array<string>, second: Array<string>) {
|
||||
return !!first.find((it) => second.includes(it))
|
||||
}
|
||||
|
||||
async function getPullRate(set: Set, card: Card, totalRate: number, lang: SupportedLanguages) {
|
||||
const otherCards = await getCards(lang, set)
|
||||
.then((cards) => cards
|
||||
.map((it) => it[1])
|
||||
// filter cards with same rarity
|
||||
// exclude cards that is only avialable in other boosters
|
||||
.filter((it) => it.rarity === card.rarity && (!it.boosters || !card.boosters || arrayIntersect(card.boosters, it.boosters)))
|
||||
)
|
||||
const count = otherCards.length // remove one to exclude current card as it ill match itself
|
||||
|
||||
return 1 * (totalRate / 100) / count * 100
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cards filtered by the language they are available in
|
||||
* @param lang the language of the cards
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { objectKeys } from '@dzeio/object-util'
|
||||
import { objectKeys, objectMap } from '@dzeio/object-util'
|
||||
import { Set, SupportedLanguages } from '../../../interfaces'
|
||||
import { SetResume, Set as SetSingle } from '../../../meta/definitions/api'
|
||||
import { cardToCardSimple, getCards } from './cardUtil'
|
||||
@ -101,6 +101,8 @@ export async function setToSetSingle(set: Set, lang: SupportedLanguages): Promis
|
||||
id: set.serie.id,
|
||||
name: set.serie.name[lang] as string
|
||||
},
|
||||
boosters: set.boosters ? objectMap(set.boosters, (v, k) => v[lang]) : undefined,
|
||||
pullRates: set.pullRates,
|
||||
symbol: pics[1],
|
||||
tcgOnline: set.tcgOnline,
|
||||
abbreviation: (set.abbreviations?.official || set.abbreviations?.[lang]) ? {
|
||||
|
Reference in New Issue
Block a user