Skip to content

Commit

Permalink
Update 106-dev from main branch (#115)
Browse files Browse the repository at this point in the history
* Strip out cheating code.
Closes #111

* Pull current test stuff into main branch (#114)

* 106 Add a unit test class; add tests for the parser
* Add a test that "d10 d10" == "2d10"
* Helper script for test.py
* Add a github action to run tests

Co-authored-by: @slashingweapon
  • Loading branch information
freiheit authored Mar 25, 2024
1 parent b70533d commit d1eed8a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Test

on: [push]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11"]
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run tests
run: |
python test.py
20 changes: 4 additions & 16 deletions mvkroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def parse_dice(dicestr: str):
return dicecounts


def roll_dice(dicecounts, cheat=False):
def roll_dice(dicecounts):
"""Returns a dictionary of dieSize => rolls[]"""
dicerolls = {
20: [],
Expand All @@ -88,11 +88,7 @@ def roll_dice(dicecounts, cheat=False):
try:
for size, num in dicecounts.items():
if num > 0:
# pylint: disable=unused-variable
if cheat:
dicerolls[size] = [size for idx in range(0, num)]
else:
dicerolls[size] = [random.randint(1, size) for idx in range(0, num)]
dicerolls[size] = [random.randint(1, size) for idx in range(0, num)]
except Exception as exc:
raise RollError("Exception while rolling dice.") from exc

Expand Down Expand Up @@ -201,7 +197,6 @@ def mvkroll(dicestr: str):
logger.debug("Roll %s", {dicestr})

answer = ""
cheat = False
advantage = False
disadvantage = False

Expand All @@ -210,9 +205,6 @@ def mvkroll(dicestr: str):
elif re.search(r"advantage", dicestr, flags=re.IGNORECASE):
advantage = True

if re.search(r"cheat", dicestr, flags=re.IGNORECASE):
cheat = True

dicecounts = parse_dice(dicestr)

# advantage and disadvantage need _at least_ 2d20
Expand All @@ -225,7 +217,7 @@ def mvkroll(dicestr: str):
dicecounts[20] = 1
answer += "_No advantage/disadvantage, setting 1d20_\n"

dicerolls = roll_dice(dicecounts, cheat)
dicerolls = roll_dice(dicecounts)

# the d20 is called the "Fortune Die"
fortunedicerolls = dicerolls[20]
Expand Down Expand Up @@ -259,9 +251,6 @@ def mvkroll(dicestr: str):

answer += calc_impact(fortunedicerolls, characterdicerolls)

if cheat:
answer = "\n# Cheating #\n" + answer + "\n# Cheater #\n"

return answer


Expand All @@ -271,10 +260,9 @@ def plainroll(dicestr: str):
logger.debug("Roll %s", {dicestr})

answer = ""
cheat = False

dicecounts = parse_dice(dicestr)
dicerolls = roll_dice(dicecounts, cheat)
dicerolls = roll_dice(dicecounts)

answer += print_dice(dicerolls)

Expand Down

0 comments on commit d1eed8a

Please sign in to comment.