1
0
Files
battle_royale_sim/bot.py
2025-07-26 10:04:19 +02:00

79 lines
2.9 KiB
Python

import asyncio
#import datetime
#import pytz
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
if 'arena' in context.application.bot_data:
Arena= context.application.bot_data['arena']
if len(Arena.get_alive_players()) <= 1: return # TODO break the loop
print(f'{chat_id}: Guarino ha trovato l\'arena')
await context.bot.send_message(chat_id, f'Giorno #{Arena.day}')
msg= _dbg.play_one_day_debug(Arena)
await context.bot.send_message(chat_id, msg)
else:
print('Arena non trovata')
await context.bot.send_message(chat_id, 'Che e\' successo? un Guarino ha rubato l\'arena, avvia una nuova partita con /start')
async def bot_start(update, context):
await update.message.reply_text(_botsyms.START_MSG)
chat_id = update.effective_chat.id
print(f'{chat_id}: Sto costruendo il mondo di gioco...')
Arena= _brsim.init_arena()
await update.message.reply_text('Ho creato il mondo di gioco')
context.application.bot_data['arena'] = Arena
async def echo(update, context):
testo_ricevuto = update.message.text
await update.message.reply_text(_botsyms.WIP_MSG)
async def start_game(update, context):
chat_id = update.effective_chat.id
if 'arena' not in context.application.bot_data:
print(f'{chat_id}: Arena non trovata')
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:
print(f'{chat_id}: Guarino pretende che ci siano almeno 2 giocatori')
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=86400, first=1, chat_id= chat_id)
#timezone = pytz.timezone('Europe/Rome')
#context.job_queue.run_daily(
#loop_game,
#time=datetime.time(hour=23, minute=40, second=0, tzinfo= timezone),
#chat_id=chat_id,
#name=str(chat_id)
#)
print(f'Job giornaliero creato per la chat {chat_id}')
def main():
application = Application.builder().token(_botsyms.TOKEN).build()
application.add_handler(CommandHandler('start', bot_start))
application.add_handler(CommandHandler('start_game', start_game))
# 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...')
application.run_polling()
if __name__ == '__main__':
main()