DiceExtension: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| Line 45: | Line 45: | ||
"manifest_version": 1 | "manifest_version": 1 | ||
} | } | ||
</pre> | |||
DiceRoll.i18n.php | |||
<pre> | |||
<?php | |||
$messages = []; | |||
$messages['en'] = [ | |||
'diceroll-invalid-notation' => 'Invalid dice notation. Please use the format: XdY[+/-Z]', | |||
]; | |||
$messages['fr'] = [ | |||
'diceroll-invalid-notation' => 'Notation de dé invalide. Veuillez utiliser le format : XdY[+/-Z]', | |||
]; | |||
// Add translations for other languages as needed | |||
return $messages; | |||
</pre> | </pre> | ||
Revision as of 08:30, 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:
{
"name": "DiceRoll",
"author": "Your Name",
"version": "1.0",
"description": "Allows users to roll dice within MediaWiki pages.",
"license-name": "MIT",
"type": "parserhook",
"manifest_version": 1
}
DiceRoll.i18n.php
<?php
$messages = [];
$messages['en'] = [
'diceroll-invalid-notation' => 'Invalid dice notation. Please use the format: XdY[+/-Z]',
];
$messages['fr'] = [
'diceroll-invalid-notation' => 'Notation de dé invalide. Veuillez utiliser le format : XdY[+/-Z]',
];
// Add translations for other languages as needed
return $messages;
Call with
{{#rollDice: 3d6+2}} => Result: [Total of the rolled dice] + 2
{{#rollDice: 2d8-1}} => Result: [Total of the rolled dice] - 1
{{#rollDice: 1d10}} => Result: [Total of the rolled dice]