forked from hanelyp/fancydice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bbcode.php
80 lines (69 loc) · 1.55 KB
/
bbcode.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
/**
*
* @package hanelyp\fancydice
* @copyright (c) 2015 Peter Hanely
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
namespace hanelyp\fancydice;
class bbcode
{
private dice;
static myself = false;
var $config;
var $macros;
function setup($config)
{
$this->config = $config;
$this->macros = $this->get_macros();
}
function get_macros()
{
//global $config;
$macros = false;
if (isset($this->config['fancyDiceMacro_1']))
{
$macros = array();
$i = 1;
while (isset($config['fancyDiceMacro_'.$i]))
{
$macro = json_decode($this->config['fancyDiceMacro_'.$i]);
foreach ($macro as $key=>$value)
{
$macros[$key] = $value;
}
$i++;
}
}
return $macros;
}
static function singlet()
{
if (!self::$myself)
{
self::$myself = new bbcode($user->config);
}
return self::$myself;
}
public function prep_dice($spec, $uid)
{
$seed = rand();
$secure = $this->validate($seed);
return '[dice seed='.$seed.' secure='.$secure.':'.$uid.']'.$spec.'[/dice]';
}
// not the most secure, but enough to discourage fiddling with the seed
function validate($seed)
{
return sha1($this->config['fancyDiceSecure'].$seed);
}
public function replace_dice($spec, $seed, $secure)
{
// validate seed against secure
$valid = $this->validate($seed)==$secure?'':' invalid';
$dice = new \hanelyp\fancydice\fancydice($this->macros, $seed);
$roll = $dice->roll($spec);
$total = $dice->sum($roll);
return $spec.' => '.join(', ', $roll).' => '.$total.$valid;
}
}