|
@@ -0,0 +1,183 @@
|
|
|
|
+<?php
|
|
|
|
+/**
|
|
|
|
+ * Created by PhpStorm.
|
|
|
|
+ * User: stanley-king
|
|
|
|
+ * Date: 2018/1/23
|
|
|
|
+ * Time: 上午11:24
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+namespace room;
|
|
|
|
+
|
|
|
|
+class room_extend
|
|
|
|
+{
|
|
|
|
+ private $mParams;
|
|
|
|
+ public function __construct($params)
|
|
|
|
+ {
|
|
|
|
+ $this->mParams = $params;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public function bargain_count()
|
|
|
|
+ {
|
|
|
|
+ if(empty($this->mParams)) {
|
|
|
|
+ return 0;
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ return intval($this->mParams['bargain_count']);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+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)
|
|
|
|
+ {
|
|
|
|
+ $value = $this->power($count);
|
|
|
|
+ if($value === false) return false;
|
|
|
|
+
|
|
|
|
+ if($this->discount + $value > $this->total_amount) {
|
|
|
|
+ $value = $this->total_amount - $this->discount;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $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;
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+class bargain_manager
|
|
|
|
+{
|
|
|
|
+ private $mDateId;
|
|
|
|
+ private $mFriends;
|
|
|
|
+ private $mParams;
|
|
|
|
+ private $mBargainId;
|
|
|
|
+ private $mRoomId;
|
|
|
|
+
|
|
|
|
+ public function __construct($roomid)
|
|
|
|
+ {
|
|
|
|
+ $this->mRoomId = $roomid;
|
|
|
|
+ $this->mDateId = strtotime(date('Y-m-d',time()));
|
|
|
|
+ $mod_bargain = Model('room_bargain');
|
|
|
|
+ $info = $mod_bargain->getBargainByRoom($roomid,true);
|
|
|
|
+ $bargain = new bargain($info);
|
|
|
|
+ $this->mBargainId = $bargain->bargain_id();
|
|
|
|
+ $this->mParams = new bargain_parameter($bargain);
|
|
|
|
+
|
|
|
|
+ $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;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public function bargain($userid)
|
|
|
|
+ {
|
|
|
|
+ $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($this->mDateId,$userid);
|
|
|
|
+
|
|
|
|
+ $room_extend = new room_extend($info);
|
|
|
|
+ $count = $room_extend->bargain_count();
|
|
|
|
+
|
|
|
|
+ $value = $this->mParams->bargain($count);
|
|
|
|
+ if($value === false) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ if($count > 0) {
|
|
|
|
+ $mod_room->editExtend($this->mDateId,$userid,['bargain_count' => ['exp', 'bargain_count+1']]);
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ $mod_room->addExtend($this->mDateId,$userid,['bargain_count' => 1]);
|
|
|
|
+ }
|
|
|
|
+ $mod_room->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()]);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ return ['value' => $value,'success' => true,'discount' => $this->mParams->discount(),'user_num' => $this->mParams->user_num()];
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private function bargained($userid)
|
|
|
|
+ {
|
|
|
|
+ if(array_key_exists($userid,$this->mFriends)) {
|
|
|
|
+ return $this->mFriends[$userid];
|
|
|
|
+ } else {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|