-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.ts
100 lines (95 loc) · 2.85 KB
/
utils.ts
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { UICONS, UiconsIndex } from '../src'
import { MASTERFILE_URL } from './styles'
import type { Asset, Pokemon, Props, RawMon } from './types'
export async function getMasterfile() {
const masterfile = await fetch(MASTERFILE_URL)
const json: { pokemon: Record<string, Pokemon> } = await masterfile.json()
const rawMons: RawMon[] = []
Object.entries(json.pokemon).forEach(([id, pokemon]) => {
const forms = Object.entries(pokemon.forms)
if (forms.length) {
forms.forEach(([form, { tempEvolutions }]) => {
rawMons.push({ id: +id, form: +form })
if (tempEvolutions) {
Object.keys(tempEvolutions).forEach((evo) => {
rawMons.push({ id: +id, form: +form, evo: +evo })
})
}
})
} else {
rawMons.push({ id: +id })
}
Object.keys(pokemon.tempEvolutions || {}).forEach((evo) => {
rawMons.push({ id: +id, evo: +evo })
})
})
return rawMons
}
export async function getMonsFromMf(
icon: Asset,
audio: Asset,
rawMons: RawMon[]
): Promise<Props[]> {
const newUicons = new UICONS(icon.path)
const newUaudio = new UICONS(audio.path)
await Promise.all([newUicons.remoteInit(), newUaudio.remoteInit()])
return rawMons.map(({ id, form, evo }) => {
let title = id.toString()
if (form) title += `_${form}`
if (evo) title += `_${evo}`
return {
title,
src: newUicons.pokemon(id, evo, form),
cry: newUaudio.pokemon(id, evo, form),
}
})
}
function parseArgs(args: string[], key: string): string | null {
const found = args.find((arg) => arg.startsWith(key))
return found ? found.split(key)[1] : null
}
export async function getMonsFromIndex(
icon: Asset,
audio: Asset
): Promise<Props[]> {
const iconIndex = await fetch(icon.path + '/index.json')
const iconJson: UiconsIndex = await iconIndex.json()
const newUaudio = new UICONS(audio.path)
await newUaudio.remoteInit()
return (
iconJson.pokemon
?.sort((a, b) => {
try {
const aId = a.split('_')[0].split('.')[0]
const bId = b.split('_')[0].split('.')[0]
return +aId - +bId
} catch {
return 0
}
})
.map((file) => {
const title = file.split('.')[0]
const [id, ...rest] = title.split('_')
const form = parseArgs(rest, '_f') ?? 0
const evo = parseArgs(rest, '_e') ?? 0
const gender = parseArgs(rest, '_g') ?? 0
const alignment = parseArgs(rest, '_a') ?? 0
const bread = parseArgs(rest, '_b') ?? 0
const shiny = !!parseArgs(rest, '_s') ?? false
return {
title,
src: `${icon.path}/pokemon/${file}`,
cry: newUaudio.pokemon(
id,
evo,
form,
0,
gender,
alignment,
bread,
shiny
),
}
}) || []
)
}