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

Separate generated builds into a new data manager #2552

Merged
merged 14 commits into from
Nov 27, 2024
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
1 change: 1 addition & 0 deletions libs/gi/db-ui/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export * from './useDatabase'
export * from './useDBMeta'
export * from './useDisplayArtifact'
export * from './useEquippedInTeam'
export * from './useGeneratedBuildList'
export * from './useOptConfig'
export * from './useTeam'
export * from './useTeamChar'
Expand Down
8 changes: 8 additions & 0 deletions libs/gi/db-ui/src/hooks/useGeneratedBuildList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { useDataManagerBase } from '@genshin-optimizer/common/database-ui'
import { useDatabase } from './useDatabase'

export function useGeneratedBuildList(listId: string) {
const database = useDatabase()

return useDataManagerBase(database.generatedBuildList, listId)
}
5 changes: 5 additions & 0 deletions libs/gi/db/src/Database/ArtCharDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { BuildDataManager } from './DataManagers/BuildDataManager'
import { BuildTcDataManager } from './DataManagers/BuildTcDataManager'
import { CharMetaDataManager } from './DataManagers/CharMetaDataManager'
import { CharacterDataManager } from './DataManagers/CharacterDataManager'
import { GeneratedBuildListDataManager } from './DataManagers/GeneratedBuildListDataManager'
import { OptConfigDataManager } from './DataManagers/OptConfigDataManager'
import { TeamCharacterDataManager } from './DataManagers/TeamCharacterDataManager'
import { TeamDataManager } from './DataManagers/TeamDataManager'
Expand All @@ -27,6 +28,7 @@ export class ArtCharDatabase extends Database {
buildTcs: BuildTcDataManager
weapons: WeaponDataManager
optConfigs: OptConfigDataManager
generatedBuildList: GeneratedBuildListDataManager
charMeta: CharMetaDataManager
builds: BuildDataManager
teamChars: TeamCharacterDataManager
Expand Down Expand Up @@ -63,6 +65,8 @@ export class ArtCharDatabase extends Database {
this.weapons.ensureEquipments()

// Depends on arts
this.generatedBuildList = new GeneratedBuildListDataManager(this)
frzyc marked this conversation as resolved.
Show resolved Hide resolved
CordonZeus22 marked this conversation as resolved.
Show resolved Hide resolved
// Depends on arts and generatedBuildList
this.optConfigs = new OptConfigDataManager(this)

this.buildTcs = new BuildTcDataManager(this)
Expand Down Expand Up @@ -112,6 +116,7 @@ export class ArtCharDatabase extends Database {
this.chars,
this.weapons,
this.arts,
this.generatedBuildList,
CordonZeus22 marked this conversation as resolved.
Show resolved Hide resolved
this.optConfigs,
this.buildTcs,
this.charMeta,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { objKeyMap } from '@genshin-optimizer/common/util'
import {
allArtifactSlotKeys,
type ArtifactSlotKey,
} from '@genshin-optimizer/gi/consts'
import type { ArtCharDatabase } from '../ArtCharDatabase'
import { DataManager } from '../DataManager'

export interface GeneratedBuild {
weaponId?: string
artifactIds: Record<ArtifactSlotKey, string | undefined>
}

export interface GeneratedBuildList {
builds: GeneratedBuild[]
buildDate: number
}

export class GeneratedBuildListDataManager extends DataManager<
string,
'generatedBuildList',
GeneratedBuildList,
GeneratedBuildList,
ArtCharDatabase
> {
constructor(database: ArtCharDatabase) {
super(database, 'generatedBuildList')
}

override validate(obj: unknown): GeneratedBuildList | undefined {
if (typeof obj !== 'object' || obj === null) return undefined
let { builds, buildDate } = obj as GeneratedBuildList

if (!Array.isArray(builds)) {
builds = []
buildDate = 0
} else {
builds = builds
.map((build) => {
if (typeof build !== 'object' || build === null) return undefined
const { artifactIds: artifactIdsRaw } = build as GeneratedBuild
if (typeof artifactIdsRaw !== 'object' || artifactIdsRaw === null)
return undefined
let { weaponId } = build as GeneratedBuild
if (weaponId && !this.database.weapons.get(weaponId))
weaponId = undefined

const artifactIds = objKeyMap(allArtifactSlotKeys, (slotKey) =>
this.database.arts.get(artifactIdsRaw[slotKey])?.slotKey === slotKey
? artifactIdsRaw[slotKey]
: undefined
)

return { artifactIds, weaponId }
})
.filter((build) => build !== undefined)

if (!Number.isInteger(buildDate)) buildDate = 0
}

CordonZeus22 marked this conversation as resolved.
Show resolved Hide resolved
return {
buildDate,
builds,
}
}

new(data: GeneratedBuildList) {
frzyc marked this conversation as resolved.
Show resolved Hide resolved
const id = this.generateKey()
this.set(id, { ...data })
return id
}
}
70 changes: 31 additions & 39 deletions libs/gi/db/src/Database/DataManagers/OptConfigDataManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,21 @@ import {
clamp,
deepClone,
deepFreeze,
objKeyMap,
validateArr,
} from '@genshin-optimizer/common/util'
import type {
ArtifactSetKey,
ArtifactSlotKey,
LocationCharacterKey,
MainStatKey,
} from '@genshin-optimizer/gi/consts'
import {
allArtifactSetKeys,
allArtifactSlotKeys,
allLocationCharacterKeys,
artSlotMainKeys,
} from '@genshin-optimizer/gi/consts'
import type { ArtCharDatabase } from '../ArtCharDatabase'
import { DataManager } from '../DataManager'
import type { GeneratedBuildList } from './GeneratedBuildListDataManager'

export const maxBuildsToShowList = [1, 2, 3, 4, 5, 8, 10] as const
export const maxBuildsToShowDefault = 5
Expand Down Expand Up @@ -53,10 +51,6 @@ export interface StatFilterSetting {
}
export type StatFilters = Record<string, StatFilterSetting[]>

export type GeneratedBuild = {
weaponId?: string
artifactIds: Record<ArtifactSlotKey, string | undefined>
}
export interface OptConfig {
artSetExclusion: ArtSetExclusion
statFilters: StatFilters
Expand All @@ -81,8 +75,7 @@ export interface OptConfig {
useTeammateBuild: boolean // teammates loadout exclusion flag

//generated opt builds
builds: Array<GeneratedBuild>
buildDate: number
generatedBuildListId?: string
CordonZeus22 marked this conversation as resolved.
Show resolved Hide resolved

// upOpt
upOptLevelLow: number
Expand Down Expand Up @@ -119,8 +112,7 @@ export class OptConfigDataManager extends DataManager<
compareBuild,
levelLow,
levelHigh,
builds,
buildDate,
generatedBuildListId,
useTeammateBuild,
upOptLevelLow,
upOptLevelHigh,
Expand Down Expand Up @@ -200,27 +192,11 @@ export class OptConfigDataManager extends DataManager<
.filter(([_, a]) => a.length)
)

if (!Array.isArray(builds)) {
builds = []
buildDate = 0
} else {
builds = builds
.map((build) => {
if (typeof build !== 'object') return undefined
const { weaponId, artifactIds: artifactIdsRaw } =
build as GeneratedBuild
if (!this.database.weapons.get(weaponId)) return undefined
const artifactIds = objKeyMap(allArtifactSlotKeys, (slotKey) =>
this.database.arts.get(artifactIdsRaw[slotKey])?.slotKey === slotKey
? artifactIdsRaw[slotKey]
: undefined
)

return { artifactIds, weaponId }
})
.filter((b) => b) as GeneratedBuild[]
if (!Number.isInteger(buildDate)) buildDate = 0
}
if (
generatedBuildListId &&
!this.database.generatedBuildList.get(generatedBuildListId)
)
generatedBuildListId = undefined
CordonZeus22 marked this conversation as resolved.
Show resolved Hide resolved
if (typeof useTeammateBuild !== 'boolean') useTeammateBuild = false

return {
Expand All @@ -238,8 +214,7 @@ export class OptConfigDataManager extends DataManager<
compareBuild,
levelLow,
levelHigh,
builds,
buildDate,
generatedBuildListId,
useTeammateBuild,
upOptLevelLow,
upOptLevelHigh,
Expand All @@ -263,8 +238,7 @@ export class OptConfigDataManager extends DataManager<
useExcludedArts,
excludedLocations,
artExclusion,
buildDate,
builds,
generatedBuildListId,
...rest
} = optConfig
if (!overrideOptTarget) return rest
Expand All @@ -278,6 +252,26 @@ export class OptConfigDataManager extends DataManager<
if (!this.set(id, data)) return ''
return id
}
newOrSetGeneratedBuildList(optConfigId: string, list: GeneratedBuildList) {
const optConfig = this.get(optConfigId)

if (!optConfig) {
console.warn(`OptConfig not found for ID: ${optConfigId}`)
return false
}

const listId = optConfig.generatedBuildListId
const generatedBuildList =
listId && this.database.generatedBuildList.get(listId)

if (listId && generatedBuildList) {
return this.database.generatedBuildList.set(listId, list)
} else {
return this.database.optConfigs.set(optConfigId, {
generatedBuildListId: this.database.generatedBuildList.new(list),
})
}
}
}

const initialBuildSettings: OptConfig = deepFreeze({
Expand All @@ -302,9 +296,7 @@ const initialBuildSettings: OptConfig = deepFreeze({
useTeammateBuild: false,
upOptLevelLow: 0,
upOptLevelHigh: 19,

builds: [],
buildDate: 0,
generatedBuildListId: undefined,
})

export function handleArtSetExclusion(
Expand Down
2 changes: 1 addition & 1 deletion libs/gi/db/src/Database/DataManagers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import {
initCustomTarget,
validateCustomMultiTarget,
} from './CustomMultiTarget'
import type { GeneratedBuild } from './GeneratedBuildListDataManager'
import type {
ArtSetExclusion,
ArtSetExclusionKey,
GeneratedBuild,
OptConfig,
StatFilterSetting,
StatFilters,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import {
} from '@genshin-optimizer/common/util'
import { allArtifactSlotKeys } from '@genshin-optimizer/gi/consts'
import type { GeneratedBuild } from '@genshin-optimizer/gi/db'
import { TeamCharacterContext, useOptConfig } from '@genshin-optimizer/gi/db-ui'
import {
TeamCharacterContext,
useGeneratedBuildList,
useOptConfig,
} from '@genshin-optimizer/gi/db-ui'
import {
DataContext,
GraphContext,
Expand Down Expand Up @@ -83,9 +87,12 @@ export default function ChartCard({
const {
teamChar: { optConfigId },
} = useContext(TeamCharacterContext)
const { builds: generatedBuilds } = useOptConfig(optConfigId) ?? {
builds: [] as GeneratedBuild[],
const { generatedBuildListId } = useOptConfig(optConfigId) ?? {
generatedBuildListId: undefined,
}
const { builds: generatedBuilds } = useGeneratedBuildList(
generatedBuildListId ?? ''
) ?? { builds: [] as GeneratedBuild[] }

const [sliderLow, setSliderLow] = useState(-Infinity)
const [sliderHigh, setSliderHigh] = useState(Infinity)
Expand Down
Loading