2025-07-26 18:28:46 +02:00
import os as _os
2025-07-26 19:04:17 +02:00
from utils import logs as _log
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-26 14:44:16 +02:00
2025-07-26 18:07:14 +02:00
from entities import arena as _arena
2025-07-26 10:04:19 +02:00
from bot_libs import player_handling as _bot_player
2025-07-26 14:44:16 +02:00
from bot_libs import simulation as _bot_sim
from bot_libs import repeating as _bot_repeat
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
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 ' ] ,
2025-07-26 15:20:03 +02:00
[ ' Add Player ' , ' Add random Players ' , ' Add random color Players ' ] ,
2025-07-26 17:56:54 +02:00
[ ' Get Players ' , ' Get Alive Players ' , ' Get Death Players ' , ' Get Ranking Players ' , ] ,
2025-07-26 11:00:20 +02:00
[ ' 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
2025-07-26 19:04:17 +02:00
_log . log_debug ( f ' { chat_id } : I \' m building the world \' s game... ' )
2025-07-26 18:07:14 +02:00
Arena = _arena . BrSimArena ( )
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 18:28:46 +02:00
async def update_bot ( update , context ) :
update . message . reply_text ( ' Sto aggiornando il Bot... ' )
chat_id = update . effective_chat . id
2025-07-26 19:04:17 +02:00
if update . message . chat . id not in _botsyms . SUPER_USERS : return _log . log_warning ( f ' update_bot: user { chat_id } not allowed ' )
_log . log_debug ( f ' update_bot: { chat_id } bot is updating... ' )
err = _os . system ( f ' cd { _botsyms . BOT_PATH } ; git pull ' )
if err :
_log . log_error ( f ' update_bot: { chat_id } error { err } while trying to update the app ' )
return await update . message . reply_text ( ' Errore durante l \' aggiornamento del Bot ' )
err = _os . system ( ' systemctl --user start battle_royale.service ' )
if err :
_log . log_error ( f ' update_bot: { chat_id } error { err } while trying to upstart the app ' )
return await update . message . reply_text ( ' Errore durante il riavvio del Bot ' )
_log . log_debug ( f ' update_bot: { chat_id } bot successfully updated ' )
2025-07-26 18:38:40 +02:00
await update . message . reply_text ( ' Bot aggiornato e riavviato! ' )
2025-07-26 18:28:46 +02:00
2025-07-26 11:00:20 +02:00
async def bot_commands ( update , context ) :
text = update . message . text
chat_id = update . effective_chat . id
2025-07-26 18:28:46 +02:00
if text == ' upstart ' : return await update_bot ( update , context )
2025-07-26 11:00:20 +02:00
if text == ' Init/Restart ' :
2025-07-26 19:04:17 +02:00
_log . log_info ( f ' bot_command: { chat_id } Init/Restart ' )
2025-07-26 11:00:20 +02:00
return await bot_start ( update , context )
if text == ' Add Player ' :
2025-07-26 19:04:17 +02:00
_log . log_info ( f ' bot_command: { chat_id } Add Player ' )
2025-07-26 11:00:20 +02:00
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 ' ] )
2025-07-26 12:38:06 +02:00
return await update . message . reply_text ( ' Inserisci il Nome del giocatore (o piu \' nomi separati da virgola) ' )
2025-07-26 11:00:20 +02:00
if text == ' Get Players ' :
2025-07-26 19:04:17 +02:00
_log . log_info ( f ' bot_command: { chat_id } Get Players ' )
2025-07-26 11:00:20 +02:00
return await _bot_player . get_players ( update , context )
if text == ' Get Alive Players ' :
2025-07-26 19:04:17 +02:00
_log . log_info ( f ' bot_command: { chat_id } Get Alive Players ' )
2025-07-26 11:00:20 +02:00
return await _bot_player . get_alive_players ( update , context )
if text == ' Get Death Players ' :
2025-07-26 19:04:17 +02:00
_log . log_info ( f ' bot_command: { chat_id } Get Death Players ' )
2025-07-26 11:00:20 +02:00
return await _bot_player . get_death_players ( update , context )
2025-07-26 17:56:54 +02:00
if text == ' Get Ranking Players ' :
2025-07-26 19:04:17 +02:00
_log . log_info ( f ' bot_command: { chat_id } Get Ranking Players ' )
2025-07-26 17:56:54 +02:00
return await _bot_player . get_ranking_players ( update , context )
2025-07-26 11:00:20 +02:00
if text == ' Simulate Day ' :
2025-07-26 19:04:17 +02:00
_log . log_info ( f ' bot_command: { chat_id } Simulate Day ' )
2025-07-26 14:44:16 +02:00
return await _bot_sim . simulate_day ( context , chat_id )
2025-07-26 11:00:20 +02:00
if text == ' Run Periodically ' :
2025-07-26 19:04:17 +02:00
_log . log_info ( 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
2025-07-26 15:20:03 +02:00
waiting_for_name = context . application . bot_data . get ( ' ask_name ' )
if waiting_for_name or text in [ ' Add random Players ' , ' Add random color Players ' ] :
2025-07-26 19:04:17 +02:00
_log . log_debug ( f ' bot_command: { chat_id } Collected Player Name { text } ' )
2025-07-26 15:20:03 +02:00
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 ' ]
2025-07-26 15:53:26 +02:00
players = [ p . get_name_and_stats ( ) for p in Arena . get_players ( ) ]
2025-07-26 15:20:03 +02:00
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 } ' )
2025-07-26 11:15:44 +02:00
waiting_for_seconds = context . application . bot_data . get ( ' ask_seconds ' )
if waiting_for_seconds :
2025-07-26 19:04:17 +02:00
_log . log_debug ( f ' bot_command: { chat_id } User Wants to auto-run the game every { text } seconds ' )
2025-07-26 11:15:44 +02:00
try : text = int ( text )
except : return
seconds = max ( 1 , text )
2025-07-26 14:44:16 +02:00
return await _bot_repeat . start_loop_game ( update , context , seconds )
2025-07-26 12:38:06 +02:00
2025-07-26 19:04:17 +02:00
_log . log_debug ( f ' bot_command: { chat_id } sent this text: { text } ' )
2025-07-26 11:00:20 +02:00
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 ( MessageHandler ( filters . TEXT & ~ filters . COMMAND , bot_commands ) )
2025-07-23 21:17:09 +02:00
2025-07-26 19:04:17 +02:00
_log . log_debug ( ' Bot is running... ' )
2025-07-23 21:17:09 +02:00
application . run_polling ( )
if __name__ == ' __main__ ' :
main ( )