Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent gear OCR from overwriting a gear's type #93

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tof-tools",
"version": "3.1.0",
"version": "3.1.1",
"private": true,
"scripts": {
"dev": "next dev",
Expand Down
5 changes: 1 addition & 4 deletions src/features/GearOCRModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,7 @@ export const GearOCRModal = ({
<GearPiece
gearSnap={tempGearSnap as Gear}
gearState={tempGearState}
onGearTypeChange={(gearType) => {
tempGearState.type = gearType;
}}
showGearOCRButton={false}
disableGearTypeChange
/>
)}
</Grid>
Expand Down
6 changes: 3 additions & 3 deletions src/features/GearPiece.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type { ReactNode } from 'react';
import { GearTypeIcon } from '../components/GearTypeIcon/GearTypeIcon';
import { GearTypeSelector } from '../components/GearTypeSelector/GearTypeSelector';
import type { CoreElementalType } from '../constants/elemental-type';
import { Gear } from '../models/gear';
import type { Gear } from '../models/gear';
import type { GearType } from '../models/gear-type';
import { getPossibleRandomStatTypes } from '../models/gear-type';
import { RandomStat } from '../models/random-stat';
Expand All @@ -29,7 +29,7 @@ import { EmptyStatEditor, StatEditor } from './StatEditor';
export interface GearPieceProps {
gearSnap: Gear;
gearState: Gear;
showGearOCRButton?: boolean;
showGearOCRButton?: { onGearChangeFromOCR: (gearFromOCR: Gear) => void };
disableGearTypeChange?: boolean;
onGearTypeChange?: (gearType: GearType) => void;
showSaveGearButton?: Pick<SaveGearModalProps, 'targetLoadout'>;
Expand Down Expand Up @@ -97,7 +97,7 @@ export const GearPiece = ({
{showGearOCRButton && (
<GearOCRModal
onFinalizeGear={(replacementGear) => {
Gear.copy(replacementGear, gearState);
showGearOCRButton.onGearChangeFromOCR(replacementGear);
}}
enforceGearType={
disableGearTypeChange ? gearSnap.type.id : undefined
Expand Down
7 changes: 6 additions & 1 deletion src/features/gear-comparer/LoadoutGear.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ export function LoadoutGear() {
onGearTypeChange={(gearType) => {
gearComparerState.selectedGearTypeId = gearType.id;
}}
showGearOCRButton
showGearOCRButton={{
onGearChangeFromOCR(gearFromOCR) {
gearComparerState.selectedLoadout.gearSet.setGear(gearFromOCR);
gearComparerState.selectedGearTypeId = gearFromOCR.type.id;
},
}}
showStatSummary={elementalType}
maxTitanStatsContent={
gearSnap.stars !== 5 &&
Expand Down
7 changes: 6 additions & 1 deletion src/features/gear-comparer/ReplacementGear.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ export function ReplacementGear() {
onGearTypeChange={(gearType) => {
gearComparerState.selectedGearTypeId = gearType.id;
}}
showGearOCRButton
showGearOCRButton={{
onGearChangeFromOCR(gearFromOCR) {
gearComparerState.replacementGearGearSet.setGear(gearFromOCR);
gearComparerState.selectedGearTypeId = gearFromOCR.type.id;
},
}}
showSaveGearButton={{ targetLoadout: gearComparerState.selectedLoadout }}
showStatSummary={elementalType}
maxTitanStatsContent={
Expand Down
8 changes: 7 additions & 1 deletion src/features/loadouts/LoadoutGearSet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ export function LoadoutGearSet() {
<GearPiece
gearSnap={gearSnap}
gearState={gearState}
showGearOCRButton
showGearOCRButton={{
onGearChangeFromOCR(gearFromOCR) {
if (gearFromOCR.type.id === gearTypeId) {
gearSetState.setGear(gearFromOCR);
}
},
}}
disableGearTypeChange
showStatSummary={loadoutSnap.elementalType}
data-testid={gearTypeId}
Expand Down
11 changes: 4 additions & 7 deletions src/models/gear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ export class Gear implements Persistable<GearDto> {
public get type() {
return this._type;
}
public set type(type: GearType) {
this._type = type;
this.resetRandomStats();
}

public get stars() {
return this._stars;
Expand Down Expand Up @@ -418,9 +414,10 @@ export class Gear implements Persistable<GearDto> {
};
}

// Copy all gear properties over except for the id
/** Copy all gear properties over except for the id, as long as the gear's type is the same */
public static copy(from: Gear, to: Gear) {
to.type = from.type;
if (from.type.id !== to.type.id) return;

to.stars = from.stars;

to.randomStats = [];
Expand Down Expand Up @@ -467,7 +464,7 @@ export class Gear implements Persistable<GearDto> {

const gearType = gearTypesLookup.byId[typeId];
this._id = id;
this.type = gearType;
this._type = gearType;
this.stars = stars;
this.isAugmented = !!isAugmented;
this.isTitan = !!isTitan;
Expand Down
4 changes: 4 additions & 0 deletions src/states/gear-comparer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export class GearComparerState implements Persistable<GearComparerStateDto> {
return this.selectedLoadout.getGearValue(this.selectedGearTypeId);
}

public get replacementGearGearSet(): GearSet {
return this._replacementGearGearSet;
}

public get replacementGear(): Gear {
return this._replacementGearGearSet.getGearByType(this.selectedGearTypeId);
}
Expand Down
7 changes: 7 additions & 0 deletions src/states/migrations/state-migrations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { initializeChangelogState } from './v1/initialize-changelog-state';
import { migrateTeamsGearSetsStatsToLoadouts } from './v1/migrate-teams-gear-sets-stats-to-loadouts';
import { fixGearSetsGearTypeData } from './v2/fix-gear-sets-gear-type-data';

export const stateMigrations: StateMigration[] = [
{
Expand All @@ -9,6 +10,12 @@ export const stateMigrations: StateMigration[] = [
initializeChangelogState();
},
},
{
version: 2,
migrate: () => {
fixGearSetsGearTypeData();
},
},
];

interface StateMigration {
Expand Down
66 changes: 66 additions & 0 deletions src/states/migrations/v2/fix-gear-sets-gear-type-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { nanoid } from 'nanoid';

import type { GearName } from '../../../constants/gear-types';
import type { GearSetDtoV2 } from '../../../models/gear-set';
import type { GearComparerStateDto } from '../../gear-comparer';
import type { LoadoutsStateDto } from '../../loadouts';

/** Data fix for potential issue with gear sets gear type data that may have been introduced in 3.0.0.
*
* A gear set's structure looks something like this:
* { gearsByTypeId: { Helmet: { id: ..., type: 'Helmet', randomStats: ... }, Eyepiece: { id: ..., type: 'Eyepiece', randomStats: ... } }}
*
* In gear comparer's gear OCR feature, a gear's type can be overwritten, causing an issue where a gear of a different type can be in a gearsByTypeId slot of another type.
*
* e.g. { gearsByTypeId: { Helmet: { id: ..., type: 'Eyepiece', randomStats: ... }, Eyepiece: { id: ..., type: 'Eyepiece', randomStats: ... } }}
*
* Fix this by identifying the incorrect gears and resetting them to an empty gear of the correct type */
export function fixGearSetsGearTypeData() {
const loadoutsStateJson = localStorage.getItem('loadouts');
const gearComparerStateJson = localStorage.getItem('gearComparer');

const loadoutsState = loadoutsStateJson
? (JSON.parse(loadoutsStateJson) as LoadoutsStateDto)
: undefined;
const gearComparerState = gearComparerStateJson
? (JSON.parse(gearComparerStateJson) as GearComparerStateDto)
: undefined;

const gearSets: GearSetDtoV2[] = [];
if (loadoutsState) {
gearSets.push(
...loadoutsState.loadoutList.map(
(loadoutItem) => loadoutItem.loadout.gearSet
)
);
}
if (gearComparerState) {
gearSets.push(gearComparerState.replacementGearGearSet);
}

gearSets.forEach((gearSet) => {
const gearsByTypeId = gearSet.gearsByTypeId;
Object.keys(gearsByTypeId).forEach((gearTypeId) => {
const gear = gearsByTypeId[gearTypeId as GearName];
if (gear?.typeId !== gearTypeId) {
gearsByTypeId[gearTypeId as GearName] = {
id: nanoid(),
typeId: gearTypeId as GearName,
stars: 0,
randomStats: [undefined, undefined, undefined, undefined],
augmentStats: [],
isAugmented: false,
isTitan: false,
version: 1,
};
}
});
});

if (loadoutsState) {
localStorage.setItem('loadouts', JSON.stringify(loadoutsState));
}
if (gearComparerState) {
localStorage.setItem('gearComparer', JSON.stringify(gearComparerState));
}
}
Loading