-
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix distribution of attribute ranges.
GitOrigin-RevId: 8320e7255bd32f52cee372a924b1628385f8a276
- Loading branch information
Showing
10 changed files
with
158 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,61 @@ | ||
import maxBy from '@deities/hephaestus/maxBy.tsx'; | ||
|
||
export type AttributeRangeValue = 1 | 2 | 3 | 4 | 5; | ||
export type AttributeRange = 1 | 2 | 3 | 4 | 5; | ||
export type AttributeRangeWithZero = 0 | 1 | 2 | 3 | 4 | 5; | ||
export type LargeAttributeRangeWithZero = | ||
| AttributeRangeWithZero | ||
| 6 | ||
| 7 | ||
| 8 | ||
| 9 | ||
| 10; | ||
|
||
export function validateAttributeRange( | ||
value?: number | null, | ||
): value is AttributeRangeValue { | ||
): value is AttributeRange { | ||
return !!value && value >= 1 && value <= 5; | ||
} | ||
|
||
export default function getAttributeRange<T>( | ||
list: ReadonlyArray<T>, | ||
extract: (entry: T) => number, | ||
min: number = 0, | ||
length: number = 5, | ||
) { | ||
const entry = maxBy(list, extract); | ||
if (!entry) { | ||
return []; | ||
} | ||
|
||
const step = (extract(entry) - min) / 4; | ||
const step = (extract(entry) - min) / (length - 1); | ||
return Array.from( | ||
{ length: 5 }, | ||
{ length }, | ||
(_, index) => min + index * step - (index > 0 ? step / 2 : 0), | ||
); | ||
} | ||
|
||
export function getAttributeRangeValue( | ||
range: ReadonlyArray<number>, | ||
value: number, | ||
): AttributeRangeValue { | ||
const index = range.findIndex((item) => item >= value); | ||
return (index === -1 ? range.length : index + 1) as AttributeRangeValue; | ||
) { | ||
if (value < range[0]) { | ||
return 0; | ||
} | ||
|
||
const index = range.findLastIndex((item) => item <= value); | ||
return (index === -1 ? range.length : index + 1) as AttributeRangeWithZero; | ||
} | ||
|
||
export function getLargeAttributeRangeValue( | ||
range: ReadonlyArray<number>, | ||
value: number, | ||
) { | ||
if (value < range[0]) { | ||
return 0; | ||
} | ||
|
||
const index = range.findLastIndex((item) => item <= value); | ||
return ( | ||
index === -1 ? range.length : index + 1 | ||
) as LargeAttributeRangeWithZero; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.