From 8f3d6dfaff37cb1e9140f7b73837c66d716d74cf83602c885075d942bbdaabf5 Mon Sep 17 00:00:00 2001 From: andrea Date: Sat, 26 Jul 2025 21:25:38 +0200 Subject: [PATCH] explicit logs --- bot.py | 9 ++++----- bot_libs/commands_handling.py | 25 +++++++++---------------- bot_libs/player_handling.py | 27 ++++++++++++++------------- bot_libs/repeating.py | 8 ++++---- bot_libs/simulation.py | 11 +++++++---- 5 files changed, 38 insertions(+), 42 deletions(-) diff --git a/bot.py b/bot.py index 1ac550f..f59e33f 100644 --- a/bot.py +++ b/bot.py @@ -25,7 +25,7 @@ async def bot_start(update, context): reply_markup= ReplyKeyboardMarkup(keyboard, one_time_keyboard=False, resize_keyboard=True) chat_id = update.effective_chat.id - _log.log_debug(f'{chat_id}: I\'m building the world\'s game...') + _log.log_debug(f'bot_start: {chat_id} - I\'m building the world\'s game...') Arena= _arena.BrSimArena() await update.message.reply_text('Ho creato il mondo di gioco', reply_markup=reply_markup) @@ -35,10 +35,9 @@ 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}') + _log.log_info(f'bot_command: {chat_id} - text received: {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': @@ -66,7 +65,7 @@ async def bot_commands(update, context): if text == 'upstart': return await _bot_upstart.update_bot(update, context) - _log.log_debug(f'bot_command: {chat_id} sent this text: {text}') + _log.log_debug(f'bot_command: {chat_id} - sent this text: {text}') await update.message.reply_text(_botsyms.WIP_MSG) def main(): @@ -75,7 +74,7 @@ def main(): application.add_handler(CommandHandler('start', bot_start)) application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, bot_commands)) - _log.log_debug('Bot is running...') + _log.log_info('main: Bot is running...') application.run_polling() if __name__ == '__main__': diff --git a/bot_libs/commands_handling.py b/bot_libs/commands_handling.py index 8b9a4cf..e3bfff7 100644 --- a/bot_libs/commands_handling.py +++ b/bot_libs/commands_handling.py @@ -6,18 +6,18 @@ 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}') + _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()) + await _bot_player.add_player(update, context, chat_id, player.strip()) elif text == 'Add random Players': - await _bot_player.add_random_players(update, context, colors_names= False) + 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, colors_names= True) + await _bot_player.add_random_players(update, context, chat_id, colors_names= True) Arena= context.application.bot_data['arena'] players= [p.get_name_and_stats() for p in Arena.get_players()] @@ -25,7 +25,7 @@ async def get_name_from_input(context, update, chat_id, text): 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') + _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) @@ -34,34 +34,27 @@ async def get_seconds_from_input(context, update, chat_id, text): ### 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) + return await _bot_player.get_players(update, context, chat_id) 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) + return await _bot_player.get_alive_players(update, context, chat_id) 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) + return await _bot_player.get_death_players(update, context, chat_id) 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) + return await _bot_player.get_ranking_players(update, context, chat_id) 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']) diff --git a/bot_libs/player_handling.py b/bot_libs/player_handling.py index 9b3bc49..5789012 100644 --- a/bot_libs/player_handling.py +++ b/bot_libs/player_handling.py @@ -2,12 +2,13 @@ import random as _rand from utils import logs as _log from bot_libs import syms as _bot_syms -async def add_player(update, context, name): - _log.log_info(f'add_player: {name}') +async def add_player(update, context, chat_id, name): + _log.log_info(f'add_player: {chat_id} - {name}') Arena= context.application.bot_data['arena'] + if len(Arena.get_players()) >= 70: return # prevent message too long error Arena.add_player(name) -async def add_random_players(update, context, colors_names= False): +async def add_random_players(update, context, chat_id, colors_names= False): if colors_names: names= _bot_syms.COLORS_NAMES else: names= _bot_syms.RANDOM_NAMES @@ -17,34 +18,34 @@ async def add_random_players(update, context, colors_names= False): players_num= _rand.randint(min_players, max_players) _rand.shuffle(names) lucky_players= _rand.sample(names, players_num) - _log.log_info(f'add_random_players: extracting {players_num} random players for the game') - for name in lucky_players: await add_player(update, context, name) + _log.log_info(f'add_random_players: {chat_id} - extracting {players_num} random players for the game') + for name in lucky_players: await add_player(update, context, chat_id, name) -async def get_players(update, context): +async def get_players(update, context, 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) - _log.log_info(f'get_players: {players_str}') + _log.log_info(f'get_players: {chat_id} - {players_str}') await update.message.reply_text(f'Ecco i giocatori presenti nel mondo do gioco: \n{players_str}') -async def get_alive_players(update, context): +async def get_alive_players(update, context, chat_id): Arena= context.application.bot_data['arena'] alive= [a.get_name_and_stats() for a in Arena.get_alive_players()] alive_str= '\n'.join(alive) - _log.log_info(f'get_alive_players: {alive_str}') + _log.log_info(f'get_alive_players: {chat_id} - {alive_str}') await update.message.reply_text(f'Ecco i giocatori ancora vivi: \n{alive_str}') -async def get_death_players(update, context): +async def get_death_players(update, context, chat_id): Arena= context.application.bot_data['arena'] death= [d.get_name_and_stats() for d in Arena.get_death_players()] death_str= '\n'.join(death) - _log.log_info(f'get_death_players: {death_str}') + _log.log_info(f'get_death_players: {chat_id} - {death_str}') await update.message.reply_text(f'Ecco i giocatori morti x.x: \n{death_str}') -async def get_ranking_players(update, context): +async def get_ranking_players(update, context, chat_id): Arena = context.application.bot_data['arena'] leaderboard_text = Arena.get_ranking() - _log.log_info(f'{leaderboard_text}') + _log.log_info(f'get_ranking_players: {chat_id} - {leaderboard_text}') await update.message.reply_text( f'{leaderboard_text}', parse_mode='Markdown' diff --git a/bot_libs/repeating.py b/bot_libs/repeating.py index aff88ce..6c8db57 100644 --- a/bot_libs/repeating.py +++ b/bot_libs/repeating.py @@ -3,22 +3,22 @@ from bot_libs import simulation as _bot_sim async def _loop_game(context): chat_id = context.job.chat_id - _log.log_info(f'_loop_game: run on {chat_id}') + _log.log_info(f'_loop_game: {chat_id} - run game simulation day') return await _bot_sim.simulate_day(context, chat_id) async def start_loop_game(update, context, seconds): await update.message.reply_text(f'Ok capo!! giochero\' per te ogni {seconds}s') chat_id = update.effective_chat.id if 'arena' not in context.application.bot_data: - _log.log_info(f'start_loop_game: {chat_id} Arena not found') + _log.log_info(f'start_loop_game: {chat_id} - Arena not found') await update.message.reply_text(f'Arena non trovata, avviare con /start') return Arena= context.application.bot_data['arena'] if len(Arena.get_players()) < 2: - _log.log_info(f'start_loop_game: {chat_id} Not enough player to start the match') + _log.log_info(f'start_loop_game: {chat_id} - Not enough player to start the match') await update.message.reply_text(f'Servono almeno 2 giocatori. Ecco i giocatori presenti nel mondo do gioco: \n{Arena.get_players()}') return context.job_queue.run_repeating(_loop_game, interval= seconds, first=1, chat_id= chat_id) - _log.log_info(f'start_loop_game: Repeating Job created for: {chat_id}') + _log.log_info(f'start_loop_game: {chat_id} - Cron job started') diff --git a/bot_libs/simulation.py b/bot_libs/simulation.py index cfe509e..2352fd1 100644 --- a/bot_libs/simulation.py +++ b/bot_libs/simulation.py @@ -1,12 +1,14 @@ import debug as _dbg from utils import logs as _log -def get_winner(context, Arena): + +def get_winner(context, Arena, chat_id): winner= Arena.get_alive_players()[0] try: context.job.schedule_removal() - _log.log_info(f'simulate_day: Loop removed') + _log.log_info(f'simulate_day: {chat_id} Loop removed') except: pass + msg= f'{winner.get_name_and_stats()} Vince la cruenta battaglia ' msg+= f'uccidendo {winner.get_kills()} giocatori ' msg+= f'e schivando {winner.get_dodges()} colpi nemici, e vive felice e ' @@ -18,9 +20,10 @@ def get_winner(context, Arena): msg+= 'content# con Guarino' return msg + async def simulate_day(context, chat_id): if 'arena' not in context.application.bot_data: - _log.log_info('Arena not Found') + _log.log_info('simulate_day: {chat_id} Arena not Found') await context.bot.send_message(chat_id, 'Che e\' successo? un Guarino ha rubato l\'arena, avvia una nuova partita con /start') return @@ -34,5 +37,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(context, Arena) + msg= get_winner(context, Arena, chat_id) return await context.bot.send_message(chat_id, msg)