-
Notifications
You must be signed in to change notification settings - Fork 14
/
abclimadasar_answer.js
44 lines (44 loc) · 1.99 KB
/
abclimadasar_answer.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
const similarity = require('similarity')
const threshold = 0.72 // semakin tinggi nilai, semakin mirip
module.exports = {
async before(m) {
this.game = this.game ? this.game : {}
let id = 'family100_' + m.chat
if (!(id in this.game)) return !0
let room = this.game[id]
let text = m.text.toLowerCase().replace(/[^\w\s\-]+/, '')
let isSurrender = /^((me)?nyerah|surr?ender)$/i.test(m.text)
if (!isSurrender) {
let index = room.huruf.findIndex(v => v.toLowerCase().replace(/[^\w\s\-]+/, '') === text)
if (index < 0) {
if (Math.max(...room.huruf.filter((_, index) => !room.terjawab[index]).map(huruf => similarity(huruf, text))) >= threshold) m.reply('Dikit lagi!')
return !0
}
if (room.terjawab[index]) return !0
let users = global.db.data.users[m.sender]
room.terjawab[index] = m.sender
users.exp += room.winScore
}
let isWin = room.terjawab.length === room.terjawab.filter(v => v).length
let caption = `
*Soal inisial :* ${room.soal}
Terdapat *${room.huruf.length}* jawaban${room.jawaban.find(v => v.includes(' ')) ? `
(beberapa jawaban terdapat spasi)
`: ''}
${isWin ? `*SEMUA JAWABAN TERJAWAB*\n*${room.jawaban}*` : isSurrender ? '*MENYERAH!*' : ''}
${Array.from(room.huruf, (huruf, index) => {
return isSurrender || room.terjawab[index] ? `(${index + 1}) ${huruf} ${room.terjawab[index] ? '@' + room.terjawab[index].split('@')[0] : ''} ✅`.trim() : false
}).filter(v => v).join('\n')}
${isSurrender ? '' : `+${room.winScore} XP tiap jawaban benar`}
`.trim()
m.reply(caption, null, {
contextInfo: {
mentionedJid: this.parseMention(caption)
}
}).then(msg => {
return this.game[id].msg = msg
}).catch(_ => _)
if (isWin || isSurrender) delete this.game[id]
return !0
}
}