stanley-king 7 rokov pred
rodič
commit
87faeb2be0

+ 86 - 0
helper/bonus/thief_vilator.php

@@ -0,0 +1,86 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: stanley-king
+ * Date: 2017/6/24
+ * Time: 上午11:40
+ */
+
+namespace bonus;
+
+use algorithm;
+use errcode;
+use predeposit_helper;
+use gain_policy;
+
+class thief_vilator
+{
+    private $mFromID;
+    private $mDateId;
+
+    const prifix = 'bonus_thief';
+    const min_threshold = 200;
+
+    public function __construct($fromid)
+    {
+        $this->mFromID = $fromid;
+        $this->mDateId = strtotime(date('Y-m-d',time()));
+    }
+
+    public function thief(&$error)
+    {
+        if($this->can_thief() == false) {
+            $error = ['code' => errcode::ErrBonus,'msg' => '每天只能偷一次好友的红包'];
+            return false;
+        }
+        $amount = $this->calc_amount();
+        if($amount == false) {
+            $error = ['code' => errcode::ErrBonus,'msg' => '今日不能偷该用户的红包~'];
+            return false;
+        }
+        else {
+            $this->add_thief();
+        }
+
+        return $amount;
+    }
+
+    private function calc_amount()
+    {
+        $pred = new predeposit_helper($this->mFromID);
+        $usable_amount = $pred->total_bonus();
+
+        $policy = new gain_policy(5, $usable_amount);
+        return $policy->calculate();
+    }
+
+    private function can_thief()
+    {
+        if(!isset($_SESSION[self::prifix]) || !array_key_exists($this->mDateId,$_SESSION[self::prifix])) {
+            $_SESSION[self::prifix] = [];
+            $_SESSION[self::prifix][$this->mDateId] = [];
+        }
+
+        $fromid = $this->mFromID;
+        $mids = &$_SESSION[self::prifix][$this->mDateId];
+        if(algorithm::binary_search($mids,$fromid)) {
+            return false;
+        } else {
+            return true;
+        }
+    }
+    private function add_thief()
+    {
+        if(!isset($_SESSION[self::prifix]) || !array_key_exists($this->mDateId,$_SESSION[self::prifix])) {
+            $_SESSION[self::prifix] = [];
+            $_SESSION[self::prifix][$this->mDateId] = [];
+        }
+
+        $fromid = $this->mFromID;
+        $mids = &$_SESSION[self::prifix][$this->mDateId];
+        if(algorithm::binary_search($mids,$fromid) == false) {
+            $pos = algorithm::lower_bonud($mids,$fromid);
+            algorithm::array_insert($mids,$pos,$fromid);
+        }
+    }
+}

+ 1 - 0
helper/bonus_helper.php

@@ -23,6 +23,7 @@ require_once (BASE_ROOT_PATH . '/helper/bonus/allocator.php');
 require_once (BASE_ROOT_PATH . '/helper/bonus/witholder.php');
 require_once (BASE_ROOT_PATH . '/helper/bonus/open_sharer.php');
 require_once (BASE_ROOT_PATH . '/helper/bonus/scaler.php');
+require_once (BASE_ROOT_PATH . '/helper/bonus/thief_vilator.php');
 
 
 class bonus_helper

+ 6 - 0
helper/session_helper.php

@@ -11,6 +11,7 @@ require_once (BASE_ROOT_PATH . '/helper/relation_helper.php');
 require_once (BASE_ROOT_PATH . '/helper/account_helper.php');
 require_once (BASE_ROOT_PATH . '/helper/push_helper.php');
 require_once (BASE_ROOT_PATH . '/helper/login_helper.php');
+require_once (BASE_ROOT_PATH . '/helper/bonus_helper.php');
 
 class session_helper
 {
@@ -316,4 +317,9 @@ class session_helper
 
         return false;
     }
+    static public function thief($fromid,&$err)
+    {
+        $thief = new bonus\thief_vilator($fromid);
+        return $thief->thief($err);
+    }
 }

+ 16 - 8
mobile/control/member_bonus.php

@@ -1068,14 +1068,22 @@ class member_bonusControl extends mbMemberControl
         }
         else
         {
-            $bonuses = account_helper::gain_bonus($user_id, $_SESSION['member_id'], 10);
-            if($bonuses == false || empty($bonuses)) {
-                return self::outerr(errcode::ErrBonus,"这次没偷着");
-            } else {
-                $bonus = $bonuses[0];
-                $tips = "您成功偷到{$bonus['bonus_value']}元";
-                return self::outsuccess(array("bonuses" => $bonuses,
-                    "info" => ['count' => 1, 'money' => $bonus['bonus_value'],'tips' => $tips]));
+            $amount = session_helper::thief($user_id,$err);
+            if($amount == false)
+            {
+                return self::outerr($err['code'],$err['msg']);
+            }
+            else
+            {
+                $bonuses = account_helper::gain_bonus($user_id, $_SESSION['member_id'], $amount);
+                if($bonuses == false || empty($bonuses)) {
+                    return self::outerr(errcode::ErrBonus,"这次没偷着");
+                } else {
+                    $bonus = $bonuses[0];
+                    $tips = "您成功偷到{$bonus['bonus_value']}元";
+                    return self::outsuccess(array("bonuses" => $bonuses,
+                        "info" => ['count' => 1, 'money' => $bonus['bonus_value'],'tips' => $tips]));
+                }
             }
         }
     }

+ 0 - 1
test/TestGD.php

@@ -20,5 +20,4 @@ class TestGD extends PHPUnit_Framework_TestCase
         imagecopy($back, $icon, 60, 60, 0, 0, imagesx($icon), imagesy($icon));
         imagejpeg($back, $dest_img,100);
     }
-
 }