Skip to content

Commit

Permalink
[docs] fixing up some docs, fixing the logic around rigged dice, fixi…
Browse files Browse the repository at this point in the history
…ng 'd' (#8)
  • Loading branch information
jpetrucciani authored Oct 29, 2020
1 parent abcfefa commit 5c3aaf9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ jobs:
python-version: ${{ matrix.python-version }}
architecture: x64
- name: install requirements
run: pip install tox pytest pytest-cov
run: pip install -r requirements.dev.txt
- name: run Tox
run: tox -e py
26 changes: 16 additions & 10 deletions gamble/models/dice.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""
import random
from typing import List, Union, Tuple
from gamble.errors import GambleException


class Die:
Expand All @@ -18,7 +19,7 @@ def __init__(self, sides: int = 6) -> None:
@arg sides: the number of sides to this die
"""
if abs(int(sides)) < 2:
raise Exception("A die must have at least 2 sides")
raise GambleException("A die must have at least 2 sides")
self.sides = abs(int(sides))
self.negative = sides <= 0
self.multiplier = -1 if self.negative else 1
Expand Down Expand Up @@ -121,16 +122,16 @@ class RiggedDie(Die):

def __init__(self, sides: int = 6, rigged_factor: int = 50) -> None:
"""
@cc 2
@cc 3
@desc create a new Rigged die
@arg sides: the number of sides to this die
@arg rigged_factor: int from 0-100 to manipulate the die into a high roll
"""
self.rigged_factor = rigged_factor
if rigged_factor < 0 or rigged_factor > 100:
raise Exception(
"The rigged factor must be between 0 and 100must have at least 2 sides"
)
raise GambleException("The rigged factor must be between 0 and 100")
if sides < 2:
raise GambleException("the die must have at least 2 sides")

super().__init__(sides)

Expand All @@ -154,7 +155,7 @@ class Dice:
@desc a group of die objects
"""

def __init__(self, init_string: str = "2d6", rigged_factor: int = 0) -> None:
def __init__(self, init_string: str = "2d6", rigged_factor: int = -1) -> None:
"""
@cc 2
@desc create a new d notation group of dice
Expand All @@ -170,13 +171,18 @@ def __init__(self, init_string: str = "2d6", rigged_factor: int = 0) -> None:
if "d" in d_string:
die_settings = [int(x) for x in d_string.split("d") if x]
if len(die_settings) == 1:
self.dice.append(self.create_die(die_settings[0], rigged_factor))
self.dice.append(
self.create_die(die_settings[0], rigged_factor=rigged_factor)
)
elif len(die_settings) > 1:
num, value = die_settings
negative = -1 if num < 0 else 1
self.dice.extend(
[self.create_die(value * negative, rigged_factor)] * abs(num)
[self.create_die(value * negative, rigged_factor=rigged_factor)]
* abs(num)
)
else:
raise GambleException("cannot create a die with no value!")
else:
self.bonuses.append(int(d_string))
self.dice = list(sorted(self.dice))
Expand Down Expand Up @@ -226,15 +232,15 @@ def min(self) -> int:
"""
return sum([*[x.min for x in self.dice], *self.bonuses])

def create_die(self, sides: int, rigged_factor: int) -> Union[Die, RiggedDie]:
def create_die(self, sides: int, rigged_factor: int = -1) -> Union[Die, RiggedDie]:
"""
@cc 2
@arg sides: the number of sides on a die
@arg rigged_factor: int from 0-100 to manipulate the die into a high roll
@desc helper to create dice
@ret A Die object that can be rigged
"""
if rigged_factor:
if rigged_factor != -1:
return RiggedDie(sides, rigged_factor)
return Die(sides)

Expand Down
3 changes: 1 addition & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[mypy]
python_version = 3.6
python_version = 3.9
disallow_untyped_defs = True
ignore_missing_imports = True

Expand All @@ -9,4 +9,3 @@ max-line-length = 100
max-complexity = 20

[tool:pytest]
log_print = False
8 changes: 8 additions & 0 deletions tests/models/test_dice.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
import pytest
from gamble import Die, RiggedDie, Dice
from gamble.errors import GambleException


def test_die_init() -> None:
Expand Down Expand Up @@ -53,6 +54,13 @@ def test_dice_init() -> None:
assert 2 <= roll <= 12
assert dice.rolls == 1

# test that you can't make just a 'd'
try:
dice = Dice("d")
assert False
except GambleException:
assert True


def test_dice_complex() -> None:
"""tests complex dice string setup"""
Expand Down

0 comments on commit 5c3aaf9

Please sign in to comment.