This repository has been archived by the owner on Oct 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpaimon_gui.py
426 lines (360 loc) · 14 KB
/
paimon_gui.py
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# SPDX-License-Identifier: MIT
# Copyright (c) 2021 scmanjarrez. All rights reserved.
# This work is licensed under the terms of the MIT license.
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from datetime import datetime
import database as db
import threads as th
import util as ut
STATE = {}
def _state(uid, state, value):
if uid not in STATE:
STATE[uid] = {}
if state not in STATE[uid]:
STATE[uid][state] = value
def del_state(uid):
if uid in STATE:
del STATE[uid]
def _del_substate(uid, state):
if uid in STATE and state in STATE[uid]:
del STATE[uid][state]
def button(buttons):
return [InlineKeyboardButton(bt[0], callback_data=bt[1]) for bt in buttons]
def menu(update, context):
uid = ut.uid(update)
if not db.banned(uid):
if not db.cached(uid):
ut.not_started(update)
else:
main_menu(update)
def main_menu(update):
kb = [button([("🌙 Resin 🌙", 'resin_menu')]),
button([("⚙ Settings ⚙", 'settings_menu')])]
resp = ut.send
if update.callback_query is not None:
resp = ut.edit
resp(update, "Main Menu", reply_markup=InlineKeyboardMarkup(kb))
def _tracking_status(uid):
if th.is_unsync(uid):
return '🟠'
if th.is_tracked(uid):
return '🟢'
return '🔴'
def resin_menu(update):
uid = ut.uid(update)
cur_resin = db.get_resin(uid)
tracking = _tracking_status(uid)
kb = [button([(f"🌙 {cur_resin} 🌙", 'resin_menu'),
(f"Tracking: {tracking}", 'tracking_menu')]),
button(ut.gui_cap_format(uid)),
button([("Spend", 'spend_menu'),
("Refill", 'refill_menu')]),
button([("« Back to Menu", 'main_menu')])]
ut.edit(update, "Resin Menu", InlineKeyboardMarkup(kb))
def tracking_menu(update):
uid = ut.uid(update)
_state(uid, ut.CMD.TRACK, list(ut.TRACK_MAX))
st_track = STATE[uid][ut.CMD.TRACK]
tracking = _tracking_status(uid)
kb = [button([(f"Tracking: {tracking}", 'tracking_menu')]),
button([("« Back to Resin", 'resin_menu'),
("« Back to Menu", 'main_menu')])]
track_txt = "Start Tracking"
if th.is_unsync(uid):
track_txt = "Synchronize"
if th.is_unsync(uid) or not th.is_tracked(uid):
kb.insert(1, button([("˄", 'tracking_up0'),
(" ", 'nop'),
("˄", 'tracking_up1'),
("˄", 'tracking_up2')]))
kb.insert(2, button([(st_track[0], 'nop'),
(":", 'nop'),
(st_track[1], 'nop'),
(st_track[2], 'nop')]))
kb.insert(3, button([("˅", 'tracking_down0'),
(" ", 'nop'),
("˅", 'tracking_down1'),
("˅", 'tracking_down2')]))
kb.insert(4, button([(track_txt, 'tracking_start')]))
if th.is_unsync(uid) or th.is_tracked(uid):
idx = 1
if th.is_unsync(uid):
idx = 5
kb.insert(idx, button([("Stop Tracking", 'tracking_stop')]))
ut.edit(update, "Tracking Menu", InlineKeyboardMarkup(kb))
def tracking_start(update, context):
uid = ut.uid(update)
mm = int(update.callback_query.message.reply_markup
.inline_keyboard[2][0]['text'])
s1 = int(update.callback_query.message.reply_markup
.inline_keyboard[2][2]['text'])
s2 = int(update.callback_query.message.reply_markup
.inline_keyboard[2][3]['text'])
th.new_thread(context.bot, uid, mm*60 + s1*10 + s2)
tracking_menu(update)
def tracking_stop(update):
uid = ut.uid(update)
th.del_thread(uid)
tracking_menu(update)
def tracking_updown(update, up=True):
uid = ut.uid(update)
_state(uid, ut.CMD.TRACK, list(ut.TRACK_MAX))
st_track = STATE[uid][ut.CMD.TRACK]
txt = 'tracking_down'
if up:
txt = 'tracking_up'
pos = int(update.callback_query.data.split(txt)[1])
if up:
if st_track[pos] < ut.TRACK_MAX[pos]:
st_track[pos] += 1
else:
if st_track[pos] > 0:
st_track[pos] -= 1
tracking_menu(update)
def spend_menu(update):
uid = ut.uid(update)
cur_resin = db.get_resin(uid)
kb = [button([(f"🌙 {cur_resin} 🌙", 'spend_menu')]),
[],
button([("« Back to Resin", 'resin_menu'),
("« Back to Menu", 'main_menu')])]
if cur_resin >= 10:
kb[1].append(button([("10", 'spend_r10')])[0])
else:
kb[1].append(button([("No Resin Left!", 'nop')])[0])
if cur_resin >= 20:
kb[1].append(button([("20", 'spend_r20')])[0])
if cur_resin >= 30:
kb[1].append(button([("30", 'spend_r30')])[0])
if cur_resin >= 40:
kb[1].append(button([("40", 'spend_r40')])[0])
if cur_resin >= 60:
kb[1].append(button([("60", 'spend_r60')])[0])
if cur_resin >= 80:
kb[1].append(button([("80", 'spend_r80')])[0])
if cur_resin >= 90:
kb[1].append(button([("90", 'spend_r90')])[0])
if cur_resin >= 120:
kb[1].append(button([("120", 'spend_r120')])[0])
ut.edit(update, "Spend Menu", InlineKeyboardMarkup(kb))
def spend_resin(update):
uid = ut.uid(update)
resin = int(update.callback_query.data.split('spend_r')[1])
db.dec_resin(uid, resin)
spend_menu(update)
def refill_menu(update):
uid = ut.uid(update)
cur_resin = db.get_resin(uid)
_state(uid, ut.CMD.REFILL, list(ut.REFILL_BASE))
st_refill = STATE[uid][ut.CMD.REFILL]
kb = [button([(f"🌙 {cur_resin} 🌙", 'refill_menu')]),
button([("˄", 'refill_up0'),
("˄", 'refill_up1'),
("˄", 'refill_up2')]),
button([(st_refill[0], 'nop'),
(st_refill[1], 'nop'),
(st_refill[2], 'nop')]),
button([("˅", 'refill_down0'),
("˅", 'refill_down1'),
("˅", 'refill_down2')]),
button([("Refill", 'refill_r')]),
button([("« Back to Resin", 'resin_menu'),
("« Back to Menu", 'main_menu')])]
ut.edit(update, "Refill Menu", InlineKeyboardMarkup(kb))
def refill_resin(update):
uid = ut.uid(update)
r0 = (update.callback_query.message.reply_markup
.inline_keyboard[2][0]['text'])
r1 = (update.callback_query.message.reply_markup
.inline_keyboard[2][1]['text'])
r2 = (update.callback_query.message.reply_markup
.inline_keyboard[2][2]['text'])
value = int("".join([r0, r1, r2]))
db.inc_resin(uid, value)
refill_menu(update)
def refill_updown(update, up=True):
uid = ut.uid(update)
_state(uid, ut.CMD.REFILL, list(ut.REFILL_BASE))
st_refill = STATE[uid][ut.CMD.REFILL]
cur_resin = db.get_resin(uid)
txt = 'refill_up'
if not up:
txt = 'refill_down'
pos = int(update.callback_query.data.split(txt)[1])
if up:
st_refill[pos] = (st_refill[pos] + 1) % 10
else:
st_refill[pos] = (st_refill[pos] - 1) % 10
if int("".join(str(c) for c in st_refill)) > ut.RESIN_MAX - cur_resin:
new_state = ut.RESIN_MAX - cur_resin
if new_state < 0:
new_state = 0
STATE[uid][ut.CMD.REFILL] = [int(el) for el in f"{new_state:03d}"]
refill_menu(update)
def settings_menu(update):
kb = [button([("⏰ Warning Settings ⏰", 'settings_warn_menu')]),
button([("🌎 Timezone Settings 🌎", 'settings_timezone_menu')]),
button([("« Back to Menu", 'main_menu')])]
ut.edit(update, "Settings Menu", InlineKeyboardMarkup(kb))
def settings_warn_menu(update):
uid = ut.uid(update)
cur_warn = db.get_warn(uid)
if cur_warn != -1:
_state(uid, ut.CMD.WARN, [int(cw) for cw in f"{cur_warn:03d}"])
warn_icon = '🔔'
else:
_state(uid, ut.CMD.WARN, [int(cw) for cw in f"{ut.RESIN_MAX-10:03d}"])
cur_warn = 'disabled'
warn_icon = '🔕'
st_warn = STATE[uid][ut.CMD.WARN]
kb = [button([(f"Threshold: {cur_warn}", 'settings_warn_menu'),
(f"Resin Warnings: {warn_icon}", 'warn_toggle')]),
button([("˄", 'warn_up0'),
("˄", 'warn_up1'),
("˄", 'warn_up2')]),
button([(st_warn[0], 'nop'),
(st_warn[1], 'nop'),
(st_warn[2], 'nop')]),
button([("˅", 'warn_down0'),
("˅", 'warn_down1'),
("˅", 'warn_down2')]),
button([("Set Warning Threshold", 'warn_threshold')]),
button([("« Back to Settings", 'settings_menu'),
("« Back to Menu", 'main_menu')])]
ut.edit(update, "Warnings Settings Menu", InlineKeyboardMarkup(kb))
def _warn_value(update):
r0 = (update.callback_query.message.reply_markup
.inline_keyboard[2][0]['text'])
r1 = (update.callback_query.message.reply_markup
.inline_keyboard[2][1]['text'])
r2 = (update.callback_query.message.reply_markup
.inline_keyboard[2][2]['text'])
value = int("".join([r0, r1, r2]))
return value
def warn_toggle(update):
uid = ut.uid(update)
cur_warn = db.get_warn(uid)
if cur_warn == -1:
db.set_warn(uid, _warn_value(update))
else:
db.unset_warn(uid)
settings_warn_menu(update)
def warn_threshold(update):
uid = ut.uid(update)
db.set_warn(uid, _warn_value(update))
settings_warn_menu(update)
def warn_updown(update, up=True):
uid = ut.uid(update)
cur_warn = db.get_warn(uid)
if cur_warn != -1:
_state(uid, ut.CMD.WARN, [int(cw) for cw in f"{cur_warn:03d}"])
else:
_state(uid, ut.CMD.WARN, [int(cw) for cw in f"{ut.RESIN_MAX-10:03d}"])
st_warn = STATE[uid][ut.CMD.WARN]
txt = 'warn_down'
if up:
txt = 'warn_up'
pos = int(update.callback_query.data.split(txt)[1])
if up:
st_warn[pos] = (st_warn[pos] + 1) % 10
else:
st_warn[pos] = (st_warn[pos] - 1) % 10
value = int(''.join(str(c) for c in st_warn))
if value > 159:
STATE[uid][ut.CMD.WARN] = [1, 5, 9]
settings_warn_menu(update)
def settings_timezone_menu(update):
uid = ut.uid(update)
bot_hour, bot_minutes = map(int, datetime.now()
.strftime("%H:%M").split(':'))
tz_hour, tz_minutes = db.get_timezone(uid).split(':')
if tz_hour == 'null':
tz = 'disabled'
else:
tz = ut.normalize_timezone(tz_hour, tz_minutes)
kb = [button([(f"Bot Hour: {bot_hour:02}:{bot_minutes:02}",
'settings_timezone_menu')]),
button([(f"Current timezone: {tz}", 'timezone_menu')]),
button([("« Back to Settings", 'settings_menu'),
("« Back to Menu", 'main_menu')])]
ut.edit(update, "Timezone Settings Menu", InlineKeyboardMarkup(kb))
def timezone_menu(update, modified=False):
uid = ut.uid(update)
tz_hour, tz_minutes = db.get_timezone(uid).split(':')
if tz_hour == 'null':
tz = 'disabled'
cur_hour, cur_minutes = ut.user_hour(0, 0, 0, 0)
else:
tz = ut.normalize_timezone(tz_hour, tz_minutes)
cur_hour, cur_minutes = ut.user_hour(0, 0,
int(tz_hour), int(tz_minutes))
if modified:
cur_hour, cur_minutes = STATE[uid][ut.CMD.TZ]
else:
_del_substate(uid, ut.CMD.TZ)
kb = [button([(f"Current timezone: {tz}", 'timezone_menu')]),
button([("˄", 'timezone_up0'),
("˄", 'timezone_up1'),
("˄", 'timezone_up2'),
("˄", 'timezone_up3')]),
button([(cur_hour // 10, 'nop'),
(cur_hour % 10, 'nop'),
(cur_minutes // 10, 'nop'),
(cur_minutes % 10, 'nop')]),
button([("˅", 'timezone_down0'),
("˅", 'timezone_down1'),
("˅", 'timezone_down2'),
("˅", 'timezone_down3')]),
button([("Set Hour", 'timezone_set')]),
button([("« Back to Timezone Settings", 'settings_timezone_menu'),
("« Back to Menu", 'main_menu')])]
if tz != 'disabled':
kb.insert(len(kb) - 1,
button([("Disable Timezone", 'timezone_disable')]))
ut.edit(update, "Timezone Menu", InlineKeyboardMarkup(kb))
def _timezone_value(update):
r0 = (update.callback_query.message.reply_markup
.inline_keyboard[2][0]['text'])
r1 = (update.callback_query.message.reply_markup
.inline_keyboard[2][1]['text'])
r2 = (update.callback_query.message.reply_markup
.inline_keyboard[2][2]['text'])
r3 = (update.callback_query.message.reply_markup
.inline_keyboard[2][3]['text'])
tz_hour = int("".join([r0, r1]))
tz_minutes = int("".join([r2, r3]))
return tz_hour, tz_minutes
def timezone_updown(update, up=True):
uid = ut.uid(update)
txt = 'timezone_down'
if up:
txt = 'timezone_up'
pos = int(update.callback_query.data.split(txt)[1])
tz_hour, tz_minutes = _timezone_value(update)
values = [tz_hour // 10, tz_hour % 10, tz_minutes // 10, tz_minutes % 10]
if up:
values[pos] = (values[pos] + 1) % 10
else:
values[pos] = (values[pos] - 1) % 10
tz_hour = int("".join(str(v) for v in values[:2]))
if tz_hour > 23:
tz_hour = 23
tz_minutes = int("".join(str(v) for v in values[2:]))
if tz_minutes > 59:
tz_minutes = 59
_state(uid, ut.CMD.TZ, [tz_hour, tz_minutes]) # only update first time
STATE[uid][ut.CMD.TZ] = [tz_hour, tz_minutes]
timezone_menu(update, modified=True)
def timezone_disable(update):
uid = ut.uid(update)
db.unset_timezone(uid)
settings_timezone_menu(update)
def timezone_set(update):
uid = ut.uid(update)
value_hour, value_minutes = _timezone_value(update)
bot_hour, bot_minutes = map(int, datetime.now()
.strftime("%H:%M").split(':'))
tz_hour = value_hour - bot_hour
tz_minutes = value_minutes - bot_minutes
db.set_timezone(uid, tz_hour, tz_minutes)
settings_timezone_menu(update)