DiceExtension: Difference between revisions
(Created page with " Diceroll.php <script lang=php> <?php // Define a function to handle the dice rolling function wfDiceRollParserFunction($parser, $rollString = '') { // Parse the input string (e.g., "2d6") preg_match('/(\d+)d(\d+)/', $rollString, $matches); if (count($matches) !== 3) { return 'Invalid dice notation. Please use the format: XdY'; } $numDice = intval($matches[1]); $numSides = intval($matches[2]); // Roll the dice $total = 0; for...") |
No edit summary |
||
Line 2: | Line 2: | ||
Diceroll.php | Diceroll.php | ||
< | <nowiki> | ||
<?php | <?php | ||
// Define a function to handle the dice rolling | // Define a function to handle the dice rolling | ||
Line 31: | Line 31: | ||
} | } | ||
</ | </nowiki> | ||
extension.json: |
Revision as of 07:50, 13 February 2024
Diceroll.php
<?php
// Define a function to handle the dice rolling
function wfDiceRollParserFunction($parser, $rollString = '') {
// Parse the input string (e.g., "2d6")
preg_match('/(\d+)d(\d+)/', $rollString, $matches);
if (count($matches) !== 3) {
return 'Invalid dice notation. Please use the format: XdY';
}
$numDice = intval($matches[1]);
$numSides = intval($matches[2]);
// Roll the dice
$total = 0;
for ($i = 0; $i < $numDice; $i++) {
$total += mt_rand(1, $numSides);
}
// Return the result
return $total;
}
// Register the parser function with MediaWiki
$wgHooks['ParserFirstCallInit'][] = 'wfDiceRollSetup';
function wfDiceRollSetup($parser) {
$parser->setFunctionHook('rollDice', 'wfDiceRollParserFunction');
return true;
}
extension.json: