fix badge inventory (#128)

This commit is contained in:
dank074 2023-01-13 02:16:43 -06:00 committed by GitHub
parent a4d328a582
commit 5ebf58855c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 13 deletions

View File

@ -39,7 +39,7 @@ export const InventoryBadgeView: FC<{}> = props =>
<AutoGrid columnCount={ 4 }> <AutoGrid columnCount={ 4 }>
{ badgeCodes && (badgeCodes.length > 0) && badgeCodes.map((badgeCode, index) => { badgeCodes && (badgeCodes.length > 0) && badgeCodes.map((badgeCode, index) =>
{ {
if(activeBadgeCodes.indexOf(badgeCode) >= 0) return null; if(isWearingBadge(badgeCode)) return null;
return <InventoryBadgeItemView key={ index } badgeCode={ badgeCode } /> return <InventoryBadgeItemView key={ index } badgeCode={ badgeCode } />
}) } }) }

View File

@ -10,7 +10,7 @@ const useInventoryBadgesState = () =>
{ {
const [ needsUpdate, setNeedsUpdate ] = useState(true); const [ needsUpdate, setNeedsUpdate ] = useState(true);
const [ badgeCodes, setBadgeCodes ] = useState<string[]>([]); const [ badgeCodes, setBadgeCodes ] = useState<string[]>([]);
const [ badgeIds, setBadgeIds ] = useState<number[]>([]); const [ badgeIds, setBadgeIds ] = useState<Map<string, number>>(new Map<string, number>());
const [ activeBadgeCodes, setActiveBadgeCodes ] = useState<string[]>([]); const [ activeBadgeCodes, setActiveBadgeCodes ] = useState<string[]>([]);
const [ selectedBadgeCode, setSelectedBadgeCode ] = useState<string>(null); const [ selectedBadgeCode, setSelectedBadgeCode ] = useState<string>(null);
const { isVisible = false, activate = null, deactivate = null } = useSharedVisibility(); const { isVisible = false, activate = null, deactivate = null } = useSharedVisibility();
@ -41,7 +41,7 @@ const useInventoryBadgesState = () =>
const composer = new SetActivatedBadgesComposer(); const composer = new SetActivatedBadgesComposer();
for(let i = 0; i < maxBadgeCount; i++) composer.addActivatedBadge(newValue[i] || null); for(let i = 0; i < maxBadgeCount; i++) composer.addActivatedBadge(newValue[i] ?? '');
SendMessageComposer(composer); SendMessageComposer(composer);
@ -55,7 +55,7 @@ const useInventoryBadgesState = () =>
if(index === -1) return 0; if(index === -1) return 0;
return (badgeIds[index] || 0); return (badgeIds.get(badgeCode) ?? 0);
} }
useMessageEvent<BadgesEvent>(BadgesEvent, event => useMessageEvent<BadgesEvent>(BadgesEvent, event =>
@ -65,23 +65,25 @@ const useInventoryBadgesState = () =>
setBadgeIds(prevValue => setBadgeIds(prevValue =>
{ {
const newValue = [ ...prevValue ]; const newValue = new Map(prevValue);
parser.getAllBadgeCodes().forEach(code => parser.getAllBadgeCodes().forEach(code =>
{ {
const exists = badgeCodes.indexOf(code) >= 0;
const badgeId = parser.getBadgeId(code); const badgeId = parser.getBadgeId(code);
if(newValue.indexOf(badgeId) >= 0) return; newValue.set(code, badgeId);
newValue.push(badgeId); if(exists) return;
badgesToAdd.push(code); badgesToAdd.push(code);
}); });
return newValue; return newValue;
}); });
setBadgeCodes(prevValue => [ ...prevValue, ...badgesToAdd ]);
setActiveBadgeCodes(parser.getActiveBadgeCodes()); setActiveBadgeCodes(parser.getActiveBadgeCodes());
setBadgeCodes(prev => [ ...prev, ...badgesToAdd ]);
}); });
useMessageEvent<BadgeReceivedEvent>(BadgeReceivedEvent, event => useMessageEvent<BadgeReceivedEvent>(BadgeReceivedEvent, event =>
@ -101,10 +103,9 @@ const useInventoryBadgesState = () =>
setBadgeIds(prevValue => setBadgeIds(prevValue =>
{ {
const newValue = [ ...prevValue ]; const newValue = new Map(prevValue);
if(unseen) newValue.unshift(parser.badgeId) newValue.set(parser.badgeCode, parser.badgeId);
else newValue.push(parser.badgeId);
return newValue; return newValue;
}); });