Finished working on calc command

This commit is contained in:
Davte
2020-05-18 15:04:18 +02:00
parent ea549b63c8
commit ea5c63c1f8
2 changed files with 80 additions and 23 deletions

View File

@@ -1015,14 +1015,16 @@ default_useful_tools_messages = {
}, },
'help_section': None, 'help_section': None,
'instructions': { 'instructions': {
'en': "🔢 Calculator 🧮\n\n" 'en': "🔢 <b>Calculator</b> 🧮\n\n"
"Enter an algebraic expression after /calc to get its " "Enter an algebraic expression after /calc to get its "
"result, or use the command in reply to a message containing " "result, or use the command in reply to a message containing "
"an expression, or use the keyboard below.\n", "an expression, or use the keyboard below.\n\n"
'it': "🔢 Calcolatrice 🧮\n\n" "- <code></code>: show information about special keys\n",
'it': "🔢 <b>Calcolatrice</b> 🧮\n\n"
"Inserisci un'espressione algebrica dopo /calcola per " "Inserisci un'espressione algebrica dopo /calcola per "
"ottenerne il risultato, oppure usa il comando in risposta, " "ottenerne il risultato, oppure usa il comando in risposta, "
"o ancora usa la tastiera qui sotto.\n", "o ancora usa la tastiera qui sotto.\n\n"
"- <code></code>: mostra informazioni sui tasti speciali\n",
}, },
'invalid_expression': { 'invalid_expression': {
'en': "Invalid expression: {error}", 'en': "Invalid expression: {error}",
@@ -1032,6 +1034,32 @@ default_useful_tools_messages = {
'en': "calculate", 'en': "calculate",
'it': "calcola", 'it': "calcola",
}, },
'message_input': {
'en': "🔢 <b>Calculator</b> 🧮\n\n"
"<i>Enter an expression</i>",
'it': "🔢 <b>Calcolatrice</b> 🧮\n\n"
"<i>Mandami l'espressione</i>",
},
'special_keys': {
'en': "<b>Special keys</b>\n"
"- <code>**</code>: exponentiation\n"
"- <code>//</code>: floor division\n"
"- <code>mod</code>: modulus (remainder of division)\n"
"- <code>MR</code>: result of last expression\n"
"- <code></code>: show this help message\n"
"- <code>💬</code>: write your expression in a message\n"
"- <code>⬅️</code>: delete last character\n"
"- <code>✅</code>: start a new line (and a new expression)\n",
'it': "<b>Tasti speciali</b>\n"
"- <code>**</code>: elevamento a potenza\n"
"- <code>//</code>: quoziente della divisione\n"
"- <code>mod</code>: resto della divisione\n"
"- <code>MR</code>: risultato dell'espressione precedente\n"
"- <code></code>: mostra questo messaggio\n"
"- <code>💬</code>: invia un messaggio con l'espressione\n"
"- <code>⬅️</code>: cancella ultimo carattere\n"
"- <code>✅</code>: vai a capo (inizia una nuova espressione)\n",
},
'use_buttons': { 'use_buttons': {
'en': "Use buttons to enter an algebraic expression.\n\n" 'en': "Use buttons to enter an algebraic expression.\n\n"
"<i>The input will be displayed after you stop typing for a " "<i>The input will be displayed after you stop typing for a "

View File

@@ -38,8 +38,8 @@ def get_calc_buttons() -> OrderedDict:
) )
buttons['_'] = dict( buttons['_'] = dict(
value='_', value='_',
symbol='_', symbol='MR',
order='A4', order='B5',
) )
buttons[0] = dict( buttons[0] = dict(
value='0', value='0',
@@ -129,35 +129,30 @@ def get_calc_buttons() -> OrderedDict:
buttons['del'] = dict( buttons['del'] = dict(
value='del', value='del',
symbol='⬅️', symbol='⬅️',
order='F2', order='E5',
) )
buttons['('] = dict( buttons['('] = dict(
value='(', value='(',
symbol='(', symbol='(',
order='A5', order='A4',
) )
buttons[')'] = dict( buttons[')'] = dict(
value=')', value=')',
symbol=')', symbol=')',
order='B5', order='A5',
) )
buttons['m'] = dict( buttons['info'] = dict(
value=')', value='info',
symbol=')', symbol='',
order='C5', order='C5',
) )
buttons['u'] = dict( buttons['parser'] = dict(
value=')', value='parser',
symbol=')', symbol='💬',
order='D5', order='D5',
) )
buttons['d'] = dict(
value=')',
symbol=')',
order='E5',
)
return buttons return buttons
@@ -225,9 +220,11 @@ async def _calculate_button(bot: Bot,
'useful_tools', 'calculate_command', 'use_buttons', 'useful_tools', 'calculate_command', 'use_buttons',
language=language language=language
) )
reply_markup = get_calculator_keyboard(additional_data=[record_id])
else: else:
record_id = data[0] record_id = data[0]
reply_markup = get_calculator_keyboard(
additional_data=([record_id] if record_id else None)
)
if record_id not in bot.shared_data['calc']: if record_id not in bot.shared_data['calc']:
bot.shared_data['calc'][record_id] = [] bot.shared_data['calc'][record_id] = []
asyncio.ensure_future( asyncio.ensure_future(
@@ -236,6 +233,34 @@ async def _calculate_button(bot: Bot,
language=language) language=language)
) )
update['data'] = data update['data'] = data
if len(data) and data[-1] in ('info', 'parser'):
command = data[-1]
if command == 'parser':
reply_markup = None
bot.set_individual_text_message_handler(
handler=_calculate_command,
user_id=user_record['telegram_id']
)
elif command == 'info':
reply_markup = make_inline_keyboard(
[
make_button(
text='Ok',
prefix='calc:///',
delimiter='|',
data=[record_id, 'back']
)
]
)
text = bot.get_message(
'useful_tools', 'calculate_command', (
'special_keys' if command == 'info'
else 'message_input' if command == 'parser'
else ''
),
language=language
)
else:
bot.shared_data['calc'][record_id].append(update) bot.shared_data['calc'][record_id].append(update)
# Edit the update with the button if a new text is specified # Edit the update with the button if a new text is specified
if not text: if not text:
@@ -309,7 +334,7 @@ async def calculate_session(bot: Bot,
while queue_len != len(queue): while queue_len != len(queue):
queue_len = len(queue) queue_len = len(queue)
await asyncio.sleep(buffer_seconds) await asyncio.sleep(buffer_seconds)
last_entry = max(queue, key=lambda u: u['id']) last_entry = max(queue, key=lambda u: u['id'], default=None)
# Delete record-associated queue # Delete record-associated queue
queue = queue.copy() queue = queue.copy()
del bot.shared_data['calc'][record_id] del bot.shared_data['calc'][record_id]
@@ -336,6 +361,8 @@ async def calculate_session(bot: Bot,
input_value = data[1] input_value = data[1]
if input_value == 'del': if input_value == 'del':
expression = expression[:-1] expression = expression[:-1]
elif input_value == 'back':
pass
elif input_value in calc_buttons: elif input_value in calc_buttons:
expression += calc_buttons[input_value]['value'] expression += calc_buttons[input_value]['value']
else: else:
@@ -362,6 +389,8 @@ async def calculate_session(bot: Bot,
'useful_tools', 'calculate_command', 'instructions', 'useful_tools', 'calculate_command', 'instructions',
language=language language=language
) )
if last_entry is None:
return
await bot.edit_message_text( await bot.edit_message_text(
text=text, text=text,
update=last_entry, update=last_entry,