APPLICATION.php 3.0 KB

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