From 31637dc845204efe106e78acc85981e549b29c9a03a007eab2600bb6cdc30d85 Mon Sep 17 00:00:00 2001 From: green Date: Sat, 26 Jul 2025 17:56:54 +0200 Subject: [PATCH] ranking as commands or at the end of each day --- bot.py | 5 +++- bot_libs/player_handling.py | 9 +++++++ bot_libs/simulation.py | 3 +++ entities/arena.py | 48 +++++++++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 1 deletion(-) diff --git a/bot.py b/bot.py index 501acea..6dfe273 100644 --- a/bot.py +++ b/bot.py @@ -18,7 +18,7 @@ async def bot_start(update, context): keyboard = [ ['Init/Restart'], ['Add Player', 'Add random Players', 'Add random color Players'], - ['Get Players', 'Get Alive Players', 'Get Death Players'], + ['Get Players', 'Get Alive Players', 'Get Death Players', 'Get Ranking Players',], ['Simulate Day', 'Run Periodically'] ] reply_markup= ReplyKeyboardMarkup(keyboard, one_time_keyboard=False, resize_keyboard=True) @@ -53,6 +53,9 @@ async def bot_commands(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 == 'Get Ranking Players': + print(f'bot_command: {chat_id} Get Ranking Players') + return await _bot_player.get_ranking_players(update, context) if text == 'Simulate Day': print(f'bot_command: {chat_id} Simulate Day') return await _bot_sim.simulate_day(context, chat_id) diff --git a/bot_libs/player_handling.py b/bot_libs/player_handling.py index 21e75fa..c05e7fa 100644 --- a/bot_libs/player_handling.py +++ b/bot_libs/player_handling.py @@ -39,3 +39,12 @@ async def get_death_players(update, context): death_str= '\n'.join(death) print(f'get_death_players: {death_str}') await update.message.reply_text(f'Ecco i giocatori morti x.x: \n{death_str}') + +async def get_ranking_players(update, context): + Arena = context.application.bot_data['arena'] + leaderboard_text = Arena.get_ranking() + print(f'{leaderboard_text}') + await update.message.reply_text( + f'{leaderboard_text}', + parse_mode='Markdown' + ) diff --git a/bot_libs/simulation.py b/bot_libs/simulation.py index 867bc8c..70bc136 100644 --- a/bot_libs/simulation.py +++ b/bot_libs/simulation.py @@ -27,6 +27,9 @@ async def simulate_day(context, chat_id): 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) + #Print the ranking each day + msg= Arena.get_ranking() + 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) diff --git a/entities/arena.py b/entities/arena.py index 649b8c4..9a74440 100644 --- a/entities/arena.py +++ b/entities/arena.py @@ -51,6 +51,54 @@ class BrSimArena(): res.append(p) return res + def sort_players_by_kills_and_health(self, players): + def player_sort_key(player): + kills= player.get_kills() + health= player.get_health() + #Negative values to sort in decr mode + return (-kills, -health) + return sorted(players, key=player_sort_key) + + def get_ranking(self): + medals = ['🥇', '🥈', '🥉'] + leaderboard = [] + leaderboard.append('🏆 *Classifica attuale:* \n') + alive_players= self.get_alive_players(); + alive_sorted= self.sort_players_by_kills_and_health(alive_players) + death_players= self.get_death_players() + death_sorted= self.sort_players_by_kills_and_health(death_players) + + # Alive Players + alive_players= self.get_alive_players(); + alive_sorted= self.sort_players_by_kills_and_health(alive_players) + for index, player in enumerate(alive_sorted): + name = player.get_name() + kills = player.get_kills() + health = '♥️' * player.get_health() + if index < 3: + position = medals[index] + else: + position = f"{index + 1}." + + line = f"{position} {name} - {kills} uccision{'i' if kills != 1 else 'e'}, {health}" + leaderboard.append(line) + + # Death players: + death_players= self.get_death_players() + death_sorted= self.sort_players_by_kills_and_health(death_players) + + if death_sorted: + leaderboard.append("\n-- GIOCATORI ELIMINATI --") + for player in death_sorted: + name = player.get_name() + kills = player.get_kills() + health = player.get_health() + + line = f"💀 {name} - {kills} uccision{'i' if kills != 1 else 'e'}" + leaderboard.append(line) + + return '\n'.join(leaderboard) + def do_random_event(self): #XXX random player does random action according to his inventory health, wounds, available weapons on the world, etc... pass