Skip to content
This repository has been archived by the owner on Oct 23, 2019. It is now read-only.

Refactored gaming plugin for project consistency and rewrote coin flip function #391

Open
wants to merge 23 commits into
base: gonzobot
Choose a base branch
from

Conversation

leonthemisfit
Copy link

The plugin's docstrings were not consistent with the rest of the project and have
been updated to better reflect that. The coin flip function has also been rewritten
with new logic that estimates large flip amounts in a cleaner more maintainable
manner while still providing very similar results to the original method.

A-UNDERSCORE-D
A-UNDERSCORE-D previously approved these changes Feb 5, 2019
plugins/gaming.py Outdated Show resolved Hide resolved
plugins/gaming.py Show resolved Hide resolved
@leonthemisfit leonthemisfit changed the title Refactored gaming plugin for project consistency and rewrote coin flip function WIP Refactored gaming plugin for project consistency and rewrote coin flip function Feb 6, 2019
plugins/gaming.py Outdated Show resolved Hide resolved
Updates the docstrings in the gaming plugin to be more
consistent with the rest of the project.
The user facing strings were hardcoded into the function
rather than being constants. Having them as constants
improves readability and maintainability.
The original way of calculating coin flips was more complex
and harder to read than needed. This changes it to use a
simpler and cleaner method that's still statistically close
enough to actual coin flip results.
The clamp function was originally necessary to deal with
some issues with the coin flip function. The new logic
for coin flip makes this no longer necessary and therefore
it can be removed.
Added my name (leonthemisfit) to the modified by
comment because of making significant changes.
There was a typo in the new coin flip logic where it was
supposed to run a proper simulation if flips were under
the roll limit but instead would only run if they were over.

Also moved hardcoded roll limit to a constant because it
is used in multiple places.
As per request the format strings utilizing a single quote
within the string have been modified to remove the quotes
and use the {!r} formatter. The string for invalid rolls has
also been moved to a constant for consistency with the
rest of the plugin as well as improving readability.
The n_rolls function was incorrectly checking if the number
of sides was less than the roll limit rather than the number
of rolls to perform.
This adds the ability to approximate fudge rolls greater
than the roll limit in n_rolls. This also majorly refactors
n_rolls because the changes to fudge dice make it possible
to use the approximation calculations in a more generic
way that is applicable to both fudge and non fudge dice.
The wrong variable was being checked resulting in
simulating very large numbers rather than approximating
them. This patch fixes that.
@codecov

This comment has been minimized.

Because 'F' is a letter and not a number if a person were to roll
a fudge die less than the roll limit the call to random.randint
would fail. This has been replaced with a correct checking
of fudge dice and ensuring the correct values are passed.

"Uh, Big Bird, F is not a number... F is a letter"
  --Kramit the Frog, Sensimilla Street
The current location of the explanatory comment is actually
beneath a large chunk of where the approximation happens. This
is remedied by moving the comment above those approximations.
The original parameter names were unclear and has already
caused confusion over the process of cleaning this up, so a quick
refactor has been done to clear up confusion.
@leonthemisfit leonthemisfit changed the title WIP Refactored gaming plugin for project consistency and rewrote coin flip function Refactored gaming plugin for project consistency and rewrote coin flip function Mar 18, 2019
plugins/gaming.py Outdated Show resolved Hide resolved
Copy link

@linuxdaemon linuxdaemon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using round() instead of int() is likely to produce more accurate results

plugins/gaming.py Outdated Show resolved Hide resolved
plugins/gaming.py Outdated Show resolved Hide resolved
The two places that int() was used to get the floor of a number
has been replaced with round because linuxdaemon.
This simply adds a return type in the docstring of the nrolls
function in gaming.py.
@linuxdaemon linuxdaemon self-requested a review March 20, 2019 11:48
Copy link

@linuxdaemon linuxdaemon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add some unit tests, something along the lines of https://gist.github.com/linuxdaemon/29cad9c3371f1e9182f3686d63a72170 should work.

In order to provide better documentation and readability as to
how rolls are approximated the formulas have been moved to
their own functions. This allows each formula to be better
documented in docstrings as well as giving them concise names
so that there's no reliance on just variable names to understand
how the values are being found.
In order to continue to improve readability and testability the
approximation of rolls has been moved to a separate function.
This allows n_rolls to be shorter and more readable.
The last step in this refactor is to move dice roll simulation to a
separate function. This final piece of refactoring represents all
logic being separated into logical units. This provides the best
means to document and test the various functions. This also
provides better maintainability.
Rather than having multiple return statements which can often
cause confusion, the results of the two function calls are stored
and then returned at the end of the code block. This can also
help future changes where the returned values may be
manipulated for additional features or other unforeseen reasons.
4 digits is plenty of precision for the calculations that use this to
be accurate. This is also done so that tests in docstrings will be
easier to read.
Like find_variance  4 digits of precision is plenty for its use here
and it improves readability of test results.
This commit adds simple tests/examples to the docstrings of
util functions that are non-random (for simplicity) so that you
can get an idea of what kind of data they return.
There was a discrepancy between my local test results and the
test results from travis with the return type from find_midpoint.
The tests in the plugin have been updated to reflect the results
travis expects rather than my local machine.
@linuxdaemon linuxdaemon self-requested a review March 20, 2019 13:54
Copy link

@linuxdaemon linuxdaemon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants