From 460a6ccdec78b8ab01160531dcf6626e0fa0d64ae237a5120f732865ef82ba0d Mon Sep 17 00:00:00 2001 From: andrea Date: Sat, 26 Jul 2025 20:58:35 +0200 Subject: [PATCH] clean bot and fix autostop with scheduled game --- bot.py | 73 +++++++---------------------------- bot_libs/commands_handling.py | 68 ++++++++++++++++++++++++++++++++ bot_libs/simulation.py | 6 +-- 3 files changed, 85 insertions(+), 62 deletions(-) create mode 100644 bot_libs/commands_handling.py diff --git a/bot.py b/bot.py index 0255213..1ac550f 100644 --- a/bot.py +++ b/bot.py @@ -1,5 +1,3 @@ -from utils import logs as _log - from telegram.ext import Application from telegram.ext import CommandHandler from telegram.ext import MessageHandler @@ -7,40 +5,14 @@ from telegram.ext import filters from telegram import ReplyKeyboardMarkup from telegram import ReplyKeyboardRemove +from utils import logs as _log + from entities import arena as _arena -from bot_libs import player_handling as _bot_player -from bot_libs import simulation as _bot_sim -from bot_libs import repeating as _bot_repeat from bot_libs import upstart as _bot_upstart from bot_libs import syms as _botsyms +from bot_libs import commands_handling as _bot_commands -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, player.strip()) - - elif text == 'Add random Players': - await _bot_player.add_random_players(update, context, colors_names= False) - - elif text == 'Add random color Players': - await _bot_player.add_random_players(update, context, colors_names= True) - - 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') - try: text= int(text) - except: return - seconds= max(1, text) - return await _bot_repeat.start_loop_game(update, context, seconds) - async def bot_start(update, context): await update.message.reply_text(_botsyms.START_MSG) @@ -63,51 +35,34 @@ async def bot_commands(update, context): text= update.message.text chat_id = update.effective_chat.id + _log.log_info(f'bot_command: {chat_id} input: {text}') + if text == 'Init/Restart': _log.log_info(f'bot_command: {chat_id} Init/Restart') return await bot_start(update, context) if text == 'Add Player': - _log.log_info(f'bot_command: {chat_id} Add Player') - 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)') - + return await _bot_commands.bot_command_add_player(context, update, chat_id) if text == 'Get Players': - _log.log_info(f'bot_command: {chat_id} Get Players') - return await _bot_player.get_players(update, context) - + return await _bot_commands.bot_command_get_players(context, update, chat_id) if text == 'Get Alive Players': - _log.log_info(f'bot_command: {chat_id} Get Alive Players') - return await _bot_player.get_alive_players(update, context) - + return await _bot_commands.bot_command_get_alive_players(context, update, chat_id) if text == 'Get Death Players': - _log.log_info(f'bot_command: {chat_id} Get Death Players') - return await _bot_player.get_death_players(update, context) - + return await _bot_commands.bot_command_get_death_players(context, update, chat_id) if text == 'Get Ranking Players': - _log.log_info(f'bot_command: {chat_id} Get Ranking Players') - return await _bot_player.get_ranking_players(update, context) - + return await _bot_commands.bot_command_get_ranking_players(context, update, chat_id) if text == 'Simulate Day': - _log.log_info(f'bot_command: {chat_id} Simulate Day') - return await _bot_sim.simulate_day(context, chat_id) - + return await _bot_commands.bot_command_simulate_day(context, update, chat_id) if text == 'Run Periodically': - _log.log_info(f'bot_command: {chat_id} Run Periodically') - context.application.bot_data['ask_seconds'] = 1 - if 'ask_name' in context.application.bot_data: - del(context.application.bot_data['ask_name']) - return await update.message.reply_text('Inserisci il numero di secondi, ad esempio \n(60 = 1 minuto)(600 = 10 minuti)\n(3600 = 1 ora)\n(86400 = 1 giorno)') + return await _bot_commands.bot_command_simulate_day_cron(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 _get_name_from_input(context, update, chat_id, text) + return await _bot_commands.get_name_from_input(context, update, chat_id, text) waiting_for_seconds= context.application.bot_data.get('ask_seconds') if waiting_for_seconds: - return await _get_seconds_from_input(context, update, chat_id, text) + return await _bot_commands.get_seconds_from_input(context, update, chat_id, text) if text == 'upstart': return await _bot_upstart.update_bot(update, context) diff --git a/bot_libs/commands_handling.py b/bot_libs/commands_handling.py new file mode 100644 index 0000000..8b9a4cf --- /dev/null +++ b/bot_libs/commands_handling.py @@ -0,0 +1,68 @@ +from utils import logs as _log +from bot_libs import player_handling as _bot_player +from bot_libs import simulation as _bot_simulation +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, player.strip()) + + elif text == 'Add random Players': + await _bot_player.add_random_players(update, context, colors_names= False) + + elif text == 'Add random color Players': + await _bot_player.add_random_players(update, context, colors_names= True) + + 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') + try: text= int(text) + except: return + seconds= max(1, text) + return await _bot_repeat.start_loop_game(update, context, seconds) + +### basic commands handling + +async def bot_command_add_player(context, update, chat_id): + _log.log_info(f'bot_command: {chat_id} Add Player') + 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): + _log.log_info(f'bot_command: {chat_id} Get Players') + return await _bot_player.get_players(update, context) + +async def bot_command_get_alive_players(context, update, chat_id): + _log.log_info(f'bot_command: {chat_id} Get Alive Players') + return await _bot_player.get_alive_players(update, context) + +async def bot_command_get_death_players(context, update, chat_id): + _log.log_info(f'bot_command: {chat_id} Get Death Players') + return await _bot_player.get_death_players(update, context) + +async def bot_command_get_ranking_players(context, update, chat_id): + _log.log_info(f'bot_command: {chat_id} Get Ranking Players') + return await _bot_player.get_ranking_players(update, context) + +async def bot_command_simulate_day(context, update, chat_id): + _log.log_info(f'bot_command: {chat_id} Simulate Day') + return await _bot_simulation.simulate_day(context, chat_id) + +async def bot_command_simulate_day_cron(context, update, chat_id): + _log.log_info(f'bot_command: {chat_id} Run Periodically') + context.application.bot_data['ask_seconds'] = 1 + if 'ask_name' in context.application.bot_data: + del(context.application.bot_data['ask_name']) + return await update.message.reply_text('Inserisci il numero di secondi, ad esempio \n(60 = 1 minuto)(600 = 10 minuti)\n(3600 = 1 ora)\n(86400 = 1 giorno)') diff --git a/bot_libs/simulation.py b/bot_libs/simulation.py index dad4172..cfe509e 100644 --- a/bot_libs/simulation.py +++ b/bot_libs/simulation.py @@ -1,7 +1,7 @@ -from utils import logs as _log import debug as _dbg +from utils import logs as _log -def get_winner(Arena): +def get_winner(context, Arena): winner= Arena.get_alive_players()[0] try: context.job.schedule_removal() @@ -34,5 +34,5 @@ async def simulate_day(context, chat_id): msg= Arena.get_ranking() await context.bot.send_message(chat_id, msg) if len(Arena.get_alive_players()) == 1: - msg= get_winner(Arena) + msg= get_winner(context, Arena) return await context.bot.send_message(chat_id, msg)