thief_vilator.php 2.4 KB

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