Skip to content

Commit

Permalink
SRO: Implement floor (#2829)
Browse files Browse the repository at this point in the history
  • Loading branch information
priolette authored Mar 10, 2025
1 parent 92c66b9 commit a83ff24
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 6 deletions.
14 changes: 14 additions & 0 deletions libs/game-opt/engine/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
import { addCustomOperation } from '@genshin-optimizer/pando/engine'

export * from './calculator'
export * from './IConditionalData'
export * from './listing'
export * from './read'
export * from './tag'
export * from './util'

{
const floorCalc = (args: (number | string)[]): number => {
const x = args[0] as number
return Math.floor(x)
}
addCustomOperation('floor', {
range: ([r]) => ({ min: floorCalc([r.max]), max: floorCalc([r.min]) }),
monotonicity: () => [{ inc: true, dec: false }],
calc: floorCalc,
})
}
2 changes: 1 addition & 1 deletion libs/game-opt/formula-ui/src/context/CalcContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ import type { Calculator, Tag } from '@genshin-optimizer/game-opt/engine'
import { createContext } from 'react'
// Use the game-opt generic Calculator.
// In game-specific UI, cast this calc to the game's Calculator
export const CalcContext = createContext<Calculator<Tag> | null>(null)
export const CalcContext = createContext<Calculator<Tag, 'floor'> | null>(null)
9 changes: 7 additions & 2 deletions libs/game-opt/sheet-ui/src/types/conditional.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import type {
Calculator,
IConditionalData,
Tag,
} from '@genshin-optimizer/game-opt/engine'
import type { ReactNode } from 'react'
import type { Field } from './field'
import type { Header } from './header'

export type Conditional = {
metadata: IConditionalData
label: ReactNode | ((calc: Calculator, value: number) => ReactNode)
badge?: ReactNode | ((calc: Calculator, value: number) => ReactNode)
label:
| ReactNode
| ((calc: Calculator<Tag, 'floor'>, value: number) => ReactNode)
badge?:
| ReactNode
| ((calc: Calculator<Tag, 'floor'>, value: number) => ReactNode)
header?: Header
fields?: Field[]
targeted?: boolean
Expand Down
4 changes: 2 additions & 2 deletions libs/sr/formula/src/calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { allLightConeKeys, allRelicSetKeys } from '@genshin-optimizer/sr/consts'
import type { Tag } from './data/util'
import { tagStr } from './data/util'

export type PartialMeta = PartialMetaBase<Tag>
export type PartialMeta = PartialMetaBase<Tag, 'floor'>

export class Calculator extends Base<Tag> {
export class Calculator extends Base<Tag, 'floor'> {
override toDebug(): DebugCalculator {
return new DebugCalculator(
this,
Expand Down
14 changes: 14 additions & 0 deletions libs/sr/formula/src/formulaText.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { assertUnreachable } from '@genshin-optimizer/common/util'
import type { CalcResult } from '@genshin-optimizer/pando/engine'
import type { PartialMeta } from './calculator'

Expand Down Expand Up @@ -37,6 +38,9 @@ export function translate(

let formula: string, prec: number
switch (op) {
// TODO: handle `subscript` and `vtag`
case 'subscript':
case 'vtag':
case 'const':
formula = `${val}` // TODO: Add % here if `tag` indicates percent constant
prec = Infinity
Expand All @@ -57,7 +61,17 @@ export function translate(

formula = `${dem} / (${num1} + ${num2})`
prec = details.prod.prec
break
}
case 'floor': {
const [x] = ops

formula = `\u230A${x}\u230B`
prec = Infinity
break
}
default:
assertUnreachable(op)
}
let name: string | undefined, sheet: string | undefined
if (tag) {
Expand Down
6 changes: 5 additions & 1 deletion libs/sr/formula/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Preset } from '@genshin-optimizer/game-opt/engine'
import { cmpEq, cmpNE } from '@genshin-optimizer/pando/engine'
import type { NumNode, OP } from '@genshin-optimizer/pando/engine'
import { cmpEq, cmpNE, custom } from '@genshin-optimizer/pando/engine'
import type {
AscensionKey,
LightConeKey,
Expand All @@ -23,6 +24,9 @@ import {
reader,
} from './data/util'

export const floor = <P extends OP>(x: NumNode<P> | number) =>
custom('floor', x)

export function withPreset(
preset: Preset,
...data: TagMapNodeEntries
Expand Down

0 comments on commit a83ff24

Please sign in to comment.