Skip to content

Commit

Permalink
fix(misc): improve display of /probabilities (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
natelandau authored Sep 26, 2023
1 parent af9e291 commit e495f38
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 168 deletions.
17 changes: 2 additions & 15 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,17 @@
# Todo List

- [x] Admin: Settings for managing campaigns (ie, who can create, who can edit, etc)
- [x] Character: Mark characters as dead
- [x] Character: Migrate to "owned" characters
- [ ] Character: Add Concept
- [ ] Character: Add Character generator
- [ ] Character: Add Character generation roll bot
- [ ] Campaign: Renumber chapters
- [ ] Campaign: View any campaign, not just active one
- [ ] Campaign: Associate characters with campaigns
- [ ] Campaign: If only one campaign, always set it as active
- [ ] Campaign: Improve campaign view
- [ ] Gameplay: Button to create macro from rolling traits
- [ ] Statistics: Pull stats based on timeframe
- [ ] Storyteller: Add notes
- [x] Storyteller: Allow creating storyteller NPCs with the chargen wizard
- [x] Storyteller: Allow updating player characters
- [x] Storyteller: Allow updating storyteller characters
- [x] Storyteller: Add custom traits to storyteller characters
- [ ] Refactor: Centralize pagination for long responses
- [ ] Tests: Add tests for options
- [ ] Refactor: Move logic out of cogs and to services or db models
- [ ] Tests: Add tests for converters
- [ ] Tests: Add tests for cogs

## Move to Guild User Objects

- [x] rewrite user_svc to use guild user objects
- [ ] change all cogs and references to use guild user objects
- [ ] migrate all user database objects to guild_user objects
- [ ] migrate all foreign keys from user database objects to guild_user objects
222 changes: 111 additions & 111 deletions poetry.lock

Large diffs are not rendered by default.

31 changes: 0 additions & 31 deletions src/valentina/cogs/gameplay.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from discord.ext import commands

from valentina.constants import DEFAULT_DIFFICULTY, DiceType, RollResultType
from valentina.models import Probability
from valentina.models.bot import Valentina
from valentina.utils import errors
from valentina.utils.converters import ValidCharTrait, ValidImageURL, ValidMacroFromID
Expand Down Expand Up @@ -52,36 +51,6 @@ async def throw(

await perform_roll(ctx, pool, difficulty, DiceType.D10.value, comment, character=character)

@roll.command(name="probability", description="Calculate the probability of a roll")
async def probability(
self,
ctx: discord.ApplicationContext,
pool: discord.Option(int, "The number of dice to roll", required=True),
difficulty: Option(
int,
"The difficulty of the roll",
required=True,
),
hidden: Option(
bool,
description="Make the probability only visible to you (default False).",
default=False,
),
) -> None:
"""Roll the dice.
Args:
hidden (bool, optional): Make the statistics only visible to you (default true). Defaults to True.
ctx (discord.ApplicationContext): The context of the command
difficulty (int): The difficulty of the roll
pool (int): The number of dice to roll
"""
probabilities = Probability(
ctx, pool=pool, difficulty=difficulty, dice_size=DiceType.D10.value
)
embed = await probabilities.get_embed()
await ctx.respond(embed=embed, ephemeral=hidden)

@roll.command(name="traits", description="Throw a roll based on trait names")
async def traits(
self,
Expand Down
35 changes: 32 additions & 3 deletions src/valentina/cogs/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from discord.commands import Option
from discord.ext import commands

from valentina.constants import SPACER, EmbedColor
from valentina.models import Statistics
from valentina.constants import SPACER, DiceType, EmbedColor
from valentina.models import Probability, Statistics
from valentina.models.bot import Valentina
from valentina.models.db_tables import Character, Macro
from valentina.utils.changelog_parser import ChangelogParser
Expand All @@ -21,8 +21,37 @@ class Misc(commands.Cog):
def __init__(self, bot: Valentina) -> None:
self.bot: Valentina = bot

@commands.slash_command(name="probability", description="Calculate the probability of a roll")
async def probability(
self,
ctx: discord.ApplicationContext,
pool: discord.Option(int, "The number of dice to roll", required=True),
difficulty: Option(
int,
"The difficulty of the roll",
required=True,
),
hidden: Option(
bool,
description="Make the probability only visible to you (default False)",
default=False,
),
) -> None:
"""Roll the dice.
Args:
hidden (bool, optional): Make the statistics only visible to you (default true). Defaults to True.
ctx (discord.ApplicationContext): The context of the command
difficulty (int): The difficulty of the roll
pool (int): The number of dice to roll
"""
probabilities = Probability(
ctx, pool=pool, difficulty=difficulty, dice_size=DiceType.D10.value
)
embed = await probabilities.get_embed()
await ctx.respond(embed=embed, ephemeral=hidden)

@commands.slash_command(name="user_info", description="View information about a user")
@discord.guild_only()
async def user_info(
self,
ctx: discord.ApplicationContext,
Expand Down
12 changes: 9 additions & 3 deletions src/valentina/models/probability.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,16 @@ def _get_description(self) -> str:
*(Chance that any specific die will come up with the specified value)*
```python
Critical Success (10): {self.probabilities['critical_dice']:.2f}%
Success (>= {self.pool}): {self.probabilities['success_dice']:.2f}%
Failure (< {self.pool}): {self.probabilities['failure_dice']:.2f}%
Success (>= {self.difficulty}): {self.probabilities['success_dice']:.2f}%
Failure (< {self.difficulty}): {self.probabilities['failure_dice']:.2f}%
Botch (1): {self.probabilities['botch_dice']:.2f}%
```
> DEFINITIONS:
> - _Critical Success_: More successes than dice rolled
> - _Success_: At least one success after all dice are tallied
> - _Failure_: Zero successes after all dice are tallied
> - _Botch_: Negative successes after all dice are tallied
"""

async def get_embed(self) -> discord.Embed:
Expand All @@ -155,6 +161,6 @@ async def get_embed(self) -> discord.Embed:
color=EmbedColor.INFO.value,
)
embed.description = self._get_description()
embed.set_footer(text=f"Based on {self.trials:,} trials")
embed.set_footer(text=f"Probabilities based on {self.trials:,} trials")

return embed
4 changes: 2 additions & 2 deletions src/valentina/utils/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


async def select_changelog_version_1(ctx: discord.AutocompleteContext) -> list[str]:
"""Populate the autocomplete for the version option."""
"""Populate the autocomplete for the version option. This is for the first of two options."""
bot = cast(Valentina, ctx.bot)
possible_versions = bot.guild_svc.fetch_changelog_versions()

Expand All @@ -27,7 +27,7 @@ async def select_changelog_version_1(ctx: discord.AutocompleteContext) -> list[s


async def select_changelog_version_2(ctx: discord.AutocompleteContext) -> list[str]:
"""Populate the autocomplete for the version option."""
"""Populate the autocomplete for the version option. This is for the second of two options."""
bot = cast(Valentina, ctx.bot)
possible_versions = bot.guild_svc.fetch_changelog_versions()

Expand Down
7 changes: 6 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,14 +347,19 @@ def mock_ctx4(mocker):
@pytest.fixture()
def mock_autocomplete_ctx1(mocker, mock_member, mock_guild1):
"""Create a mock autocomplete context object with user 1."""
mock_bot = mocker.MagicMock()
mock_bot.user_svc.update_or_add = MagicMock(return_value=mock_member)
mock_bot.__class__ = commands.Bot

mock_interaction = mocker.MagicMock()
mock_interaction.guild = mock_guild1
mock_interaction.user = mock_member

# Mock the ctx object
mock_ctx = mocker.MagicMock()
mock_ctx.interaction = mock_interaction

mock_ctx.options = {}
mock_ctx.bot = mock_bot
mock_ctx.__class__ = discord.AutocompleteContext

return mock_ctx
Expand Down
2 changes: 1 addition & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ async def test_get_embed(self, mock_ctx):
assert isinstance(embed, discord.Embed)
result = embed.to_dict()

assert result["footer"]["text"] == IsStr(regex=r"Based on [0-9,]+ trials")
assert result["footer"]["text"] == IsStr(regex=r"Probabilities based on [0-9,]+ trials")
assert result["description"] == IsStr()
assert isinstance(result["fields"], list)
2 changes: 1 addition & 1 deletion user_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ The core features of Valentina Noir are covered in the previous sections. These

- **Help**: Use `/help` to get help on various topics, including commands, traits, and macros.
- **Coinflip**: Use `/coinflip` to flip a coin.
- **Roll Probabilities**: Use `/roll probability` to see the probability of rolling a certain number of successes.
- **Roll Probabilities**: Use `/probability` to see the probability of rolling a certain number of successes.
- **Roll Statistics**: Use `/reference statistics` to see the diceroll statistics for a guild, user, or character.
- **Reference Information**: Use `/reference` for information on various topics such as health, magic, disciplines, and more.
- **Adding Roll Result Images**: Use `/roll upload_thumbnail` to add an image or animated gif to a roll result.
Expand Down

0 comments on commit e495f38

Please sign in to comment.