Initial I Want to Heal app
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
export type PvpContentType = 'dungeon' | 'raid'
|
||||
export type CpuDifficulty = 1 | 2 | 3 | 4 | 5
|
||||
|
||||
export type CpuPvpLeaderboardEntry = {
|
||||
characterName: string
|
||||
className: string
|
||||
contentType: PvpContentType
|
||||
encountersCleared: number
|
||||
cpuDifficulty: CpuDifficulty
|
||||
result: 'victory' | 'defeat'
|
||||
completedAt: string
|
||||
}
|
||||
|
||||
const cpuLeaderboardKey = 'chronicle.pvpCpuLeaderboard.v1'
|
||||
|
||||
export function randomCpuDifficulty(): CpuDifficulty {
|
||||
return (Math.floor(Math.random() * 5) + 1) as CpuDifficulty
|
||||
}
|
||||
|
||||
export function loadCpuPvpLeaderboard(contentType?: PvpContentType): CpuPvpLeaderboardEntry[] {
|
||||
try {
|
||||
const raw = JSON.parse(localStorage.getItem(cpuLeaderboardKey) ?? '[]') as CpuPvpLeaderboardEntry[]
|
||||
return raw
|
||||
.filter((entry) => !contentType || entry.contentType === contentType)
|
||||
.sort((left, right) =>
|
||||
right.encountersCleared - left.encountersCleared
|
||||
|| right.cpuDifficulty - left.cpuDifficulty
|
||||
|| right.completedAt.localeCompare(left.completedAt),
|
||||
)
|
||||
.slice(0, 12)
|
||||
} catch {
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
export function recordCpuPvpLeaderboard(entry: CpuPvpLeaderboardEntry) {
|
||||
const current = loadCpuPvpLeaderboard()
|
||||
const next = [...current, entry]
|
||||
.sort((left, right) =>
|
||||
right.encountersCleared - left.encountersCleared
|
||||
|| right.cpuDifficulty - left.cpuDifficulty
|
||||
|| right.completedAt.localeCompare(left.completedAt),
|
||||
)
|
||||
.slice(0, 30)
|
||||
localStorage.setItem(cpuLeaderboardKey, JSON.stringify(next))
|
||||
}
|
||||
Reference in New Issue
Block a user