50 lines
1.7 KiB
JavaScript
50 lines
1.7 KiB
JavaScript
![]() |
import { readFileSync, readdirSync } from "fs"
|
||
|
|
||
|
const base_api_url = "http://localhost:3000"
|
||
|
|
||
|
const token = "eyJhbGciOiJIUzI1NiIsInR5cCI6Imp3dCJ9.eyJ1c2VybmFtZSI6Imdsb2J1em1hIiwiYWRtaW4iOnRydWUsImlkIjoiMDc4YWViYTYtNzZlNC00YzRkLTg3NjYtNjA0N2JhNjkxM2Y5IiwibWF4QWdlIjo2MDQ4MDB9.acd6b0fab88719f708fa0553f18a6b034b2c3a84659e3a63b32e87f22c611d3e"
|
||
|
|
||
|
let bsets_sanitized_names = readdirSync(import.meta.dirname + "/data/bsets/")
|
||
|
|
||
|
function get_line_data(line) {
|
||
|
let data = line.split(" ")
|
||
|
let amount = parseInt(data[0])
|
||
|
let name = data.slice(1).join(" ").split("/")[0].replace(/[^a-zA-Z0-9]/gim,"-").toLowerCase()
|
||
|
return {"sanitized_name":name, "amount":amount}
|
||
|
}
|
||
|
|
||
|
for(const sanitized_name of bsets_sanitized_names) {
|
||
|
let path = import.meta.dirname + "/data/bsets/" + sanitized_name + "/"
|
||
|
let deck_files = readdirSync(path)
|
||
|
for( const deck_file of deck_files) {
|
||
|
let api_object = {
|
||
|
"selected_bset": sanitized_name,
|
||
|
"name": deck_file.split(".txt")[0],
|
||
|
"commander_name": "",
|
||
|
"cards": []
|
||
|
}
|
||
|
|
||
|
let data = readFileSync(path + deck_file, 'utf8')
|
||
|
let lines = data.split(/\n/)
|
||
|
lines = lines.filter((line) => line.match(/[0-9]+\s[\w]+/) != undefined)
|
||
|
let commander_line = lines[lines.length - 1]
|
||
|
api_object["commander_name"] = get_line_data(commander_line).sanitized_name
|
||
|
for(const card of lines.slice(0, lines.length - 1)){
|
||
|
api_object.cards.push(get_line_data(card))
|
||
|
}
|
||
|
|
||
|
|
||
|
fetch(base_api_url + '/api/account/decks/create', {
|
||
|
method: "POST",
|
||
|
headers: {Authorization: 'Bearer ' + token},
|
||
|
body: JSON.stringify(api_object)
|
||
|
}).then((res) => {
|
||
|
if(res.status == 200) {
|
||
|
console.log("Deck created")
|
||
|
} else {
|
||
|
console.log("Problem with : " + deck_file)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|