Skip to content

Commit

Permalink
feature: Sort the weapons by type and then power (#1203)
Browse files Browse the repository at this point in the history
  • Loading branch information
NigelBreslaw authored Apr 11, 2024
1 parent 65b77f4 commit b00351a
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
18 changes: 13 additions & 5 deletions native/app/store/AccountInventoryLogic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { DestinyItem, GuardianGear, VaultData } from "@/app/bungie/Types.ts";
import type { DestinyItem, DestinyItemSort, GuardianGear, VaultData } from "@/app/bungie/Types.ts";
import {
UiCellType,
armorPageBuckets,
Expand All @@ -14,6 +14,7 @@ import {
} from "@/app/inventory/Common.ts";
import type { AccountSliceGetter, AccountSliceSetter } from "@/app/store/AccountSlice.ts";
import { itemsDefinition, rawProfileData } from "@/app/store/Definitions.ts";
import { typeAndPowerSort } from "@/app/utilities/Helpers.ts";
import { create } from "mutative";

// ------------------------------
Expand Down Expand Up @@ -74,7 +75,7 @@ export function buildUIData(get: AccountSliceGetter, itemBuckets: number[]): UiC
if (equipped) {
equipSectionCell.equipped = returnDestinyIconData(equipped);
}
equipSectionCell.inventory = returnInventoryArray(bucketItems);
equipSectionCell.inventory = returnInventoryArray(bucketItems, bucket);

dataArray.push(equipSectionCell);
}
Expand Down Expand Up @@ -102,7 +103,7 @@ function returnVaultUiData(itemBuckets: number[], vaultData: VaultData): UiCell[
dataArray.push(separator);

// get an array of all the items
const totalItemsArray = returnInventoryArray(bucketItems);
const totalItemsArray = returnInventoryArray(bucketItems, bucket);

let itemsLeft = totalItemsArray.length;
let count = 0;
Expand Down Expand Up @@ -188,10 +189,17 @@ function returnBorderColor(item: DestinyItem): string {
return "#555555";
}

function returnInventoryArray(characterGear: GuardianGear): DestinyIconData[] {
const weaponBuckets = [1498876634, 2465295065, 953998645];

function returnInventoryArray(characterGear: GuardianGear, bucketHash: number): DestinyIconData[] {
const inventoryArray: DestinyIconData[] = [];

for (const item of characterGear.inventory) {
let existingArray = characterGear.inventory as DestinyItemSort[];
if (weaponBuckets.includes(bucketHash)) {
existingArray = existingArray.sort(typeAndPowerSort);
}

for (const item of existingArray) {
const iconData = returnDestinyIconData(item);
inventoryArray.push(iconData);
}
Expand Down
60 changes: 60 additions & 0 deletions native/app/utilities/Helpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { DestinyItemSort } from "@/app/bungie/Types.ts";

// biome-ignore lint/complexity/noBannedTypes: <explanation>
export const debounce = (func: Function, delay = 0) => {
let timeoutId: string | number | NodeJS.Timeout | undefined;
Expand Down Expand Up @@ -82,3 +84,61 @@ export async function benchmarkAsync<T extends any[], R>(func: (...args: T) => P
export function bitmaskContains(bitmask: number, value: number): boolean {
return (bitmask & value) === value;
}

export function typeAndPowerSort(a: DestinyItemSort, b: DestinyItemSort): number {
/// subtype
if (a.itemSubType > b.itemSubType) {
return 1;
}
if (a.itemSubType < b.itemSubType) {
return -1;
}

// /// primaryStat
if (a.primaryStat < b.primaryStat) {
return 1;
}
if (a.primaryStat > b.primaryStat) {
return -1;
}

/// tierType
if (a.tierType < b.tierType) {
return 1;
}
if (a.tierType > b.tierType) {
return -1;
}

if (a.damageType > b.damageType) {
return 1;
}
if (a.damageType < b.damageType) {
return -1;
}

if (a.destinyClass > b.destinyClass) {
return 1;
}
if (a.destinyClass < b.destinyClass) {
return -1;
}

// /// itemHash
if (a.itemHash > b.itemHash) {
return 1;
}
if (a.itemHash < b.itemHash) {
return -1;
}

// /// Criteria: masterwork
if (!a.masterwork && b.masterwork) {
return 1;
}
if (a.masterwork && !b.masterwork) {
return -1;
}

return a.itemInstanceId < b.itemInstanceId ? 1 : -1;
}

0 comments on commit b00351a

Please sign in to comment.