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) {
let crates_raw;
try {
crates_raw = JSON.parse(description);
const parsed = JSON.parse(description);
// here we can check additional fields
crates_raw = parsed.crates;
} catch (e) {
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 {
@ -24,14 +27,19 @@ export function validateJSON(description) {
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) {
const crates_raw = JSON.parse(description);
const parsed = JSON.parse(description);
const crates_raw = parsed.crates;
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,
items: Array.from(crate.items.map((card, _i) => ({
...pn_to_card(card.pn),
@ -41,14 +49,22 @@ export function JSONToCrates(description) {
warnings: [],
occupiedHP: 0,
})));
return {
// some additional fields go here
crates: crates
};
}
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)
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
}))),
type: crate.crate_mode
})))
}, null, 2)
}

View File

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