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 {
|
2024-01-08 15:31:22 +08:00
|
|
|
const parsed = JSON.parse(description);
|
|
|
|
// here we can check additional fields
|
|
|
|
crates_raw = parsed.crates;
|
2023-12-12 18:21:09 +08:00
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const crate_modes = useShopStore.getState().crate_modes;
|
2024-01-08 15:31:22 +08:00
|
|
|
const modes_order = useShopStore.getState().modes_order;
|
2023-12-12 18:21:09 +08:00
|
|
|
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) {
|
2023-12-15 13:49:54 +08:00
|
|
|
if (!(card.pn in pn_to_card) || card.options === undefined) return false;
|
2023-12-13 12:39:15 +08:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2024-01-08 15:31:22 +08:00
|
|
|
// only last one should be spare cards
|
|
|
|
return crates_raw.filter((crate) => !modes_order.includes(crate.type)).length === 1 && crates_raw[crates_raw.length - 1].type === "no_crate";
|
2023-12-12 18:21:09 +08:00
|
|
|
}
|
|
|
|
|
2024-01-08 15:31:22 +08:00
|
|
|
// no validation in this function
|
2023-12-12 18:21:09 +08:00
|
|
|
export function JSONToCrates(description) {
|
2024-01-08 15:31:22 +08:00
|
|
|
const parsed = JSON.parse(description);
|
|
|
|
const crates_raw = parsed.crates;
|
2023-12-12 18:21:09 +08:00
|
|
|
const pn_to_card = useShopStore.getState().getCardDescriptionByPn;
|
2024-01-25 17:10:14 +08:00
|
|
|
const fanTrayAvailable = useShopStore.getState().fanTrayAvailableForMode;
|
2024-01-08 15:31:22 +08:00
|
|
|
|
|
|
|
const crates = Array.from(crates_raw.map((crate, c_i) => ({
|
|
|
|
id: crate.type === "no_crate" ? "spare" : "crate" + c_i,
|
|
|
|
name: crate.type === "no_crate" ? "Spare cards" : undefined,
|
2024-01-25 17:10:14 +08:00
|
|
|
fan_tray: fanTrayAvailable(crate.type) && crate.fan_tray === true,
|
2023-12-12 18:21:09 +08:00
|
|
|
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-15 13:49:54 +08:00
|
|
|
options_data: card.options || {}
|
2023-12-12 18:21:09 +08:00
|
|
|
}))),
|
|
|
|
warnings: [],
|
|
|
|
occupiedHP: 0,
|
|
|
|
})));
|
2024-01-08 15:31:22 +08:00
|
|
|
|
|
|
|
return {
|
|
|
|
// some additional fields go here
|
|
|
|
crates: crates
|
|
|
|
};
|
2023-12-12 18:21:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function CratesToJSON(crates) {
|
2024-01-25 17:10:14 +08:00
|
|
|
const fanTrayAvailable = useShopStore.getState().fanTrayAvailableForMode;
|
2024-01-08 15:31:22 +08:00
|
|
|
return JSON.stringify({
|
|
|
|
// additional fields can go here
|
|
|
|
crates: 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
|
|
|
|
}))),
|
2024-01-25 17:10:14 +08:00
|
|
|
type: crate.crate_mode,
|
|
|
|
fan_tray: (!fanTrayAvailable(crate.crate_mode) ? undefined : true) && crate.fan_tray
|
2024-01-08 15:31:22 +08:00
|
|
|
})))
|
|
|
|
}, null, 2)
|
2023-12-12 18:21:09 +08:00
|
|
|
}
|