1
0
Files
battle_royale_sim/bot.py

97 lines
3.6 KiB
Python

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
import main as _brsim
from bot_libs import player_handling as _bot_player
from bot_libs import simulation as _bot_sim
from bot_libs import repeating as _bot_repeat
from bot_libs import syms as _botsyms
async def bot_start(update, context):
await update.message.reply_text(_botsyms.START_MSG)
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)
chat_id = update.effective_chat.id
print(f'{chat_id}: I\'m building the world\'s game...')
Arena= _brsim.init_arena()
await update.message.reply_text('Ho creato il mondo di gioco', reply_markup=reply_markup)
context.application.bot_data['arena'] = Arena
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
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 (o piu\' nomi separati da virgola)')
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 _bot_sim.simulate_day(context, chat_id)
if text == 'Run Periodically':
print(f'bot_command: {chat_id} Run Periodically')
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)')
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 _bot_repeat.start_loop_game(update, context, seconds)
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'])
players= text.split(',')
for player in players:
await _bot_player.add_player(update, context, player.strip())
return
print(f'bot_command: {chat_id} sent this text: {text}')
await update.message.reply_text(_botsyms.WIP_MSG)
def main():
application = Application.builder().token(_botsyms.TOKEN).build()
application.add_handler(CommandHandler('start', bot_start))
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, bot_commands))
print('Bot is running...')
application.run_polling()
if __name__ == '__main__':
main()