Feat: Add creating decks
This commit is contained in:
parent
7145906862
commit
aaa0bee853
26 changed files with 1279 additions and 180 deletions
|
@ -19,7 +19,7 @@ const Bsets = [
|
|||
{"name": "War of the Spark", sets: ["war"]},
|
||||
{"name": "Dominaria", sets: ["dom"]},
|
||||
{"name": "March of the machine", sets: ["mom","mat"]},
|
||||
{"name": "Innistrad Midnight Hunt", sets: ["wow","mid"]},
|
||||
{"name": "Innistrad Midnight Hunt", sets: ["vow","mid"]},
|
||||
{"name": "Guilds of Ravnica", sets: ["rna","grn"]},
|
||||
{"name": "Ixalan", sets: ["rix","xln"]},
|
||||
{"name": "Amonkhet", sets: ["hou","akh"]},
|
||||
|
@ -36,13 +36,13 @@ const Bsets = [
|
|||
{"name": "Ravnica", sets: ["rav","gpt","dis"]},
|
||||
{"name": "Kamigawa 3x", sets: ["chk","bok","sok"]},
|
||||
{"name": "Mirrodin", sets: ["5dn","dst","mrd"]},
|
||||
{"name": "Onslaught", sets: ["ons","lng","scg"]},
|
||||
{"name": "Onslaught", sets: ["ons","lgn","scg"]},
|
||||
{"name": "Odyssey", sets: ["ody","tor","jud"]},
|
||||
{"name": "Invasion", sets: ["inv","pls","apc"]},
|
||||
{"name": "Masques", sets: ["mmq","nem","pcy"]},
|
||||
{"name": "Urza", sets: ["usg","ulg","uds"]},
|
||||
{"name": "Tempest", sets: ["tmp","sth","exo"]},
|
||||
{"name": "Mirage", sets: ["wth","mir","wis"]},
|
||||
{"name": "Mirage", sets: ["wth","mir","vis"]},
|
||||
{"name": "Ice Age", sets: ["csp","ice","all"]},
|
||||
{"name": "Time Spiral", sets: ["fut","tsp","plc","tsb"]},
|
||||
{"name": "Lorwyn-Shadowmoor", sets: ["lrw","shm","mor","eve"]},
|
||||
|
|
|
@ -61,20 +61,36 @@ async function createJson() {
|
|||
include: {
|
||||
sets: {
|
||||
include: {
|
||||
cards: true
|
||||
cards: {
|
||||
include: {
|
||||
decks: true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
decks: true
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
let bsets_export = []
|
||||
let all_cards = []
|
||||
bsets.forEach((bset) => {
|
||||
let icons = []
|
||||
let set_codes = []
|
||||
bset.sets.forEach((set) => {
|
||||
all_cards = [...all_cards, ...set.cards]
|
||||
icons.push(set.icon_svg_uri)
|
||||
set_codes.push(set.code)
|
||||
let cards_temp = set.cards
|
||||
for(let i = 0; i < cards_temp.length; i++){
|
||||
cards_temp[i].bset_id = bset.id
|
||||
}
|
||||
all_cards = [...all_cards, ...cards_temp]
|
||||
})
|
||||
bsets_export.push({name: bset.name, sanitized_name: bset.sanitized_name, icons, set_codes})
|
||||
})
|
||||
|
||||
|
||||
writeFileSync(import.meta.dirname + "/json/misc/bsets.json",JSON.stringify(bsets_export), 'utf8')
|
||||
|
||||
|
||||
|
||||
|
@ -86,23 +102,43 @@ async function createJson() {
|
|||
const colorsData = {"mono-white": structuredClone(type_dict),"mono-black": structuredClone(type_dict),"mono-blue": structuredClone(type_dict),"mono-green": structuredClone(type_dict),"mono-red": structuredClone(type_dict),"colorless": structuredClone(type_dict),"multicolor": structuredClone(type_dict)}
|
||||
|
||||
for (const card of all_cards) {
|
||||
let card_object = {
|
||||
"name": card.name,
|
||||
"sanitized_name": card.sanitized_name,
|
||||
"normal_image": card.normal_image,
|
||||
"small_image": card.small_image,
|
||||
"type": card.type,
|
||||
"layout": card.layout,
|
||||
"price": card.price,
|
||||
"cardmarket_uri": card.cardmarket_uri,
|
||||
"nbr_decks": card.decks.length,
|
||||
"total_decks": bsets.find((bset) => bset.id == card.bset_id).decks.length,
|
||||
"colors": card.colors,
|
||||
}
|
||||
|
||||
card_object.percent_decks = (card_object.total_decks != 0) ? parseInt(10000 * (card_object.nbr_decks / card_object.total_decks)) / 100 : 0
|
||||
|
||||
const colorName = getColorName(card.colors)
|
||||
if (card.type == "land") {
|
||||
if (colorName != "") {
|
||||
landsData[colorName].push(card)
|
||||
landsData[colorName].push(card_object)
|
||||
}
|
||||
}
|
||||
|
||||
if (card.colors.length <= 1) {
|
||||
colorsData[colorName][card.type].push(card)
|
||||
colorsData[colorName][card.type].push(card_object)
|
||||
} else {
|
||||
colorsData["multicolor"][card.type].push(card)
|
||||
colorsData["multicolor"][card.type].push(card_object)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for (const index of Object.keys(colorsData)) {
|
||||
writeFileSync(import.meta.dirname + "/json/top/" + index + ".json",JSON.stringify(colorsData[index]), 'utf8')
|
||||
let JSONToWrite = colorsData[index]
|
||||
for (const key of Object.keys(JSONToWrite)){
|
||||
JSONToWrite[key].sort((a,b) => b.percent_decks - a.percent_decks)
|
||||
}
|
||||
writeFileSync(import.meta.dirname + "/json/top/" + index + ".json",JSON.stringify(JSONToWrite), 'utf8')
|
||||
}
|
||||
|
||||
const landsJsonRoot = [[],[],[],[]]
|
||||
|
@ -114,7 +150,9 @@ async function createJson() {
|
|||
} else {
|
||||
landsJsonRoot[3].push({ "name": colorName, "count": landsData[colorName].length})
|
||||
}
|
||||
writeFileSync(import.meta.dirname + "/json/lands/" + colorName + ".json",JSON.stringify(landsData[colorName]), 'utf8')
|
||||
let JSONToWrite = landsData[colorName]
|
||||
JSONToWrite.sort((a,b) => b.percent_decks - a.percent_decks)
|
||||
writeFileSync(import.meta.dirname + "/json/lands/" + colorName + ".json",JSON.stringify(JSONToWrite), 'utf8')
|
||||
}
|
||||
writeFileSync(import.meta.dirname + "/json/lands/lands.json",JSON.stringify(landsJsonRoot), 'utf8')
|
||||
}
|
||||
|
|
|
@ -10,7 +10,7 @@ console.log('Status Code:', scryfallSets.status);
|
|||
const sets = await scryfallSets.json();
|
||||
|
||||
// Read the data from the exported fr_cards.json extracted from Scryfall Bulk Data
|
||||
const fileBytes = fs.readFileSync(import.meta.dirname + '/data/fr_cards.json')
|
||||
const fileBytes = fs.readFileSync(import.meta.dirname + '/data/default-cards-20241210100712.json')
|
||||
let scryfallData = JSON.parse(fileBytes)
|
||||
|
||||
// Connect to postgres database
|
||||
|
@ -23,6 +23,8 @@ const client = new Client({
|
|||
})
|
||||
await client.connect()
|
||||
|
||||
const two_faced_layouts = ["transform","modal_dfc","double_faced_token","reversible_card"]
|
||||
|
||||
try {
|
||||
const setRes = await client.query('SELECT id FROM set')
|
||||
const preUpdateSetRows = setRes.rows
|
||||
|
@ -47,15 +49,15 @@ try {
|
|||
});
|
||||
|
||||
// Define counter for logging
|
||||
let total_no_fr_name = 0
|
||||
let total_inserted = 0
|
||||
let total_skipped = 0
|
||||
|
||||
// For each card check if we need to upload it to the database
|
||||
for (const carte of scryfallData) {
|
||||
if(!preUpdateCardsIds.includes(carte.id)){
|
||||
let type = null
|
||||
const card_type = carte.type_line.toLowerCase()
|
||||
if(!preUpdateCardsIds.includes(carte.id) && carte.layout != "art_series"){
|
||||
let type = ""
|
||||
const layout = carte.layout
|
||||
const card_type = (carte.type_line == undefined) ? carte.card_faces[0].type_line.toLowerCase() : carte.type_line.toLowerCase()
|
||||
|
||||
if(card_type.includes("creature")){
|
||||
type = "creature"
|
||||
|
@ -73,27 +75,28 @@ try {
|
|||
type = "land"
|
||||
}
|
||||
|
||||
try {
|
||||
if(two_faced_layouts.includes(layout)) {
|
||||
const addingCardsQuery = await client.query('INSERT INTO carte(id, name, released_at, small_image, small_image_back, normal_image, normal_image_back, type_line, colors, set_id, rarity, cardmarket_uri, price, type, sanitized_name, set_code, layout) VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17)', [carte.id, carte.name, carte.released_at, carte.card_faces[0].image_uris.small, carte.card_faces[1].image_uris.small, carte.card_faces[0].image_uris.normal, carte.card_faces[0].image_uris.normal, carte.type_line, carte.color_identity, carte.set_id, carte.rarity, carte.purchase_uris?.cardmarket, carte.prices.eur, type, carte.name.replace(/[^a-zA-Z0-9]/gim,"-").toLowerCase(), carte.set, layout])
|
||||
|
||||
} else {
|
||||
const addingCardsQuery = await client.query('INSERT INTO carte(id, name, released_at, small_image, normal_image, type_line, colors, set_id, rarity, cardmarket_uri, price, type, sanitized_name, set_code, layout) VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15)', [carte.id, carte.name, carte.released_at, carte.image_uris.small, carte.image_uris.normal, carte.type_line, carte.color_identity, carte.set_id, carte.rarity, carte.purchase_uris?.cardmarket, carte.prices.eur, type, carte.name.replace(/[^a-zA-Z0-9]/gim,"-").toLowerCase(), carte.set, layout])
|
||||
|
||||
if(carte.printed_name == undefined) {
|
||||
// If the card doesn't have a french name, print it to the console and skip
|
||||
|
||||
//console.log("Erreur sur la carte : " + carte.name)
|
||||
//console.log("Scryfall URI : " + carte.scryfall_uri)
|
||||
//console.log("API URI : " + carte.uri)
|
||||
total_no_fr_name = total_no_fr_name + 1
|
||||
continue
|
||||
}
|
||||
total_inserted = total_inserted + 1
|
||||
} catch (err) {
|
||||
console.log(carte.uri)
|
||||
console.log(carte.layout)
|
||||
console.log(err)
|
||||
total_skipped = total_skipped + 1
|
||||
}
|
||||
|
||||
// Add the card to the database
|
||||
const addingCardsQuery = await client.query('INSERT INTO carte(id, name_en, name_fr, released_at, small_image, normal_image, mana_cost, cmc, type_line_en, type_line_fr, oracle_text_en, oracle_text_fr, power, toughness, colors, keywords, set_id, rarity, cardmarket_uri, type, sanitized_name) VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21)', [carte.id, carte.name, carte.printed_name, carte.released_at, carte.image_uris.small, carte.image_uris.normal, carte.mana_cost, carte.cmc, carte.type_line, carte.printed_type_line, carte.oracle_text, carte.printed_text, carte.power, carte.toughness, carte.color_identity, carte.keywords, carte.set_id, carte.rarity, carte.purchase_uris?.cardmarket, type, carte.name.replace(/[^a-zA-Z0-9]/gim,"-").toLowerCase()])
|
||||
total_inserted = total_inserted + 1
|
||||
} else {
|
||||
total_skipped = total_skipped + 1
|
||||
}
|
||||
|
||||
}
|
||||
console.log("Un total de " + total_no_fr_name + " cartes n'ont pas de nom français.")
|
||||
console.log("Un total de " + total_inserted + " cartes ont été insérées.")
|
||||
console.log("Un total de " + total_skipped + " cartes ont été ignorées.")
|
||||
} catch (err) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue