Skip to content

Commit

Permalink
fix ranks and uuid data saving
Browse files Browse the repository at this point in the history
  • Loading branch information
Manwe-777 committed Sep 15, 2023
1 parent b30bfd7 commit 8bd3126
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 101 deletions.
3 changes: 2 additions & 1 deletion src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ function App(props: AppProps) {
window.toolDbWorker = toolDbWorkerRef.current;

window.toolDbWorker.addEventListener("message", (e) => {
// console.warn("Worker REDUX_ACTION", action.type, action.arg);
if (e.data.type === "REDUX_ACTION") {
const action = e.data.arg;
// console.warn("Worker REDUX_ACTION", action.type, action.arg);
console.warn("Worker REDUX_ACTION", action.type, action.arg);
reduxAction(dispatch, {
type: action.type,
arg: action.arg,
Expand Down
2 changes: 1 addition & 1 deletion src/info.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":"6.3.1","branch":"dev","timestamp":1694221792794}
{"version":"6.3.1","branch":"dev","timestamp":1694805357905}
59 changes: 27 additions & 32 deletions src/toolDb/upsertDbCards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,42 @@ import reduxAction from "../redux/reduxAction";
import store from "../redux/stores/rendererStore";
import { DbCardsData, defaultCardsData } from "../types/dbTypes";
import getLocalSetting from "../utils/getLocalSetting";
import getUserNamespacedKey from "./getUserNamespacedKey";
import { getLocalData, putData } from "./worker-wrapper";
import { getData, putData } from "./worker-wrapper";

export default async function upsertDbCards(cards: Cards) {
console.log("> Upsert cards", cards);

const uuid = getLocalSetting("playerId") || "default";
const { dispatch } = store;

const { pubKey } = store.getState().renderer;
getData(`${uuid}-cards`, true).then((uuidData) => {
if (uuidData) {
const newData = {
cards,
prevCards:
new Date().getTime() - (uuidData as DbCardsData).updated >
1000 * 60 * 24
? (uuidData as DbCardsData).cards
: (uuidData as DbCardsData).prevCards,
updated: new Date().getTime(),
};

getLocalData(getUserNamespacedKey(pubKey, `${uuid}-cards`)).then(
(uuidData) => {
if (uuidData) {
const newData = {
reduxAction(dispatch, {
type: "SET_UUID_CARDS_DATA",
arg: { cards: newData, uuid },
});

putData<DbCardsData>(`${uuid}-cards`, newData, true);
} else {
putData<DbCardsData>(
`${uuid}-cards`,
{
...defaultCardsData,
cards,
prevCards:
new Date().getTime() - (uuidData as DbCardsData).updated >
1000 * 60 * 24
? (uuidData as DbCardsData).cards
: (uuidData as DbCardsData).prevCards,
updated: new Date().getTime(),
};

reduxAction(dispatch, {
type: "SET_UUID_CARDS_DATA",
arg: { cards: newData, uuid },
});

putData<DbCardsData>(`${uuid}-cards`, newData, true);
} else {
putData<DbCardsData>(
`${uuid}-cards`,
{
...defaultCardsData,
cards,
updated: new Date().getTime(),
},
true
);
}
},
true
);
}
);
});
}
51 changes: 23 additions & 28 deletions src/toolDb/upsertDbInventory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import {
defaultInventoryData,
} from "../types/dbTypes";
import getLocalSetting from "../utils/getLocalSetting";
import getUserNamespacedKey from "./getUserNamespacedKey";
import { getLocalData, putData } from "./worker-wrapper";
import { getData, putData } from "./worker-wrapper";

export default async function upsertDbInventory(
inventory: Partial<DbInventoryInfo>
Expand All @@ -17,33 +16,29 @@ export default async function upsertDbInventory(
const uuid = getLocalSetting("playerId") || "default";
const { dispatch } = store;

const { pubKey } = store.getState().renderer;
getData(`${uuid}-inventory`, true).then((uuidData) => {
if (uuidData) {
const newData: DbInventoryData = {
...(uuidData as DbInventoryData),
...inventory,
updated: new Date().getTime(),
};

getLocalData(getUserNamespacedKey(pubKey, `${uuid}-inventory`)).then(
(uuidData) => {
if (uuidData) {
const newData: DbInventoryData = {
...(uuidData as DbInventoryData),
...inventory,
updated: new Date().getTime(),
};

reduxAction(dispatch, {
type: "SET_UUID_INVENTORY_DATA",
arg: { inventory: newData, uuid },
});
reduxAction(dispatch, {
type: "SET_UUID_INVENTORY_DATA",
arg: { inventory: newData, uuid },
});

putData<DbInventoryData>(`${uuid}-inventory`, newData, true);
} else {
putData<DbInventoryData>(
`${uuid}-inventory`,
{
...defaultInventoryData,
updated: new Date().getTime(),
},
true
);
}
putData<DbInventoryData>(`${uuid}-inventory`, newData, true);
} else {
putData<DbInventoryData>(
`${uuid}-inventory`,
{
...defaultInventoryData,
updated: new Date().getTime(),
},
true
);
}
);
});
}
71 changes: 36 additions & 35 deletions src/toolDb/upsertDbRank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import {
defaultRankData,
} from "../types/dbTypes";
import getLocalSetting from "../utils/getLocalSetting";
import getUserNamespacedKey from "./getUserNamespacedKey";
import { getLocalData, putData } from "./worker-wrapper";
import { getData, putData } from "./worker-wrapper";

export default async function upsertDbRank(rank: Partial<CombinedRankInfo>) {
console.log("> Upsert rank", rank);
Expand All @@ -18,42 +17,44 @@ export default async function upsertDbRank(rank: Partial<CombinedRankInfo>) {

const pubKey = getLocalSetting("pubkey");

getLocalData(getUserNamespacedKey(pubKey, `${uuid}-rank`)).then(
(uuidData) => {
if (uuidData) {
const newData: DbRankData = {
...(uuidData as DbRankData),
...rank,
updated: new Date().getTime(),
};
getData(`${uuid}-rank`, true).then((uuidData) => {
console.warn("uuidData", uuidData);
if (uuidData) {
const newData: DbRankData = {
...(uuidData as DbRankData),
...rank,
updated: new Date().getTime(),
};

console.warn("newData", newData);

reduxAction(dispatch, {
type: "SET_UUID_RANK_DATA",
arg: { rank: newData, uuid },
});
reduxAction(dispatch, {
type: "SET_UUID_RANK_DATA",
arg: { rank: newData, uuid },
});

putData<DbRankData>(`${uuid}-rank`, newData, true);
putData<DbRankDataWithKey>(`rank-${pubKey}`, {
...newData,
uuid,
pubKey: pubKey,
});
} else {
putData<DbRankData>(
`${uuid}-rank`,
{
...defaultRankData,
updated: new Date().getTime(),
},
true
);
putData<DbRankDataWithKey>(`rank-${pubKey}`, {
putData<DbRankData>(`${uuid}-rank`, newData, true);
putData<DbRankDataWithKey>(`rank-${pubKey}`, {
...newData,
uuid,
pubKey: pubKey,
});
} else {
console.warn("defaultRankData", defaultRankData);
putData<DbRankData>(
`${uuid}-rank`,
{
...defaultRankData,
updated: new Date().getTime(),
uuid,
pubKey: pubKey,
});
}
},
true
);
putData<DbRankDataWithKey>(`rank-${pubKey}`, {
...defaultRankData,
updated: new Date().getTime(),
uuid,
pubKey: pubKey,
});
}
);
});
}
2 changes: 1 addition & 1 deletion src/toolDb/upsertDbuserdata.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DbUserids } from "../types/dbTypes";
import { getData, putData } from "./worker-wrapper";

export default async function upsertDbUserids(newData: DbUserids) {
export default async function upsertDbUserdata(newData: DbUserids) {
console.log("> Upsert userdata", newData);

getData<DbUserids>("userids", true)
Expand Down
6 changes: 3 additions & 3 deletions tooldb-worker/getDataLocal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default function getDataLocal(msgId: string, key: string) {
return new Promise((resolve) => {
self.toolDb.store.get(key, (err, data) => {
if (err) {
self.postMessage({ type: `${msgId}_ERR` });
self.postMessage({ type: `${msgId}_ERR`, err });
} else if (data) {
try {
const json = JSON.parse(data);
Expand All @@ -13,10 +13,10 @@ export default function getDataLocal(msgId: string, key: string) {

resolve(json.v);
} catch (_e) {
self.postMessage({ type: `${msgId}_ERR` });
self.postMessage({ type: `${msgId}_ERR`, err: _e });
}
} else {
self.postMessage({ type: `${msgId}_ERR` });
self.postMessage({ type: `${msgId}_ERR`, err: "No data" });
}
});
});
Expand Down

0 comments on commit 8bd3126

Please sign in to comment.