shaker_helper.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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 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 = 10;
  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.8;
  68. $end = 1.0;
  69. }
  70. elseif ($this->strength == 4) {
  71. $start = 0.6;
  72. $end = 0.8;
  73. }
  74. elseif ($this->strength == 3) {
  75. $start = 0.4;
  76. $end = 0.6;
  77. }
  78. elseif ($this->strength == 2) {
  79. $start = 0.2;
  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 = 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. $bonus = account_helper::gain_bonus($user_id, $_SESSION['member_id'], $value);
  121. if($bonus != false) {
  122. $this->add_gained_friend(array($user_id));
  123. } else {
  124. $err = array('code' => errcode::ErrShake, 'msg' => self::err_msg);
  125. }
  126. return $bonus;
  127. }
  128. else
  129. {
  130. if ($this->gained_system() == false) {
  131. $err = array('code' => errcode::ErrShake, 'msg' => self::err_msg);
  132. return false;
  133. }
  134. else
  135. {
  136. $policy = new gain_policy($this->strength, 10);
  137. $value = $policy->calculate();
  138. if($value == false) {
  139. $err = array('code' => errcode::ErrShake, 'msg' => self::err_msg);
  140. return false;
  141. }
  142. $bonus = account_helper::gain_system($_SESSION['member_id'], $value);
  143. if($bonus != false) {
  144. $this->save_gained_system();
  145. } else {
  146. $err = array('code' => errcode::ErrShake, 'msg' => self::err_msg);
  147. }
  148. return $bonus;
  149. }
  150. }
  151. }
  152. private function lost(&$err)
  153. {
  154. $pred = new predeposit_helper($_SESSION['member_id']);
  155. $total_amount = $pred->total_bonus();
  156. if(intval($total_amount * 100 + 0.5) <= 0) {
  157. $err = array('code' => errcode::ErrShake,'msg' => self::err_msg);
  158. return false;
  159. }
  160. $friends = $this->lost_friends();
  161. $user_id = $this->find_lost_friend($friends);
  162. if($user_id == false) {
  163. $err = array('code' => errcode::ErrShake, 'msg' => self::err_msg);
  164. return false;
  165. }
  166. $policy = new gain_policy($this->strength, $total_amount);
  167. $value = $policy->calculate();
  168. if($value == false) {
  169. $err = array('code' => errcode::ErrShake, 'msg' => self::err_msg);
  170. return false;
  171. }
  172. $bonus = account_helper::lost_bonus($_SESSION['member_id'],$user_id, $value);
  173. if($bonus != false) {
  174. $this->add_lost_friend(array($user_id));
  175. } else {
  176. $err = array('code' => errcode::ErrShake, 'msg' => self::err_msg);
  177. return false;
  178. }
  179. return $bonus;
  180. }
  181. private function find_lost_friend($friends)
  182. {
  183. while (true)
  184. {
  185. $count = count($friends);
  186. if($count == 0) {
  187. return false;
  188. }
  189. $pos = mt_rand(0,$count -1);
  190. return $friends[$pos];
  191. }
  192. }
  193. private function find_gain_friend($friends, &$usable_amount)
  194. {
  195. while (true)
  196. {
  197. $count = count($friends);
  198. if($count == 0) return false;
  199. $pos = mt_rand(0,$count -1);
  200. $mod_bonus = Model('user_bonus');
  201. $usable_amount = $mod_bonus->getUsableSum($friends[$pos]);
  202. if(intval($usable_amount * 100 + 0.5) > 0) {
  203. return $friends[$pos];
  204. } else {
  205. $this->add_poor_friend($friends[$pos]);
  206. array_splice($friends,$pos,1);
  207. }
  208. }
  209. }
  210. public function shake($strength,&$err)
  211. {
  212. if($this->can_shake() == false) {
  213. $period = self::shake_expire;
  214. $err = array('code' => errcode::ErrShake,'msg' => "摇得太频繁啦,{$period}秒钟只能摇一次~");
  215. return false;
  216. }
  217. $this->strength = $strength;
  218. $direct = $this->direction();
  219. if($direct == self::direct_gain) {
  220. $bonuses = $this->gain($err);
  221. }
  222. else {
  223. $bonuses = $this->lost($err);
  224. }
  225. if($bonuses != false) {
  226. $this->save_shake();
  227. }
  228. return $bonuses;
  229. }
  230. private function save_gained_system()
  231. {
  232. if(isset($_SESSION['game_shake']['gained_system'])) {
  233. $_SESSION['game_shake']['gained_system'] = array();
  234. }
  235. $system = &$_SESSION['game_shake']['gained_system'];
  236. if(empty($system)) {
  237. $system['user_id'] = $_SESSION['member_id'];
  238. $system['last_time'] = time();
  239. $system['count'] = 1;
  240. } else {
  241. $system['user_id'] = $_SESSION['member_id'];
  242. $system['last_time'] = time();
  243. $system['count'] = intval($system['count']) + 1;
  244. }
  245. }
  246. private function gained_system()
  247. {
  248. if(!isset($_SESSION['game_shake']['gained_system'])) {
  249. return true;
  250. } else {
  251. $param = $_SESSION['game_shake']['gained_system'];
  252. $friend = new friend($param);
  253. return $friend->can_gain_system();
  254. }
  255. }
  256. private function add_gained_friend($user_ids)
  257. {
  258. if(!isset($_SESSION['game_shake']['gained_friends'])) {
  259. $_SESSION['game_shake']['gained_friends'] = array();
  260. }
  261. $friends = &$_SESSION['game_shake']['gained_friends'];
  262. foreach ($user_ids as $user_id)
  263. {
  264. if(isset($friends[$user_id])) {
  265. $friends[$user_id]['user_id'] = $user_id;
  266. $friends[$user_id]['last_time'] = time();
  267. $friends[$user_id]['count'] = intval($friends[$user_id]['count']) + 1;
  268. } else {
  269. $friends[$user_id]['user_id'] = $user_id;
  270. $friends[$user_id]['last_time'] = time();
  271. $friends[$user_id]['count'] = 1;
  272. }
  273. }
  274. }
  275. private function add_poor_friend($user_id)
  276. {
  277. if(!isset($_SESSION['game_shake']['poor_friends'])) {
  278. $_SESSION['game_shake']['poor_friends'] = array();
  279. }
  280. $friends = &$_SESSION['game_shake']['poor_friends'];
  281. if(isset($friends[$user_id])) {
  282. $friends[$user_id]['user_id'] = $user_id;
  283. $friends[$user_id]['last_time'] = time();
  284. $friends[$user_id]['count'] = intval($friends[$user_id]['count']) + 1;
  285. } else {
  286. $friends[$user_id]['user_id'] = $user_id;
  287. $friends[$user_id]['last_time'] = time();
  288. $friends[$user_id]['count'] = 1;
  289. }
  290. }
  291. private function del_poor_friend($user_id)
  292. {
  293. if(!isset($_SESSION['game_shake']['poor_friends'])) {
  294. $_SESSION['game_shake']['poor_friends'] = array();
  295. }
  296. $friends = &$_SESSION['game_shake']['poor_friends'];
  297. if(isset($friends[$user_id])) {
  298. unset($friends[$user_id]);
  299. }
  300. }
  301. private function add_lost_friend($user_ids)
  302. {
  303. if(!isset($_SESSION['game_shake']['losted_friends'])) {
  304. $_SESSION['game_shake']['losted_friends'] = array();
  305. }
  306. $friends = &$_SESSION['game_shake']['losted_friends'];
  307. foreach ($user_ids as $user_id)
  308. {
  309. if(isset($friends[$user_id])) {
  310. $friends[$user_id]['user_id'] = $user_id;
  311. $friends[$user_id]['last_time'] = time();
  312. $friends[$user_id]['count'] = intval($friends[$user_id]['count']) + 1;
  313. } else {
  314. $friends[$user_id]['user_id'] = $user_id;
  315. $friends[$user_id]['last_time'] = time();
  316. $friends[$user_id]['count'] = 1;
  317. }
  318. }
  319. }
  320. //可以偷的好友
  321. private function gain_friends()
  322. {
  323. if(!isset($_SESSION['game_shake']['gained_friends'])) {
  324. $_SESSION['game_shake']['gained_friends'] = array();
  325. }
  326. $friends = $_SESSION['game_shake']['gained_friends'];
  327. $exids = [];
  328. foreach ($friends as $uid => $param)
  329. {
  330. $friend = new friend($param);
  331. if($friend->can_gain() == false) {
  332. $exids[] = $friend->user_id();
  333. }
  334. }
  335. if(!isset($_SESSION['game_shake']['poor_friends'])) {
  336. $_SESSION['game_shake']['poor_friends'] = array();
  337. }
  338. $poor_friends = $_SESSION['game_shake']['poor_friends'];
  339. foreach ($poor_friends as $uid => $param)
  340. {
  341. $friend = new friend($param);
  342. if($friend->is_poor()) {
  343. $exids[] = $friend->user_id();
  344. } else {
  345. $this->del_poor_friend($friend->user_id());
  346. }
  347. }
  348. $all_friends = relation_helper::friends(session_helper::memberid());
  349. sort($all_friends,SORT_NUMERIC);
  350. $all_friends = array_merge(array_unique($all_friends,SORT_NUMERIC),[]);
  351. sort($all_friends);
  352. foreach ($exids as $uid)
  353. {
  354. $pos = algorithm::bsearch($uid,$all_friends);
  355. if($pos != -1) {
  356. array_splice($all_friends,$pos,1);
  357. }
  358. }
  359. return $all_friends;
  360. }
  361. private function lost_friends()
  362. {
  363. if(!isset($_SESSION['game_shake']['losted_friends'])) {
  364. $_SESSION['game_shake']['losted_friends'] = array();
  365. }
  366. $friends = $_SESSION['game_shake']['losted_friends'];
  367. $exids = array();
  368. foreach ($friends as $param)
  369. {
  370. $friend = new friend($param);
  371. if($friend->can_lost() == false) {
  372. array_push($exids,$friend->user_id());
  373. }
  374. }
  375. $subscriber = relation_helper::subscriber($_SESSION['member_id']);
  376. $follower = relation_helper::follower($_SESSION['member_id']);
  377. $all_friends = array_merge($subscriber,$follower);
  378. sort($all_friends,SORT_NUMERIC);
  379. $all_friends = array_merge(array_unique($all_friends,SORT_NUMERIC),[]);
  380. sort($all_friends,SORT_NUMERIC);
  381. foreach ($exids as $uid)
  382. {
  383. $pos = algorithm::bsearch($uid,$all_friends);
  384. if($pos != -1) {
  385. array_splice($all_friends,$pos,1);
  386. }
  387. }
  388. return $all_friends;
  389. }
  390. private function can_shake()
  391. {
  392. if(!isset($_SESSION['game_shake'])) {
  393. $_SESSION['game_shake'] = array();
  394. }
  395. if(isset($_SESSION['game_shake']['last_time']))
  396. {
  397. $last_time = intval($_SESSION['game_shake']['last_time']);
  398. if($last_time + self::shake_expire >= time()) {
  399. return false;
  400. } else {
  401. return true;
  402. }
  403. }
  404. else {
  405. return true;
  406. }
  407. }
  408. private function save_shake()
  409. {
  410. if(!isset($_SESSION['game_shake'])) {
  411. $_SESSION['game_shake'] = array();
  412. }
  413. $_SESSION['game_shake']['last_time'] = time();
  414. }
  415. private function direction()
  416. {
  417. if(!isset($_SESSION['game_shake'])) {
  418. $_SESSION['game_shake'] = array();
  419. }
  420. if(!isset($_SESSION['game_shake']['direction'])) {
  421. $_SESSION['game_shake']['direction'] = self::direct_gain;
  422. }
  423. else
  424. {
  425. $rand = mt_rand(1,100);
  426. $direct = ($rand % 5) == 0 ? self::direct_lost : self::direct_gain;
  427. $_SESSION['game_shake']['direction'] = $direct;
  428. }
  429. return $_SESSION['game_shake']['direction'];
  430. }
  431. }