Skip to content

Commit

Permalink
better account id and display
Browse files Browse the repository at this point in the history
  • Loading branch information
Manwe-777 committed Sep 17, 2023
1 parent 522a6cd commit 971dda6
Show file tree
Hide file tree
Showing 13 changed files with 161 additions and 53 deletions.
9 changes: 9 additions & 0 deletions public/tooldb-worker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ function afterLogin() {
newestDate = data[uuid];
newest = uuid;
}
self.toolDb.addKeyListener(self.toolDb.getUserNamespacedKey(`${uuid}-displayname`), (msg) => {
if (msg.type === "put") {
(0, reduxAction_1.default)("SET_UUID_DISPLAYNAME", {
displayName: msg.v.displayName,
uuid,
});
}
});
self.toolDb.subscribeData(`${uuid}-displayname`, true);
self.toolDb.addKeyListener(self.toolDb.getUserNamespacedKey(`${uuid}-cards`), (msg) => {
if (msg.type === "put") {
(0, reduxAction_1.default)("SET_UUID_CARDS_DATA", { cards: msg.v, uuid });
Expand Down
2 changes: 1 addition & 1 deletion src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function App(props: AppProps) {
// 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
56 changes: 34 additions & 22 deletions src/components/popups/ArenaIdSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,45 @@ export default function ArenaIdSelector(props: ArenaIdSelectorProps) {
<Close fill="var(--color-text-hover)" />
</div>
<div style={{ margin: "22px 16px 16px 16px" }}>
<h3 style={{ marginBottom: "16px" }}>
Arena User ID{" "}
<i style={{ color: "var(--color-text-dark)" }}>
(Wizards Account ID)
</i>
</h3>
{Object.keys(uuidData).map((uuid) => {
const selected = currentUUID === uuid;
return (
<div key={`uuid-selector-${uuid}`}>
<Radio
text={uuid}
value={selected}
callback={() => {
switchPlayerUUID(uuid);
}}
/>
</div>
);
})}
<h3 style={{ marginBottom: "16px" }}>MTG Arena Account User and ID</h3>
{Object.keys(uuidData)
.filter((v) => v !== "undefined")
.map((uuid) => {
const { displayName } = uuidData[uuid];
const selected = currentUUID === uuid;
return (
<div key={`uuid-selector-${uuid}`}>
<Radio
text={
<p>
{displayName || "Unknown"}
{` - `}
<i
style={{
color: "var(--color-text-dark)",
}}
>
{uuid}
</i>
</p>
}
value={selected}
callback={() => {
switchPlayerUUID(uuid);
}}
/>
</div>
);
})}

<p style={{ marginTop: "16px", color: "var(--color-text-dark)" }}>
<b style={{ color: "var(--color-r)" }}>Warning: </b>
Your cards, decks, matches and rank data are filtered depending on
your account id. Switching this while you play with a different
your MTGA account. Switching while you play with a different MTGA
account will result on data being mixed between accounts unexpectedly.
MTG Arena Tool might switch between account ids to avoid this from
{` `}
<b>MTG Arena Tool will switch</b> between accounts automatically
depending on what it sees as the current account to avoid this from
happening.
</p>
</div>
Expand Down
8 changes: 4 additions & 4 deletions src/daemon/fetchPlayerId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import switchPlayerUUID from "../utils/switchPlayerUUID";
export default function fetchPlayerId(): Promise<string | undefined> {
return new Promise((resolve) => {
if (globalData.daemon) {
globalData.daemon.getPlayerId().then((pid) => {
if (pid && pid !== "") {
switchPlayerUUID(pid);
resolve(pid);
globalData.daemon.getPlayerId().then((d) => {
if (d && d.playerId !== "") {
switchPlayerUUID(d.playerId, d.displayName);
resolve(d.playerId);
} else {
resolve(undefined);
}
Expand Down
13 changes: 10 additions & 3 deletions src/daemon/mtgaTrackerDaemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ import getLocalSetting from "../utils/getLocalSetting";
import globalData from "../utils/globalData";
import setLocalSetting from "../utils/setLocalSetting";

interface DaemonPlayerId {
playerId: string;
displayName?: string;
personaId?: string;
elapsedTime: number;
}

interface DaemonInventory {
gems: number;
gold: number;
Expand Down Expand Up @@ -272,11 +279,11 @@ export default class MtgaTrackerDaemon {
.catch(() => []);
}

public getPlayerId(): Promise<string | null> {
public getPlayerId(): Promise<DaemonPlayerId | null> {
console.log("mtgaTrackerDaemon getPlayerId()");
return axios
.get(`${this._url}/playerId`)
.then((d) => d.data.playerId)
.get<DaemonPlayerId>(`${this._url}/playerId`)
.then((d) => d.data)
.catch(() => null);
}

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":1694811826188}
{"version":"6.3.1","branch":"dev","timestamp":1694979864001}
1 change: 1 addition & 0 deletions src/redux/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const actions = {
SET_LIVE_FEED: MainDataSlice.setLiveFeed,
SET_LIVE_FEED_MATCH: MainDataSlice.setLiveFeedMatch,
SET_UUID: MainDataSlice.setUUID,
SET_UUID_DISPLAYNAME: MainDataSlice.setUUIDDisplayName,
SET_UUID_RANK_DATA: MainDataSlice.setUUIDRank,
SET_UUID_INVENTORY_DATA: MainDataSlice.setUUIDInventory,
SET_UUID_CARDS_DATA: MainDataSlice.setUUIDCards,
Expand Down
17 changes: 17 additions & 0 deletions src/redux/slices/mainDataSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const mainState = {
rank: DbRankData;
inventory: DbInventoryData;
cards: DbCardsData;
displayName: string | null;
}
>,
liveFeed: [] as string[],
Expand All @@ -41,6 +42,7 @@ export function makeDefaultUUIDData(): MainState["uuidData"][""] {
rank: { ...defaultRankData, updated: 0 },
inventory: { ...defaultInventoryData, updated: 0 },
cards: defaultCardsData,
displayName: null,
};
}

Expand Down Expand Up @@ -72,6 +74,20 @@ const mainDataSlice = createSlice({
setUUID: (state: MainState, action: PayloadAction<string>): void => {
state.currentUUID = action.payload;
},
setUUIDDisplayName: (
state: MainState,
action: PayloadAction<{ displayName: string | null; uuid: string }>
): void => {
if (!state.uuidData[action.payload.uuid]) {
state.uuidData[action.payload.uuid] = {
...makeDefaultUUIDData(),
displayName: action.payload.displayName,
};
} else {
state.uuidData[action.payload.uuid].displayName =
action.payload.displayName;
}
},
setUUIDRank: (
state: MainState,
action: PayloadAction<{ rank: DbRankData; uuid: string }>
Expand Down Expand Up @@ -152,6 +168,7 @@ export const {
setLiveFeedMatch,
setUUID,
setForceCollection,
setUUIDDisplayName,
setUUIDRank,
setUUIDInventory,
setUUIDCards,
Expand Down
27 changes: 27 additions & 0 deletions src/toolDb/upsertDbDisplayName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import reduxAction from "../redux/reduxAction";
import store from "../redux/stores/rendererStore";
import { DbDisplayName } from "../types/dbTypes";
import { putData } from "./worker-wrapper";

export default async function upsertDbDisplayName(
displayName: string,
uuid: string
) {
console.log("> Upsert displayName", displayName, uuid);

const { dispatch } = store;

reduxAction(dispatch, {
type: "SET_UUID_DISPLAYNAME",
arg: { displayName, uuid },
});

putData<DbDisplayName>(
`${uuid}-displayname`,
{
displayName,
updated: new Date().getTime(),
},
true
);
}
5 changes: 5 additions & 0 deletions src/types/dbTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ export type DbInventoryInfo = Omit<
"SeqId" | "Changes" | "CustomTokens" | "Vouchers" | "Cosmetics"
>;

export type DbDisplayName = {
displayName: string | null;
updated: number;
};

export type DbCardsData = {
cards: Cards;
prevCards: Cards;
Expand Down
9 changes: 8 additions & 1 deletion src/utils/switchPlayerUUID.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ import _ from "lodash";

import reduxAction from "../redux/reduxAction";
import store from "../redux/stores/rendererStore";
import upsertDbDisplayName from "../toolDb/upsertDbDisplayName";
import upsertDbUserdata from "../toolDb/upsertDbuserdata";
import setLocalSetting from "./setLocalSetting";

export default async function switchPlayerUUID(uuid: string) {
export default async function switchPlayerUUID(
uuid: string,
displayName?: string
) {
console.info("switchPlayerUUID", uuid);
setLocalSetting("playerId", uuid);
const { dispatch, getState } = store;
Expand All @@ -15,6 +19,9 @@ export default async function switchPlayerUUID(uuid: string) {
type: "SET_UUID",
arg: uuid,
});
if (displayName) {
upsertDbDisplayName(displayName, uuid);
}

upsertDbUserdata({ [uuid]: new Date().getTime() });
}
Expand Down
20 changes: 19 additions & 1 deletion tooldb-worker/afterLogin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
/* eslint-disable no-restricted-globals */
import { DbCardsData, DbInventoryData, DbRankData } from "./dbTypes";
import {
DbCardsData,
DbDisplayName,
DbInventoryData,
DbRankData,
} from "./dbTypes";
import handleDraftsIndex from "./handleDraftsIndex";
import handleLiveFeed from "./handleLiveFeed";
import handleMatchesIndex from "./handleMatchesIndex";
Expand Down Expand Up @@ -43,6 +48,19 @@ export default function afterLogin() {
newest = uuid;
}

self.toolDb.addKeyListener<DbDisplayName>(
self.toolDb.getUserNamespacedKey(`${uuid}-displayname`),
(msg) => {
if (msg.type === "put") {
reduxAction("SET_UUID_DISPLAYNAME", {
displayName: msg.v.displayName,
uuid,
});
}
}
);
self.toolDb.subscribeData(`${uuid}-displayname`, true);

self.toolDb.addKeyListener<DbCardsData>(
self.toolDb.getUserNamespacedKey(`${uuid}-cards`),
(msg) => {
Expand Down
45 changes: 25 additions & 20 deletions tooldb-worker/dbTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,26 @@ export interface InventoryInfo {
Boosters: { CollationId: number; SetCode: string; Count: number }[];
Vouchers: any;
Cosmetics:
| any
| {
ArtStyles: {
Type: string;
Id: string;
ArtId: number;
Variant: string;
}[];
Avatars: { Id: string; Type: string }[];
Pets: { Type: string; Id: string; Name: string; Variant: string }[];
Sleeves: { Id: string; Type: string }[];
Emotes: {
Id: string;
Type: string;
Page: string;
FlipType: string;
Category: string;
Treatment: string;
}[];
};
| any
| {
ArtStyles: {
Type: string;
Id: string;
ArtId: number;
Variant: string;
}[];
Avatars: { Id: string; Type: string }[];
Pets: { Type: string; Id: string; Name: string; Variant: string }[];
Sleeves: { Id: string; Type: string }[];
Emotes: {
Id: string;
Type: string;
Page: string;
FlipType: string;
Category: string;
Treatment: string;
}[];
};
}

export interface CombinedRankInfo {
Expand Down Expand Up @@ -161,6 +161,11 @@ export type DbInventoryInfo = Omit<
"SeqId" | "Changes" | "CustomTokens" | "Vouchers" | "Cosmetics"
>;

export type DbDisplayName = {
displayName: string | null;
updated: number;
};

export type DbCardsData = {
cards: Cards;
prevCards: Cards;
Expand Down

0 comments on commit 971dda6

Please sign in to comment.