-
Notifications
You must be signed in to change notification settings - Fork 1
/
get.ts
53 lines (51 loc) · 1.51 KB
/
get.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
import axios from 'axios'
import type { NextApiRequest, NextApiResponse } from 'next'
type Problem = {
id: string
size: number
sums: {
cols: number[]
rows: number[]
}
matrix: number[][]
}
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const id = (req.query.id as string) || 'MDo4LDM0MCw5OTA='
return axios
.get(`https://www.puzzle-aquarium.com/?e=${id}`)
.then((r) => {
const lines = (r.data as string).split('\n')
const line = lines.find((v) => v.includes('var task'))
if (!line) return ''
const x = line.split('var task')[1]
const a = x.indexOf("'")
if (a === -1) return ''
const b = x.indexOf("'", a + 1)
if (b === -1) return ''
return x.slice(a + 1, b)
})
.then((raw) => {
if (!raw) return res.json({ id: 'INVALID', sums: [], frame: [], size: 0 })
const [rawSums, rawGroups] = raw.split(';')
const sums = rawSums.split('_').map((v) => parseInt(v))
const size = sums.length / 2
const [colSums, rowSums] = [
sums.slice(0, size),
sums.slice(size, size * 2),
]
const groups = rawGroups.split(',').map((v) => parseInt(v))
const matrix = [] as number[][]
for (let i = 0; i < size; i++)
matrix.push(groups.slice(i * size, (i + 1) * size))
return res.json({
id,
size,
sums: { cols: colSums, rows: rowSums },
matrix,
play: `https://www.puzzle-aquarium.com/?e=${id}`,
} as Problem)
})
}