shaker_helper.php 15 KB

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