bargain_room.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2017/12/14
  6. * Time: 下午6:18
  7. */
  8. namespace room;
  9. class bargain
  10. {
  11. private $mParams;
  12. public function __construct($params) {
  13. $this->mParams = $params;
  14. }
  15. public function format() {
  16. $result = [];
  17. $result['bargain_id'] = $this->bargain_id();
  18. $result['goods_id'] = $this->goods_id();
  19. $result['discount'] = $this->discount();
  20. $result['lowest_price'] = $this->lowest_price();
  21. $cur_price = $this->goods_price() - $this->discount();
  22. $cur_price = intval($cur_price * 100 + 0.5);
  23. $cur_price = $cur_price < 0 ? 0 : $cur_price;
  24. $result['cur_price'] = $cur_price / 100;
  25. $result['add_time'] = $this->add_time();
  26. $result['over_time'] = $this->over_time();
  27. $result['user_num'] = $this->user_num();
  28. $result['closed'] = $this->closed();
  29. return $result;
  30. }
  31. public function bargain_id() {
  32. return intval($this->mParams['bargain_id']);
  33. }
  34. public function room() {
  35. return intval($this->mParams['room_id']);
  36. }
  37. public function creator() {
  38. return intval($this->mParams['user_id']);
  39. }
  40. public function goods_id() {
  41. return intval($this->mParams['goods_id']);
  42. }
  43. public function goods_price() {
  44. return floatval($this->mParams['goods_price']);
  45. }
  46. public function cost_price() {
  47. return floatval($this->mParams['cost_price']);
  48. }
  49. public function lowest_price() {
  50. return floatval($this->mParams['lowest_price']);
  51. }
  52. public function discount() {
  53. return floatval($this->mParams['discount']);
  54. }
  55. public function add_time() {
  56. return intval($this->mParams['add_time']);
  57. }
  58. public function over_time() {
  59. return intval($this->mParams['over_time']);
  60. }
  61. public function user_num() {
  62. return intval($this->mParams['user_num']);
  63. }
  64. public function has_over() {
  65. return $this->over_time() <= time();
  66. }
  67. public function random() {
  68. return (intval($this->mParams['type']) == 1);
  69. }
  70. public function total_num() {
  71. return intval($this->mParams['total_num']);
  72. }
  73. public function closed() {
  74. return intval($this->mParams['closed']) == 1;
  75. }
  76. public function rewarded() {
  77. return intval($this->mParams['rewarded']) == 1;
  78. }
  79. }
  80. class bargain_room extends base_room
  81. {
  82. private $mManager;
  83. public function __construct($cinfos, $participants = [])
  84. {
  85. parent::__construct($cinfos,$participants);
  86. $this->mRoomType = proto_type::sroom_bargain;
  87. $this->mManager = new bargain_manager($this->room_id());
  88. }
  89. public function bargainOp($input)
  90. {
  91. $this->clear();
  92. $room_key = $input['room_key'];
  93. if(empty($room_key)) {
  94. return false;
  95. }
  96. $userinfo = $this->find($room_key);
  97. if($userinfo == false) return false;
  98. $userid = $userinfo['userid'];
  99. $result = $this->mManager->bargain($userid,$state);
  100. if($result !== false)
  101. {
  102. $value = $result['value'];
  103. $success = $result['success'];
  104. $discount = $result['discount'];
  105. $user_num = $result['user_num'];
  106. if($success)
  107. {
  108. $this->relay_reply([$room_key],'ret_bargain',['from' => $userinfo,'value' => $value,'discount' => $discount,'user_num' => $user_num]);
  109. $this->relay_broadcast('bargain',['from' => $userinfo,'value' => $value,'discount' => $discount,'user_num' => $user_num]);
  110. if($state == bargain_manager::closing) {
  111. $this->relay_broadcast('bargain_close',['from' => $userinfo]);
  112. }
  113. }
  114. else {
  115. $this->relay_reply([$room_key],'ret_bargain',['from' => $userinfo,'value' => $value,'discount' => $discount,'user_num' => $user_num]);
  116. }
  117. return true;
  118. }
  119. else
  120. {
  121. return false;
  122. }
  123. }
  124. public function closeOp($input)
  125. {
  126. $this->clear();
  127. $room_key = $input['room_key'];
  128. if(empty($room_key)) {
  129. return false;
  130. }
  131. $userinfo = $this->find($room_key);
  132. if($userinfo == false) return false;
  133. $userid = $userinfo['userid'];
  134. $result = $this->mManager->close($userid);
  135. if($result)
  136. {
  137. $this->relay_reply([$room_key],'ret_close',['from' => $userinfo]);
  138. $this->relay_broadcast('bargain_close',['from' => $userinfo]);
  139. return true;
  140. }
  141. else
  142. {
  143. return false;
  144. }
  145. }
  146. }