2025-07-26 11:00:20 +02:00
from telegram . ext import Application
from telegram . ext import CommandHandler
from telegram . ext import MessageHandler
from telegram . ext import filters
from telegram import ReplyKeyboardMarkup
from telegram import ReplyKeyboardRemove
2025-07-23 21:17:09 +02:00
import main as _brsim
2025-07-25 23:41:44 +02:00
import debug as _dbg
2025-07-26 10:04:19 +02:00
from bot_libs import player_handling as _bot_player
2025-07-26 10:13:20 +02:00
from bot_libs import syms as _botsyms
2025-07-23 21:17:09 +02:00
2025-07-26 11:00:20 +02:00
async def simulate_day ( context , chat_id ) :
2025-07-23 23:14:35 +02:00
if ' arena ' in context . application . bot_data :
2025-07-25 23:41:44 +02:00
Arena = context . application . bot_data [ ' arena ' ]
if len ( Arena . get_alive_players ( ) ) < = 1 : return # TODO break the loop
2025-07-24 21:28:42 +02:00
print ( f ' { chat_id } : Guarino ha trovato l \' arena ' )
2025-07-25 23:41:44 +02:00
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 )
2025-07-23 23:14:35 +02:00
else :
print ( ' Arena non trovata ' )
2025-07-25 23:41:44 +02:00
await context . bot . send_message ( chat_id , ' Che e \' successo? un Guarino ha rubato l \' arena, avvia una nuova partita con /start ' )
2025-07-23 21:17:09 +02:00
2025-07-26 11:00:20 +02:00
async def loop_game ( context ) :
chat_id = context . job . chat_id
return await simulate_day ( context , chat_id )
2025-07-24 21:28:42 +02:00
async def bot_start ( update , context ) :
2025-07-23 23:14:35 +02:00
await update . message . reply_text ( _botsyms . START_MSG )
2025-07-23 21:17:09 +02:00
2025-07-26 11:00:20 +02:00
keyboard = [
[ ' Init/Restart ' ] ,
[ ' Add Player ' ] ,
[ ' Get Players ' , ' Get Alive Players ' , ' Get Death Players ' ] ,
[ ' Simulate Day ' , ' Run Periodically ' ]
]
reply_markup = ReplyKeyboardMarkup ( keyboard , one_time_keyboard = False , resize_keyboard = True )
2025-07-23 23:43:26 +02:00
chat_id = update . effective_chat . id
print ( f ' { chat_id } : Sto costruendo il mondo di gioco... ' )
2025-07-23 21:17:09 +02:00
Arena = _brsim . init_arena ( )
2025-07-23 23:43:26 +02:00
2025-07-26 11:00:20 +02:00
await update . message . reply_text ( ' Ho creato il mondo di gioco ' , reply_markup = reply_markup )
2025-07-23 21:17:09 +02:00
context . application . bot_data [ ' arena ' ] = Arena
2025-07-26 11:15:44 +02:00
async def start_loop_game ( update , context , seconds ) :
await update . message . reply_text ( f ' Ok capo!! giochero \' per te ogni { seconds } secondi ' )
2025-07-25 23:41:44 +02:00
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
2025-07-26 11:15:44 +02:00
context . job_queue . run_repeating ( loop_game , interval = seconds , first = 1 , chat_id = chat_id )
2025-07-25 23:41:44 +02:00
print ( f ' Job giornaliero creato per la chat { chat_id } ' )
2025-07-26 11:00:20 +02:00
async def bot_commands ( update , context ) :
text = update . message . text
chat_id = update . effective_chat . id
if text == ' Init/Restart ' :
print ( f ' bot_command: { chat_id } Init/Restart ' )
return await bot_start ( update , context )
if text == ' Add Player ' :
print ( f ' bot_command: { chat_id } Add Player ' )
context . application . bot_data [ ' ask_name ' ] = 1
2025-07-26 11:15:44 +02:00
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 ' )
2025-07-26 11:00:20 +02:00
if text == ' Get Players ' :
print ( f ' bot_command: { chat_id } Get Players ' )
return await _bot_player . get_players ( update , context )
if text == ' Get Alive Players ' :
print ( f ' bot_command: { chat_id } Get Alive Players ' )
return await _bot_player . get_alive_players ( update , context )
if text == ' Get Death Players ' :
print ( f ' bot_command: { chat_id } Get Death Players ' )
return await _bot_player . get_death_players ( update , context )
if text == ' Simulate Day ' :
print ( f ' bot_command: { chat_id } Simulate Day ' )
return await simulate_day ( context , chat_id )
if text == ' Run Periodically ' :
print ( f ' bot_command: { chat_id } Run Periodically ' )
2025-07-26 11:15:44 +02:00
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) ' )
2025-07-26 11:00:20 +02:00
#return await simulate_day(context, chat_id)
2025-07-26 11:15:44 +02:00
waiting_for_seconds = context . application . bot_data . get ( ' ask_seconds ' )
if waiting_for_seconds :
print ( 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 start_loop_game ( update , context , seconds )
2025-07-26 11:00:20 +02:00
waiting_for_name = context . application . bot_data . get ( ' ask_name ' )
if waiting_for_name :
print ( f ' bot_command: { chat_id } Collected Player Name { text } ' )
del ( context . application . bot_data [ ' ask_name ' ] )
return await _bot_player . add_player ( update , context , text )
print ( f ' { chat_id } ha inviato questo testo: { text } ' )
await update . message . reply_text ( _botsyms . WIP_MSG )
2025-07-23 21:17:09 +02:00
def main ( ) :
2025-07-23 23:14:35 +02:00
application = Application . builder ( ) . token ( _botsyms . TOKEN ) . build ( )
2025-07-23 21:17:09 +02:00
application . add_handler ( CommandHandler ( ' start ' , bot_start ) )
2025-07-26 11:00:20 +02:00
#application.add_handler(CommandHandler('start_game', start_loop_game))
2025-07-26 10:04:19 +02:00
# player handler
2025-07-26 11:00:20 +02:00
#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))
2025-07-26 10:04:19 +02:00
2025-07-26 11:00:20 +02:00
application . add_handler ( MessageHandler ( filters . TEXT & ~ filters . COMMAND , bot_commands ) )
2025-07-23 21:17:09 +02:00
print ( ' Bot in esecuzione... ' )
application . run_polling ( )
if __name__ == ' __main__ ' :
main ( )