Files
discord-decomp/modules/badges/BadgeDirectoryStore.tsx
T
2026-11-01 12:12:55 +02:00

227 lines
6.6 KiB
TypeScript

// Metro module 8109 (f8110).
import UserStore from "../../stores/UserStore";
import Durations from "../../utils/Durations";
import { resolveProfileBadgeId } from "./BadgeIdResolution";
import Backoff from "../../_bundle/Backoff";
import { fetchBadgeDirectory } from "./BadgeDirectoryActionCreators";
import Module589 from "../../_bundle/Module589";
import Dispatcher from "../../Dispatcher";
const HOUR = Durations.Millis.HOUR;
let c5146_5 = new Map();
let c5146_6 = new Set();
let c5146_7 = new Set();
let c5146_8 = new Map();
let c5146_9 = new Map();
let c5146_10 = new Map();
class BadgeDirectoryStore extends Module589.Store {
static displayName = "BadgeDirectoryStore";
initialize() {
this.waitFor(UserStore);
}
getBadges(a0) {
const r4 = a0 == null ? UserStore.getCurrentUser()?.id : a0;
if (r4 == null) {
return [];
}
const r6 = c5146_5.get(r4);
return r6 != null ? Array.from(r6.values()) : [];
}
hasCatalogFor(a0) {
return c5146_6.has(a0);
}
isCatalogStaleFor(a0) {
const r6 = c5146_8.get(a0);
return r6 == null || Date.now() - r6 >= HOUR;
}
hasCatalogFetchErrorFor(a0) {
const r5 = a0 == null ? UserStore.getCurrentUser()?.id : a0;
return r5 != null && c5146_7.has(r5);
}
getBadgeById(a0, a1) {
const r6 = a1 == null ? UserStore.getCurrentUser()?.id : a1;
const r9 = r6 != null;
let r10 = void 0;
if (r9) {
const r12 = c5146_5.get(r6);
r10 = r12 == null ? void 0 : r12.get(a0);
}
return r10;
}
getSingleRequirementProgress(a0, a1) {
const progress = this.getBadgeById(a0, a1)?.progress;
if (progress != null && progress.length !== 0) {
return progress[0];
}
}
getCurrentTier(a0, a1) {
const badgeById = this.getBadgeById(a0, a1);
if (badgeById?.current_tier != null) {
return badgeById.tiers.find((tier) => tier.key === badgeById.current_tier);
}
}
getObtainedAt(a0, a1) {
const badgeById3 = this.getBadgeById(a0, a1);
const r1 = badgeById3 != null;
let r9 = void 0;
if (r1) {
const r11 = badgeById3.current_tier != null;
let r12 = void 0;
if (r11) {
const tier_obtained_at = badgeById3.tier_obtained_at;
r12 = tier_obtained_at == null ? void 0 : tier_obtained_at[badgeById3.current_tier];
}
if (r12 == null) {
r12 = badgeById3.obtained_at;
}
r9 = r12;
}
return r9;
}
getNextTier(a0, a1) {
const badgeById2 = this.getBadgeById(a0, a1);
if (badgeById2?.next_tier != null) {
return badgeById2.tiers.find((tier) => tier.key === badgeById2.next_tier);
}
}
getRemainingToNextTier(a0, a1) {
const nextTier = this.getNextTier(a0, a1);
const singleRequirementProgress = this.getSingleRequirementProgress(a0, a1);
const r2 = nextTier == null;
let r12 = 0;
if (!r2) {
const r13 = singleRequirementProgress == null;
r12 = 0;
if (!r13) {
const r15 = Math;
let r8 = nextTier == null ? void 0 : nextTier.requirements[0]?.threshold;
if (r8 == null) {
r8 = 0;
}
r12 = r15.max(0, r8 - singleRequirementProgress.current);
}
}
return r12;
}
}
const badgeDirectoryStore = new BadgeDirectoryStore(Dispatcher, {
BADGE_DIRECTORY_FETCH_START: function handleFetchStart({ userId }) {
const set = new Set(c5146_7);
c5146_7 = set;
set.delete(userId);
},
BADGE_DIRECTORY_FETCH_SUCCESS: function handleFetchSuccess({ userId, badges }) {
const map = new Map(c5146_5);
map.set(userId, new Map(badges.map((badge) => [badge.badge_id, badge])));
c5146_5 = map;
c5146_6 = new Set(c5146_6).add(userId);
const set = new Set(c5146_7);
c5146_7 = set;
set.delete(userId);
c5146_8 = new Map(c5146_8).set(userId, Date.now());
},
BADGE_DIRECTORY_FETCH_FAILURE: function handleFetchFailure({ userId }) {
c5146_7 = new Set(c5146_7).add(userId);
},
BADGE_FETCH_SUCCESS: function handleBadgeFetchSuccess({ userId, badge }) {
const map = new Map(c5146_5);
let r15 = map.get(userId);
if (r15 == null) {
r15 = new Map();
}
const map2 = new Map(r15);
map2.set(badge.badge_id, badge);
map.set(userId, map2);
c5146_5 = map;
},
USER_PROFILE_FETCH_SUCCESS: function handleUserProfileFetchSuccess({ userProfile }) {
let r24;
const id = userProfile.user.id;
if (c5146_6.has(id)) {
let badges = userProfile.badges;
if (badges == null) {
badges = [];
}
const length = badges.filter((badge) => resolveProfileBadgeId(badge.id) != null).length;
let r33 = 0;
const r35 = c5146_5.get(id);
let r9 = r35 == null ? void 0 : r35.values();
if (r9 == null) {
r9 = [];
}
let r39 = r9[Symbol.iterator]();
const next = r39.next;
while (true) {
const r42 = r39 === void 0 ? void 0 : (r24 = next.call(r39)).done ? r39 = void 0 : r24.value;
if (r39 === void 0) {
if (length !== r33) {
let r56 = c5146_10.get(id);
if (r56 == null) {
r56 = 0;
}
if (!(Date.now() >= r56)) {
break;
}
let r63 = c5146_9.get(id);
if (r63 == null) {
r63 = new Backoff(Durations.Millis.MINUTE, HOUR, true);
}
c5146_9.set(id, r63);
c5146_10.set(id, Date.now() + r63.fail());
fetchBadgeDirectory(id);
break;
}
const r48 = c5146_9.get(id);
if (r48 != null) {
r48.succeed();
}
c5146_10.delete(id);
break;
}
try {
if (r42.owned) {
r33 = r33 + 1;
}
} catch (r45) {
if (r39 !== void 0 && typeof r39.return === "function") {
try {
r39.return();
} catch {}
}
throw r45;
}
}
}
return false;
},
LOGOUT: function handleReset() {
c5146_5 = new Map();
c5146_6 = new Set();
c5146_7 = new Set();
c5146_8 = new Map();
c5146_9.clear();
c5146_10.clear();
}
});
export default badgeDirectoryStore;
export function getSingleRequirementThreshold(a0) {
let r4 = a0 == null ? void 0 : a0.requirements[0]?.threshold;
if (r4 == null) {
r4 = 0;
}
return r4;
}
export function getObtainedAtFromBadge(a0) {
if (a0 != null) {
const r7 = a0.current_tier != null;
let r8 = void 0;
if (r7) {
const tier_obtained_at = a0.tier_obtained_at;
r8 = tier_obtained_at == null ? void 0 : tier_obtained_at[a0.current_tier];
}
if (r8 == null) {
r8 = a0.obtained_at;
}
return r8;
}
}