Skip to content

Commit

Permalink
Fixups
Browse files Browse the repository at this point in the history
  • Loading branch information
bhollis committed Aug 9, 2024
1 parent 7956257 commit 44f5080
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 38 deletions.
43 changes: 16 additions & 27 deletions src/app/inventory/spreadsheets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { customStatsSelector, languageSelector } from 'app/dim-api/selectors';
import { gaEvent } from 'app/google';
import { LoadoutsByItem, loadoutsByItemSelector } from 'app/loadout/selectors';
import { buildStatInfo, getColumns } from 'app/organizer/Columns';
import { SpreadsheetContext } from 'app/organizer/table-types';
import { CSVColumn, SpreadsheetContext } from 'app/organizer/table-types';
import { D1_StatHashes } from 'app/search/d1-known-values';
import { TOTAL_STAT_HASH } from 'app/search/d2-known-values';
import { ThunkResult } from 'app/store/types';
Expand All @@ -28,15 +28,15 @@ import { DimStore } from './store-types';
function getClass(type: DestinyClass) {
switch (type) {
case DestinyClass.Titan:
return 'titan';
return 'Titan';
case DestinyClass.Hunter:
return 'hunter';
return 'Hunter';
case DestinyClass.Warlock:
return 'warlock';
return 'Warlock';
case DestinyClass.Unknown:
return 'unknown';
return 'Unknown';
case DestinyClass.Classified:
return 'classified';
return 'Classified';
}
}

