Files
battle_royale_sim/bot.py
2025-11-15 18:40:34 +01:00

117 lines
4.2 KiB
Python

import random as _rand
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
from utils import logs as _log
from entities import arena as _arena
from entities.items import syms as _isyms
from bot_libs import special_commands as _scmd
from bot_libs import syms as _botsyms
from bot_libs import commands_handling as _cmd
def bot_get_random_items():
items= []
nitems= _rand.randint(3,5)
for n in range(nitems):
itype= _rand.sample(_isyms.ITEMS_LIST, 1)[0]
items.append(itype)
return items
async def bot_start(update, context):
await update.message.reply_text(_botsyms.START_MSG)
if 'ask_name' in context.application.bot_data:
del(context.application.bot_data['ask_name'])
if 'ask_seconds' in context.application.bot_data:
del(context.application.bot_data['ask_seconds'])
keyboard = [
['Init/Restart'],
['Add Player', 'Add random Players', 'Add random color Players'],
['Get Players', 'Get Alive Players', 'Get Death Players', 'Get Ranking Players',],
['Simulate Day', 'Run Periodically', 'Show Map UTF8', 'Show Map Image']
]
reply_markup= ReplyKeyboardMarkup(keyboard, one_time_keyboard=False, resize_keyboard=True)
chat_id = update.effective_chat.id
_log.log_debug(f'bot_start: {chat_id} - I\'m building the world\'s game...')
items= bot_get_random_items()
Arena= _arena.BrSimArena(items= items)
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
username= update.effective_chat.username
first_name= update.effective_chat.first_name
last_name= update.effective_chat.last_name
chat_type= update.effective_chat.type.name # PRIVAT|BOT|ETC..
_log.log_info(f'bot_command: \
user id="{chat_id}" \
username="{username}" \
first_name="{first_name}" \
last_name="{last_name}" \
chat_type="{chat_type}"')
# init or restart the game
if text == 'Init/Restart':
return await bot_start(update, context)
# player game commands
if text == 'Add Player':
return await _cmd.cmd_add_player(context, update, chat_id)
if text == 'Get Players':
return await _cmd.cmd_get_players(context, update, chat_id)
if text == 'Get Alive Players':
return await _cmd.cmd_get_alive_players(context, update, chat_id)
if text == 'Get Death Players':
return await _cmd.cmd_get_death_players(context, update, chat_id)
if text == 'Get Ranking Players':
return await _cmd.cmd_get_ranking_players(context, update, chat_id)
if text == 'Simulate Day':
return await _cmd.cmd_simulate_day(context, update, chat_id)
if text == 'Run Periodically':
return await _cmd.cmd_simulate_day_cron(context, update, chat_id)
if text == 'Add random Players':
return await _cmd.cmd_add_random_players(context, update, chat_id)
if text == 'Add random color Players':
return await _cmd.cmd_add_random_color_players(context, update, chat_id)
if text == 'Show Map UTF8':
return await _cmd.cmd_show_game_map_unicode(context, update, chat_id)
if text == 'Show Map Image':
return await _cmd.cmd_show_game_map_image(context, update, chat_id)
# special commands
if text == 'upstart': return await _scmd.update_bot(update, context)
if text == 'logs': return await _scmd.show_logs(update, context)
# get user input
if context.application.bot_data.get('ask_name'):
return await _cmd.cmd_get_player_name(context, update, chat_id, text)
if context.application.bot_data.get('ask_seconds'):
return await _cmd.cmd_get_cron_time(context, update, chat_id, text)
_log.log_debug(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))
_log.log_info('main: Bot is running...')
application.run_polling()
if __name__ == '__main__':
main()