forked from FantoX001/Miku-MD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath.js
48 lines (43 loc) · 1.48 KB
/
math.js
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
let modes = {
noob: [-3, 3,-3, 3, '+-', 15000, 10],
easy: [-10, 10, -10, 10, '*/+-', 20000, 40],
medium: [-40, 40, -20, 20, '*/+-', 40000, 150],
hard: [-100, 100, -70, 70, '*/+-', 60000, 350],
extreme: [-999999, 999999, -999999, 999999, '*/', 99999, 9999],
impossible: [-99999999999, 99999999999, -99999999999, 999999999999, '*/', 30000, 35000],
impossible2: [-999999999999999, 999999999999999, -999, 999, '/', 30000, 50000]
}
let operators = {
'+': '+',
'-': '-',
'*': '×',
'/': '÷'
}
function randomInt(from, to) {
if (from > to) [from, to] = [to, from]
from = Math.floor(from)
to = Math.floor(to)
return Math.floor((to - from) * Math.random() + from)
}
function pickRandom(list) {
return list[Math.floor(Math.random() * list.length)]
}
function genMath(mode) {
return new Promise((resolve, reject) => {
let [a1, a2, b1, b2, ops, time, bonus] = modes[mode]
let a = randomInt(a1, a2)
let b = randomInt(b1, b2)
let op = pickRandom([...ops])
let result = (new Function(`return ${a} ${op.replace('/', '*')} ${b < 0 ? `(${b})` : b}`))()
if (op == '/') [a, result] = [result, a]
hasil = {
soal: `${a} ${operators[op]} ${b}`,
mode: mode,
waktu: time,
hadiah: bonus,
jawaban: result
}
resolve(hasil)
})
}
module.exports = { modes, operators, randomInt, pickRandom, genMath }