APPLICATION_Helper.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * App helper file.
  4. *
  5. * Provides a helper to perform app related operations.
  6. *
  7. * @category Helper.
  8. */
  9. /**
  10. * Require dependent files if not present.
  11. */
  12. require_once(PATH::HELPER . "Helper.php");
  13. /**
  14. * Application helper.
  15. *
  16. * Contains app-related utilities.
  17. *
  18. * @category Helper.
  19. */
  20. final class APPLICATION extends Helper{
  21. /**
  22. * Checks if a value is a valid one in a constant class.
  23. *
  24. * It will intentionally fail when used for a non constant class.
  25. *
  26. * @param string $class Name of the class.
  27. * @param int $value Value to check.
  28. * @return bool True if the value is defined, false otherwise.
  29. */
  30. public static function valid_id($class, $value){
  31. if (strtoupper($class) != $class){
  32. return false;
  33. }
  34. if (!class_exists($class, false)){
  35. return false;
  36. }
  37. $reflect = new ReflectionClass($class);
  38. if (in_array($value, $reflect->getConstants())){
  39. return true;
  40. }
  41. else{
  42. return false;
  43. }
  44. }
  45. /**
  46. * Forms an image URL.
  47. *
  48. * It will intentionally fail when used for a non constant class.
  49. *
  50. * @param string $type Image type. valid values are:
  51. * ICON, LOGO, CURRENCY, UNIT,
  52. * AREA, SKILL, ESSENCE, RUNE,
  53. * GRIND, ELEMENT, SKILL_GUILD, DECORATION,
  54. * SOURCE, SKILL_LEADER, INVENTORY, EFFECT,
  55. * BUILDING
  56. * @param string|int $name Filename, with no path or extension. It can also
  57. * skip the padding. An id can be used.
  58. * @return string URL if found. If not, the URL to an 'unknown' icon.
  59. */
  60. public static function img($type, $name){
  61. $pad = 1;
  62. $url = URL::IMG["UNKNOWN"];
  63. while ($pad < 10){
  64. if (isset(PATH::IMG[$type]) && file_exists(PATH::IMG[$type] . str_pad($name, $pad, '0', STR_PAD_LEFT) . ".png")){
  65. $url = URL::IMG[$type] . str_pad($name, $pad, '0', STR_PAD_LEFT) . ".png";
  66. break;
  67. }
  68. $pad ++;
  69. }
  70. return $url;
  71. }
  72. /**
  73. * Calculates the effective damage.
  74. *
  75. * @param int $atk Attack stat.
  76. * @param int $crr Critical rate stat.
  77. * @param int $crd Critical damage stat.
  78. * @return int Damage stat.
  79. */
  80. public static function calculate_dmg($atk, $crr, $crd){
  81. if (! is_numeric($atk) || ! is_numeric($crr) || ! is_numeric($crd)){
  82. return 0;
  83. }
  84. if ($crr > 100){
  85. $crr = 100;
  86. }
  87. $edmg = ceil(($atk * (100 - $crr) / 100) + (($atk + ($atk * $crd / 100)) * $crr / 100));
  88. return $edmg;
  89. }
  90. /**
  91. * Calculates the effective HP of a unit.
  92. * @param int $hp HP stat
  93. * @param int $def Defense stat.
  94. * @return int Effective HP.
  95. */
  96. public static function calculate_ehp($hp, $def){
  97. $ehp = ceil(((($def * 3.5) + 1140) * $hp) / 1000);
  98. return $ehp;
  99. }
  100. }
  101. ?>