split main bot lib into multiple smaller libs and improve end game message
This commit is contained in:
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)
|
||||
Reference in New Issue
Block a user