123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 2017/12/14
- * Time: 下午6:18
- */
- namespace room;
- class bargain
- {
- private $mParams;
- public function __construct($params) {
- $this->mParams = $params;
- }
- public function format() {
- $result = [];
- $result['bargain_id'] = $this->bargain_id();
- $result['goods_id'] = $this->goods_id();
- $result['discount'] = $this->discount();
- $result['lowest_price'] = $this->lowest_price();
- $cur_price = $this->goods_price() - $this->discount();
- $cur_price = intval($cur_price * 100 + 0.5);
- $cur_price = $cur_price < 0 ? 0 : $cur_price;
- $result['cur_price'] = $cur_price / 100;
- $result['add_time'] = $this->add_time();
- $result['over_time'] = $this->over_time();
- $result['user_num'] = $this->user_num();
- $result['closed'] = $this->closed();
- return $result;
- }
- public function bargain_id() {
- return intval($this->mParams['bargain_id']);
- }
- public function room() {
- return intval($this->mParams['room_id']);
- }
- public function creator() {
- return intval($this->mParams['user_id']);
- }
- public function goods_id() {
- return intval($this->mParams['goods_id']);
- }
- public function goods_price() {
- return floatval($this->mParams['goods_price']);
- }
- public function cost_price() {
- return floatval($this->mParams['cost_price']);
- }
- public function lowest_price() {
- return floatval($this->mParams['lowest_price']);
- }
- public function discount() {
- return floatval($this->mParams['discount']);
- }
- public function add_time() {
- return intval($this->mParams['add_time']);
- }
- public function over_time() {
- return intval($this->mParams['over_time']);
- }
- public function user_num() {
- return intval($this->mParams['user_num']);
- }
- public function has_over() {
- return $this->over_time() <= time();
- }
- public function random() {
- return (intval($this->mParams['type']) == 1);
- }
- public function total_num() {
- return intval($this->mParams['total_num']);
- }
- public function closed() {
- return intval($this->mParams['closed']) == 1;
- }
- public function rewarded() {
- return intval($this->mParams['rewarded']) == 1;
- }
- }
- class bargain_room extends base_room
- {
- private $mManager;
- public function __construct($cinfos, $participants = [])
- {
- parent::__construct($cinfos,$participants);
- $this->mRoomType = proto_type::sroom_bargain;
- $this->mManager = new bargain_manager($this->room_id());
- }
- public function bargainOp($input)
- {
- $this->clear();
- $room_key = $input['room_key'];
- if(empty($room_key)) {
- return false;
- }
- $userinfo = $this->find($room_key);
- if($userinfo == false) return false;
- $userid = $userinfo['userid'];
- $result = $this->mManager->bargain($userid,$state);
- if($result !== false)
- {
- $value = $result['value'];
- $success = $result['success'];
- $discount = $result['discount'];
- $user_num = $result['user_num'];
- if($success)
- {
- $this->relay_reply([$room_key],'ret_bargain',['from' => $userinfo,'value' => $value,'discount' => $discount,'user_num' => $user_num]);
- $this->relay_broadcast('bargain',['from' => $userinfo,'value' => $value,'discount' => $discount,'user_num' => $user_num]);
- if($state == bargain_manager::closing) {
- $this->relay_broadcast('bargain_close',['from' => $userinfo]);
- }
- }
- else {
- $this->relay_reply([$room_key],'ret_bargain',['from' => $userinfo,'value' => $value,'discount' => $discount,'user_num' => $user_num]);
- }
- return true;
- }
- else
- {
- return false;
- }
- }
- public function closeOp($input)
- {
- $this->clear();
- $room_key = $input['room_key'];
- if(empty($room_key)) {
- return false;
- }
- $userinfo = $this->find($room_key);
- if($userinfo == false) return false;
- $userid = $userinfo['userid'];
- $result = $this->mManager->close($userid);
- if($result)
- {
- $this->relay_reply([$room_key],'ret_close',['from' => $userinfo]);
- $this->relay_broadcast('bargain_close',['from' => $userinfo]);
- return true;
- }
- else
- {
- return false;
- }
- }
- }
|