shaker_helper.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 16/6/28
  6. * Time: 上午11:33
  7. */
  8. require_once (BASE_ROOT_PATH . '/helper/account_helper.php');
  9. require_once (BASE_ROOT_PATH . '/helper/predeposit_helper.php');
  10. class friend
  11. {
  12. //last_time,count,user_id
  13. private $mParam;
  14. public function __construct($param)
  15. {
  16. $this->mParam = $param;
  17. }
  18. public function last_time() {
  19. return intval($this->mParam['last_time']);
  20. }
  21. public function can_gain() {
  22. return (time() - intval($this->mParam['last_time']) >= shaker_helper::max_gain_period);
  23. }
  24. public function can_gain_system() {
  25. return (time() - intval($this->mParam['last_time']) >= shaker_helper::max_gain_period_system);
  26. }
  27. public function can_lost() {
  28. return (time() - intval($this->mParam['last_time']) >= shaker_helper::max_lost_period);
  29. }
  30. public function user_id() {
  31. return intval($this->mParam['user_id']);
  32. }
  33. public function count() {
  34. return intval($this->mParam['count']);
  35. }
  36. }
  37. class gain_policy
  38. {
  39. const max_amount = 50;
  40. private $strength;
  41. private $total_amount;
  42. public function __construct($strength,$amount) {
  43. $this->strength = $strength;
  44. $this->total_amount = $amount;
  45. }
  46. public function calculate()
  47. {
  48. $scale = $this->scale();
  49. if($scale == false) {
  50. return false;
  51. }
  52. $cur_value = intval($this->total_amount) >= self::max_amount ? self::max_amount : $this->total_amount;
  53. $value = intval($this->scale() * $cur_value * 100 + 0.5);
  54. if($value == 0) {
  55. return false;
  56. }
  57. else {
  58. return $value / 100;
  59. }
  60. }
  61. private function scale()
  62. {
  63. if($this->strength == 5) {
  64. $start = 0.8;
  65. $end = 1.0;
  66. }
  67. elseif ($this->strength == 4) {
  68. $start = 0.6;
  69. $end = 0.8;
  70. }
  71. elseif ($this->strength == 3) {
  72. $start = 0.4;
  73. $end = 0.6;
  74. }
  75. elseif ($this->strength == 2) {
  76. $start = 0.2;
  77. $end = 0.4;
  78. }
  79. elseif ($this->strength == 1) {
  80. $start = 0.0;
  81. $end = 0.2;
  82. }
  83. else {
  84. return false;
  85. }
  86. $start = intval($start * 100 + 0.5);
  87. $end = intval($end * 100 + 0.5);
  88. $ret = mt_rand($start,$end);
  89. return $ret / 100;
  90. }
  91. }
  92. class shaker_helper
  93. {
  94. const max_gain_period = 12 * 3600;
  95. const max_gain_period_system = 300;
  96. const max_lost_period = 1800;
  97. const err_msg = '您此次么有抢到好友红包...邀请更多好友,更多机会哦~';
  98. const shake_expire = 5;
  99. const direct_gain = 0;
  100. const direct_lost = 1;
  101. const max_strength = 5;
  102. private $strength = 1;
  103. private function gain(&$err)
  104. {
  105. $friends = $this->gain_friends();
  106. $usable_amount = 0.00;
  107. $user_id = $this->find_gain_friend($friends, $usable_amount);
  108. if ($user_id != false)
  109. {
  110. $policy = new gain_policy($this->strength, $usable_amount);
  111. $value = $policy->calculate();
  112. if($value == false) {
  113. $err = array('code' => errcode::ErrShake, 'msg' => self::err_msg);
  114. return false;
  115. }
  116. $bonus = account_helper::gain_bonus($user_id, $_SESSION['member_id'], $value);
  117. if($bonus != false) {
  118. $this->add_gained_friend(array($user_id));
  119. } else {
  120. $err = array('code' => errcode::ErrShake, 'msg' => self::err_msg);
  121. }
  122. return $bonus;
  123. }
  124. else
  125. {
  126. if ($this->gained_system() == false) {
  127. $err = array('code' => errcode::ErrShake, 'msg' => self::err_msg);
  128. return false;
  129. }
  130. else
  131. {
  132. $policy = new gain_policy($this->strength, 10);
  133. $value = $policy->calculate();
  134. if($value == false) {
  135. $err = array('code' => errcode::ErrShake, 'msg' => self::err_msg);
  136. return false;
  137. }
  138. $bonus = account_helper::gain_sysBonus($_SESSION['member_id'], $value);
  139. if($bonus != false) {
  140. $this->save_gained_system();
  141. }
  142. return $bonus;
  143. }
  144. }
  145. }
  146. private function lost(&$err)
  147. {
  148. $predo = new predeposit_helper($_SESSION['member_id']);
  149. $total_amount = $predo->get_predeposit_total();
  150. if(intval($total_amount * 100 + 0.5) <= 0) {
  151. $err = array('code' => errcode::ErrShake,'msg' => self::err_msg);
  152. return false;
  153. }
  154. $friends = $this->lost_friends();
  155. $user_id = $this->find_lost_friend($friends);
  156. if($user_id == false) {
  157. $err = array('code' => errcode::ErrShake, 'msg' => self::err_msg);
  158. return false;
  159. }
  160. $policy = new gain_policy($this->strength, $total_amount);
  161. $value = $policy->calculate();
  162. if($value == false) {
  163. $err = array('code' => errcode::ErrShake, 'msg' => self::err_msg);
  164. return false;
  165. }
  166. $bonus = account_helper::lost_bonus($_SESSION['member_id'],$user_id, $value);
  167. if($bonus != false) {
  168. $this->add_lost_friend(array($user_id));
  169. } else {
  170. $err = array('code' => errcode::ErrShake, 'msg' => self::err_msg);
  171. }
  172. return $bonus;
  173. }
  174. private function find_lost_friend($friends)
  175. {
  176. while (true)
  177. {
  178. $count = count($friends);
  179. if($count == 0) {
  180. return false;
  181. }
  182. $pos = mt_rand(0,$count -1);
  183. return $friends[$pos];
  184. }
  185. }
  186. private function find_gain_friend($friends, &$usable_amount)
  187. {
  188. while (true)
  189. {
  190. $count = count($friends);
  191. if($count == 0) return false;
  192. $pos = mt_rand(0,$count -1);
  193. $mod_bonus = Model('user_bonus');
  194. $usable_amount = $mod_bonus->getUsableSum($friends[$pos]);
  195. if(intval($usable_amount * 100 + 0.5) > 0) {
  196. return $friends[$pos];
  197. } else {
  198. array_splice($friends,$pos,1);
  199. }
  200. }
  201. }
  202. public function shake($strength,&$err)
  203. {
  204. if($this->can_shake() == false) {
  205. $period = self::shake_expire;
  206. $err = array('code' => errcode::ErrShake,'msg' => "摇得太频繁啦,{$period}秒钟只能摇一次~");
  207. return false;
  208. }
  209. $this->strength = $strength;
  210. $direct = $this->direction();
  211. if($direct == self::direct_gain) {
  212. $bonuses = $this->gain($err);
  213. }
  214. else {
  215. $bonuses = $this->lost($err);
  216. }
  217. if($bonuses != false) {
  218. $this->save_shake();
  219. }
  220. return $bonuses;
  221. }
  222. private function save_gained_system()
  223. {
  224. if(isset($_SESSION['game_shake']['gained_system'])) {
  225. $_SESSION['game_shake']['gained_system'] = array();
  226. }
  227. $system = &$_SESSION['game_shake']['gained_system'];
  228. if(empty($system)) {
  229. $system['user_id'] = $_SESSION['member_id'];
  230. $system['last_time'] = time();
  231. $system['count'] = 1;
  232. } else {
  233. $system['user_id'] = $_SESSION['member_id'];
  234. $system['last_time'] = time();
  235. $system['count'] = intval($system['count']) + 1;
  236. }
  237. }
  238. private function gained_system()
  239. {
  240. if(!isset($_SESSION['game_shake']['gained_system'])) {
  241. return true;
  242. } else {
  243. $param = $_SESSION['game_shake']['gained_system'];
  244. $friend = new friend($param);
  245. return $friend->can_gain_system();
  246. }
  247. }
  248. private function add_gained_friend($user_ids)
  249. {
  250. if(!isset($_SESSION['game_shake']['gained_friends'])) {
  251. $_SESSION['game_shake']['gained_friends'] = array();
  252. }
  253. $friends = &$_SESSION['game_shake']['gained_friends'];
  254. foreach ($user_ids as $user_id)
  255. {
  256. if(isset($friends[$user_id])) {
  257. $friends[$user_id]['user_id'] = $user_id;
  258. $friends[$user_id]['last_time'] = time();
  259. $friends[$user_id]['count'] = intval($friends[$user_id]['count']) + 1;
  260. } else {
  261. $friends[$user_id]['user_id'] = $user_id;
  262. $friends[$user_id]['last_time'] = time();
  263. $friends[$user_id]['count'] = 1;
  264. }
  265. }
  266. }
  267. private function add_lost_friend($user_ids)
  268. {
  269. if(!isset($_SESSION['game_shake']['losted_friends'])) {
  270. $_SESSION['game_shake']['losted_friends'] = array();
  271. }
  272. $friends = &$_SESSION['game_shake']['losted_friends'];
  273. foreach ($user_ids as $user_id)
  274. {
  275. if(isset($friends[$user_id])) {
  276. $friends[$user_id]['user_id'] = $user_id;
  277. $friends[$user_id]['last_time'] = time();
  278. $friends[$user_id]['count'] = intval($friends[$user_id]['count']) + 1;
  279. } else {
  280. $friends[$user_id]['user_id'] = $user_id;
  281. $friends[$user_id]['last_time'] = time();
  282. $friends[$user_id]['count'] = 1;
  283. }
  284. }
  285. }
  286. //可以偷的好友
  287. private function gain_friends()
  288. {
  289. if(!isset($_SESSION['game_shake']['gained_friends'])) {
  290. $_SESSION['game_shake']['gained_friends'] = array();
  291. }
  292. $friends = $_SESSION['game_shake']['gained_friends'];
  293. $exids = array();
  294. foreach ($friends as $uid => $param)
  295. {
  296. $friend = new friend($param);
  297. if($friend->can_gain() == false) {
  298. array_push($exids,$friend->user_id());
  299. }
  300. }
  301. $subscriber = relation_helper::subscriber($_SESSION['member_id']);
  302. $follower = relation_helper::follower($_SESSION['member_id']);
  303. $all_friends = array_merge($subscriber,$follower);
  304. sort($all_friends,SORT_NUMERIC);
  305. $all_friends = array_merge(array_unique($all_friends,SORT_NUMERIC),[]);
  306. sort($all_friends);
  307. foreach ($exids as $uid)
  308. {
  309. $pos = algorithm::bsearch($uid,$all_friends);
  310. if($pos != -1) {
  311. array_splice($all_friends,$pos,1);
  312. }
  313. }
  314. return $all_friends;
  315. }
  316. private function lost_friends()
  317. {
  318. if(!isset($_SESSION['game_shake']['losted_friends'])) {
  319. $_SESSION['game_shake']['losted_friends'] = array();
  320. }
  321. $friends = $_SESSION['game_shake']['losted_friends'];
  322. $exids = array();
  323. foreach ($friends as $param)
  324. {
  325. $friend = new friend($param);
  326. if($friend->can_lost() == false) {
  327. array_push($exids,$friend->user_id());
  328. }
  329. }
  330. $subscriber = relation_helper::subscriber($_SESSION['member_id']);
  331. $follower = relation_helper::follower($_SESSION['member_id']);
  332. $all_friends = array_merge($subscriber,$follower);
  333. sort($all_friends,SORT_NUMERIC);
  334. $all_friends = array_merge(array_unique($all_friends,SORT_NUMERIC),[]);
  335. sort($all_friends,SORT_NUMERIC);
  336. foreach ($exids as $uid)
  337. {
  338. $pos = algorithm::bsearch($uid,$all_friends);
  339. if($pos != -1) {
  340. array_splice($all_friends,$pos,1);
  341. }
  342. }
  343. return $all_friends;
  344. }
  345. private function can_shake()
  346. {
  347. if(!isset($_SESSION['game_shake'])) {
  348. $_SESSION['game_shake'] = array();
  349. }
  350. if(isset($_SESSION['game_shake']['last_time']))
  351. {
  352. $last_time = intval($_SESSION['game_shake']['last_time']);
  353. if($last_time + self::shake_expire >= time()) {
  354. return false;
  355. } else {
  356. return true;
  357. }
  358. }
  359. else {
  360. return true;
  361. }
  362. }
  363. private function save_shake()
  364. {
  365. if(!isset($_SESSION['game_shake'])) {
  366. $_SESSION['game_shake'] = array();
  367. }
  368. $_SESSION['game_shake']['last_time'] = time();
  369. }
  370. private function direction()
  371. {
  372. if(!isset($_SESSION['game_shake'])) {
  373. $_SESSION['game_shake'] = array();
  374. }
  375. if(!isset($_SESSION['game_shake']['direction'])) {
  376. $_SESSION['game_shake']['direction'] = self::direct_gain;
  377. }
  378. else
  379. {
  380. $rand = mt_rand(1,100);
  381. $direct = ($rand % 5) == 0 ? self::direct_lost : self::direct_gain;
  382. $_SESSION['game_shake']['direction'] = $direct;
  383. }
  384. return $_SESSION['game_shake']['direction'];
  385. }
  386. }