Expand Down Expand Up @@ -100,14 +100,12 @@ export function generateCSVExportData(
loadoutsByItem: LoadoutsByItem,
customStats: CustomStatDef[],
) {
const nameMap: { [storeId: string]: string } = {};
const storeNamesById: { [storeId: string]: string } = {};
let allItems: DimItem[] = [];
for (const store of stores) {
allItems = allItems.concat(store.items);
nameMap[store.id] =
store.id === 'vault'
? 'Vault'
: `${capitalizeFirstLetter(getClass(store.classType))}(${store.powerLevel})`;
storeNamesById[store.id] =
store.id === 'vault' ? 'Vault' : `${getClass(store.classType)}(${store.powerLevel})`;
}
allItems.sort(compareBy((item) => item.index));
let items: DimItem[] = [];
Expand Down Expand Up @@ -186,8 +184,6 @@ export function generateCSVExportData(
// unknown columns end up here
// then perks
];

// Damn, this needs to be column ids, not column names
columns.sort(
compareBy((c) => {
if (c.id === 'perks') {
Expand All @@ -197,26 +193,26 @@ export function generateCSVExportData(
const index = order.indexOf(c.id);
if (index < 0) {
// A new column was added and we need to add it to the order above
throw new Error('missing-column-order ' + c.id);
throw new Error(`missing-column-order ${c.id}`);
}
return index;
}),
);

const context: SpreadsheetContext = { nameMap, maxPerks };
const context: SpreadsheetContext = { storeNamesById, maxPerks };
const data = items.map((item) => {
let row: Record<string, unknown> = {};
const row: Record<string, unknown> = {};
for (const column of columns) {
const value = column.value(item);
if (column.csv) {
row[column.csv] ||= value;
} else if (column.csvVal) {
const values = column.csvVal!(value, item, context);
const values = column.csvVal(value, item, context);
if (!values || values.length === 0) {
continue;
}
if (Array.isArray(values[0])) {
for (const [key, value] of values as [string, string | number | boolean | undefined][]) {
for (const [key, value] of values as CSVColumn[]) {
row[key] ||= value;
}
} else {
Expand Down Expand Up @@ -255,8 +251,8 @@ export function downloadCsvFiles(type: 'weapon' | 'armor' | 'ghost'): ThunkResul
loadoutsForItem,
customStats,
);
data.sort(localizedSorter(language, (r: any) => r['Name']));
downloadCsv(`destiny${type}`, Papa.unparse(data));
data.sort(localizedSorter(language, (r) => (r as { Name: string }).Name));
downloadCsv(`destiny-${type}`, Papa.unparse(data));
};
}

Expand Down Expand Up @@ -337,13 +333,6 @@ export function importTagsNotesFromCsv(files: File[]): ThunkResult<number | unde
};
}

function capitalizeFirstLetter(str: string) {
if (!str || str.length === 0) {
return '';
}
return str.charAt(0).toUpperCase() + str.slice(1);
}

function downloadCsv(filename: string, csv: string) {
const filenameWithExt = `${filename}.csv`;
gaEvent('file_download', {
Expand Down
27 changes: 17 additions & 10 deletions src/app/organizer/Columns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import {
getItemKillTrackerInfo,
getItemYear,
getMasterworkStatNames,
getSpecialtySocketMetadatas,
isArtificeSocket,
isD1Item,
isKillTrackerSocket,
Expand Down Expand Up @@ -202,6 +201,8 @@ export function getColumns(
return `stat:${statName}:${statName === 'rof' ? '=' : '>='}${value}`;
},
csvVal: (_value, item) => {
// Re-find the stat instead of using the value passed in, because the
// value passed in can be different if it's Recoil.
const stat = item.stats?.find((s) => s.statHash === statHash);
return [csvStatNames.get(statHash) ?? `UnknownStat ${statHash}`, stat?.value ?? 0];
},
Expand Down Expand Up @@ -237,6 +238,8 @@ export function getColumns(
},
filter: (value) => `basestat:${_.invert(statHashByName)[column.statHash]}:>=${value}`,
csvVal: (_value, item) => {
// Re-find the stat instead of using the value passed in, because the
// value passed in can be different if it's Recoil.
const stat = item.stats?.find((s) => s.statHash === column.statHash);
return [
`${csvStatNames.get(column.statHash) ?? `UnknownStatBase ${column.statHash}`} (Base)`,
Expand Down Expand Up @@ -418,7 +421,7 @@ export function getColumns(
defaultSort: SortDirection.DESC,
filter: (value) => `${value ? '' : '-'}is:crafted`,
// TODO: nicer to put the date in the CSV
csvVal: (value) => ['Crafted', Boolean(value) ? 'crafted' : false],
csvVal: (value) => ['Crafted', value ? 'crafted' : false],
}),
!isSpreadsheet &&
c({
Expand Down Expand Up @@ -503,18 +506,20 @@ export function getColumns(
.map((m) => `modslot:${m}`)
.join(' ')
: ``,
csvVal: (_val, item) => {
return ['Seasonal Mod', getSpecialtySocketMetadatas(item)?.map((m) => m.slotTag) ?? ''];
},
csvVal: (value) => [
'Seasonal Mod',
// Yes, this is an array
value?.split(',') ?? [],
],
}),
destinyVersion === 1 &&
c({
id: 'percentComplete',
header: t('Organizer.Columns.PercentComplete'),
csv: '% Leveled',
value: (item) => item.percentComplete,
cell: (value) => percent(value),
filter: (value) => `percentage:>=${value}`,
csvVal: (value) => ['% Leveled', (value * 100).toFixed(0)],
}),
destinyVersion === 2 &&
isWeapon &&
Expand Down Expand Up @@ -608,9 +613,11 @@ export function getColumns(
? buildSocketNames(item)
: [];

return _.times(maxPerks, (index) => {
return [`Perks ${index}`, perks[index]] as [name: string, value: string | undefined];
});
// Return multiple columns
return _.times(
maxPerks,
(index) => [`Perks ${index}`, perks[index]] as [name: string, value: string | undefined],
);
},
}),
destinyVersion === 2 &&
Expand Down Expand Up @@ -785,7 +792,7 @@ export function getColumns(
header: t('Organizer.Columns.Location'),
value: (item) => item.owner,
cell: (_val, item) => <StoreLocation storeId={item.owner} />,
csvVal: (value, _item, { nameMap }) => ['Owner', nameMap[value]],
csvVal: (value, _item, { storeNamesById }) => ['Owner', storeNamesById[value]],
}),
c({
id: 'loadouts',
Expand Down
2 changes: 1 addition & 1 deletion src/app/organizer/table-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export interface ColumnDefinition<V extends Value = Value> {
}

export interface SpreadsheetContext {
nameMap: { [key: string]: string };
storeNamesById: { [key: string]: string };
maxPerks: number;
}

Expand Down

0 comments on commit 44f5080

Please sign in to comment.