-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
334 lines (295 loc) · 9.4 KB
/
functions.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
/**
* PunBB Dice Roller extension functions file
*
* @copyright (C) 2009 Raul Ferriz
* @license http://www.gnu.org/licenses/gpl.html GPL version 3
* @package pun_dice
*
* Changelog:
* v0.6
* Added support for Critics (use [dice="critic"]...[/dice])
* When a result is the max result, add a dice
* v0.5
* Added support for objecttive dice (do)
* v0.4
* Added support for multiplication and division
* v0.3.3
* Added support for parentesis
* v0.3.2
* Added support for quoting rolling dices
* v0.3.1
* Fixed bug with interaction agains bbcode [code][/code]
* v0.3 Added conditions
* First steps in multilanguage support
* v0.1 Initial release
*/
if (!defined('FORUM')) die();
if (!function_exists('dr_parse'))
{
function dr_parse($input_text, $is_quote = false)
{
global $dr_active_post;
srand($dr_active_post);
// If the message contains a code tag we have to split it up (text within [code][/code] shouldn't be touched)
if (strpos($input_text, '[code]') !== false && strpos($input_text, '[/code]') !== false)
{
list($inside_code, $outside_code) = split_text($input_text, '[code]', '[/code]', $errors);
$input_text = implode("\0", $outside_code);
}
else
{
$outside_code = array ($input_text);
}
foreach($outside_code as $key => $text)
{
// If the message contains a dice tag we have to split it up (text within [dice][/dice] shouldn't be touched)
if (strpos($text, '[dice') !== false && strpos($text, '[/dice]') !== false)
{
list($inside, $outside) = split_text($text, '[dice', '[/dice]', $errors);
}
if (isset($inside))
{
$num_tokens = count ($inside);
for($i = 0; $i < $num_tokens; $i++)
{
// Reset options
$allow_critics = false;
$dice_params = array();
$block = $inside[$i];
if (strpos($block, ']') !== false)
{
$params = substr ($block, 0, strpos($block, ']'));
$block = substr($block, strpos($block, ']') + 1);
// Search for params
if (strpos($params, '=') !== false)
{
// Clean params
$params = preg_replace ('/("|"|\'|=|]| )?/', '', $params);
$dice_params = explode (',', $params);
// Search for options
if (array_search ('critic', $dice_params) !== false)
$allow_critics = true;
}
}
$is_quote = (stripos($block, '>') !== false) or (stripos($block, '<') !== false);
if ($is_quote)
{
// We must convert some simbols, only when when we come for quoting
$block = str_replace (
array('>', '<'),
array('>', '<'),
$block);
}
// Split in expressions
$expressions = dr_split_expression($block);
$block = '( ' . $block . ' ) : ';
if (isset($expressions))
{
$to_eval = '';
foreach ($expressions as $expression)
{
$tmp_result = dr_parse_expression($expression, $allow_critics);
$block .= $tmp_result;
$to_eval .= $tmp_result;
}
// We must convert some simbols for evalue them
$to_eval = str_replace ( array(">", "<"),
array(">", "<"),
$to_eval);
// We can only have one operator of any type
if ((substr_count ($to_eval, '<=') > 1) or
(substr_count ($to_eval, '>=') > 1) or
(substr_count ($to_eval, '<') > 1) or
(substr_count ($to_eval, '>') > 1) or
(substr_count ($to_eval, '=') > 1))
{
$block .= " : " . DR_INVALID_EXPRESSION;
}
else
{
// Evaluate the result
if ((strpos($to_eval, '<') !== false) or
(strpos($to_eval, '>') !== false) or
(strpos($to_eval, '=') !== false))
{
eval('$block .= " : " . ((' . $to_eval . ') === true ? DR_TRUE_STRING : DR_FALSE_STRING );');
}
else
{
eval('$block .= " = " . (' . $to_eval . ');');
}
}
}
else
{
$block .= DR_INVALID_EXPRESSION;
}
if ($is_quote)
{
// We must restore some simbols, only when when we come for quoting
$block = str_replace ( array('>', '<'),
array('>', '<'),
$block);
}
if (count($dice_params) > 0)
{
$dice_params = '="' . implode (',', $dice_params) . '"]';
}
else
{
$dice_params = ']';
}
$inside[$i] = $dice_params . $block;
}
}
// If we split up the message before we have to concatenate it together again (dice tags)
if (isset($inside))
{
$text = '';
$num_tokens = count($outside);
for ($j = 0; $j < $num_tokens; $j++)
{
$text .= $outside[$j];
if (isset($inside[$j]))
$text .= '[dice'. $inside[$j] . '[/dice]';
}
}
$outside_code[$key] = $text;
}
// If we split up the message before we have to concatenate it together again (code tags)
if (isset($inside_code))
{
$outside_code = explode("\0", $input_text);
$input_text = '';
$num_tokens = count($outside_code);
for ($i = 0; $i < $num_tokens; ++$i)
{
$input_text .= $outside_code[$i];
if (isset($inside_code[$i]))
$input_text .= '[code]'. $inside_code[$i] . '[/code]';
}
}
else
{
$input_text = $outside_code[0];
}
return $input_text;
}
}
if (!function_exists('dr_split_expression'))
{
/**
* Helper function, search $tag in $string, set $tag_position and $founded only if matching new $tag
* position is lesser than existing $tag_position
*/
function dr_search_tag($string, $tag, &$tag_position, &$founded)
{
if ((strpos($string, $tag) !== false) and (($tag_position === false) or ($tag_position > strpos($string, $tag))))
{
$tag_position = strpos($string, $tag);
$founded = $tag;
}
}
/**
* This functions split $text in chunk of operators and expressions
*/
function dr_split_expression($text)
{
$result = array ();
$string = $text;
$op_position = false;
$operator = '';
do
{
$op_position = false;
$operator = '';
dr_search_tag($string, '>=', $op_position, $operator);
dr_search_tag($string, '<=', $op_position, $operator);
dr_search_tag($string, '=', $op_position, $operator);
dr_search_tag($string, '>', $op_position, $operator);
dr_search_tag($string, '<', $op_position, $operator);
dr_search_tag($string, '*', $op_position, $operator);
dr_search_tag($string, '/', $op_position, $operator);
dr_search_tag($string, '+', $op_position, $operator);
dr_search_tag($string, '-', $op_position, $operator);
dr_search_tag($string, '(', $op_position, $operator);
dr_search_tag($string, ')', $op_position, $operator);
if ($op_position !== false)
{
$result[] = substr($string, 0, $op_position);
$result[] = $operator;
$string = substr($string, strlen($result[count($result) - 2]) + strlen($operator));
}
else
{
$result[] = $string;
}
}
while ($op_position !== false);
return $result;
}
}
if (!function_exists('dr_parse_expression'))
{
function dr_parse_expression($expression, $allow_critics) {
$result_expression = $expression;
$result = $result_expression;
if (strpos ($result_expression, 'do') !== false)
{
if ( $allow_critics )
echo "Critics not allowed with objective dice, ignoring.";
$result = '';
$throw = explode('do', $result_expression);
if (isset($throw))
{
$results = array ();
$num_dices = $throw[0];
$j = 0;
$result .= ' /*';
for (; $j < $num_dices; $j++)
{
$results[] = dr_roll_dice((int)$throw[1]);
if ($j > 0)
$result .= ', ';
$result .= $results[$j];
}
$result .= '*/ ';
sort($results);
$result .= $results[(($j-1)/2)];
}
unset($throw);
}
else
if (strpos ($result_expression, 'd') !== false)
{
$result = '';
$throw = explode('d', $result_expression);
if (isset($throw))
{
$sum_result = 0;
$num_dices = $throw[0];
for ($j = 0; $j < $num_dices; $j++)
{
$dice_result = dr_roll_dice((int)$throw[1]);
$sum_result += $dice_result;
if ($j > 0)
$result .= ' + ';
$result .= $dice_result;
if ( $allow_critics and ($dice_result == (int)$throw[1]))
{
$num_dices ++;
}
}
}
}
return $result;
}
}
if (!function_exists('dr_roll_dice'))
{
function dr_roll_dice($faces) {
return rand(1, $faces);
}
}
?>