Skip to content

Commit

Permalink
Prepare for data to come already-decompresse from the API
Browse files Browse the repository at this point in the history
  • Loading branch information
gausie committed Nov 22, 2024
1 parent a6ebffc commit ddc1ec4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
6 changes: 5 additions & 1 deletion packages/greenbox-data/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import jsoncrush from "jsoncrush";

import { compressFamiliars, expandFamiliars, RawFamiliar } from "./familiars.js";
import {
compressFamiliars,
expandFamiliars,
RawFamiliar,
} from "./familiars.js";
import { compressIotMs, expandIotMs, RawIotM } from "./iotms.js";
import { compressItems, expandItems, RawItem } from "./items.js";
import { compressMeta, expandMeta, Meta } from "./meta.js";
Expand Down
34 changes: 18 additions & 16 deletions packages/greenbox-web/src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,21 @@ export const fetchPlayerData = createAsyncThunk(
`https://oaf.loathers.net/api/greenbox/${playerId}`,
);
const json = await response.json();
if (response.status !== 200) {

if (response.ok) {
throw new Error(json.error);
}
return json.greenboxString || (json.data as string);

if (typeof json.data === "string") {
// The server will soon serve the object directly, but this code is to bridge the migration
try {
return api.expand(json.data);
} catch {
throw new Error("Error parsing player data");
}
}

return json.data;
},
);

Expand Down Expand Up @@ -307,24 +318,15 @@ export const greenboxSlice = createSlice({
.addCase(fetchPlayerData.fulfilled, (state, action) => {
// Set current player id
state.playerId = action.meta.arg;
// Parse and load greenbox string
const greenboxString = action.payload;
try {
state.playerData = api.expand(greenboxString);
state.error.playerData = false;
state.errorMessage.playerData = undefined;
} catch {
state.error.playerData = true;
state.errorMessage.playerData = "Error parsing player data";
}

state.loading.playerData = false;
state.playerData = action.payload;
state.error.playerData = false;
state.errorMessage.playerData = undefined;
})
.addCase(loadPlayerData, (state, action) => {
state.playerId = null;
const greenboxString = action.payload;

try {
state.playerData = api.expand(greenboxString);
state.playerData = api.expand(action.payload);
} catch {
state.error.playerData = true;
state.errorMessage.playerData = "Error parsing player data";
Expand Down

0 comments on commit ddc1ec4

Please sign in to comment.