Skip to content

Commit

Permalink
Fix team exim (#1826)
Browse files Browse the repository at this point in the history
* Fix team exim

* opttarget test

* format

* remove console.log
  • Loading branch information
frzyc authored Mar 26, 2024
1 parent 4263ac4 commit 1a0bdfd
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 16 deletions.
25 changes: 13 additions & 12 deletions libs/gi/db/src/Database/DataManagers/BuildTcDataManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { clamp, objKeyMap } from '@genshin-optimizer/common/util'
import { clamp, objKeyMap, objMap } from '@genshin-optimizer/common/util'
import type {
ArtifactRarity,
ArtifactSetKey,
Expand Down Expand Up @@ -98,9 +98,9 @@ export class BuildTcDataManager extends DataManager<

return buildTc
}
export(buildTcId: string): object {
export(buildTcId: string): object | undefined {
const buildTc = this.database.buildTcs.get(buildTcId)
if (!buildTc) return {}
if (!buildTc) return undefined
return buildTc
}
import(data: object): string {
Expand Down Expand Up @@ -200,15 +200,16 @@ function validateCharTCArtifactSlots(
)
)
return initCharTCArtifactSlots()
allArtifactSlotKeys.forEach((slotKey) => {
;(slots as BuildTc['artifact']['slots'])[slotKey].level = clamp(
(slots as BuildTc['artifact']['slots'])[slotKey].level,
0,
artMaxLevel[(slots as BuildTc['artifact']['slots'])[slotKey].rarity]
)
})

return slots as BuildTc['artifact']['slots']
return objMap(
slots as BuildTc['artifact']['slots'],
({ level, rarity, ...rest }) => {
return {
level: clamp(level, 0, artMaxLevel[rarity]),
rarity,
...rest,
}
}
)
}
function validateCharTcOptimization(
optimization: unknown
Expand Down
77 changes: 77 additions & 0 deletions libs/gi/db/src/Database/DataManagers/TeamDataManager.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { DBLocalStorage } from '@genshin-optimizer/common/database'
import { ArtCharDatabase } from '../ArtCharDatabase'
import { initCharTC } from './BuildTcDataManager'
import type { LoadoutDatum } from './TeamDataManager'

describe('export and import test', () => {
const dbStorage = new DBLocalStorage(localStorage)
const dbIndex = 1
let database = new ArtCharDatabase(dbIndex, dbStorage)

beforeEach(() => {
dbStorage.clear()
database = new ArtCharDatabase(dbIndex, dbStorage)
})
test('exim', () => {
// Create a team [Raiden, null, bennett, null]

const raidenId = database.teamChars.new('RaidenShogun', {
buildIds: [database.builds.new()],
buildTcIds: [database.buildTcs.new(initCharTC('EngulfingLightning'))],
optConfigId: database.optConfigs.new({
optimizationTarget: ['test'],
}),
})
expect(database.teamChars.get(raidenId)?.buildIds.length).toEqual(1)
expect(database.teamChars.get(raidenId)?.buildTcIds.length).toEqual(1)
const bennettId = database.teamChars.new('Bennett', {
buildIds: [database.builds.new()],
buildTcIds: [database.buildTcs.new(initCharTC('SapwoodBlade'))],
})
const teamId = database.teams.new({
loadoutData: [
{ teamCharId: raidenId } as LoadoutDatum,
undefined,
{ teamCharId: bennettId } as LoadoutDatum,
],
})

const dbTeam = database.teams.get(teamId)!
expect(dbTeam).toBeTruthy()
expect(dbTeam.loadoutData[0]?.teamCharId).toEqual(raidenId)
expect(dbTeam.loadoutData[2]?.teamCharId).toEqual(bennettId)

const exp = database.teams.export(teamId)
expect(exp).toBeTruthy()
expect((exp as any).loadoutData[0].key).toEqual('RaidenShogun')
expect((exp as any).loadoutData[0].optConfig.optimizationTarget).toEqual([
'test',
])

let res: object | undefined = undefined
expect(() => {
const json = JSON.stringify(exp)
res = JSON.parse(json)
}).not.toThrow()
expect(res).toBeTruthy()
const importTeamId = database.teams.import(exp)
const importTeam = database.teams.get(importTeamId)!
expect(importTeam).toBeTruthy()

const raidenTeamChar = database.teamChars.get(
importTeam.loadoutData[0]?.teamCharId
)
expect(raidenTeamChar?.key).toEqual('RaidenShogun')
expect(raidenTeamChar?.buildIds.length).toEqual(0)
expect(raidenTeamChar?.buildTcIds.length).toEqual(1)
expect(
database.optConfigs.get(raidenTeamChar?.optConfigId)?.optimizationTarget
).toEqual(['test'])
const bennettTeamChar = database.teamChars.get(
importTeam.loadoutData[2]?.teamCharId
)
expect(bennettTeamChar?.key).toEqual('Bennett')
expect(bennettTeamChar?.buildIds.length).toEqual(0)
expect(bennettTeamChar?.buildTcIds.length).toEqual(1)
})
})
11 changes: 7 additions & 4 deletions libs/gi/db/src/Database/DataManagers/TeamDataManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,18 @@ export class TeamDataManager extends DataManager<
}
}
import(data: object): string {
const { teamChars, ...rest } = data as Team & { teamChars: object[] }
const { loadoutData, ...rest } = data as Team & { loadoutData: object[] }
const id = this.generateKey()
if (
!this.set(id, {
...rest,
name: `${rest.name ?? ''} (Imported)`,
loadoutData: teamChars.map((obj) => ({
teamCharId: this.database.teamChars.import(obj),
})),
loadoutData: loadoutData.map(
(obj) =>
obj && {
teamCharId: this.database.teamChars.import(obj),
}
),
} as Team)
)
return ''
Expand Down

0 comments on commit 1a0bdfd

Please sign in to comment.