bargain_manager.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. use QueueClient;
  10. class room_extend
  11. {
  12. private $mParams;
  13. public function __construct($params)
  14. {
  15. $this->mParams = $params;
  16. }
  17. public function bargain_count($dateid)
  18. {
  19. if($this->dateid() != $dateid) {
  20. return 0;
  21. }
  22. if(empty($this->mParams)) {
  23. return 0;
  24. }
  25. else {
  26. return intval($this->mParams['bargain_count']);
  27. }
  28. }
  29. public function dateid()
  30. {
  31. if(empty($this->mParams)) {
  32. return 0;
  33. }
  34. else {
  35. return intval($this->mParams['dateid']);
  36. }
  37. }
  38. public function exist()
  39. {
  40. return !empty($this->mParams);
  41. }
  42. public function date_empty($dateid)
  43. {
  44. if($this->dateid() == $dateid) {
  45. return false;
  46. }
  47. else {
  48. return true;
  49. }
  50. }
  51. }
  52. class bargain_parameter
  53. {
  54. private $total_num;
  55. private $mRandom;
  56. private $total_amount;
  57. private $discount;
  58. private $closed;
  59. private $over_time;
  60. private $user_num;
  61. public function __construct(bargain $bargain)
  62. {
  63. $amount = $bargain->goods_price() - $bargain->lowest_price();
  64. $this->total_amount = intval($amount * 100 + 0.5);
  65. $this->mRandom = $bargain->random();
  66. $this->total_num = $bargain->total_num();
  67. $this->discount = intval($bargain->discount() * 100 + 0.5);
  68. $this->closed = $bargain->closed();
  69. $this->over_time = $bargain->over_time();
  70. $this->user_num = $bargain->user_num();
  71. }
  72. public function overed()
  73. {
  74. if($this->closed) return true;
  75. if($this->over_time <= time()) return true;
  76. return $this->completed();
  77. }
  78. private function completed() {
  79. return ($this->total_amount <= $this->discount);
  80. }
  81. public function bargain($count,&$completed)
  82. {
  83. $completed = false;
  84. $value = $this->power($count);
  85. if($value === false) return false;
  86. if($this->discount + $value > $this->total_amount) {
  87. $value = $this->total_amount - $this->discount;
  88. $completed = true;
  89. $this->closed = true;
  90. }
  91. $this->discount += $value;
  92. $this->user_num += 1;
  93. return $value / 100;
  94. }
  95. private function power($count)
  96. {
  97. $average = intval($this->total_amount / $this->total_num + 0.5);
  98. if($this->mRandom)
  99. {
  100. $max_average = $average / pow(2,$count - 1);
  101. $max_average = intval($max_average + 0.5);
  102. return intval(mt_rand(1,$max_average));
  103. }
  104. else
  105. {
  106. global $config;
  107. $max_day_count = $config['bargain_goods']['max_day_count'];
  108. if($count >= $max_day_count) {
  109. return false;
  110. } else {
  111. return $average;
  112. }
  113. }
  114. }
  115. public function discount() {
  116. return $this->discount / 100;
  117. }
  118. public function user_num() {
  119. return $this->user_num;
  120. }
  121. public function close() {
  122. $this->closed = 1;
  123. }
  124. }
  125. class bargain_manager
  126. {
  127. const unknown_state = 0;
  128. const normal = 1;
  129. const closing = 2;
  130. private $mFriends;
  131. private $mParams;
  132. private $mBargainId;
  133. private $mRoomId;
  134. private $mBargain;
  135. public function __construct($roomid)
  136. {
  137. $this->mRoomId = $roomid;
  138. $mod_bargain = Model('room_bargain');
  139. $info = $mod_bargain->getBargainByRoom($roomid,true);
  140. $this->mBargain = new bargain($info);
  141. $this->mBargainId = $this->mBargain->bargain_id();
  142. $this->mParams = new bargain_parameter($this->mBargain);
  143. $this->mFriends = [];
  144. $mod_room = Model('room');
  145. $items = $mod_room->getRoomMsg($roomid,proto_type::msg_type_bargain);
  146. foreach ($items as $item) {
  147. $uid = intval($item['member_id']);
  148. $data = json_decode($item['msg'],true);
  149. $value = $data['value'];
  150. $this->mFriends[$uid] = $value;
  151. }
  152. }
  153. private function dateid() {
  154. return strtotime(date('Y-m-d',time()));
  155. }
  156. public function bargain($userid,&$state)
  157. {
  158. $state = self::unknown_state;
  159. $value = $this->bargained($userid);
  160. if($value != false) {
  161. return ['value' => $value,'success' => false,'discount' => $this->mParams->discount(),'user_num' => $this->mParams->user_num()];
  162. }
  163. if($this->mParams->overed()) {
  164. return ['value' => 0,'success' => false,'discount' => $this->mParams->discount(),'user_num' => $this->mParams->user_num()];
  165. }
  166. $mod_room = Model('room');
  167. $info = $mod_room->getRoomExtend($userid);
  168. $room_extend = new room_extend($info);
  169. $count = $room_extend->bargain_count($this->dateid());
  170. $value = $this->mParams->bargain($count,$completed);
  171. if($value === false) {
  172. return false;
  173. }
  174. else
  175. {
  176. if($room_extend->exist())
  177. {
  178. if($room_extend->date_empty($this->dateid())) {
  179. $mod_room->editExtend($userid,['bargain_count' => 1,'dateid' => $this->dateid()]);
  180. }
  181. else {
  182. $mod_room->editExtend($userid,['bargain_count' => ['exp', 'bargain_count+1']]);
  183. }
  184. }
  185. else {
  186. $mod_room->addExtend($userid,['bargain_count' => 1,'dateid' => $this->dateid()]);
  187. }
  188. $mod_bargain = Model('room_bargain');
  189. if($completed) {
  190. $mod_bargain->editBargain($this->mBargainId,['discount' => ['exp',"discount+{$value}"],'user_num' => ['exp',"user_num+1"],'closed' => 1]);
  191. QueueClient::push('onAsyncBargain',['type' => "close", 'bargain_id' => $this->mBargainId]);
  192. $state = self::closing;
  193. }
  194. else {
  195. $mod_bargain->editBargain($this->mBargainId,['discount' => ['exp',"discount+{$value}"],'user_num' => ['exp',"user_num+1"]]);
  196. }
  197. $mod_room->addRoomMsg(['room_id' => $this->mRoomId,'member_id' => $userid,
  198. 'type' => proto_type::msg_type_bargain,'msg' => json_encode(['value' => $value]),'add_time' => time(),'msg_type' => 0]);
  199. return ['value' => $value,'success' => true,'discount' => $this->mParams->discount(),'user_num' => $this->mParams->user_num()];
  200. }
  201. }
  202. public function close($userid)
  203. {
  204. if($this->mBargain->creator() != $userid) {
  205. return false;
  206. }
  207. if($this->mParams->overed()) {
  208. return false;
  209. }
  210. $this->mParams->close();
  211. $mod_bargain = Model('room_bargain');
  212. $mod_bargain->editBargain($this->mBargainId,['closed' => 1]);
  213. QueueClient::push('onAsyncBargain',['type' => "close", 'bargain_id' => $this->mBargainId]);
  214. return true;
  215. }
  216. private function bargained($userid)
  217. {
  218. if(array_key_exists($userid,$this->mFriends)) {
  219. return $this->mFriends[$userid];
  220. } else {
  221. return false;
  222. }
  223. }
  224. }