Change JSON structure from array to object, so that we can add more fields later

Signed-off-by: Egor Savkin <es@m-labs.hk>
This commit is contained in:
Egor Savkin 2024-01-08 15:31:22 +08:00
parent 963f342c89
commit 83be26ecbb
3 changed files with 36 additions and 16 deletions

File diff suppressed because one or more lines are too long

View File

@ -6,11 +6,14 @@ import {v4 as uuidv4} from "uuid";
export function validateJSON(description) { export function validateJSON(description) {
let crates_raw; let crates_raw;
try { try {
crates_raw = JSON.parse(description); const parsed = JSON.parse(description);
// here we can check additional fields
crates_raw = parsed.crates;
} catch (e) { } catch (e) {
return false; return false;
} }
const crate_modes = useShopStore.getState().crate_modes; const crate_modes = useShopStore.getState().crate_modes;
const modes_order = useShopStore.getState().modes_order;
const pn_to_card = useShopStore.getState().pn_to_cards; const pn_to_card = useShopStore.getState().pn_to_cards;
try { try {
@ -24,14 +27,19 @@ export function validateJSON(description) {
return false; return false;
} }
return true; // 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) { export function JSONToCrates(description) {
const crates_raw = JSON.parse(description); const parsed = JSON.parse(description);
const crates_raw = parsed.crates;
const pn_to_card = useShopStore.getState().getCardDescriptionByPn; const pn_to_card = useShopStore.getState().getCardDescriptionByPn;
return Array.from(crates_raw.map((crate, c_i) => ({
id: "crate" + c_i, 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,
crate_mode: crate.type, crate_mode: crate.type,
items: Array.from(crate.items.map((card, _i) => ({ items: Array.from(crate.items.map((card, _i) => ({
...pn_to_card(card.pn), ...pn_to_card(card.pn),
@ -41,14 +49,22 @@ export function JSONToCrates(description) {
warnings: [], warnings: [],
occupiedHP: 0, occupiedHP: 0,
}))); })));
return {
// some additional fields go here
crates: crates
};
} }
export function CratesToJSON(crates) { export function CratesToJSON(crates) {
return JSON.stringify(Array.from(crates.map((crate, _i) => ({ return JSON.stringify({
items: Array.from(crate.items.map((card, _) => ({ // additional fields can go here
pn: card.name_number, crates: Array.from(crates.map((crate, _i) => ({
options: (card.options_data && card.options) ? FilterOptions(card.options, card.options_data) : null items: Array.from(crate.items.map((card, _) => ({
}))), pn: card.name_number,
type: crate.crate_mode options: (card.options_data && card.options) ? FilterOptions(card.options, card.options_data) : null
}))), null, 2) }))),
type: crate.crate_mode
})))
}, null, 2)
} }

View File

@ -77,10 +77,14 @@ const useImportJSON = ((set, get) => ({
closeImport: () => set(state => ({ closeImport: () => set(state => ({
importShouldOpen: false importShouldOpen: false
})), })),
_loadDescription: () => set(state => ({ _loadDescription: () => set(state => {
const parsed = JSONToCrates(state.importValue.value);
// if (parsed.crates[-1].crate_mode !== "")
return {
importShouldOpen: false, importShouldOpen: false,
crates: JSONToCrates(state.importValue.value) // additional fields go here
})), crates: parsed.crates
}}),
loadDescription: () => { loadDescription: () => {
get()._loadDescription() get()._loadDescription()
get().crates.forEach((crate, _i) => { get().crates.forEach((crate, _i) => {