|
@@ -8,6 +8,7 @@
|
|
|
|
|
|
namespace room;
|
|
|
|
|
|
+use QueueClient;
|
|
|
class room_extend
|
|
|
{
|
|
|
private $mParams;
|
|
@@ -86,13 +87,16 @@ class bargain_parameter
|
|
|
return ($this->total_amount <= $this->discount);
|
|
|
}
|
|
|
|
|
|
- public function bargain($count)
|
|
|
+ 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;
|
|
@@ -128,15 +132,23 @@ class bargain_parameter
|
|
|
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 $mDateId;
|
|
|
private $mFriends;
|
|
|
private $mParams;
|
|
|
private $mBargainId;
|
|
|
private $mRoomId;
|
|
|
+ private $mBargain;
|
|
|
|
|
|
public function __construct($roomid)
|
|
|
{
|
|
@@ -144,9 +156,9 @@ class bargain_manager
|
|
|
$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->mBargain = new bargain($info);
|
|
|
+ $this->mBargainId = $this->mBargain->bargain_id();
|
|
|
+ $this->mParams = new bargain_parameter($this->mBargain);
|
|
|
|
|
|
$this->mFriends = [];
|
|
|
$mod_room = Model('room');
|
|
@@ -159,8 +171,9 @@ class bargain_manager
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public function bargain($userid)
|
|
|
+ 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()];
|
|
@@ -174,24 +187,53 @@ class bargain_manager
|
|
|
|
|
|
$room_extend = new room_extend($info);
|
|
|
$count = $room_extend->bargain_count($this->mDateId);
|
|
|
- $value = $this->mParams->bargain($count);
|
|
|
+ $value = $this->mParams->bargain($count,$completed);
|
|
|
if($value === false) {
|
|
|
return false;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
+
|
|
|
if(!$room_extend->date_empty($this->mDateId)) {
|
|
|
$mod_room->editExtend($userid,['bargain_count' => ['exp', 'bargain_count+1']]);
|
|
|
} else {
|
|
|
$mod_room->addExtend($userid,['bargain_count' => 1,'dateid' => $this->mDateId]);
|
|
|
}
|
|
|
+
|
|
|
$mod_bargain = Model('room_bargain');
|
|
|
- $mod_bargain->editBargain($this->mBargainId,['discount' => ['exp',"discount+{$value}"],'user_num' => ['exp',"user_num+1"]]);
|
|
|
+ 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()]);
|
|
|
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)) {
|