1 <?php
2 3 4 5 6 7 8 9 10 11 12 13 14
15
16 App::uses('Helper', 'View');
17 App::uses('Folder', 'Utility');
18 App::uses('File', 'Utility');
19
20 class XlrfunctionsHelper extends Helper {
21 public $helpers = array(
22 'Html',
23 'Number'
24 );
25
26
27
28 29 30 31 32 33 34 35 36 37 38
39 public function ratioSparklineBulletChart($value, $options = array()) {
40 $options += array('scripts' => true);
41 if ($options['scripts']) {
42 echo $this->Html->script(array('jquery.sparkline', 'xlrstats.sparkline.player.ratio.bullet'));
43 }
44 unset($options['scripts']);
45
46
47 return "<span class='ratiosparkline'>$value,5,5</span>";
48 }
49
50
51
52 53 54 55 56 57 58 59 60 61 62
63 public function levelSparklinePieChart($value, $options = array()) {
64 $options += array('scripts' => true);
65 if ($options['scripts']) {
66 echo $this->Html->script(array('jquery.sparkline', 'xlrstats.sparkline.player.level.pie'));
67 }
68 unset($options['scripts']);
69
70 $diff = 100 - $value;
71 return "<span class='levelsparkline'>$value,$diff</span>";
72 }
73
74
75
76 77 78 79 80 81
82 public function stripColors($text) {
83 $pattern = '/\^[0-9]/';
84 $text = preg_replace($pattern, '', $text);
85 return $text;
86 }
87
88
89
90 91 92 93 94
95 public function isHome() {
96 $here = convertSlash($this->request->here);
97 $installDir = convertSlash($this->request->base);
98
99 if ($here == Configure::read('server_id') || $here == $installDir || $here == $installDir . '_' . Configure::read('server_id') || preg_match('/pages_home/', $here)) {
100 return true;
101 } else {
102 return false;
103 }
104 }
105
106
107
108 109 110 111 112 113 114 115 116
117 public function getCarouselImages() {
118 $serverID = Configure::read('server_id');
119 $gameName = Configure::read('servers.' . $serverID . '.gamename');
120
121 $dir = new Folder(WWW_ROOT . 'img' . DS . 'carousel');
122 $files = $dir->read(true);
123 $folders = $files[0];
124
125 if (in_array($gameName, $folders)) {
126 $dir = new Folder(WWW_ROOT . 'img' . DS . 'carousel' . DS . $gameName);
127 } else {
128 $dir = new Folder(WWW_ROOT . 'img' . DS . 'carousel' . DS . 'default');
129 }
130
131 $files = $dir->read(true, false, true);
132 $files = $files[1];
133
134 foreach($files as $file) {
135 $n = explode('carousel' . DS, $file);
136 $i = explode(DS, $n[1]);
137 $x[] = $i[0] . '/' . $i[1];
138 }
139
140 $files = $x;
141
142 $firstElement = $files[0];
143 unset($files[0]);
144
145 return array($firstElement, $files);
146 }
147
148
149
150 151 152 153 154 155 156 157
158 public function getWeaponName($weapon=null) {
159 $weaponList = Configure::read('weapons');
160
161 if (!isset($weaponList[$weapon][0])) {
162 $weaponName = $weapon;
163 } else {
164 $weaponName = $weaponList[$weapon][0];
165 }
166
167 if(empty($weaponName)) {
168 $weaponName = $weapon;
169 }
170
171 return $weaponName;
172 }
173
174
175
176 177 178 179 180 181 182 183 184 185 186
187 public function getWeaponImage($weapon, $options = array()) {
188 $weaponList = Configure::read('weapons');
189 if (!isset($weaponList[$weapon][2])) {
190 return false;
191 }
192
193 $weaponImage = $weaponList[$weapon][2];
194
195 if($weaponImage == 'image.png') {
196 return false;
197 }
198
199 $imagePath = Configure::read('weapons.image_path');
200 $weaponImage = $imagePath . $weaponImage;
201
202 $imageLink = $this->Html->image($weaponImage, $options);
203
204 return $imageLink;
205 }
206
207
208
209 210 211 212
213 public function getWeaponLink($weapon) {
214 $weaponList = Configure::read('weapons');
215
216 if (!isset($weaponList[$weapon][3])) {
217 $weaponLink = false;
218 } else {
219 $weaponLink = $weaponList[$weapon][3];
220 }
221
222 if(empty($weaponLink)) {
223 $weaponLink = false;
224 }
225
226 return $weaponLink;
227 }
228
229
230
231 232 233 234
235 public function getWeaponDescription($weapon) {
236 $weaponList = Configure::read('weapons');
237
238 if (!isset($weaponList[$weapon][1])) {
239 $weaponDescription = false;
240 } else {
241 $weaponDescription = $weaponList[$weapon][1];
242 }
243
244 if(empty($weaponDescription)) {
245 $weaponDescription = false;
246 }
247
248 return $weaponDescription;
249 }
250
251
252 253 254 255 256 257 258 259
260 public function getMapName($map) {
261 $mapName = Configure::read('maps.' . $map . '.0');
262
263 if(!$mapName) {
264 $mapName = $map;
265 }
266 return $mapName;
267 }
268
269
270
271 272 273 274 275 276 277 278 279 280 281
282 public function getMapImage($map, $options = array()) {
283 $mapList = Configure::read('maps');
284 if (!isset($mapList[$map][2])) {
285 return false;
286 }
287
288 $mapImage = $mapList[$map][2];
289
290 if($mapImage == 'image.png') {
291 return false;
292 }
293
294 $imagePath = Configure::read('maps.image_path');
295 $mapImage = $imagePath . $mapImage;
296
297 $imageOptions = array();
298
299 if(isset($options['style'])) {
300 $imageOptions['style'] = $options['style'];
301 }
302
303 $imageLink = $this->Html->image($mapImage, $imageOptions);
304
305 return $imageLink;
306 }
307
308
309
310 311 312 313
314 public function getMapLink($map) {
315 $mapList = Configure::read('maps');
316
317 if (!isset($mapList[$map][3])) {
318 $mapLink = false;
319 } else {
320 $mapLink = $mapList[$map][3];
321 }
322
323 if(empty($mapLink)) {
324 $mapLink = false;
325 }
326
327 return $mapLink;
328 }
329
330
331
332 333 334 335
336 public function getMapDescription($map) {
337 $mapList = Configure::read('maps');
338
339 if (!isset($mapList[$map][1])) {
340 $mapDescription = false;
341 } else {
342 $mapDescription = $mapList[$map][1];
343 }
344
345 if(empty($mapDescription)) {
346 $mapDescription = false;
347 }
348
349 return $mapDescription;
350 }
351
352
353
354 355 356 357 358 359 360 361 362
363 public function getTeamName($team) {
364 $teamName = Configure::read('teams.' . $team);
365
366 if(!$teamName) {
367 $teamName = $team;
368 }
369 return $teamName;
370 }
371
372
373
374 375 376 377 378 379
380 public function getBodyPartName($bodyPart) {
381 $bodyPartsArray = Configure::read('body_parts');
382
383 if(!$bodyPartsArray) {
384 return array('fixed_name' => null, 'body_part_name' => $bodyPart);
385 }
386
387 $fixedName = null;
388 $bodyPartName = $bodyPart;
389
390 foreach ($bodyPartsArray as $key => $value) {
391 if(array_key_exists($bodyPart, $value)) {
392 $fixedName = $key;
393 $bodyPartName = $value[$bodyPart];
394 }
395 }
396 return array('fixed_name' => $fixedName, 'body_part_name' => $bodyPartName);
397 }
398
399
400
401 402 403 404 405 406 407
408 public function colorizeHitZone($percentage) {
409 $color = 'yellow';
410 if($percentage > 0.2 && $percentage <= 0.35) {
411 $color = 'orange';
412 } elseif ($percentage > 0.35) {
413 $color = '#FF0000';
414 }
415 return $color;
416 }
417
418
419
420 421 422 423 424 425
426 public function getPopulatedLeagueVars() {
427 for($i = 1; $i<=5; $i++) {
428 $leagues[$i] = $this->requestAction('leagues/leaguesJson/' . $i);
429
430
431 if ($leagues[$i][1] == 0) {
432 unset($leagues[$i]);
433 } else {
434 $leagueNumber[] = $i;
435 }
436 }
437
438 $leagueCount = count($leagues);
439
440 switch($leagueCount) {
441 case 0:
442 return false;
443 break;
444 case 1:
445 $span = array('span12');
446 $detailedStats = array(true);
447 break;
448 case 2:
449 $span = array('span6', 'span6');
450 $detailedStats = array(false, false);
451 break;
452 case 3:
453 $span = array('span12', 'span6', 'span6');
454 $detailedStats = array(true, false, false);
455 break;
456 case 4:
457 $span = array('span12', 'span4', 'span4', 'span4');
458 $detailedStats = array(true, false, false, false);
459 break;
460 case 5:
461 unset($leagues[5]);
462 $span = array('span12', 'span4', 'span4', 'span4');
463 $detailedStats = array(true, false, false, false);
464 break;
465 }
466
467 return array($leagues, $leagueNumber, $span, $detailedStats);
468
469 }
470
471
472
473 474 475 476 477 478 479 480 481
482 public function getTopActionData($topActions = array(), $bestAction, $option = 'weapon') {
483 $topActionData = '';
484
485 foreach ($topActions as $action => $score) {
486 if($option == 'weapon') {
487 $actionName = $this->getWeaponName($action);
488 } elseif ($option == 'map') {
489 $actionName = $this->getMapName($action);
490 }
491
492 if($action == $bestAction) {
493 $topActionData .= '{
494 name: "' . $actionName . '",
495 y: '. $score .',
496 sliced: true,
497 selected: true
498 }, ';
499 } else {
500 $topActionData .= '["' . $actionName . '", ' . $score . '], ';
501 }
502 }
503 return $topActionData;
504 }
505
506
507 508 509
510 public function showLicenseIcon()
511 {
512
513 if (Configure::read('Cache.disable')) {
514 return '<a href="#" title="page not cached"><i class="icon-unlock-alt text-error"></i></a> ';
515 } else {
516
517 $json = Cache::read('licenseKeyInfo', '1week');
518 if (!$json) {
519 $json = Cache::read('licenseKeyInfo', '5min');
520 }
521
522 if (!$json) {
523 $icon = '<i class="icon-question-sign text-error"></i>';
524 $message = __('Unable to retrieve license information');
525 } elseif (!$json['result']['valid']) {
526 $message = $json['error']['message'];
527 $icon = '<i class="icon-warning-sign text-error"></i>';
528 } else {
529 if ($json['result']['type'] == 'F') $message = __('Free -NON-Commercial- XLRstats License');
530 else $message = __('Commercial XLRstats License');
531 $icon = '<i class="icon-check text-success"></i>';
532 }
533 return $this->Html->link($icon,
534 'http://www.xlrstats.com/pages/xlrstats.com/licensing', array(
535 'target' => '_blank',
536 'escape' => false,
537 'title' => $message
538 ));
539 }
540 }
541
542 543 544 545
546 public function listPlayers($data) {
547 $result = '<ul>';
548 foreach ($data as $name) {
549 $result .= '<li>' . $name . '</li>';
550 }
551 $result .= '</ul>';
552 $result .= '<small>';
553 $result .= __('...more info on the Online Players and Worldmap page');
554 $result .= '</small>';
555 return $result;
556 }
557
558 559 560 561 562 563 564 565
566 public function fixName($playerName, $fixedName='', $defaultName='Unknown Soldier')
567 {
568 if ($fixedName != '') {
569 $playerName = $fixedName;
570 }
571 if ($playerName == '')
572 $playerName = $defaultName;
573
574 $displayName = $this->sanitizeMe($playerName);
575 return $displayName;
576 }
577
578 579 580 581 582 583 584
585 public function sanitizeMe($str) {
586 return htmlentities($str, ENT_QUOTES, 'UTF-8');
587 }
588 }