1
0

cleanings to make code more compact and readable

This commit is contained in:
andrea
2025-07-27 08:17:12 +02:00
parent c82f31a101
commit 0bfa74c0e2
2 changed files with 54 additions and 44 deletions

41
bot.py
View File

@@ -8,9 +8,9 @@ from telegram import ReplyKeyboardRemove
from utils import logs as _log
from entities import arena as _arena
from bot_libs import special_commands as _bot_special_cmd
from bot_libs import special_commands as _scmd
from bot_libs import syms as _botsyms
from bot_libs import commands_handling as _bot_commands
from bot_libs import commands_handling as _cmd
async def bot_start(update, context):
@@ -41,34 +41,39 @@ async def bot_commands(update, context):
_log.log_info(f'bot_command: {chat_id} - text received: {text}')
# init or restart the game
if text == 'Init/Restart':
return await bot_start(update, context)
# player game commands
if text == 'Add Player':
return await _bot_commands.bot_command_add_player(context, update, chat_id)
return await _cmd.cmd_add_player(context, update, chat_id)
if text == 'Get Players':
return await _bot_commands.bot_command_get_players(context, update, chat_id)
return await _cmd.cmd_get_players(context, update, chat_id)
if text == 'Get Alive Players':
return await _bot_commands.bot_command_get_alive_players(context, update, chat_id)
return await _cmd.cmd_get_alive_players(context, update, chat_id)
if text == 'Get Death Players':
return await _bot_commands.bot_command_get_death_players(context, update, chat_id)
return await _cmd.cmd_get_death_players(context, update, chat_id)
if text == 'Get Ranking Players':
return await _bot_commands.bot_command_get_ranking_players(context, update, chat_id)
return await _cmd.cmd_get_ranking_players(context, update, chat_id)
if text == 'Simulate Day':
return await _bot_commands.bot_command_simulate_day(context, update, chat_id)
return await _cmd.cmd_simulate_day(context, update, chat_id)
if text == 'Run Periodically':
return await _bot_commands.bot_command_simulate_day_cron(context, update, chat_id)
return await _cmd.cmd_simulate_day_cron(context, update, chat_id)
if text == 'Add random Players':
return await _cmd.cmd_add_random_players(context, update, chat_id)
if text == 'Add random color Players':
return await _cmd.cmd_add_random_color_players(context, update, chat_id)
waiting_for_name= context.application.bot_data.get('ask_name')
if waiting_for_name or text in ['Add random Players', 'Add random color Players']:
return await _bot_commands.get_name_from_input(context, update, chat_id, text)
# special commands
if text == 'upstart': return await _scmd.update_bot(update, context)
if text == 'logs': return await _scmd.show_logs(update, context)
waiting_for_seconds= context.application.bot_data.get('ask_seconds')
if waiting_for_seconds:
return await _bot_commands.get_seconds_from_input(context, update, chat_id, text)
if text == 'upstart': return await _bot_special_cmd.update_bot(update, context)
if text == 'logs': return await _bot_special_cmd.show_logs(update, context)
# get user input
if context.application.bot_data.get('ask_name'):
return await _cmd.cmd_get_player_name(context, update, chat_id, text)
if context.application.bot_data.get('ask_seconds'):
return await _cmd.cmd_get_cron_time(context, update, chat_id, text)
_log.log_debug(f'bot_command: {chat_id} - sent this text: {text}')
await update.message.reply_text(_botsyms.WIP_MSG)

View File

@@ -5,27 +5,24 @@ from bot_libs import repeating as _bot_repeat
### parse user input
async def get_name_from_input(context, update, chat_id, text):
_log.log_info(f'bot_command: {chat_id} - Collected Player Name {text}')
if 'ask_name' in context.application.bot_data:
del(context.application.bot_data['ask_name'])
players= text.split(',')
for player in players:
await _bot_player.add_player(update, context, chat_id, player.strip())
elif text == 'Add random Players':
await _bot_player.add_random_players(update, context, chat_id, colors_names= False)
elif text == 'Add random color Players':
await _bot_player.add_random_players(update, context, chat_id, colors_names= True)
async def _cmd_list_of_players(context, update, chat_id):
Arena= context.application.bot_data['arena']
players= [p.get_name_and_stats() for p in Arena.get_players()]
players_str= '\n'.join(players)
return await update.message.reply_text(f'Ecco i {len(players)} giocatori presenti nel mondo do gioco: \n{players_str}')
async def get_seconds_from_input(context, update, chat_id, text):
_log.log_info(f'bot_command: {chat_id} - User Wants to auto-run the game every {text} seconds')
async def cmd_get_player_name(context, update, chat_id, text):
_log.log_info(f'cmd_get_player_name: {chat_id} - Collected Player Name {text}')
if not 'ask_name' in context.application.bot_data: return
del(context.application.bot_data['ask_name'])
players= text.split(',')
for player in players:
await _bot_player.add_player(update, context, chat_id, player.strip())
return await _cmd_list_of_players(context, update, chat_id)
async def cmd_get_cron_time(context, update, chat_id, text):
_log.log_info(f'cmd_get_cron_time: {chat_id} - User Wants to auto-run the game every {text} seconds')
try: text= int(text)
except: return
seconds= max(1, text)
@@ -35,28 +32,36 @@ async def get_seconds_from_input(context, update, chat_id, text):
### basic commands handling
async def bot_command_add_player(context, update, chat_id):
async def cmd_add_random_players(context, update, chat_id):
await _bot_player.add_random_players(update, context, chat_id, colors_names= False)
return await _cmd_list_of_players(context, update, chat_id)
async def cmd_add_random_color_players(context, update, chat_id):
await _bot_player.add_random_players(update, context, chat_id, colors_names= True)
return await _cmd_list_of_players(context, update, chat_id)
async def cmd_add_player(context, update, chat_id):
context.application.bot_data['ask_name'] = 1
if 'ask_seconds' in context.application.bot_data:
del(context.application.bot_data['ask_seconds'])
return await update.message.reply_text('Inserisci il Nome del giocatore (o piu\' nomi separati da virgola)')
async def bot_command_get_players(context, update, chat_id):
async def cmd_get_players(context, update, chat_id):
return await _bot_player.get_players(update, context, chat_id)
async def bot_command_get_alive_players(context, update, chat_id):
async def cmd_get_alive_players(context, update, chat_id):
return await _bot_player.get_alive_players(update, context, chat_id)
async def bot_command_get_death_players(context, update, chat_id):
async def cmd_get_death_players(context, update, chat_id):
return await _bot_player.get_death_players(update, context, chat_id)
async def bot_command_get_ranking_players(context, update, chat_id):
async def cmd_get_ranking_players(context, update, chat_id):
return await _bot_player.get_ranking_players(update, context, chat_id)
async def bot_command_simulate_day(context, update, chat_id):
async def cmd_simulate_day(context, update, chat_id):
return await _bot_simulation.simulate_day(context, chat_id)
async def bot_command_simulate_day_cron(context, update, chat_id):
async def cmd_simulate_day_cron(context, update, chat_id):
context.application.bot_data['ask_seconds'] = 1
if 'ask_name' in context.application.bot_data:
del(context.application.bot_data['ask_name'])