From bb9341d84514de6abcdc75304581bc096a1ad18e Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Fri, 20 Apr 2018 19:46:33 +0200 Subject: [PATCH] Aggiunti colpi critici e rimosso il casesensitive su /cast --- bots.py | 30 +++++++++++++++++------------ cast.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ discordbot.py | 24 ++++------------------- requirements.txt | 3 ++- telegrambot.py | 21 +++++++------------- 5 files changed, 81 insertions(+), 47 deletions(-) create mode 100644 cast.py diff --git a/bots.py b/bots.py index 99b03571..3bc31a4f 100644 --- a/bots.py +++ b/bots.py @@ -1,7 +1,9 @@ import multiprocessing +import os import telegrambot import discordbot import time +import platform discord_telegram_pipe = multiprocessing.Pipe() discord = multiprocessing.Process(target=discordbot.process, args=(discord_telegram_pipe[0],), daemon=True) @@ -10,15 +12,19 @@ telegram = multiprocessing.Process(target=telegrambot.process, args=(discord_tel if __name__ == "__main__": discord.start() telegram.start() - while True: - if discord.exitcode is not None: - print("Restarting Discord Bot...") - del discord - discord = multiprocessing.Process(target=discordbot.process, args=(discord_telegram_pipe[0],), daemon=True) - discord.start() - if telegram.exitcode is not None: - print("Restarting Telegram Bot...") - del telegram - telegram = multiprocessing.Process(target=telegrambot.process, args=(discord_telegram_pipe[1],), daemon=True) - telegram.start() - time.sleep(60) + try: + while True: + if discord.exitcode is not None: + print("Restarting Discord Bot...") + del discord + discord = multiprocessing.Process(target=discordbot.process, args=(discord_telegram_pipe[0],), daemon=True) + discord.start() + if telegram.exitcode is not None: + print("Restarting Telegram Bot...") + del telegram + telegram = multiprocessing.Process(target=telegrambot.process, args=(discord_telegram_pipe[1],), daemon=True) + telegram.start() + time.sleep(60) + except KeyboardInterrupt: + if platform.system() == "Linux": + os.system("reset") \ No newline at end of file diff --git a/cast.py b/cast.py new file mode 100644 index 00000000..cbb19988 --- /dev/null +++ b/cast.py @@ -0,0 +1,50 @@ +import random +import math + + +def cast(spell_name: str, target_name: str, platform: str) -> str: + spell = spell_name.capitalize() + # Seed the rng with the spell name + # so that spells always deal the same damage + random.seed(spell) + dmg_dice = random.randrange(1, 11) + dmg_max = random.sample([4, 6, 8, 10, 12, 20, 100], 1)[0] + dmg_mod = random.randrange(math.floor(-dmg_max / 5), math.ceil(dmg_max / 5) + 1) + # Reseed the rng with a random value + # so that the dice roll always deals a different damage + random.seed() + total = dmg_mod + # Check for a critical hit + crit = 1 + while True: + crit_die = random.randrange(1, 21) + if crit_die == 20: + crit *= 2 + else: + break + for dice in range(0, dmg_dice): + total += random.randrange(1, dmg_max + 1) + if crit > 1: + if platform == "telegram": + crit_msg = f"CRITICO ×{crit}{'!' * crit}\n" + elif platform == "discord": + crit_msg = f"**CRITICO ×{crit}{'!' * crit}**\n" + total *= crit + else: + crit_msg = "" + if platform == "telegram": + return f"❇️ Ho lanciato {spell} su " \ + f"{target_name}.\n" \ + f"{crit_msg}" \ + f"{target_name} subisce {dmg_dice}d{dmg_max}" \ + f"{'+' if dmg_mod > 0 else ''}{str(dmg_mod) if dmg_mod != 0 else ''}" \ + f"{'×' + str(crit) if crit > 1 else ''}" \ + f"={total if total > 0 else 0} danni!" + elif platform == "discord": + return f"❇️ Ho lanciato **{spell}** su " \ + f"_{target_name}_.\n" \ + f"{crit_msg}" \ + f"_{target_name}_ subisce {dmg_dice}d{dmg_max}" \ + f"{'+' if dmg_mod > 0 else ''}{str(dmg_mod) if dmg_mod != 0 else ''}" \ + f"{'×' + str(crit) if crit > 1 else ''}" \ + f"=**{total if total > 0 else 0}** danni!" \ No newline at end of file diff --git a/discordbot.py b/discordbot.py index 739e4373..ecc92179 100644 --- a/discordbot.py +++ b/discordbot.py @@ -6,7 +6,6 @@ import discord.voice_client import functools import sys import db -import errors import youtube_dl import concurrent.futures import stagismo @@ -18,6 +17,7 @@ import configparser import subprocess import async_timeout import raven +import cast # Queue emojis queue_emojis = [":one:", ":two:", ":three:", ":four:", ":five:", ":six:", ":seven:", ":eight:", ":nine:", ":ten:"] @@ -341,25 +341,9 @@ async def on_message(message: discord.Message): await client.send_message(message.channel, "⚠️ Non hai specificato nessun incantesimo!\n" "Sintassi corretta: `!cast `") return - target = random.sample(list(message.server.members), 1)[0] - # Seed the rng with the spell name - # so that spells always deal the same damage - random.seed(spell) - dmg_mod = random.randrange(-2, 3) - dmg_dice = random.randrange(1, 4) - dmg_max = random.sample([4, 6, 8, 10, 12, 20, 100], 1)[0] - # Reseed the rng with a random value - # so that the dice roll always deals a different damage - random.seed() - total = dmg_mod - for dice in range(0, dmg_dice): - total += random.randrange(1, dmg_max+1) - await client.send_message(message.channel, - f"❇️ Ho lanciato **{spell}** " - f"su **{target.nick if target.nick is not None else target.name}** " - f"per {dmg_dice}d{dmg_max}" - f"{'+' if dmg_mod > 0 else ''}{str(dmg_mod) if dmg_mod != 0 else ''}" - f"=**{total if total > 0 else 0}** danni!") + target: discord.Member = random.sample(list(message.server.members), 1)[0] + await client.send_message(message.channel, cast.cast(spell_name=spell, target_name=target.name, + platform="discord")) elif message.content.startswith("!smecds"): ds = random.sample(stagismo.listona, 1)[0] await client.send_message(message.channel, f"Secondo me, è colpa {ds}.", tts=True) diff --git a/requirements.txt b/requirements.txt index 2f869495..4643bba1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,4 +6,5 @@ youtube-dl requests psycopg2 PyNaCl -async_timeout \ No newline at end of file +async_timeout +raven \ No newline at end of file diff --git a/telegrambot.py b/telegrambot.py index c488b6bf..0c034e07 100644 --- a/telegrambot.py +++ b/telegrambot.py @@ -11,6 +11,7 @@ from discord import Status as DiscordStatus import subprocess import os import time +import cast # Init the config reader import configparser @@ -132,7 +133,7 @@ def cmd_discord(bot: Bot, update: Update): def cmd_cast(bot: Bot, update: Update): try: - spell = update.message.text.split(" ", 1)[1] + spell: str = update.message.text.split(" ", 1)[1] except IndexError: bot.send_message(update.message.chat.id, "⚠️ Non hai specificato nessun incantesimo!\n" "Sintassi corretta: `/cast `") @@ -143,19 +144,10 @@ def cmd_cast(bot: Bot, update: Update): target = random.sample(session.query(db.Telegram).all(), 1)[0] # Close the session session.close() - # Seed the rng with the spell name - # so that spells always deal the same damage - random.seed(spell) - dmg_dice = random.randrange(1, 11) - dmg_max = random.sample([4, 6, 8, 10, 12, 20, 100], 1)[0] - dmg_mod = random.randrange(math.floor(-dmg_max / 5), math.ceil(dmg_max / 5) + 1) - # Reseed the rng with a random value - # so that the dice roll always deals a different damage - random.seed() - total = dmg_mod - for dice in range(0, dmg_dice): - total += random.randrange(1, dmg_max + 1) - bot.send_message(update.message.chat.id, f"❇️ Ho lanciato {spell} su {target.username if target.username is not None else target.first_name} per {dmg_dice}d{dmg_max}{'+' if dmg_mod > 0 else ''}{str(dmg_mod) if dmg_mod != 0 else ''}={total if total > 0 else 0} danni!") + bot.send_message(update.message.chat.id, cast.cast(spell_name=spell, + target_name=target.username if target.username is not None + else target.first_name, platform="telegram"), + parse_mode="HTML") def cmd_color(bot: Bot, update: Update): @@ -391,5 +383,6 @@ def process(arg_discord_connection): time.sleep(60) print("Telegrambot restarting...") + if __name__ == "__main__": process(None) \ No newline at end of file