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;
|
2024-01-30 17:20:29 +08:00
|
|
|
let order_options;
|
2023-12-12 18:21:09 +08:00
|
|
|
try {
|
2024-01-08 15:31:22 +08:00
|
|
|
const parsed = JSON.parse(description);
|
|
|
|
// here we can check additional fields
|
|
|
|
crates_raw = parsed.crates;
|
2024-01-30 17:20:29 +08:00
|
|
|
order_options = parsed.options;
|
2023-12-12 18:21:09 +08:00
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
2024-01-30 17:20:29 +08:00
|
|
|
if (!order_options) return false;
|
2023-12-12 18:21:09 +08:00
|
|
|
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) {
|
2024-11-01 17:32:20 +08:00
|
|
|
if (!crate.type || !crate.items || !crate.h_items || !crate.options || !(crate.type in crate_modes)) return false;
|
2023-12-13 12:39:15 +08:00
|
|
|
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
|
|
|
}
|
2024-11-01 17:32:20 +08:00
|
|
|
for (const card of crate.h_items) {
|
|
|
|
if (!(card.pn in pn_to_card) || card.options === undefined) 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
|
|
|
}
|
|
|
|
|
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-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-30 14:45:30 +08:00
|
|
|
options_data: crate.options,
|
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
|
|
|
}))),
|
2024-11-01 17:32:20 +08:00
|
|
|
h_items: Array.from(crate.h_items.map((card, _i) => ({
|
|
|
|
...pn_to_card(card.pn),
|
|
|
|
id: uuidv4(),
|
|
|
|
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
|
2024-01-30 17:20:29 +08:00
|
|
|
order_options_data: parsed.options,
|
2024-01-08 15:31:22 +08:00
|
|
|
crates: crates
|
|
|
|
};
|
2023-12-12 18:21:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function CratesToJSON(crates) {
|
2024-01-30 14:45:30 +08:00
|
|
|
const crateOptions = useShopStore.getState().crate_options;
|
2024-01-30 17:20:29 +08:00
|
|
|
const orderOptions = useShopStore.getState().order_options;
|
|
|
|
const orderOptionsData = useShopStore.getState().order_options_data;
|
2024-08-05 10:26:56 +08:00
|
|
|
const crateParams = useShopStore.getState().crateParams;
|
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,
|
2024-08-05 10:26:56 +08:00
|
|
|
options: (card.options_data && card[crateParams(crate.crate_mode).options]) ? FilterOptions(card[crateParams(crate.crate_mode).options], card.options_data) : null
|
2024-01-08 15:31:22 +08:00
|
|
|
}))),
|
2024-11-01 17:32:20 +08:00
|
|
|
h_items: Array.from(crate.h_items.map((card, _) => ({
|
|
|
|
pn: card.name_number,
|
|
|
|
options: (card.options_data && card[crateParams(crate.crate_mode).options]) ? FilterOptions(card[crateParams(crate.crate_mode).options], card.options_data) : null
|
|
|
|
}))),
|
2024-01-25 17:10:14 +08:00
|
|
|
type: crate.crate_mode,
|
2024-01-30 14:45:30 +08:00
|
|
|
options: FilterOptions(crateOptions, crate.options_data)
|
2024-01-30 17:20:29 +08:00
|
|
|
}))),
|
|
|
|
options: FilterOptions(orderOptions, orderOptionsData)
|
2024-01-08 15:31:22 +08:00
|
|
|
}, null, 2)
|
2023-12-12 18:21:09 +08:00
|
|
|
}
|