forked from M-Labs/web2019
93 lines
3.6 KiB
JavaScript
93 lines
3.6 KiB
JavaScript
import {useShopStore} from "./shop_store";
|
|
import {FilterOptions} from "./options/utils";
|
|
import {v4 as uuidv4} from "uuid";
|
|
|
|
|
|
export function validateJSON(description) {
|
|
let crates_raw;
|
|
let order_options;
|
|
try {
|
|
const parsed = JSON.parse(description);
|
|
// here we can check additional fields
|
|
crates_raw = parsed.crates;
|
|
order_options = parsed.options;
|
|
} catch (e) {
|
|
return false;
|
|
}
|
|
if (!order_options) return false;
|
|
const crate_modes = useShopStore.getState().crate_modes;
|
|
const modes_order = useShopStore.getState().modes_order;
|
|
const pn_to_card = useShopStore.getState().pn_to_cards;
|
|
|
|
try {
|
|
for (const crate of crates_raw) {
|
|
if (!crate.type || !crate.items || !crate.h_items || !crate.options || !(crate.type in crate_modes)) return false;
|
|
for (const card of crate.items) {
|
|
if (!(card.pn in pn_to_card) || card.options === undefined) return false;
|
|
}
|
|
for (const card of crate.h_items) {
|
|
if (!(card.pn in pn_to_card) || card.options === undefined) return false;
|
|
}
|
|
}
|
|
} catch (e) {
|
|
return false;
|
|
}
|
|
|
|
// 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";
|
|
}
|
|
|
|
// no validation in this function
|
|
export function JSONToCrates(description) {
|
|
const parsed = JSON.parse(description);
|
|
const crates_raw = parsed.crates;
|
|
const pn_to_card = useShopStore.getState().getCardDescriptionByPn;
|
|
|
|
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,
|
|
options_data: crate.options,
|
|
crate_mode: crate.type,
|
|
items: Array.from(crate.items.map((card, _i) => ({
|
|
...pn_to_card(card.pn),
|
|
id: uuidv4(),
|
|
options_data: card.options || {}
|
|
}))),
|
|
h_items: Array.from(crate.h_items.map((card, _i) => ({
|
|
...pn_to_card(card.pn),
|
|
id: uuidv4(),
|
|
options_data: card.options || {}
|
|
}))),
|
|
warnings: [],
|
|
occupiedHP: 0,
|
|
})));
|
|
|
|
return {
|
|
// some additional fields go here
|
|
order_options_data: parsed.options,
|
|
crates: crates
|
|
};
|
|
}
|
|
|
|
export function CratesToJSON(crates) {
|
|
const crateOptions = useShopStore.getState().crate_options;
|
|
const orderOptions = useShopStore.getState().order_options;
|
|
const orderOptionsData = useShopStore.getState().order_options_data;
|
|
const crateParams = useShopStore.getState().crateParams;
|
|
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[crateParams(crate.crate_mode).options]) ? FilterOptions(card[crateParams(crate.crate_mode).options], card.options_data) : null
|
|
}))),
|
|
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
|
|
}))),
|
|
type: crate.crate_mode,
|
|
options: FilterOptions(crateOptions, crate.options_data)
|
|
}))),
|
|
options: FilterOptions(orderOptions, orderOptionsData)
|
|
}, null, 2)
|
|
} |