/GameEngine/Generator.php

https://github.com/churchillyik/TRSimu · PHP · 202 lines · 181 code · 10 blank · 11 comment · 35 complexity · 23066b12856ff2cafb48adca0b81364e MD5 · raw file

  1. <?php
  2. // 生成器类
  3. class Generator
  4. {
  5. // 生成随机的16位ID,并进行MD5加密
  6. public function generateRandID()
  7. {
  8. return md5($this->generateRandStr(16));
  9. }
  10. // 生成长度为$length的随机字符串
  11. public function generateRandStr($length)
  12. {
  13. $randstr = "";
  14. for ($i = 0; $i < $length; $i++)
  15. {
  16. $randnum = mt_rand(0, 61);
  17. if ($randnum < 10)
  18. {
  19. $randstr .= chr($randnum + 48);
  20. }
  21. elseif ($randnum < 36)
  22. {
  23. $randstr .= chr($randnum + 55);
  24. }
  25. else
  26. {
  27. $randstr .= chr($randnum + 61);
  28. }
  29. }
  30. return $randstr;
  31. }
  32. // 用md5加密字符串,并取前$length字节
  33. public function encodeStr($str, $length)
  34. {
  35. $encode = md5($str);
  36. return substr($encode, 0, $length);
  37. }
  38. // 计算两个坐标之间的行程时间
  39. public function procDistanceTime($coor, $thiscoor, $ref, $mode)
  40. {
  41. global $bid28, $bid14, $building;
  42. if ($thiscoor['x'] > $coor['x'])
  43. {
  44. $xdistance = $thiscoor['x'] - $coor['x'];
  45. }
  46. else
  47. {
  48. $xdistance = $coor['x'] - $thiscoor['x'];
  49. }
  50. if (($coor['x'] < 0 && $thiscoor['x'] > 0) || ($thiscoor['x'] < 0 && $coor['x'] > 0))
  51. {
  52. $xdistance += 1;
  53. }
  54. if ($xdistance >= WORLD_MAX)
  55. {
  56. while ($xdistance >= WORLD_MAX) :
  57. $xdistance -= WORLD_MAX;
  58. endwhile;
  59. }
  60. if ($thiscoor['y'] > $coor['y'])
  61. {
  62. $ydistance = $thiscoor['y'] - $coor['y'];
  63. }
  64. else
  65. {
  66. $ydistance = $coor['y'] - $thiscoor['y'];
  67. }
  68. if (($coor['y'] < 0 && $thiscoor['y'] > 0) || ($thiscoor['y'] < 0 && $coor['y'] > 0))
  69. {
  70. $ydistance += 1;
  71. }
  72. if ($ydistance >= WORLD_MAX)
  73. {
  74. while ($ydistance >= WORLD_MAX) :
  75. $ydistance -= WORLD_MAX;
  76. endwhile;
  77. }
  78. $distance = $xdistance + $ydistance;
  79. if (!$mode)
  80. {
  81. if ($ref == 1)
  82. {
  83. $speed = 16;
  84. }
  85. elseif ($ref == 2)
  86. {
  87. $speed = 24;
  88. }
  89. else
  90. {
  91. $speed = 12;
  92. }
  93. if ($building->getTypeLevel(28) != 0)
  94. {
  95. $speed *= $bid28[$building->getTypeLevel(28)]['attri'] / 100;
  96. }
  97. }
  98. else
  99. {
  100. $speed = $ref;
  101. if ($building->getTypeLevel(14) != 0)
  102. {
  103. $speed *= $bid14[$building->getTypeLevel(14)]['attri'] / 100;
  104. }
  105. }
  106. return round(($distance / $speed) * 3600 / INCREASE_SPEED);
  107. }
  108. // 把秒转化为(时:分:秒)的格式
  109. public function getTimeFormat($time)
  110. {
  111. $min = 0;
  112. $hr = 0;
  113. while ($time >= 60) :
  114. $time -= 60;
  115. $min += 1;
  116. endwhile;
  117. while ($min > 60) :
  118. $min -= 60;
  119. $hr += 1;
  120. endwhile;
  121. if ($min < 10)
  122. {
  123. $min = "0".$min;
  124. }
  125. if ($time < 10)
  126. {
  127. $time = "0".$time;
  128. }
  129. return $hr.":".$min.":".$time;
  130. }
  131. // 把UNIX时间戳转化为一定的格式
  132. public function procMtime($time)
  133. {
  134. $now = time();
  135. if (date("j", $time) == date("j", $now) && date("n", $time) == date("n", $now) && date("y", $time) == date("y", $now))
  136. {
  137. $day = "今天";
  138. }
  139. else
  140. {
  141. $pref = 4;
  142. switch ($pref)
  143. {
  144. case 1:
  145. $day = date("m/j/y", $time);
  146. break;
  147. case 2:
  148. $day = date("j/m/y", $time);
  149. break;
  150. case 3:
  151. $day = date("j.m.y", $time);
  152. break;
  153. case 4:
  154. $day = date("Y-m-d", $time);
  155. break;
  156. default:
  157. $day = date("y/m/j", $time);
  158. break;
  159. }
  160. }
  161. $new = date("H:i", $time);
  162. return array($day, $new);
  163. }
  164. // 取得坐标为x和y的格子ID号,这是[-WORLD_MAX, WORLD_MAX]对正整数的一个既单且满的映射
  165. public function getBaseID($x, $y)
  166. {
  167. return (WORLD_MAX - $y) * (WORLD_MAX * 2 + 1) + (WORLD_MAX + $x + 1);
  168. }
  169. // 取得地图校验码
  170. public function getMapCheck($wref)
  171. {
  172. return substr(md5($wref), 5, 2);
  173. }
  174. // 页面载入开始时间
  175. public function pageLoadTimeStart()
  176. {
  177. $starttime = microtime();
  178. $startarray = explode(" ", $starttime);
  179. $starttime = $startarray[0] + $startarray[1];
  180. return $starttime;
  181. }
  182. // 页面载入结束时间
  183. public function pageLoadTimeEnd()
  184. {
  185. $endtime = microtime();
  186. $endarray = explode(" ", $endtime);
  187. $endtime = $endarray[0] + $endarray[1];
  188. return $endtime;
  189. }
  190. };
  191. $generator = new Generator;
  192. ?>