{"id":16272570,"url":"https://github.com/jifish/ezdice","last_synced_at":"2025-03-19T23:31:08.254Z","repository":{"id":56998941,"uuid":"426817786","full_name":"JiFish/ezdice","owner":"JiFish","description":"A really simple PHP library for parsing dice notation and rolling.","archived":false,"fork":false,"pushed_at":"2025-02-15T22:37:52.000Z","size":31,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-17T12:21:27.312Z","etag":null,"topics":["dice","dice-notation","dice-roller","die","library","php","php7","php8"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JiFish.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-11-11T00:12:06.000Z","updated_at":"2025-02-19T04:57:46.000Z","dependencies_parsed_at":"2022-08-21T14:50:13.472Z","dependency_job_id":null,"html_url":"https://github.com/JiFish/ezdice","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JiFish%2Fezdice","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JiFish%2Fezdice/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JiFish%2Fezdice/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JiFish%2Fezdice/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JiFish","download_url":"https://codeload.github.com/JiFish/ezdice/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244524837,"owners_count":20466510,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dice","dice-notation","dice-roller","die","library","php","php7","php8"],"created_at":"2024-10-10T18:18:17.113Z","updated_at":"2025-03-19T23:31:08.241Z","avatar_url":"https://github.com/JiFish.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# EZDice\n\nA really simple PHP library for parsing dice notation and rolling. By Joseph Fowler aka JiFish.\n\nWorks in PHP 7+.\n\n## Why use EZDice?\n\nEZDice was written because I wasn't happy with similar libraries. EZDice has the following advantages over other offerings:\n\n- No Bloat. EZDice is one simple class with no dependencies.\n- Provides the state of each die after the roll, not just the total.\n- Forgiving parser designed for humans.\n- Option to easily replace the RNG.\n- MIT licensed, ensuring you can use it freely in any project.\n\n## Usage\n\nHere's a basic example\n\n```\nrequire 'ezdice.php';\n\n$ezd = new ezdice\\EZDice();\necho($ezd-\u003eroll('1d20+2d4'));\necho(' Rolls:');\nforeach($ezd-\u003egetDiceStates() as $die) {\n    echo(' ['.$die['value'].'] ');\n}\n```\n\nExample Output:\n```\n23 Rolls: [17] [4] [2]\n```\n\n## Installing with Composer\n\nYou don't have to use composer to include EZDice in your project, but you can:\n\n`composer require jifish/ezdice`\n\n## Methods\n\n### roll($diceStr)\n\nParse **$diceStr** as dice notation, then roll those dice. Returns *(int)* total of all rolls and modifiers, or *false* if none were found.\n\nThe parser is very forgiving, ignoring whitespace and anything else it doesn't recognize. It is also case-insensitive. Dice notation is briefly documented below.\n\n### getTotal()\n\nReturns *(int)* that is the total of the last roll.\n\n### getDiceStates()\n\nReturns an *(array)* of dice that describes the state of the dice after the last roll. Each die is also an *(array)* with the following keys:\n\n- **sides** - *(int)* the number of sides the die has\n- **value** - *(int)* the value the die rolled\n- **dropped** - *(bool)* *true* if this dice was dropped, otherwise *false*. Dropped dice aren't counted towards the total.\n- **negative** - *(bool)* *true* if this dice was subtracted from the total, *false* if it was added.\n- **type** - *(str)* string with the type of dice this is. e.g. \"d6\" or \"dF\"\n- **group** - *(int)* the group number this die belongs to, starting from 1 for the first group found.\n\n### getModifier()\n\nReturns a *(string)* representing the total of all modifiers in the last roll. If there were no modifiers, or they cancelled out, an empty string is returned. You can cast this to an *(int)* if needed.\n\ne.g. if you rolled `1d8+10+1d4-2` this method would return `+8`.\n\n### getDiceGroupNumber()\n\nReturns an *(int)* containing the total number of dice groups in the last roll. e.g. if you rolled `10d4-1d6`, this method would return `2`.\n\n### getDiceModsNumber()\n\nReturns an *(int)* containing the total number of modifiers in the last roll. e.g. if you rolled `10+1d6+5-1`, this method would return `3`.\n\n### strContainsDice($diceStr)\n\nParse **$diceStr** and returns true if it contains at least one dice roll, otherwise returns false. Useful for verifying user input. Modifiers without dice don't count.\n\n### strIsStrictlyDice($diceStr, $allowWhitespace = true, $mustContainDice = true)\n\nParse **$diceStr** and determine if it only contains dice and modifiers. Whitespace is allowed unless **$allowWhitespace** is set to false, but strings containing only whitespace will never pass. Set **$mustContainDice** to false to allow plain modifiers without dice (e.g. `-4`) to pass. Returns true if **$diceStr** passes, otherwise false.\n\nThe **roll** function ignores stuff it can't parse, so this function may be useful if you need to verify a string doesn't contain additional junk.\n\n### rollStrict($diceStr, $allowWhitespace = true, $mustContainDice = true)\n\nConvenience function that returns false if **$diceStr** is not strictly dice (as above), but otherwise works the same as **roll()**.\n\n## Dice Notation\n\n- Dice notation is in the form (number of dice)**d**(dice sides). e.g. `2d10`.\n- Number of dice can be omitted to get one die of that type, e.g. `d12` \n- Additional dice can be chained with **+** and **-** operators. e.g. `2d10+1d6`.\n- Modifiers can also be given. e.g. `2d10-5`\n- d% can be used as a shorthand for a percentile dice. `1d%` and `1d100` are equivalent.\n- Append a roll with -L to drop the lowest dice in that group, or -H to drop the highest. Dropped dice are excluded from the total. e.g. `2d20-L` will roll 2 twenty sided dice and drop the lowest. You can also specify a number of dice to drop e.g. `6d6-H3` will drop the highest 3 rolls.\n- dF can be used to roll a Fudge/FATE dice. Fudge dice have 3 outcomes: -1, 0, and +1.\n- Whitespace, and anything else not recognized as a dice or a modifier, is treated like a **+** operator. e.g. `foo10 1d4bar1d4  5` is equivalent to `5+1d4+1d4+10`, or simply `2d4+15`. If this is an issue, use **rollStrict**.\n- Groups of zero dice, or dice with zero sides e.g. `0d0` are invalid and won't be parsed. \n\n## Replacing the random number generator\n\nBy default *mt_rand()* is used as the RNG, which should be fine for most applications. If you want to change this, you can extend the class and override the method **getRandomNumber($max)**, which should return an int equal to or between 1 to $max.\n\n```\nclass WeightedDice extends ezdice\\EZDice {\n    protected function getRandomNumber($max) {\n        if (mt_rand(0,1)) return $max;\n        return mt_rand(1,$max);\n    }\n}\n```\n\n## Legalese\n\nReleased under the MIT license. Copyright (c) 2021 - 2025 Joseph Fowler.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjifish%2Fezdice","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjifish%2Fezdice","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjifish%2Fezdice/lists"}