telegram bot more commands, and use the debug function to start a first day iteration
This commit is contained in:
71
bot.py
71
bot.py
@@ -4,15 +4,20 @@ import pytz
|
||||
from telegram.ext import Application, CommandHandler, MessageHandler, filters
|
||||
import bot_syms as _botsyms
|
||||
import main as _brsim
|
||||
import debug as _dbg
|
||||
|
||||
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')
|
||||
pass
|
||||
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 update.message.reply_text('Che e\' successo? un Guarino ha rubato l\'arena, avvia una nuova partita con /start')
|
||||
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)
|
||||
@@ -20,45 +25,71 @@ async def bot_start(update, context):
|
||||
chat_id = update.effective_chat.id
|
||||
print(f'{chat_id}: Sto costruendo il mondo di gioco...')
|
||||
Arena= _brsim.init_arena()
|
||||
players= Arena.get_players()
|
||||
weapons= Arena.get_weapons()
|
||||
print(f'Ecco il mondo di gioco, questi sono i giocatori: {players}')
|
||||
print(f'Ecco le armi disponibili nel mondo: {weapons}')
|
||||
|
||||
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}')
|
||||
#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
|
||||
|
||||
#context.job_queue.run_repeating(loop_game, interval=10, first=0, chat_id= chat_id)
|
||||
timezone = pytz.timezone('Europe/Rome')
|
||||
context.job_queue.run_daily(
|
||||
loop_game,
|
||||
time=datetime.time(hour=13, minute=0, second=0, tzinfo= timezone),
|
||||
chat_id=chat_id,
|
||||
name=str(chat_id)
|
||||
)
|
||||
print(f'Job giornaliero creato per la chat {chat_id}')
|
||||
|
||||
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=0, 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}')
|
||||
|
||||
async def add_player(update, context):
|
||||
name= " ".join(context.args)
|
||||
print(f'sto aggiungendo il giocatore {name} all\'arena')
|
||||
_brsim.BrSimArena
|
||||
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('addplayer', add_player))
|
||||
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))
|
||||
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo))
|
||||
|
||||
print('Bot in esecuzione...')
|
||||
|
||||
Reference in New Issue
Block a user