-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·174 lines (145 loc) · 4.57 KB
/
index.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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
'use strict'
//////////////////////////////////////////////////////////
const TOKEN = '328061181:AAGql7NWJcrQ0Y81NaYLBuLqteVffD_RBVc'
const SCORE_STEP = 2
//////////////////////////////////////////////////////////
const Telegram = require('telegram-node-bot')
const TelegramBaseController = Telegram.TelegramBaseController
const TextCommand = Telegram.TextCommand
const tg = new Telegram.Telegram(TOKEN, {
webAdmin: {
port: 1234,
host: 'localhost'
}
})
//////////////////////////////////////////////////////////
require('js-standard-library-extensions')
const fs = require('fs')
const utils = require('./utils')
const store = require('./store')
utils.initCategories()
utils.initConfigs()
//////////////////////////////////////////////////////////
class OtherwiseController extends TelegramBaseController {
handle($) {
store.validData($, (result) => {
if (result)
quiz($)
else
chooseCategory($)
})
}
}
class CategoryController extends TelegramBaseController {
categoryHandler($) {
store.validData($, (result) => {
chooseCategory($)
})
}
get routes() {
return {'categoryCommand': 'categoryHandler'}
}
}
//////////////////////////////////////////////////////////
tg.router
.when(new TextCommand('/category', 'categoryCommand'), new CategoryController())
.otherwise(new OtherwiseController())
//////////////////////////////////////////////////////////
function quiz($) {
store.getData($, (data) => {
let category = Object.copy(categories[data.category])
utils.excludeShownImages(category, data.shown_images)
if (utils.isEmptyCategory(category)) {
store.setData($, {shown_images: []}, (data) => {
completeCategory($, data)
})
} else {
let collection = utils.getRandomCollection(category, data.category)
data.shown_images.push(collection[0])
store.setData($, {shown_images: data.shown_images}, (data) => {
let check_answer = (text, messageId) => {
let answer = text == collection[2]
let score = data.score
score += answer ? SCORE_STEP : -SCORE_STEP
store.setData($, {score: score}, (data) => {
let msg = ''
if (answer)
msg = 'Ты угадал \uD83D\uDC4D Твой текущий рейтинг: <b>%d (%s)</b>.'
else
msg = 'К сожалению, ты не угадал \uD83D\uDE21 Твой текущий рейтинг: <b>%d (%s)</b>.'
tg.api.editMessageText(msg.format(score, utils.getRating(score)), {chat_id: $.chatId, message_id: messageId, parse_mode: 'HTML'}).then(() => {
quiz($)
})
})
}
$.sendPhoto({path: collection[1]}).then(() => {
chooseAnswer($, data, collection, check_answer)
})
})
}
})
}
function chooseCategory($) {
let menu = []
let categories = fs.readdirSync('categories')
for (let category of categories) {
menu.push({
text: configs[category].name,
callback: (callbackQuery, message) => {
store.setData($, {score: 0, category: category, question: configs[category].question, shown_images: []}, (data) => {
let msg = 'Я установил категорию <b>%s</b>. Удачи!'
tg.api.editMessageText(msg.format(configs[category].name), {chat_id: message.chat.id, message_id: message.messageId, parse_mode: 'HTML'}).then(() => {
quiz($)
})
})
}
})
}
$.runInlineMenu({
layout: 2,
method: 'sendMessage',
params: ['Выбери категорию'],
menu: menu
})
}
function chooseAnswer($, data, collection, check_func) {
let menu = []
for (let i = 2; i <= 5; i++) {
menu.push({
text: collection[i],
callback: (callbackQuery, message) => {
check_func(collection[i], message.messageId)
}
})
}
menu.shuffle()
$.runInlineMenu({
layout: 2,
method: 'sendMessage',
params: [data.question],
menu: menu
})
}
function completeCategory($, data) {
let msg = '<b>%s</b>, ты прошел всю категорию <b>%s</b>. Твой итоговый рейтинг: <b>%d (%s)</b>.'
$.runInlineMenu({
method: 'sendMessage',
params: [msg.format($.message.from.firstName, configs[data.category].name, data.score, utils.getRating(data.score)), {parse_mode: 'HTML'}],
menu: [
{
text: 'Начать заново',
callback: (callbackQuery, message) => {
store.setData($, {score: 0}, (data) => {
quiz($)
})
}
},
{
text: 'Выбрать другую категорию',
callback: (callbackQuery, message) => {
chooseCategory($)
}
}
]
})
}