2023-12-12 18:21:09 +08:00
|
|
|
import {useShopStore} from "./shop_store";
|
2023-12-13 12:39:15 +08:00
|
|
|
import {FilterOptions} from "./options/utils";
|
|
|
|
import {v4 as uuidv4} from "uuid";
|
2023-12-12 18:21:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
export function validateJSON(description) {
|
|
|
|
let crates_raw;
|
|
|
|
try {
|
|
|
|
crates_raw = JSON.parse(description);
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const crate_modes = useShopStore.getState().crate_modes;
|
|
|
|
const pn_to_card = useShopStore.getState().pn_to_cards;
|
|
|
|
|
2023-12-13 12:39:15 +08:00
|
|
|
try {
|
|
|
|
for (const crate of crates_raw) {
|
|
|
|
if (!crate.type || !crate.items || !(crate.type in crate_modes)) return false;
|
|
|
|
for (const card of crate.items) {
|
|
|
|
if (!(card.pn in pn_to_card)) return false;
|
|
|
|
}
|
2023-12-12 18:21:09 +08:00
|
|
|
}
|
2023-12-13 12:39:15 +08:00
|
|
|
} catch (e) {
|
|
|
|
return false;
|
2023-12-12 18:21:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function JSONToCrates(description) {
|
|
|
|
const crates_raw = JSON.parse(description);
|
|
|
|
const pn_to_card = useShopStore.getState().getCardDescriptionByPn;
|
|
|
|
return Array.from(crates_raw.map((crate, c_i) => ({
|
|
|
|
id: "crate" + c_i,
|
|
|
|
crate_mode: crate.type,
|
2023-12-13 15:17:14 +08:00
|
|
|
items: Array.from(crate.items.map((card, _i) => ({
|
2023-12-12 18:21:09 +08:00
|
|
|
...pn_to_card(card.pn),
|
2023-12-13 12:39:15 +08:00
|
|
|
id: uuidv4(),
|
2023-12-12 18:21:09 +08:00
|
|
|
options_data: card.options
|
|
|
|
}))),
|
|
|
|
warnings: [],
|
|
|
|
occupiedHP: 0,
|
|
|
|
})));
|
|
|
|
}
|
|
|
|
|
|
|
|
export function CratesToJSON(crates) {
|
|
|
|
return JSON.stringify(Array.from(crates.map((crate, _i) => ({
|
|
|
|
items: Array.from(crate.items.map((card, _) => ({
|
|
|
|
pn: card.name_number,
|
|
|
|
options: (card.options_data && card.options) ? FilterOptions(card.options, card.options_data) : null
|
|
|
|
}))),
|
|
|
|
type: crate.crate_mode
|
|
|
|
}))), null, 2)
|
|
|
|
}
|