Guest User

Untitled

a guest
Sep 23rd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. <?php
  2. function nassau_dice_filter($o) {
  3. $preg = '/\[dice=(?<num>\d*)[kd](?<dice>\d+)\]((?<res>[^\[]*)\[\/dice\])?/i';
  4.  
  5. $o->Msg = preg_replace_callback($preg, 'nassau_dice_parse', $o->Msg);
  6. }
  7. function nassau_dice_parse($m) {
  8. if (false == empty($m['res'])) return $m[0];
  9.  
  10. $dice = $m['dice'];
  11. $num = $m['num'] ? $m['num'] : 1;
  12.  
  13. $sum = array ();
  14. foreach (range(1, $num) as $_) $sum[] = rand(1, $dice);
  15. $sum = implode(', ', $sum) . ' » ' . array_sum($sum);
  16.  
  17. return sprintf('[dice=%dd%d]%s[/dice]', $num, $dice, $sum);
  18. }
  19.  
  20. function nassau_dice_format(&$data) {
  21. $msg = $data['message'];
  22. $rep = preg_replace('/\[dice=(\d*[kd]\d+)\]([^\[]+)\[\/dice\]/i',
  23. '<span style="background:#0080FF;color:white;padding:1px 4px;border-radius:6px;">Rolled [$1]: $2</span>',
  24. $msg);
  25.  
  26. $data['message'] = $rep;
  27. }
  28.  
  29. UnbRegisterHook('post.beforeadd', 'nassau_dice_filter');
  30. UnbRegisterHook('post.beforechange', 'nassau_dice_filter');
  31. UnbRegisterHook('post.postparse', 'nassau_dice_format');
Add Comment
Please, Sign In to add comment