-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtreeLevel.ts
More file actions
58 lines (49 loc) · 1.67 KB
/
treeLevel.ts
File metadata and controls
58 lines (49 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
export type TreeLevel = 1 | 2 | 3 | 4 | 5;
export interface TreeLevelRow {
level: TreeLevel;
name: string;
min: number;
max?: number;
rangeLabel: string;
}
export const TREE_LEVEL_TABLE: readonly TreeLevelRow[] = [
{ level: 1, name: '잊힌 기록의 숲', min: 0, max: 0, rangeLabel: '0개' },
{ level: 2, name: '햇살의 터전', min: 1, max: 2, rangeLabel: '1–2개' },
{ level: 3, name: '기록의 오솔길', min: 3, max: 4, rangeLabel: '3–4개' },
{ level: 4, name: '지식 나무 언덕', min: 5, max: 6, rangeLabel: '5–6개' },
{ level: 5, name: '도토리 만개 숲', min: 7, rangeLabel: '7개 이상' },
] as const;
export interface TreeLevelResult extends TreeLevelRow {
progressToNext: number;
nextMin?: number;
remainingToNext?: number;
}
export function getTreeLevel(acorns: number): TreeLevelResult {
const count = Math.max(0, Math.floor(acorns ?? 0));
const row =
TREE_LEVEL_TABLE.find(
(r) => count >= r.min && (r.max === undefined || count <= r.max)
) ?? TREE_LEVEL_TABLE[0];
const idx = TREE_LEVEL_TABLE.findIndex((r) => r.level === row.level);
const next = TREE_LEVEL_TABLE[idx + 1];
if (!next) {
return { ...row, progressToNext: 1 };
}
const span = Math.max(1, next.min - row.min);
const progressToNext = Math.min(1, (count - row.min) / span);
const remainingToNext = Math.max(0, next.min - count);
return {
...row,
progressToNext,
nextMin: next.min,
remainingToNext,
};
}
export function computeTreeLevel(acorns: number): TreeLevel {
const c = Math.max(0, Math.floor(acorns ?? 0));
if (c >= 7) return 5;
if (c >= 5) return 4;
if (c >= 3) return 3;
if (c >= 1) return 2;
return 1;
}