thief_vilator.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2017/6/24
  6. * Time: 上午11:40
  7. */
  8. namespace bonus;
  9. use algorithm;
  10. use errcode;
  11. use gain_policy;
  12. class thief_vilator
  13. {
  14. private $mFromID;
  15. private $mDateId;
  16. const prifix = 'bonus_thief';
  17. const min_threshold = 200;
  18. public function __construct($fromid)
  19. {
  20. $this->mFromID = $fromid;
  21. $this->mDateId = strtotime(date('Y-m-d',time()));
  22. }
  23. public function thief(&$error)
  24. {
  25. if($this->can_thief() == false) {
  26. $error = ['code' => errcode::ErrBonus,'msg' => '每天只能偷一次好友的红包'];
  27. return false;
  28. }
  29. $pred = new account($this->mFromID);
  30. $usable_amount = $pred->share_total_bonus();
  31. $cents = intval($usable_amount * 100 + 0.5);
  32. if($cents == 0) {
  33. $error = ['code' => errcode::ErrBonus,'msg' => '他比你更穷,还是选择富人下手吧~'];
  34. return false;
  35. }
  36. $amount = $this->calc_amount($usable_amount);
  37. if($amount == false) {
  38. $error = ['code' => errcode::ErrBonus,'msg' => '今日不能偷该用户的红包~'];
  39. return false;
  40. }
  41. else {
  42. $this->add_thief();
  43. }
  44. return $amount;
  45. }
  46. private function calc_amount($usable_amount)
  47. {
  48. $policy = new gain_policy(5, $usable_amount);
  49. return $policy->calculate();
  50. }
  51. private function can_thief()
  52. {
  53. if(!isset($_SESSION[self::prifix]) || !array_key_exists($this->mDateId,$_SESSION[self::prifix])) {
  54. $_SESSION[self::prifix] = [];
  55. $_SESSION[self::prifix][$this->mDateId] = [];
  56. }
  57. $fromid = $this->mFromID;
  58. $mids = &$_SESSION[self::prifix][$this->mDateId];
  59. if(algorithm::binary_search($mids,$fromid)) {
  60. return false;
  61. } else {
  62. return true;
  63. }
  64. }
  65. private function add_thief()
  66. {
  67. if(!isset($_SESSION[self::prifix]) || !array_key_exists($this->mDateId,$_SESSION[self::prifix])) {
  68. $_SESSION[self::prifix] = [];
  69. $_SESSION[self::prifix][$this->mDateId] = [];
  70. }
  71. $fromid = $this->mFromID;
  72. $mids = &$_SESSION[self::prifix][$this->mDateId];
  73. if(algorithm::binary_search($mids,$fromid) == false) {
  74. $pos = algorithm::lower_bonud($mids,$fromid);
  75. algorithm::array_insert($mids,$pos,$fromid);
  76. }
  77. }
  78. }