Przeglądaj źródła

complete bargain

stanley-king 7 lat temu
rodzic
commit
9eeea36dab

+ 20 - 0
data/model/room.model.php

@@ -55,4 +55,24 @@ class roomModel extends Model
     {
         return $this->table('room_msg')->insert($datas);
     }
+
+    public function getRoomExtend($dateid,$userid,$lock=false)
+    {
+        return $this->table('room_extend')->field('*')->where(['userid' => $userid, 'dateid' => $dateid])->lock($lock)->find();
+    }
+    public function addExtend($dateid,$userid,$datas)
+    {
+        $datas['dateid'] = $dateid;
+        $datas['userid'] = $userid;
+
+        return $this->table('room_extend')->insert($datas);
+    }
+    public function editExtend($dateid,$userid,$datas)
+    {
+        return $this->table('room_extend')->where(['userid' => $userid, 'dateid' => $dateid])->update($datas);
+    }
+    public function editBargain($bargain_id,$datas)
+    {
+        return $this->table('room_bargain')->where(['bargain_id' => $bargain_id])->update($datas);
+    }
 }

+ 183 - 0
helper/room/bargain_manager.php

@@ -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;
+        }
+    }
+}

+ 31 - 25
helper/room/bargain_room.php

@@ -57,56 +57,62 @@ class bargain
     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;
+    }
 }
 
 class bargain_room extends base_room
 {
-    private $mBargain;
+    private $mManager;
 
     public function __construct($cinfos, $roomkeys = [])
     {
         parent::__construct($cinfos,$roomkeys);
-        $this->mRoomType = 'bargain_goods';
-        $this->init();
-    }
 
-    private function init()
-    {
-        $mod_bargain = Model('room_bargain');
-        $info = $mod_bargain->getBargainByRoom($this->room_id(),true);
-        $this->mBargain = new bargain($info);
+        $this->mRoomType = 'bargain_goods';
+        $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;
         }
 
-        $this->mAccReq = true;
-        $ret = $this->bargain($room_key);
         $userinfo = $this->find($room_key);
         if($userinfo == false) return false;
 
-        if($ret !== false) {
-            $this->add_return([$room_key],'ret_bargain',['from' => $userinfo,'value' => $ret]);
-            $this->add_broad('bargain',['from' => $userinfo,'value' => $ret]);
+        $userid = $userinfo['userid'];
+        $result = $this->mManager->bargain($userid);
+
+        if($result !== false)
+        {
+            $value    = $result['value'];
+            $success  = $result['success'];
+            $discount = $result['discount'];
+            $user_num = $result['user_num'];
+
+            if($success) {
+                $this->add_return([$room_key],'ret_bargain',['from' => $userinfo,'value' => $value,'discount' => $discount,'user_num' => $user_num]);
+                $this->add_broad('bargain',['from' => $userinfo,'value' => $value,'discount' => $discount,'user_num' => $user_num]);
+            }
+            else {
+                $this->add_return([$room_key],'ret_bargain',['from' => $userinfo,'value' => $value,'discount' => $discount,'user_num' => $user_num]);
+            }
             return true;
         }
-        else {
+        else
+        {
             return false;
         }
     }
-
-    private function bargain($room_key)
-    {
-        $userinfo = $this->find($room_key);
-        if($userinfo == false) return false;
-        $value = 3;
-        $this->record_message($userinfo['userid'],proto_type::msg_type_bargain,json_encode(['value' => $value]));
-        return $value;
-    }
 }

+ 0 - 9
helper/room/base_room.php

@@ -36,8 +36,6 @@ abstract class base_room extends base_info
     public function inviteOp($input)
     {
         $this->clear();
-
-        $this->mAccReq = false;
         $userid = intval($input['user']);
         if($userid > 0) {
             $room_key = $this->invite($userid);
@@ -105,7 +103,6 @@ abstract class base_room extends base_info
             return false;
         }
 
-        $this->mAccReq = true;
         if($this->join($room_key))
         {
             $this->add_return([$room_key],'ret_join',$this->room_info($room_key));
@@ -125,7 +122,6 @@ abstract class base_room extends base_info
         $userinfo = $this->find($room_key);
         if($userinfo == false) return false;
 
-        $this->mAccReq = true;
         if($this->leave($room_key)) {
             $this->add_return([$room_key],'ret_leave',$userinfo);
             $this->add_broad('leave',$userinfo);
@@ -179,7 +175,6 @@ abstract class base_room extends base_info
     protected function record_message($userid,$type,$content)
     {
         $mod_room = Model('room');
-
         $mod_room->addRoomMsg(['room_id' => $this->mRoomid,'member_id' => $userid, 'type' => $type,'msg' => $content,'add_time' => time()]);
     }
 
@@ -230,10 +225,6 @@ abstract class base_room extends base_info
         $this->mCurRespMsgs['broadcast'][] = $msg;
     }
 
-    public function acc_req() {
-        return $this->mAccReq;
-    }
-
     public function join($participant)
     {
         $ret = $this->find($participant);

+ 1 - 0
helper/room_helper.php

@@ -14,3 +14,4 @@ require_once (BASE_ROOT_PATH . '/helper/room/chatwo_room.php');
 require_once (BASE_ROOT_PATH . '/helper/room/factory_client.php');
 require_once (BASE_ROOT_PATH . '/helper/room/group_room.php');
 require_once (BASE_ROOT_PATH . '/helper/room/room_client.php');
+require_once (BASE_ROOT_PATH . '/helper/room/bargain_manager.php');

+ 7 - 0
mobile/control/bargain.php

@@ -18,6 +18,13 @@ class bargainControl extends mobileControl
     {
         parent::__construct();
     }
+
+    public function indexOp()
+    {
+        return self::outsuccess(['tpl' => null],'bargain/bargain_list');
+
+    }
+
     public function createOp()
     {
         if (!session_helper::logined()) {

+ 36 - 0
test/TestRoom.php

@@ -0,0 +1,36 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: stanley-king
+ * Date: 2018/1/23
+ * Time: 上午11:27
+ */
+
+
+define('BASE_ROOT_PATH',str_replace('/test','',dirname(__FILE__)));
+
+require_once(BASE_ROOT_PATH . '/fooder.php');
+
+require_once(BASE_ROOT_PATH . '/helper/room_helper.php');
+require_once(BASE_ROOT_PATH . '/helper/relation_helper.php');
+require_once(BASE_ROOT_PATH . '/helper/performance_helper.php');
+require_once(BASE_ROOT_PATH . '/helper/algorithm.php');
+
+class TestRoom extends PHPUnit_Framework_TestCase
+{
+    public static function setUpBeforeClass()
+    {
+        Base::run_util();
+    }
+
+    public function testBargainOperator()
+    {
+        $operator = new room\bargain_manager(43);
+        $values = [];
+        for ($i = 39624; $i < 39824;$i++) {
+            $value = $operator->bargain($i);
+            $values[$i] = $value['value'];
+        }
+    }
+
+}