bargain_manager.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2018/1/23
  6. * Time: 上午11:24
  7. */
  8. namespace room;
  9. class room_extend
  10. {
  11. private $mParams;
  12. public function __construct($params)
  13. {
  14. $this->mParams = $params;
  15. }
  16. public function bargain_count($dateid)
  17. {
  18. if($this->dateid() != $dateid) {
  19. return 0;
  20. }
  21. if(empty($this->mParams)) {
  22. return 0;
  23. }
  24. else {
  25. return intval($this->mParams['bargain_count']);
  26. }
  27. }
  28. public function dateid()
  29. {
  30. if(empty($this->mParams)) {
  31. return 0;
  32. }
  33. else {
  34. return intval($this->mParams['dateid']);
  35. }
  36. }
  37. public function date_empty($dateid)
  38. {
  39. if(empty($this->mParams)) return true;
  40. if($this->dateid() == $dateid) {
  41. return false;
  42. }
  43. else {
  44. return false;
  45. }
  46. }
  47. }
  48. class bargain_parameter
  49. {
  50. private $total_num;
  51. private $mRandom;
  52. private $total_amount;
  53. private $discount;
  54. private $closed;
  55. private $over_time;
  56. private $user_num;
  57. public function __construct(bargain $bargain)
  58. {
  59. $amount = $bargain->goods_price() - $bargain->lowest_price();
  60. $this->total_amount = intval($amount * 100 + 0.5);
  61. $this->mRandom = $bargain->random();
  62. $this->total_num = $bargain->total_num();
  63. $this->discount = intval($bargain->discount() * 100 + 0.5);
  64. $this->closed = $bargain->closed();
  65. $this->over_time = $bargain->over_time();
  66. $this->user_num = $bargain->user_num();
  67. }
  68. public function overed()
  69. {
  70. if($this->closed) return true;
  71. if($this->over_time <= time()) return true;
  72. return $this->completed();
  73. }
  74. private function completed() {
  75. return ($this->total_amount <= $this->discount);
  76. }
  77. public function bargain($count)
  78. {
  79. $value = $this->power($count);
  80. if($value === false) return false;
  81. if($this->discount + $value > $this->total_amount) {
  82. $value = $this->total_amount - $this->discount;
  83. }
  84. $this->discount += $value;
  85. $this->user_num += 1;
  86. return $value / 100;
  87. }
  88. private function power($count)
  89. {
  90. $average = intval($this->total_amount / $this->total_num + 0.5);
  91. if($this->mRandom)
  92. {
  93. $max_average = $average / pow(2,$count - 1);
  94. $max_average = intval($max_average + 0.5);
  95. return intval(mt_rand(1,$max_average));
  96. }
  97. else
  98. {
  99. global $config;
  100. $max_day_count = $config['bargain_goods']['max_day_count'];
  101. if($count >= $max_day_count) {
  102. return false;
  103. } else {
  104. return $average;
  105. }
  106. }
  107. }
  108. public function discount() {
  109. return $this->discount / 100;
  110. }
  111. public function user_num() {
  112. return $this->user_num;
  113. }
  114. }
  115. class bargain_manager
  116. {
  117. private $mDateId;
  118. private $mFriends;
  119. private $mParams;
  120. private $mBargainId;
  121. private $mRoomId;
  122. public function __construct($roomid)
  123. {
  124. $this->mRoomId = $roomid;
  125. $this->mDateId = strtotime(date('Y-m-d',time()));
  126. $mod_bargain = Model('room_bargain');
  127. $info = $mod_bargain->getBargainByRoom($roomid,true);
  128. $bargain = new bargain($info);
  129. $this->mBargainId = $bargain->bargain_id();
  130. $this->mParams = new bargain_parameter($bargain);
  131. $this->mFriends = [];
  132. $mod_room = Model('room');
  133. $items = $mod_room->getRoomMsg($roomid,proto_type::msg_type_bargain);
  134. foreach ($items as $item) {
  135. $uid = intval($item['member_id']);
  136. $data = json_decode($item['msg'],true);
  137. $value = $data['value'];
  138. $this->mFriends[$uid] = $value;
  139. }
  140. }
  141. public function bargain($userid)
  142. {
  143. $value = $this->bargained($userid);
  144. if($value != false) {
  145. return ['value' => $value,'success' => false,'discount' => $this->mParams->discount(),'user_num' => $this->mParams->user_num()];
  146. }
  147. if($this->mParams->overed()) {
  148. return ['value' => 0,'success' => false,'discount' => $this->mParams->discount(),'user_num' => $this->mParams->user_num()];
  149. }
  150. $mod_room = Model('room');
  151. $info = $mod_room->getRoomExtend($userid);
  152. $room_extend = new room_extend($info);
  153. $count = $room_extend->bargain_count($this->mDateId);
  154. $value = $this->mParams->bargain($count);
  155. if($value === false) {
  156. return false;
  157. }
  158. else
  159. {
  160. if(!$room_extend->date_empty($this->mDateId)) {
  161. $mod_room->editExtend($userid,['bargain_count' => ['exp', 'bargain_count+1']]);
  162. } else {
  163. $mod_room->addExtend($userid,['bargain_count' => 1,'dateid' => $this->mDateId]);
  164. }
  165. $mod_bargain = Model('room_bargain');
  166. $mod_bargain->editBargain($this->mBargainId,['discount' => ['exp',"discount+{$value}"],'user_num' => ['exp',"user_num+1"]]);
  167. $mod_room->addRoomMsg(['room_id' => $this->mRoomId,'member_id' => $userid, 'type' => proto_type::msg_type_bargain,'msg' => json_encode(['value' => $value]),'add_time' => time()]);
  168. return ['value' => $value,'success' => true,'discount' => $this->mParams->discount(),'user_num' => $this->mParams->user_num()];
  169. }
  170. }
  171. private function bargained($userid)
  172. {
  173. if(array_key_exists($userid,$this->mFriends)) {
  174. return $this->mFriends[$userid];
  175. } else {
  176. return false;
  177. }
  178. }
  179. }