Feat: Add card list and top structure
This commit is contained in:
parent
8e33dd45fe
commit
eec6c6bfe8
19 changed files with 994 additions and 24 deletions
122
app/tools/createJson.mjs
Normal file
122
app/tools/createJson.mjs
Normal file
|
@ -0,0 +1,122 @@
|
|||
import { PrismaClient } from '@prisma/client'
|
||||
import { writeFileSync } from 'fs'
|
||||
|
||||
const db = new PrismaClient()
|
||||
|
||||
const color_names = {
|
||||
"mono-white": ["W"],
|
||||
"mono-black": ["B"],
|
||||
"mono-blue": ["U"],
|
||||
"mono-green": ["G"],
|
||||
"mono-red": ["R"],
|
||||
"colorless": [],
|
||||
"azorius": ["W","U"],
|
||||
"dimir": ["U","B"],
|
||||
"rakdos": ["B","R"],
|
||||
"gruul": ["R","G"],
|
||||
"selesnya": ["G","W"],
|
||||
"orzhov": ["W","B"],
|
||||
"izzet": ["U","R"],
|
||||
"golgari": ["B","G"],
|
||||
"boros": ["R","W"],
|
||||
"simic": ["G","U"],
|
||||
"esper": ["W","U","B"],
|
||||
"grixis": ["U","B","R"],
|
||||
"jund": ["B","R","G"],
|
||||
"naya": ["R","G","W"],
|
||||
"bant": ["G","W","U"],
|
||||
"abzan": ["W","B","G"],
|
||||
"jeskai": ["U","R","W"],
|
||||
"sultai": ["B","G","U"],
|
||||
"mardu": ["R","W","B"],
|
||||
"temur": ["G","U","R"],
|
||||
"yore-tiller": ["W","U","B","R"],
|
||||
"glint-eye": ["U","B","R","G"],
|
||||
"dune-brood": ["B","R","G","W"],
|
||||
"ink-treader": ["R","G","W","U"],
|
||||
"witch-maw": ["G","W","U","B"],
|
||||
"five-color": ["G","W","U","B","R"],
|
||||
}
|
||||
|
||||
function getColorName(colorArray) {
|
||||
const colorArrayID = colorArray.sort().join(',')
|
||||
for (const colorName of Object.keys(color_names)) {
|
||||
if(colorArrayID === color_names[colorName].sort().join(',')){
|
||||
return(colorName)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// I need to create
|
||||
// Lands
|
||||
// lands.json
|
||||
// All jsons lands from colors
|
||||
// Types
|
||||
|
||||
async function createJson() {
|
||||
console.log("Fetching data...")
|
||||
const bsets = await db.bset.findMany({
|
||||
relationLoadStrategy: "join",
|
||||
include: {
|
||||
sets: {
|
||||
include: {
|
||||
cards: true
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
let all_cards = []
|
||||
bsets.forEach((bset) => {
|
||||
bset.sets.forEach((set) => {
|
||||
all_cards = [...all_cards, ...set.cards]
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
const landsData = {}
|
||||
for (const colorName of Object.keys(color_names)) {
|
||||
landsData[colorName] = []
|
||||
}
|
||||
const type_dict = {"creature": [],"land": [],"instant": [],"sorcery": [], "planeswalker": [], "artifact": [], "enchantment": []}
|
||||
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) {
|
||||
const colorName = getColorName(card.colors)
|
||||
if (card.type == "land") {
|
||||
if (colorName != "") {
|
||||
landsData[colorName].push(card)
|
||||
}
|
||||
}
|
||||
|
||||
if (card.colors.length <= 1) {
|
||||
colorsData[colorName][card.type].push(card)
|
||||
} else {
|
||||
colorsData["multicolor"][card.type].push(card)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for (const index of Object.keys(colorsData)) {
|
||||
writeFileSync(import.meta.dirname + "/json/top/" + index + ".json",JSON.stringify(colorsData[index]), 'utf8')
|
||||
}
|
||||
|
||||
const landsJsonRoot = [[],[],[],[]]
|
||||
for (const colorName of Object.keys(color_names)){
|
||||
if(color_names[colorName].length <= 3){
|
||||
let index = color_names[colorName].length - 1
|
||||
if(index < 0) { index = 0 }
|
||||
landsJsonRoot[index].push({ "name": colorName, "count": landsData[colorName].length})
|
||||
} else {
|
||||
landsJsonRoot[3].push({ "name": colorName, "count": landsData[colorName].length})
|
||||
}
|
||||
writeFileSync(import.meta.dirname + "/json/lands/" + colorName + ".json",JSON.stringify(landsData[colorName]), 'utf8')
|
||||
}
|
||||
writeFileSync(import.meta.dirname + "/json/lands/lands.json",JSON.stringify(landsJsonRoot), 'utf8')
|
||||
}
|
||||
|
||||
createJson()
|
|
@ -33,7 +33,7 @@ try {
|
|||
|
||||
for (const set of sets.data) {
|
||||
if(!preUpdateSetIds.includes(set.id)){
|
||||
const addingSetQuery = await client.query('INSERT INTO set(id, name_en, code, set_type, released_at, icon_svg_uri) VALUES($1, $2, $3, $4, $5, $6)', [set.id, set.name, set.code, set.set_type, set.released_at, set.icon_svg_uri])
|
||||
const addingSetQuery = await client.query('INSERT INTO set(id, name_en, sanitized_name, code, set_type, released_at, icon_svg_uri) VALUES($1, $2, $3, $4, $5, $6, $7)', [set.id, set.name, set.name.replace(/[^a-zA-Z0-9]/gim,"-").toLowerCase(), set.code, set.set_type, set.released_at, set.icon_svg_uri])
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -54,6 +54,27 @@ try {
|
|||
// 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(card_type.includes("creature")){
|
||||
type = "creature"
|
||||
} else if (card_type.includes("planeswalker")) {
|
||||
type = "planeswalker"
|
||||
} else if (card_type.includes("artifact")) {
|
||||
type = "artifact"
|
||||
} else if (card_type.includes("instant")) {
|
||||
type = "instant"
|
||||
} else if (card_type.includes("enchantment")) {
|
||||
type = "enchantment"
|
||||
} else if (card_type.includes("sorcery")) {
|
||||
type = "sorcery"
|
||||
} else if (card_type.includes("land")) {
|
||||
type = "land"
|
||||
}
|
||||
|
||||
|
||||
|
||||
if(carte.printed_name == undefined) {
|
||||
// If the card doesn't have a french name, print it to the console and skip
|
||||
|
||||
|
@ -65,7 +86,7 @@ try {
|
|||
}
|
||||
|
||||
// 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) VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19)', [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.colors, carte.keywords, carte.set_id, carte.rarity, carte.purchase_uris?.cardmarket])
|
||||
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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue