123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 2018/1/23
- * Time: 上午11:24
- */
- namespace room;
- use QueueClient;
- class room_extend
- {
- private $mParams;
- public function __construct($params)
- {
- $this->mParams = $params;
- }
- public function bargain_count($dateid)
- {
- if($this->dateid() != $dateid) {
- return 0;
- }
- if(empty($this->mParams)) {
- return 0;
- }
- else {
- return intval($this->mParams['bargain_count']);
- }
- }
- public function dateid()
- {
- if(empty($this->mParams)) {
- return 0;
- }
- else {
- return intval($this->mParams['dateid']);
- }
- }
- public function exist()
- {
- return !empty($this->mParams);
- }
- public function date_empty($dateid)
- {
- if($this->dateid() == $dateid) {
- return false;
- }
- else {
- return true;
- }
- }
- }
- class bargain_parameter
- {
- private $total_num;
- private $mRandom;
- private $total_amount;
- private $discount;
- private $closed;
- private $over_time;
- private $user_num;
- public function __construct(bargain $bargain)
- {
- $amount = $bargain->goods_price() - $bargain->lowest_price();
- $this->total_amount = intval($amount * 100 + 0.5);
- $this->mRandom = $bargain->random();
- $this->total_num = $bargain->total_num();
- $this->discount = intval($bargain->discount() * 100 + 0.5);
- $this->closed = $bargain->closed();
- $this->over_time = $bargain->over_time();
- $this->user_num = $bargain->user_num();
- }
- public function overed()
- {
- if($this->closed) return true;
- if($this->over_time <= time()) return true;
- return $this->completed();
- }
- private function completed() {
- return ($this->total_amount <= $this->discount);
- }
- public function bargain($count,&$completed)
- {
- $completed = false;
- $value = $this->power($count);
- if($value === false) return false;
- if($this->discount + $value > $this->total_amount) {
- $value = $this->total_amount - $this->discount;
- $completed = true;
- $this->closed = true;
- }
- $this->discount += $value;
- $this->user_num += 1;
- return $value / 100;
- }
- private function power($count)
- {
- $average = intval($this->total_amount / $this->total_num + 0.5);
- if($this->mRandom)
- {
- $max_average = $average / pow(2,$count - 1);
- $max_average = intval($max_average + 0.5);
- return intval(mt_rand(1,$max_average));
- }
- else
- {
- global $config;
- $max_day_count = $config['bargain_goods']['max_day_count'];
- if($count >= $max_day_count) {
- return false;
- } else {
- return $average;
- }
- }
- }
- public function discount() {
- return $this->discount / 100;
- }
- public function user_num() {
- return $this->user_num;
- }
- public function close() {
- $this->closed = 1;
- }
- }
- class bargain_manager
- {
- const unknown_state = 0;
- const normal = 1;
- const closing = 2;
- private $mFriends;
- private $mParams;
- private $mBargainId;
- private $mRoomId;
- private $mBargain;
- public function __construct($roomid)
- {
- $this->mRoomId = $roomid;
- $mod_bargain = Model('room_bargain');
- $info = $mod_bargain->getBargainByRoom($roomid,true);
- $this->mBargain = new bargain($info);
- $this->mBargainId = $this->mBargain->bargain_id();
- $this->mParams = new bargain_parameter($this->mBargain);
- $this->mFriends = [];
- $mod_room = Model('room');
- $items = $mod_room->getRoomMsg($roomid,proto_type::msg_type_bargain);
- foreach ($items as $item) {
- $uid = intval($item['member_id']);
- $data = json_decode($item['msg'],true);
- $value = $data['value'];
- $this->mFriends[$uid] = $value;
- }
- }
- private function dateid() {
- return strtotime(date('Y-m-d',time()));
- }
- public function bargain($userid,&$state)
- {
- $state = self::unknown_state;
- $value = $this->bargained($userid);
- if($value != false) {
- return ['value' => $value,'success' => false,'discount' => $this->mParams->discount(),'user_num' => $this->mParams->user_num()];
- }
- if($this->mParams->overed()) {
- return ['value' => 0,'success' => false,'discount' => $this->mParams->discount(),'user_num' => $this->mParams->user_num()];
- }
- $mod_room = Model('room');
- $info = $mod_room->getRoomExtend($userid);
- $room_extend = new room_extend($info);
- $count = $room_extend->bargain_count($this->dateid());
- $value = $this->mParams->bargain($count,$completed);
- if($value === false) {
- return false;
- }
- else
- {
- if($room_extend->exist())
- {
- if($room_extend->date_empty($this->dateid())) {
- $mod_room->editExtend($userid,['bargain_count' => 1,'dateid' => $this->dateid()]);
- }
- else {
- $mod_room->editExtend($userid,['bargain_count' => ['exp', 'bargain_count+1']]);
- }
- }
- else {
- $mod_room->addExtend($userid,['bargain_count' => 1,'dateid' => $this->dateid()]);
- }
- $mod_bargain = Model('room_bargain');
- if($completed) {
- $mod_bargain->editBargain($this->mBargainId,['discount' => ['exp',"discount+{$value}"],'user_num' => ['exp',"user_num+1"],'closed' => 1]);
- QueueClient::push('onAsyncBargain',['type' => "close", 'bargain_id' => $this->mBargainId]);
- $state = self::closing;
- }
- else {
- $mod_bargain->editBargain($this->mBargainId,['discount' => ['exp',"discount+{$value}"],'user_num' => ['exp',"user_num+1"]]);
- }
- $mod_room->addRoomMsg(['room_id' => $this->mRoomId,'member_id' => $userid,
- 'type' => proto_type::msg_type_bargain,'msg' => json_encode(['value' => $value]),'add_time' => time(),'msg_type' => 0]);
- return ['value' => $value,'success' => true,'discount' => $this->mParams->discount(),'user_num' => $this->mParams->user_num()];
- }
- }
- public function close($userid)
- {
- if($this->mBargain->creator() != $userid) {
- return false;
- }
- if($this->mParams->overed()) {
- return false;
- }
- $this->mParams->close();
- $mod_bargain = Model('room_bargain');
- $mod_bargain->editBargain($this->mBargainId,['closed' => 1]);
- QueueClient::push('onAsyncBargain',['type' => "close", 'bargain_id' => $this->mBargainId]);
- return true;
- }
- private function bargained($userid)
- {
- if(array_key_exists($userid,$this->mFriends)) {
- return $this->mFriends[$userid];
- } else {
- return false;
- }
- }
- }
|