From 0df960ea22ce4dc5b17e2accfc5980757832cbbb0098cc990a144d8fe8a0d8ef Mon Sep 17 00:00:00 2001 From: andrea Date: Sat, 26 Jul 2025 10:04:19 +0200 Subject: [PATCH] split player commands from main bot --- bot.py | 37 ++++++++----------------------------- bot_libs/player_handling.py | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 29 deletions(-) create mode 100644 bot_libs/player_handling.py diff --git a/bot.py b/bot.py index fcac508..834e2ec 100644 --- a/bot.py +++ b/bot.py @@ -5,6 +5,7 @@ from telegram.ext import Application, CommandHandler, MessageHandler, filters import bot_syms as _botsyms import main as _brsim import debug as _dbg +from bot_libs import player_handling as _bot_player async def loop_game(context): chat_id = context.job.chat_id @@ -27,8 +28,6 @@ async def bot_start(update, context): Arena= _brsim.init_arena() await update.message.reply_text('Ho creato il mondo di gioco') - #await update.message.reply_text(f'Ecco la lista degli sfortunati avventurieri:\n{players}') - #await update.message.reply_text(f'Queste le armi che avranno a disposizione nell\'arena:\n{weapons}') context.application.bot_data['arena'] = Arena async def echo(update, context): @@ -58,38 +57,18 @@ async def start_game(update, context): #) print(f'Job giornaliero creato per la chat {chat_id}') -async def add_player(update, context): - name= " ".join(context.args) - print(f'sto aggiungendo il giocatore {name} all\'arena') - Arena= context.application.bot_data['arena'] - Arena.add_player(name) - print(f'Giocatori: {Arena.get_players()}') - await update.message.reply_text(f'Ecco i giocatori presenti nel mondo do gioco: \n{Arena.get_players()}') - -async def get_players(update, context): - Arena= context.application.bot_data['arena'] - print(f'Giocatori: {Arena.get_players()}') - await update.message.reply_text(f'Ecco i giocatori presenti nel mondo do gioco: \n{Arena.get_players()}') - -async def get_alive_players(update, context): - Arena= context.application.bot_data['arena'] - print(f'Giocatori: {Arena.get_players()}') - await update.message.reply_text(f'Ecco i giocatori ancora vivi: \n{Arena.get_alive_players()}') - -async def get_death_players(update, context): - Arena= context.application.bot_data['arena'] - print(f'Giocatori: {Arena.get_players()}') - await update.message.reply_text(f'Ecco i giocatori morti x.x: \n{Arena.get_death_players()}') - def main(): application = Application.builder().token(_botsyms.TOKEN).build() application.add_handler(CommandHandler('start', bot_start)) application.add_handler(CommandHandler('start_game', start_game)) - application.add_handler(CommandHandler('add_player', add_player)) - application.add_handler(CommandHandler('get_players', get_players)) - application.add_handler(CommandHandler('get_alive_players', get_alive_players)) - application.add_handler(CommandHandler('get_death_players', get_death_players)) + + # player handler + application.add_handler(CommandHandler('add_player', _bot_player.add_player)) + application.add_handler(CommandHandler('get_players', _bot_player.get_players)) + application.add_handler(CommandHandler('get_alive_players', _bot_player.get_alive_players)) + application.add_handler(CommandHandler('get_death_players', _bot_player.get_death_players)) + application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo)) print('Bot in esecuzione...') diff --git a/bot_libs/player_handling.py b/bot_libs/player_handling.py new file mode 100644 index 0000000..1eb122a --- /dev/null +++ b/bot_libs/player_handling.py @@ -0,0 +1,22 @@ + +async def add_player(update, context): + name= " ".join(context.args) + print(f'add_player: {name}') + Arena= context.application.bot_data['arena'] + Arena.add_player(name) + await update.message.reply_text(f'Ecco i giocatori presenti nel mondo do gioco: \n{Arena.get_players()}') + +async def get_players(update, context): + Arena= context.application.bot_data['arena'] + print(f'get_players: {Arena.get_players()}') + await update.message.reply_text(f'Ecco i giocatori presenti nel mondo do gioco: \n{Arena.get_players()}') + +async def get_alive_players(update, context): + Arena= context.application.bot_data['arena'] + print(f'get_alive_players: {Arena.get_alive_players()}') + await update.message.reply_text(f'Ecco i giocatori ancora vivi: \n{Arena.get_alive_players()}') + +async def get_death_players(update, context): + Arena= context.application.bot_data['arena'] + print(f'get_death_players: {Arena.get_death_players()}') + await update.message.reply_text(f'Ecco i giocatori morti x.x: \n{Arena.get_death_players()}')