/
Power.php
880 lines (804 loc) · 23 KB
/
Power.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
<?php
/**
* Power
*
* @author Jonathan Borzilleri
*/
class Power extends BasePower
{
private $tmp_keywords = array();
const POWER_ATWILL = '1_atwill';
const POWER_ENCOUNTER = '2_encounter';
const POWER_DAILY = '3_daily';
const POWER_SURGE = '4_surge';
const STATUS_USED = 'Used';
const STATUS_DISABLED = 'Disabled';
const ICON_USABLE = '/images/icon_zap.png';
const ICON_USED = '/images/icon_refresh.png';
const ICON_DISABLED = '/images/icon_skull.png';
/**
* Pre-Delete handling, we must delete our keyword associations first.
*/
public function preDelete($event) {
$this->PowerKeywords->delete();
}
/**
* Post-save handling, update our keywords appropriately.
*/
public function postSave($event) {
// After saving the Power, we need to go through the $tmp_keywords array
// and update any objects there with our ID, and save them.
foreach($this->tmp_keywords as $k) {
$k->power_id = $this->id;
$k->save();
}
}
/**
*
* @global Message
* @return bool
*/
public function updateFromForm() {
global $msg;
$cache = array();
$cache['keywords'] = array();
$cache['error'] = array();
// Power Name
$cache['name'] = $_POST['power_name'];
if( empty($_POST['power_name']) ) {
$msg->add('Name must not be empty.', Message::WARNING);
$cache['error'][] = 'name';
}
else {
$this->name = trim($_POST['power_name']);
}
// Level
$cache['level'] = $_POST['level'];
if( empty($_POST['level']) ||
1 > $_POST['level'] || 30 < $_POST['level'] ) {
$msg->add('Level must be between 1 and 30.', Message::WARNING);
$cache['error'][] = 'level';
}
else {
$this->level = (int)$_POST['level'];
}
// UseType
$cache['use_type'] = $_POST['use_type'];
if( empty($_POST['use_type']) ||
!$this->isValidUseType($_POST['use_type']) ) {
$msg->add('Usage Type must be "at-will", "encounter", or "daily."',
Message::WARNING);
$cache['error'][] = 'use_type';
}
else {
$this->use_type = $_POST['use_type'];
}
// Action Type
$cache['action_type'] = $_POST['action_type'];
if( empty($_POST['action_type']) ||
!$this->isValidActionType($_POST['action_type']) ) {
$msg->add('Invalid action type.', Message::WARNING);
$cache['error'][] = 'action_type';
}
else {
$this->action = $_POST['action_type'];
}
// Attack Stat
$cache['attack_ability'] = $_POST['attack_ability'];
if( empty($_POST['attack_ability']) ||
!$this->isValidAttackStat($_POST['attack_ability']) ) {
$msg->add('Invalid Attack Stat', Message::WARNING);
$cache['error'][] = 'attack_ability';
}
else {
$this->attack_ability = $_POST['attack_ability'];
}
// Attack Defense
$cache['defense'] = $_POST['defense'];
if( empty($_POST['defense']) ||
!$this->isValidDefense($_POST['defense']) ) {
$msg->add('Invalid Defense', Message::WARNING);
$cache['error'][] = 'defense';
}
else {
$this->defense = $_POST['defense'];
}
// Attack Bonus
$cache['attack_bonus'] = $_POST['attack_bonus'];
if( !empty($_POST['attack_bonus']) && !is_numeric($_POST['attack_bonus'])) {
$msg->add('Attack Bonus must be an integer.', Message::WARNING);
$cache['error'][] = 'attack_bonus';
}
else {
$this->attack_bonus = (int)trim(@$_POST['attack_bonus']);
}
// Sustain Action
$cache['sustain_action'] = $_POST['sustain_action'];
$cache['sustain'] = $_POST['sustain'];
if( empty($_POST['sustain_action']) ||
!$this->isValidSustainAction($_POST['sustain_action']) ) {
$msg->add('Invalid Sustain Action', Message::WARNING);
$cache['error'][] = 'sustain_action';
}
else {
$this->sustain_action = $_POST['sustain_action'];
$this->sustain = trim(@$_POST['sustain']);
}
// Magic Item
$this->item = !empty($_POST['item'])?1:0;
$cache['item'] = !empty($_POST['item']);
$this->target = trim(@$_POST['target']);
$cache['target'] = $_POST['target'];
$this->power_range = trim(@$_POST['power_range']);
$cache['power_range'] = $_POST['power_range'];
$this->hit = trim(@$_POST['hit']);
$cache['hit'] = $_POST['hit'];
$this->miss = trim(@$_POST['miss']);
$cache['miss'] = $_POST['miss'];
$this->effect = trim(@$_POST['effect']);
$cache['effect'] = $_POST['effect'];
$this->notes = trim(@$_POST['notes']);
$cache['notes'] = $_POST['notes'];
// Power Keywords
if( !empty($_POST['keywords']) && is_array($_POST['keywords']) ) {
// First: Iterate through the _POST keyword array
// For any keyword we don't already have, make a new PowerKeyword object
// and add it to the tmp_keywords array
foreach($_POST['keywords'] as $k_id) {
$cache['keywords'][$k_id] = true;
if( !$this->Keywords->contains($k_id) ) {
$k = new PowerKeyword;
$k->keyword_id = $k_id;
$this->tmp_keywords[] = $k;
}
}
// Second: Iterate through our existing keywords,
// Any that DO NOT exist in the _POST array should be unlinked
$keywords_to_unlink = array();
foreach($this->Keywords as $k) {
if( !in_array($k->id, $_POST['keywords']) ) {
$keywords_to_unlink[] = $k->id;
}
}
if( !empty($keywords_to_unlink) ) {
$this->unlink('Keywords', $keywords_to_unlink);
}
}
// Update the form cache in the session if necessary.
if( !empty($_POST['form_key']) ) {
if( empty($cache['error']) ) {
unset($_SESSION[$_POST['form_key']]);
}
else {
$cache['form_key'] = $_POST['form_key'];
$_SESSION['form_cache'] = $cache;
}
}
return empty($cache['error']);
}
/**
* Determines if an action type is valid.
*
* @param string $action The action type to validate.
* @return bool
**/
private function isValidActionType($action) {
switch($this->action) {
case 'standard':
case 'move':
case 'minor':
case 'free':
case 'interrupt':
case 'reaction':
case 'none':
return true;
break;
default:
return false;
break;
}
}
/**
* Determines if an attack ability score is valid
*
* @param string $stat the Ability score name to validate
* @return bool
*/
private function isValidAttackStat($stat) {
switch($stat) {
case 'Str':
case 'Con':
case 'Dex':
case 'Int':
case 'Wis':
case 'Cha':
case 'none':
return true;
break;
default:
return true;
break;
}
}
/**
* Determines if a defense is valid
*
* @param string $def the Defense to validate
* @return bool
*/
private function isValidDefense($def) {
switch($def) {
case 'AC':
case 'Fort':
case 'Ref':
case 'Will':
return true;
break;
default:
return false;
break;
}
}
/**
* Determines if the sustaion action type is valid
*
* @param string $action The sustain action to validate.
* @return bool
*/
private function isValidSustainAction($action) {
switch($action) {
case 'Standard':
case 'Move':
case 'Minor':
case 'Free':
case 'none':
return true;
break;
default:
return false;
break;
}
}
/**
* Determines if the usage is valid
*
* @param string $type The usage type to validate
* @return bool
*/
public function isValidUseType($type) {
switch($type) {
case self::POWER_ATWILL:
case self::POWER_ENCOUNTER:
case self::POWER_DAILY:
case self::POWER_SURGE;
return true;
break;
default:
return false;
break;
}
}
/**
* Determines if this power is of the usage type passed in
*
* @param string $type A POWER_* class constant.
* @return bool
*/
public function isUseType($type) {
return $type == $this->use_type;
}
/**
* Return a friendly display string for the power's usage type
*
* @param bool $noSpace Strip spaces out of the returned string
* @return string
*/
public function getUseTypeDisplay($noSpace = false) {
$s = '';
switch($this->use_type) {
case self::POWER_SURGE:
$s = 'Healing Surge';
break;
case self::POWER_ENCOUNTER:
$s = 'Encounter';
break;
case self::POWER_DAILY:
$s = 'Daily';
break;
case self::POWER_ATWILL:
default:
$s = 'At-Will';
break;
}
if( $noSpace ) $s = str_replace(' ', '-', $s);
return $s;
}
/**
* Refresh the power, allowing it to be used again.
*
* @param bool $overrideCost Override any healing surge requirement
* @return bool
*/
public function refresh($overrideCost = false) {
if( $this->isUseType(Power::POWER_SURGE) && !$overrideCost ) {
if( $this->Player->subtractSurge() ) {
$this->used = false;
return true;
}
}
else {
$this->used = false;
return true;
}
}
/**
* Use a power, expending it.
*
* At-Will Powers cannot be expended.
*
* @param bool #overrideCost Override any cost for using the power.
* @return bool
*/
public function usePower($overrideCost = false) {
if( !$overrideCost && $this->item &&
self::POWER_DAILY == $this->use_type ) {
// We're a magic item daily ability,
// so on use we must remove a magic item usage
if( $this->Player->subtractMagicItemUse() ) {
$this->used = true;
return true;
}
}
elseif( self::POWER_ATWILL != $this->use_type ) {
$this->used = true;
return true;
}
return false;
}
/**
* Toggle a power's used state.
*
* At-Will Powers cannot be toggled.
*
* @param bool $overrideCost Override any cost for using/refreshing the power
* @return bool
*/
public function togglePower($overrideCost = false) {
if( $this->used ) return $this->refresh($overrideCost);
else return $this->usePower($overrideCost);
}
/**
* Return a friendly display string for an action type.
*
* @return string
*/
public function getActionTypeDisplay() {
switch($this->action) {
case 'move':
return 'Move Action';
break;
case 'minor':
return 'Minor Action';
break;
case 'free':
return 'Free Action';
break;
case 'interrupt':
return 'Immediate Interrupt';
break;
case 'reaction':
return 'Immediate Reaction';
break;
case 'none':
return 'No Action';
break;
case 'standard':
default:
return "Standard Action";
break;
}
}
/**
* Perform post-processing on text fields
*
* @param string $field The field to process.
* @param bool $echo Whether to echo the result.
* @return string
*/
public function getTextFieldDisplay($field, $echo = false) {
$out = $this->$field;
$out = htmlentities($out);
$out = nl2br($out);
switch($field) {
case 'power_range':
$out = preg_replace('/(melee)/i', '<label>$1</label>',$out);
$out = preg_replace('/(ranged)/i', '<label>$1</label>',$out);
$out = preg_replace('/(area)/i', '<label>$1</label>',$out);
$out = preg_replace('/(close)/i', '<label>$1</label>',$out);
$out = preg_replace('/(personal)/i', '<label>$1</label>',$out);
case 'hit':
// Secondary Attack
$out = preg_replace('/(secondary attack:)/i','<label>$1</label>',$out);
// Secondary Taraget
$out = preg_replace('/(secondary target:)/i','<label>$1</label>',$out);
// Hit
$out = preg_replace('/(hit:)/i', '<label>$1</label>', $out);
// Aftereffect
$out = preg_replace('/(aftereffect:)/i', '<label>$1</label>', $out);
break;
case 'notes':
// Special
$out = preg_replace('/(special:)/i', '<label>$1</label>', $out);
// Trigger
$out = preg_replace('/(trigger:)/i', '<label>$1</label>', $out);
break;
default:
break;
}
if( $echo ) echo $out;
return $out;
}
/**
* Check to see if a power has a given keywords
*
* @param string $keyword A Keyword string
* @return bool
*/
public function hasKeyword($keyword) {
$k = Doctrine::getTable('Keyword')->findOneByName($keyword);
return ($k && $k->exists() && $this->Keywords->contains($k->id));
}
/**
* Return an array of attack bonuses for this power.
*
* @uses Player::getMod()
* @uses Player::getAttackBonus()
* @return array
*/
public function getAttackBonusTable() {
$power_bonus = $this->attack_bonus;
$power_bonus += (int)$this->Player->getMod($this->attack_ability);
if( $this->hasKeyword('Implement') ) {
return $this->Player->getAttackBonus(
Player::ATTACK_IMPLEMENT, $power_bonus);
}
elseif( $this->hasKeyword('Weapon') ) {
return $this->Player->getAttackBonus(
Player::ATTACK_WEAPON, $power_bonus);
}
else {
return $this->Player->getAttackBonus(null, $power_bonus);
}
}
/**
* Generate a friendly attack bonus display
*
* @uses getAttackBonusTable()
* @return string
*/
public function getAttackBonusDisplay() {
$bonus_table = $this->getAttackBonusTable();
foreach($bonus_table as $k => $b) {
if( $b >= 0 ) {
$bonus_table[$k] = '+'.$bonus_table[$k];
}
}
return implode('/',$bonus_table);
}
/**
*
*/
public function getClassDisplay() {
if( $this->item ) return 'Magic Item';
else return $this->Player->Archetype->name;
}
/**
*
*/
public function isUsable() {
return !$this->used;
}
/**
*
*/
public function isDisabled() {
return $this->item && $this->use_type == self::POWER_DAILY &&
0 == $this->Player->magic_item_uses;
}
public function getUsageStatus() {
if( $this->used ) {
return self::STATUS_USED;
}
elseif( $this->isDisabled() ) {
return self::STATUS_DISABLED;
}
return '';
}
public function getUsageIcon() {
if( $this->used ) {
return self::ICON_USED;
}
elseif( $this->isDisabled() ) {
return self::ICON_DISABLED;
}
else {
return self::ICON_USABLE;
}
}
/**
* Retrieve a cached value for a property if one exists.
*
* If a field is passed in that is not contained in the object,
* we return false.
*
* @param string $field Property name to retrieve
* @param string $form_key Key name for the form cache to check
* @return mixed The cached value, or the internal value if no cached value
*/
public function getCached($field, $form_key = null) {
if( $this->contains($field) ) {
if( !empty($form_key) && !empty($_SESSION['form_cache']) &&
$_SESSION['form_cache']['form_key'] == $form_key &&
array_key_exists($field, $_SESSION['form_cache']) ) {
return $_SESSION['form_cache'][$field];
}
else {
return $this->$field;
}
}
return false;
}
public function hasCachedKeyword($kw_id, $form_key = null) {
if( !empty($form_key) && !empty($_SESSION['form_cache']) &&
$_SESSION['form_cache']['form_key'] == $form_key ) {
return array_key_exists($kw_id, $_SESSION['form_cache']['keywords']);
}
return $this->Keywords->contains($kw_id);
}
public function hasError($field, $form_key = null) {
return ( $this->contains($field) && !empty($form_key) &&
!empty($_SESSION['form_cache']) &&
$_SESSION['form_cache']['form_key'] == $form_key &&
!empty($_SESSION['form_cache']['error']) &&
in_array($field, $_SESSION['form_cache']['error']) );
}
/**
* Generates the html for a power box and returns it.
*
* @uses getUseTypeDisplay()
* @uses getTextFieldDisplay()
* @uses getActionTypeDisplay()
* @uses getAttackBonusDisplay()
* @uses Archetype::$name
* @param bool $collapseUsed Whether to collapse the power box to it's title
* bar based on the used property.
* @param bool $echo Whether to print the generated box or return it.
* @return string
*/
public function getPowerBoxDisplay($collapseUsed = false, $echo = false) {
$box = ""; $i = 0;
// Outer div
$box .= '<div class="powerBox'.($this->isDisabled()?' disabledPower':'')
.'" id="powerBox'.$this->id.'">';
// TitleBar
$box .= '<div id="p'.$this->id.'" class="titleBar '.
$this->getUseTypeDisplay(true).'">';
// Power Class/Level
$box .= '<span class="powerLevel">'.$this->getClassDisplay().
' '.$this->level.'</span>';
// Power Title
$box .= '<span class="powerTitle">'.$this->name.'</span>';
$box .= '</div><!-- end div.titleBar -->';
// End TitleBar
// Description
// Only add the 'usedPower' class if the power is used AND we're
// collapsing powers.
$box .='<div class="description '.
($collapseUsed&&$this->used?'usedPower':'').'">';
// Statblock Row
$box .= '<div class="row'.($i%2).'" id="p'.$this->id.'usage">'; $i+=1;
// Usage/Keywords
$box .= '<div>';
// Usage
$box .= '<span class="usage">'.$this->getUseTypeDisplay().'</span>';
// Keywords
if( $this->Keywords->count() ) {
$box .= ' ♦ <span class="keywords">';
$first = true;
foreach($this->Keywords as $k) {
$box .= (!$first?', ':'').$k->name;
$first = false;
}
$box .= '</span>';
}
$box .= '</div>';
// End Usage/Keywords
// Action/Range
$box .= '<div>';
// Range
if( !empty($this->power_range) ) {
$box .= '<span class="range">'.
$this->getTextFieldDisplay('power_range').'</span>';
}
// Action Type
$box .= '<span class="actionType">'.$this->getActionTypeDisplay().'</span>';
$box .= '</div>';
// End Action/Range
// Target
if( !empty($this->target) ) {
$box .= '<div><label>Target: </label><span>'.
$this->getTextFieldDisplay('target').'</span></div>';
}
// Attack
if( 'none' != $this->attack_ability ) {
$box .= '<div><label>Attack: </label><span>';
$box .= $this->getAttackBonusDisplay().' ('.$this->attack_ability.')';
$box .= ' vs. '.$this->defense.'</span></div>';
}
// End Statblock Row
$box .= '</div>';
// Hit
if( !empty($this->hit) ) {
$box .= '<div class="row'.($i%2).'" id="p'.$this->id.'hit">'; $i+=1;
$box .= '<label>Hit: </label>';
$box .= '<span>'
.$this->scanAndParseText($this->getTextFieldDisplay('hit'))
.'</span></div>';
}
// Miss
if( !empty($this->miss) ) {
$box .= '<div class="row'.($i%2).'" id="p'.$this->id.'miss">'; $i+=1;
$box .= '<label>Miss: </label><span>';
$box .= '<span>'
.$this->scanAndParseText($this->getTextFieldDisplay('miss'))
.'</span></div>';
}
// Effect
if( !empty($this->effect) ) {
$box .= '<div class="row'.($i%2).'" id="p'.$this->id.'effect">'; $i+=1;
$box .= '<label>Effect: </label>';
$box .= '<span>'
.$this->scanAndParseText($this->getTextFieldDisplay('effect'))
.'</span></div>';
}
// Sustain
if( 'none'!=$this->sustain_action ) {
$box .= '<div class="row'.($i%2).'" id="p'.$this->id.'sustain">'; $i+=1;
$box .= '<label>Sustain '.$this->sustain_action.': </label>';
$box .= '<span>'
.$this->scanAndParseText($this->getTextFieldDisplay('sustain'))
.'</span></div>';
}
// Notes
if( !empty($this->notes) ) {
$box .= '<div class="row'.($i%2).'" id="p'.$this->id.'notes">'; $i+=1;
$box .= '<label>Notes: </label>';
$box .= '<span>'
.$this->scanAndParseText($this->getTextFieldDisplay('notes'))
.'</span></div>';
}
// End Description
$box .= '</div><!-- end div.description -->';
// End Outer Div
$box .= '</div><!-- end div#powerBox'.$this->id.' -->';
$box .= "\n\n";
if( $echo ) echo $box;
return $box;
}
/**
*
*/
public function getStringReplacement($string, $multiplier = 1) {
$result = '';
switch(strtolower($string)) {
// Main Hand Base Weapon Damage
case 'mw':
$dice = $this->Player->parseDiceString($this->Player->weapon_main_dice);
$result = ($dice['num']*$multiplier).'d'.$dice['size'];
break;
// Off Hand Base Weapon Damage
case 'ow':
$dice = $this->Player->parseDiceString($this->Player->weapon_off_dice);
$result = ($dice['num']*$multiplier).'d'.$dice['size'];
break;
// Ability Modifier tags
case 'str':
case 'con':
case 'dex':
case 'int':
case 'wis':
case 'cha':
$result = $this->Player->getMod($string);
$result = $result * $multiplier;
break;
// Main Hand Bonus Damage
case 'mdam':
$result = $this->Player->weapon_main_damage;
$result = $result * $multiplier;
break;
// Off Hand Bonus Damage
case 'odam':
$result = $this->Player->weapon_off_damage;
$result = $result * $multiplier;
break;
// Implement Bonus Damage
case 'idam':
$result = $this->Player->implement_damage;
$result = $result * $multiplier;
break;
case 'surge':
$result = $this->Player->surge_value;
$result = $result * $multiplier;
break;
case 'lvl':
$result = $this->Player->level;
$result = $result * $multiplier;
break;
case 'hlvl':
$result = floor($this->Player->level/2);
$result = $result * $multiplier;
break;
}
return $result;
}
/**
*
*/
public function parseTag($tag) {
$result = '';
$inner_tag = substr($tag, 1, -1);
preg_match_all('/[\+\-]?\d*\w*/i', $inner_tag, $matches);
foreach($matches[0] as $k => $s) {
// Don't operate on 'empty' strings
if( !empty($s) ) {
// Figure out what operation we're doing
// Addition (+) or subtraction (-);
$op = substr($s,0,1);
if( '+' == $op || '-' == $op ) {
$textString = substr($s, 1);
}
else {
$textString = $s;
$op = '+';
}
// Separate our multiplier or constant from the text string
preg_match('/(\d*)(\w*)/i', $textString, $parts);
$multiplier = empty($parts[1])?1:$parts[1];
$textString = $parts[2];
// Grab the replaced string
if( empty($textString) ) {
$temp_result = $multiplier;
}
else {
$temp_result = $this->getStringReplacement(
$textString, $multiplier);
}
// Integrate this string with the overall tag result
// If we're the first one, just set the result
if( 0 == $k ) {
$result = $temp_result;
}
elseif( is_numeric($result) ) {
// Otherwise, if we're numeric, add or subtract it appropriately
if( '+' == $op ) {
$result += $temp_result;
}
elseif( '-' == $op ) {
$result -= $temp_result;
}
}
} // End if(empty($s))
} // endforeach
return $result;
}
public function scanAndParseText($text) {
$result = $text;
if( preg_match_all('/(\[.*?\])/i', $text, $matches) ) {
foreach($matches[1] as $tag) {
$result = str_replace($tag, $this->parseTag($tag), $result);
}
}
return $result;
}
}
?>