forked from Cryz/battle_royale_sim
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
|
from telegram.ext import Application, CommandHandler, MessageHandler, filters
|
||||||
import bot_syms as _botsyms
|
import bot_syms as _botsyms
|
||||||
import main as _brsim
|
import main as _brsim
|
||||||
|
import debug as _dbg
|
||||||
|
|
||||||
async def loop_game(context):
|
async def loop_game(context):
|
||||||
chat_id = context.job.chat_id
|
chat_id = context.job.chat_id
|
||||||
if 'arena' in context.application.bot_data:
|
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')
|
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:
|
else:
|
||||||
print('Arena non trovata')
|
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):
|
async def bot_start(update, context):
|
||||||
await update.message.reply_text(_botsyms.START_MSG)
|
await update.message.reply_text(_botsyms.START_MSG)
|
||||||
@@ -20,45 +25,71 @@ async def bot_start(update, context):
|
|||||||
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}: Sto costruendo il mondo di gioco...')
|
||||||
Arena= _brsim.init_arena()
|
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('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'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'Queste le armi che avranno a disposizione nell\'arena:\n{weapons}')
|
||||||
context.application.bot_data['arena'] = Arena
|
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):
|
async def echo(update, context):
|
||||||
testo_ricevuto = update.message.text
|
testo_ricevuto = update.message.text
|
||||||
await update.message.reply_text(_botsyms.WIP_MSG)
|
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):
|
async def add_player(update, context):
|
||||||
name= " ".join(context.args)
|
name= " ".join(context.args)
|
||||||
print(f'sto aggiungendo il giocatore {name} all\'arena')
|
print(f'sto aggiungendo il giocatore {name} all\'arena')
|
||||||
_brsim.BrSimArena
|
|
||||||
Arena= context.application.bot_data['arena']
|
Arena= context.application.bot_data['arena']
|
||||||
Arena.add_player(name)
|
Arena.add_player(name)
|
||||||
print(f'Giocatori: {Arena.get_players()}')
|
print(f'Giocatori: {Arena.get_players()}')
|
||||||
await update.message.reply_text(f'Ecco i giocatori presenti nel mondo do gioco: \n{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():
|
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('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))
|
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo))
|
||||||
|
|
||||||
print('Bot in esecuzione...')
|
print('Bot in esecuzione...')
|
||||||
|
|||||||
26
debug.py
26
debug.py
@@ -3,7 +3,7 @@ import random as _rand
|
|||||||
import main as _main
|
import main as _main
|
||||||
from entities import weapon_syms as _wsyms
|
from entities import weapon_syms as _wsyms
|
||||||
|
|
||||||
def init_debug():
|
def _debug_data():
|
||||||
players= [
|
players= [
|
||||||
{
|
{
|
||||||
'name': 'Elara',
|
'name': 'Elara',
|
||||||
@@ -24,14 +24,32 @@ def init_debug():
|
|||||||
'name': 'Seraphina',
|
'name': 'Seraphina',
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
#w= _wsyms.KNIFE
|
|
||||||
##weapons= [{_wsyms.WEAPONS[w]['name' ]: 1}]
|
|
||||||
#weapons= [{w: 1}]
|
|
||||||
weapons= []
|
weapons= []
|
||||||
|
|
||||||
Arena= _main.init_arena(players, weapons)
|
Arena= _main.init_arena(players, weapons)
|
||||||
print(f'Players: {Arena.get_players()}')
|
print(f'Players: {Arena.get_players()}')
|
||||||
print(f'Weapons: {Arena.get_weapons()}')
|
print(f'Weapons: {Arena.get_weapons()}')
|
||||||
|
return Arena
|
||||||
|
|
||||||
|
def _end_game_debug(alive_players, day):
|
||||||
|
last_player= alive_players[0]
|
||||||
|
print(f'{last_player.get_name()} sopravvive e vince dopo {day} lunghi Giorni, conquistando l\'amore eterno di Guarino')
|
||||||
|
|
||||||
|
def play_one_day_debug(Arena):
|
||||||
|
alive_players= Arena.get_alive_players()
|
||||||
|
if not Arena.get_players(): return
|
||||||
|
if len(alive_players) == 1:
|
||||||
|
day= Arena.day
|
||||||
|
return _end_game_debug(alive_players, day)
|
||||||
|
|
||||||
|
p_one, p_two= _rand.sample(alive_players, 2)
|
||||||
|
_dmg, msg= p_one.attack(p_two)
|
||||||
|
Arena.next_day()
|
||||||
|
print(f'Giorno #{Arena.day}')
|
||||||
|
return msg
|
||||||
|
|
||||||
|
def init_debug_loop():
|
||||||
|
Arena= _debug_data()
|
||||||
while (len(Arena.get_alive_players()) > 1):
|
while (len(Arena.get_alive_players()) > 1):
|
||||||
alive_players= Arena.get_alive_players()
|
alive_players= Arena.get_alive_players()
|
||||||
|
|
||||||
|
|||||||
@@ -8,11 +8,21 @@ class BrSimArena():
|
|||||||
# players = [{'name': name, 'inventory': default_inventory, other_stats}]
|
# players = [{'name': name, 'inventory': default_inventory, other_stats}]
|
||||||
# weapons = [{WEAPON.KNIFE: quantity}, etc...] # this is the whole quantity of the items available on the world
|
# weapons = [{WEAPON.KNIFE: quantity}, etc...] # this is the whole quantity of the items available on the world
|
||||||
|
|
||||||
def __init__(self, players, weapons):
|
def __init__(self, players= None, weapons= None):
|
||||||
self.day= 1
|
self.day= 1
|
||||||
self.players= [_player.BrSimPlayer(p['name'], p.get('inventory')) for p in players]
|
self.players= []
|
||||||
self.weapons= []
|
self.weapons= []
|
||||||
self.eventClass = _events.ArenaEventPicker(self.players)
|
self.eventClass = _events.ArenaEventPicker(self.players)
|
||||||
|
self.init_players(players)
|
||||||
|
self.init_weapons(weapons)
|
||||||
|
|
||||||
|
def init_players(self, players):
|
||||||
|
if not players: return
|
||||||
|
for player in players:
|
||||||
|
self.add_player(player['name'], player.get('inventory'))
|
||||||
|
|
||||||
|
def init_weapons(self, weapons):
|
||||||
|
if not weapons: return
|
||||||
for weapon in weapons:
|
for weapon in weapons:
|
||||||
for wtype, quantity in weapon.items():
|
for wtype, quantity in weapon.items():
|
||||||
for i in range(quantity): self.weapons.append(_weapons.BrSimWeapon(wtype))
|
for i in range(quantity): self.weapons.append(_weapons.BrSimWeapon(wtype))
|
||||||
@@ -54,6 +64,10 @@ class BrSimArena():
|
|||||||
player= _player.BrSimPlayer(name, inventory)
|
player= _player.BrSimPlayer(name, inventory)
|
||||||
self.players.append(player)
|
self.players.append(player)
|
||||||
|
|
||||||
|
def add_weapon(self, weapon_type):
|
||||||
|
weapon= _weapons.BrSimWeapon(weapon_type)
|
||||||
|
self.weapons.append(weapon)
|
||||||
|
|
||||||
def get_players(self):
|
def get_players(self):
|
||||||
res= []
|
res= []
|
||||||
for p in self.players:
|
for p in self.players:
|
||||||
|
|||||||
@@ -101,15 +101,15 @@ class BrSimPlayer():
|
|||||||
self._equip_weapon()
|
self._equip_weapon()
|
||||||
if target.dodge():
|
if target.dodge():
|
||||||
if target.get_gender() == 'm':
|
if target.get_gender() == 'm':
|
||||||
print(f'Ehhhh voleviiii!!! sei lentoo! {target.get_name()} schiva il colpo di {self.get_name()}')
|
msg= f'Ehhhh voleviiii!!! sei lentoo! {target.get_name()} schiva il colpo di {self.get_name()}'
|
||||||
elif target.get_gender() == 'f':
|
elif target.get_gender() == 'f':
|
||||||
print(f'Ehhhh voleviiii!!! sei lentaa! {target.get_name()} schiva il colpo di {self.get_name()}')
|
msg= f'Ehhhh voleviiii!!! sei lentaa! {target.get_name()} schiva il colpo di {self.get_name()}'
|
||||||
else:
|
else:
|
||||||
print(f'Ehhhh voleviiii!!! sei lent##! {target.get_name()} schiva il colpo di {self.get_name()}')
|
msg= f'Ehhhh voleviiii!!! sei lent##! {target.get_name()} schiva il colpo di {self.get_name()}'
|
||||||
return 0
|
return 0, message
|
||||||
target.accuses_damage(self.damage)
|
target.accuses_damage(self.damage)
|
||||||
print(f'{self.get_name()} Colpisce {target.get_name()} in nome di Guarino')
|
msg= f'{self.get_name()} Colpisce {target.get_name()} in nome di Guarino'
|
||||||
return self.damage
|
return self.damage, msg
|
||||||
|
|
||||||
def get_item(self, item):
|
def get_item(self, item):
|
||||||
if self.get_inventory_weight() + item.get_weight() >= self.get_max_weight():
|
if self.get_inventory_weight() + item.get_weight() >= self.get_max_weight():
|
||||||
|
|||||||
2
main.py
2
main.py
@@ -2,7 +2,7 @@ import random as _random
|
|||||||
from entities import weapon_syms as _wsyms
|
from entities import weapon_syms as _wsyms
|
||||||
from entities import arena as _arena
|
from entities import arena as _arena
|
||||||
|
|
||||||
def init_arena(players, weapons):
|
def init_arena(players= None, weapons= None):
|
||||||
return _arena.BrSimArena(players, weapons)
|
return _arena.BrSimArena(players, weapons)
|
||||||
|
|
||||||
def run_events(Arena):
|
def run_events(Arena):
|
||||||
|
|||||||
Reference in New Issue
Block a user