forked from Cryz/battle_royale_sim
Compare commits
14 Commits
82be14e603
...
c5a6b886d6
| Author | SHA256 | Date | |
|---|---|---|---|
|
|
c5a6b886d6 | ||
|
|
a7c6dd25df | ||
|
|
888f22ac1b | ||
|
|
0a6deafd32 | ||
|
|
100bb4fe53 | ||
|
|
f360fe4200 | ||
|
|
0df960ea22 | ||
|
|
634c651cb5 | ||
|
|
07dd5611e5 | ||
|
|
09c892a786 | ||
|
|
3d2a0bcc70 | ||
|
|
c10872a47e | ||
|
|
e8f2dbbc29 | ||
|
|
62ac0e83a5 |
155
bot.py
155
bot.py
@@ -1,65 +1,140 @@
|
||||
import asyncio
|
||||
import datetime
|
||||
import pytz
|
||||
from telegram.ext import Application, CommandHandler, MessageHandler, filters
|
||||
import bot_syms as _botsyms
|
||||
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
|
||||
import debug as _dbg
|
||||
from bot_libs import player_handling as _bot_player
|
||||
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
|
||||
if 'arena' in context.application.bot_data:
|
||||
print(f'{chat_id}: Guarino ha trovato l\'arena')
|
||||
pass
|
||||
else:
|
||||
print('Arena non trovata')
|
||||
await update.message.reply_text('Che e\' successo? un Guarino ha rubato l\'arena, avvia una nuova partita con /start')
|
||||
print(f'loop_game: run on {chat_id}')
|
||||
return await simulate_day(context, chat_id)
|
||||
|
||||
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}: Sto costruendo il mondo di gioco...')
|
||||
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(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('Ho creato il mondo di gioco', reply_markup=reply_markup)
|
||||
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)
|
||||
)
|
||||
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 echo(update, context):
|
||||
testo_ricevuto = update.message.text
|
||||
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 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)')
|
||||
#return await simulate_day(context, chat_id)
|
||||
|
||||
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 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'{chat_id} ha inviato questo testo: {text}')
|
||||
await update.message.reply_text(_botsyms.WIP_MSG)
|
||||
|
||||
async def add_player(update, context):
|
||||
name= " ".join(context.args)
|
||||
print(f'sto aggiungendo il giocatore {name} all\'arena')
|
||||
_brsim.BrSimArena
|
||||
Arena= context.application.bot_data['arena']
|
||||
Arena.add_player(name)
|
||||
print(f'Giocatori: {Arena.get_players()}')
|
||||
await update.message.reply_text(f'Ecco i giocatori presenti nel mondo do gioco: \n{Arena.get_players()}')
|
||||
|
||||
|
||||
def main():
|
||||
application = Application.builder().token(_botsyms.TOKEN).build()
|
||||
|
||||
application.add_handler(CommandHandler('start', bot_start))
|
||||
application.add_handler(CommandHandler('addplayer', add_player))
|
||||
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo))
|
||||
#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))
|
||||
|
||||
print('Bot in esecuzione...')
|
||||
application.run_polling()
|
||||
|
||||
29
bot_libs/player_handling.py
Normal file
29
bot_libs/player_handling.py
Normal file
@@ -0,0 +1,29 @@
|
||||
async def add_player(update, context, name):
|
||||
#name= " ".join(context.args)
|
||||
print(f'add_player: {name}')
|
||||
Arena= context.application.bot_data['arena']
|
||||
Arena.add_player(name)
|
||||
players= [p.get_name() for p in Arena.get_players()]
|
||||
players_str= '\n'.join(players)
|
||||
await update.message.reply_text(f'Ecco i giocatori presenti nel mondo do gioco: \n{players_str}')
|
||||
|
||||
async def get_players(update, context):
|
||||
Arena= context.application.bot_data['arena']
|
||||
print(f'get_players: {Arena.get_players()}')
|
||||
players= [p.get_name() for p in Arena.get_players()]
|
||||
players_str= '\n'.join(players)
|
||||
await update.message.reply_text(f'Ecco i giocatori presenti nel mondo do gioco: \n{players_str}')
|
||||
|
||||
async def get_alive_players(update, context):
|
||||
Arena= context.application.bot_data['arena']
|
||||
print(f'get_alive_players: {Arena.get_alive_players()}')
|
||||
alive= [a.get_name() for a in Arena.get_alive_players()]
|
||||
alive_str= '\n'.join(alive)
|
||||
await update.message.reply_text(f'Ecco i giocatori ancora vivi: \n{alive_str}')
|
||||
|
||||
async def get_death_players(update, context):
|
||||
Arena= context.application.bot_data['arena']
|
||||
print(f'get_death_players: {Arena.get_death_players()}')
|
||||
death= [d.get_name() for d in Arena.get_death_players()]
|
||||
death_str= '\n'.join(death)
|
||||
await update.message.reply_text(f'Ecco i giocatori morti x.x: \n{death_str}')
|
||||
28
debug.py
28
debug.py
@@ -3,7 +3,7 @@ import random as _rand
|
||||
import main as _main
|
||||
from entities import weapon_syms as _wsyms
|
||||
|
||||
def init_debug():
|
||||
def _debug_data():
|
||||
players= [
|
||||
{
|
||||
'name': 'Elara',
|
||||
@@ -24,14 +24,34 @@ def init_debug():
|
||||
'name': 'Seraphina',
|
||||
},
|
||||
]
|
||||
#w= _wsyms.KNIFE
|
||||
##weapons= [{_wsyms.WEAPONS[w]['name' ]: 1}]
|
||||
#weapons= [{w: 1}]
|
||||
weapons= []
|
||||
|
||||
Arena= _main.init_arena(players, weapons)
|
||||
print(f'Players: {Arena.get_players()}')
|
||||
print(f'Weapons: {Arena.get_weapons()}')
|
||||
return Arena
|
||||
|
||||
def _end_game_debug(alive_players, day):
|
||||
last_player= alive_players[0]
|
||||
msg= f'{last_player.get_name()} sopravvive e vince dopo {day} lunghi Giorni, conquistando l\'amore eterno di Guarino'
|
||||
print(msg)
|
||||
return msg
|
||||
|
||||
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):
|
||||
alive_players= Arena.get_alive_players()
|
||||
|
||||
|
||||
@@ -8,11 +8,21 @@ class BrSimArena():
|
||||
# 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
|
||||
|
||||
def __init__(self, players, weapons):
|
||||
def __init__(self, players= None, weapons= None):
|
||||
self.day= 1
|
||||
self.players= [_player.BrSimPlayer(p['name'], p.get('inventory')) for p in players]
|
||||
self.players= []
|
||||
self.weapons= []
|
||||
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 wtype, quantity in weapon.items():
|
||||
for i in range(quantity): self.weapons.append(_weapons.BrSimWeapon(wtype))
|
||||
@@ -54,11 +64,12 @@ class BrSimArena():
|
||||
player= _player.BrSimPlayer(name, inventory)
|
||||
self.players.append(player)
|
||||
|
||||
def add_weapon(self, weapon_type):
|
||||
weapon= _weapons.BrSimWeapon(weapon_type)
|
||||
self.weapons.append(weapon)
|
||||
|
||||
def get_players(self):
|
||||
res= []
|
||||
for p in self.players:
|
||||
res.append(p.get_data())
|
||||
return res
|
||||
return self.players
|
||||
|
||||
def get_weapons(self):
|
||||
res= []
|
||||
|
||||
@@ -101,15 +101,15 @@ class BrSimPlayer():
|
||||
self._equip_weapon()
|
||||
if target.dodge():
|
||||
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':
|
||||
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:
|
||||
print(f'Ehhhh voleviiii!!! sei lent##! {target.get_name()} schiva il colpo di {self.get_name()}')
|
||||
return 0
|
||||
msg= f'Ehhhh voleviiii!!! sei lent##! {target.get_name()} schiva il colpo di {self.get_name()}'
|
||||
return 0, msg
|
||||
target.accuses_damage(self.damage)
|
||||
print(f'{self.get_name()} Colpisce {target.get_name()} in nome di Guarino')
|
||||
return self.damage
|
||||
msg= f'{self.get_name()} Colpisce {target.get_name()} in nome di Guarino'
|
||||
return self.damage, msg
|
||||
|
||||
def get_item(self, item):
|
||||
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 arena as _arena
|
||||
|
||||
def init_arena(players, weapons):
|
||||
def init_arena(players= None, weapons= None):
|
||||
return _arena.BrSimArena(players, weapons)
|
||||
|
||||
def run_events(Arena):
|
||||
|
||||
@@ -1,2 +1 @@
|
||||
python-telegram-bot[job-queue]==22.3
|
||||
pytz==2025.2
|
||||
|
||||
Reference in New Issue
Block a user