shaker_helper.php 14 KB

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