forked from Cryz/battle_royale_sim
split main bot lib into multiple smaller libs and improve end game message
This commit is contained in:
63
bot.py
63
bot.py
@@ -4,34 +4,13 @@ from telegram.ext import MessageHandler
|
|||||||
from telegram.ext import filters
|
from telegram.ext import filters
|
||||||
from telegram import ReplyKeyboardMarkup
|
from telegram import ReplyKeyboardMarkup
|
||||||
from telegram import ReplyKeyboardRemove
|
from telegram import ReplyKeyboardRemove
|
||||||
|
|
||||||
import main as _brsim
|
import main as _brsim
|
||||||
import debug as _dbg
|
|
||||||
from bot_libs import player_handling as _bot_player
|
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
|
from bot_libs import syms as _botsyms
|
||||||
|
|
||||||
async def simulate_day(context, chat_id):
|
|
||||||
if 'arena' in context.application.bot_data:
|
|
||||||
Arena= context.application.bot_data['arena']
|
|
||||||
if len(Arena.get_alive_players()) == 1:
|
|
||||||
winner= Arena.get_alive_players()[0]
|
|
||||||
try:
|
|
||||||
context.job.schedule_removal()
|
|
||||||
print(f'simulate_day: Loop removed')
|
|
||||||
except: pass
|
|
||||||
msg= f'{winner.get_name()} Vince la cruenta battaglia, e vive felice e contento con Guarino'
|
|
||||||
return await context.bot.send_message(chat_id, msg)
|
|
||||||
print(f'{chat_id}: Guarino ha trovato l\'arena')
|
|
||||||
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 context.bot.send_message(chat_id, 'Che e\' successo? un Guarino ha rubato l\'arena, avvia una nuova partita con /start')
|
|
||||||
|
|
||||||
async def loop_game(context):
|
|
||||||
chat_id = context.job.chat_id
|
|
||||||
print(f'loop_game: run on {chat_id}')
|
|
||||||
return await simulate_day(context, chat_id)
|
|
||||||
|
|
||||||
async def bot_start(update, context):
|
async def bot_start(update, context):
|
||||||
await update.message.reply_text(_botsyms.START_MSG)
|
await update.message.reply_text(_botsyms.START_MSG)
|
||||||
@@ -45,29 +24,12 @@ async def bot_start(update, context):
|
|||||||
reply_markup= ReplyKeyboardMarkup(keyboard, one_time_keyboard=False, resize_keyboard=True)
|
reply_markup= ReplyKeyboardMarkup(keyboard, one_time_keyboard=False, resize_keyboard=True)
|
||||||
|
|
||||||
chat_id = update.effective_chat.id
|
chat_id = update.effective_chat.id
|
||||||
print(f'{chat_id}: Sto costruendo il mondo di gioco...')
|
print(f'{chat_id}: I\'m building the world\'s game...')
|
||||||
Arena= _brsim.init_arena()
|
Arena= _brsim.init_arena()
|
||||||
|
|
||||||
await update.message.reply_text('Ho creato il mondo di gioco', reply_markup=reply_markup)
|
await update.message.reply_text('Ho creato il mondo di gioco', reply_markup=reply_markup)
|
||||||
context.application.bot_data['arena'] = Arena
|
context.application.bot_data['arena'] = Arena
|
||||||
|
|
||||||
async def start_loop_game(update, context, seconds):
|
|
||||||
await update.message.reply_text(f'Ok capo!! giochero\' per te ogni {seconds}s')
|
|
||||||
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= seconds, first=1, chat_id= chat_id)
|
|
||||||
print(f'Job giornaliero creato per la chat {chat_id}')
|
|
||||||
|
|
||||||
async def bot_commands(update, context):
|
async def bot_commands(update, context):
|
||||||
text= update.message.text
|
text= update.message.text
|
||||||
chat_id = update.effective_chat.id
|
chat_id = update.effective_chat.id
|
||||||
@@ -93,14 +55,13 @@ async def bot_commands(update, context):
|
|||||||
return await _bot_player.get_death_players(update, context)
|
return await _bot_player.get_death_players(update, context)
|
||||||
if text == 'Simulate Day':
|
if text == 'Simulate Day':
|
||||||
print(f'bot_command: {chat_id} Simulate Day')
|
print(f'bot_command: {chat_id} Simulate Day')
|
||||||
return await simulate_day(context, chat_id)
|
return await _bot_sim.simulate_day(context, chat_id)
|
||||||
if text == 'Run Periodically':
|
if text == 'Run Periodically':
|
||||||
print(f'bot_command: {chat_id} Run Periodically')
|
print(f'bot_command: {chat_id} Run Periodically')
|
||||||
context.application.bot_data['ask_seconds'] = 1
|
context.application.bot_data['ask_seconds'] = 1
|
||||||
if 'ask_name' in context.application.bot_data:
|
if 'ask_name' in context.application.bot_data:
|
||||||
del(context.application.bot_data['ask_name'])
|
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)')
|
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)')
|
||||||
#return await simulate_day(context, chat_id)
|
|
||||||
|
|
||||||
waiting_for_seconds= context.application.bot_data.get('ask_seconds')
|
waiting_for_seconds= context.application.bot_data.get('ask_seconds')
|
||||||
if waiting_for_seconds:
|
if waiting_for_seconds:
|
||||||
@@ -108,7 +69,7 @@ async def bot_commands(update, context):
|
|||||||
try: text= int(text)
|
try: text= int(text)
|
||||||
except: return
|
except: return
|
||||||
seconds= max(1, text)
|
seconds= max(1, text)
|
||||||
return await start_loop_game(update, context, seconds)
|
return await _bot_repeat.start_loop_game(update, context, seconds)
|
||||||
|
|
||||||
waiting_for_name= context.application.bot_data.get('ask_name')
|
waiting_for_name= context.application.bot_data.get('ask_name')
|
||||||
if waiting_for_name:
|
if waiting_for_name:
|
||||||
@@ -119,24 +80,16 @@ async def bot_commands(update, context):
|
|||||||
await _bot_player.add_player(update, context, player.strip())
|
await _bot_player.add_player(update, context, player.strip())
|
||||||
return
|
return
|
||||||
|
|
||||||
print(f'{chat_id} ha inviato questo testo: {text}')
|
print(f'bot_command: {chat_id} sent this text: {text}')
|
||||||
await update.message.reply_text(_botsyms.WIP_MSG)
|
await update.message.reply_text(_botsyms.WIP_MSG)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
application = Application.builder().token(_botsyms.TOKEN).build()
|
application = Application.builder().token(_botsyms.TOKEN).build()
|
||||||
|
|
||||||
application.add_handler(CommandHandler('start', bot_start))
|
application.add_handler(CommandHandler('start', bot_start))
|
||||||
#application.add_handler(CommandHandler('start_game', start_loop_game))
|
|
||||||
|
|
||||||
# player handler
|
|
||||||
#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))
|
|
||||||
|
|
||||||
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, bot_commands))
|
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, bot_commands))
|
||||||
|
|
||||||
print('Bot in esecuzione...')
|
print('Bot is running...')
|
||||||
application.run_polling()
|
application.run_polling()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
23
bot_libs/repeating.py
Normal file
23
bot_libs/repeating.py
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
from bot_libs import simulation as _bot_sim
|
||||||
|
|
||||||
|
async def _loop_game(context):
|
||||||
|
chat_id = context.job.chat_id
|
||||||
|
print(f'_loop_game: run on {chat_id}')
|
||||||
|
return await _bot_sim.simulate_day(context, chat_id)
|
||||||
|
|
||||||
|
async def start_loop_game(update, context, seconds):
|
||||||
|
await update.message.reply_text(f'Ok capo!! giochero\' per te ogni {seconds}s')
|
||||||
|
chat_id = update.effective_chat.id
|
||||||
|
if 'arena' not in context.application.bot_data:
|
||||||
|
print(f'start_loop_game: {chat_id} Arena not found')
|
||||||
|
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'start_loop_game: {chat_id} Not enough player to start the match')
|
||||||
|
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= seconds, first=1, chat_id= chat_id)
|
||||||
|
print(f'start_loop_game: Repeating Job created for: {chat_id}')
|
||||||
32
bot_libs/simulation.py
Normal file
32
bot_libs/simulation.py
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
import debug as _dbg
|
||||||
|
|
||||||
|
def get_winner(Arena):
|
||||||
|
winner= Arena.get_alive_players()[0]
|
||||||
|
try:
|
||||||
|
context.job.schedule_removal()
|
||||||
|
print(f'simulate_day: Loop removed')
|
||||||
|
except: pass
|
||||||
|
day= Arena.day
|
||||||
|
if winner.player_gender_is_male():
|
||||||
|
msg= f'Giorno #{day}: {winner.get_name()} Vince la cruenta battaglia, e vive felice e contento con Guarino'
|
||||||
|
elif winner.player_gender_is_female():
|
||||||
|
msg= f'Giorno #{day}: {winner.get_name()} Vince la cruenta battaglia, e vive felice e contenta con Guarino'
|
||||||
|
else:
|
||||||
|
msg= f'Giorno #{day}: {winner.get_name()} Vince la cruenta battaglia, e vive felice e content# con Guarino'
|
||||||
|
return msg
|
||||||
|
|
||||||
|
async def simulate_day(context, chat_id):
|
||||||
|
if 'arena' not in context.application.bot_data:
|
||||||
|
print('Arena not Found')
|
||||||
|
await context.bot.send_message(chat_id, 'Che e\' successo? un Guarino ha rubato l\'arena, avvia una nuova partita con /start')
|
||||||
|
return
|
||||||
|
|
||||||
|
Arena= context.application.bot_data['arena']
|
||||||
|
if len(Arena.get_alive_players()) <= 1: return await context.bot.send_message(chat_id, 'Il gioco e\' finito, Grazie per aver giocato!')
|
||||||
|
|
||||||
|
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)
|
||||||
|
if len(Arena.get_alive_players()) == 1:
|
||||||
|
msg= get_winner(Arena)
|
||||||
|
return await context.bot.send_message(chat_id, msg)
|
||||||
@@ -28,6 +28,15 @@ class BrSimPlayer():
|
|||||||
def get_gender(self):
|
def get_gender(self):
|
||||||
return self.gender
|
return self.gender
|
||||||
|
|
||||||
|
def player_gender_is_male(self):
|
||||||
|
return self.gender == 'm'
|
||||||
|
|
||||||
|
def player_gender_is_female(self):
|
||||||
|
return self.gender == 'f'
|
||||||
|
|
||||||
|
def player_gender_is_not_binary(self):
|
||||||
|
return self.gender == '-'
|
||||||
|
|
||||||
def get_inventory(self):
|
def get_inventory(self):
|
||||||
return self.inventory
|
return self.inventory
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user