123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 16/6/28
- * Time: 上午11:33
- */
- require_once (BASE_ROOT_PATH . '/helper/account_helper.php');
- require_once (BASE_ROOT_PATH . '/helper/bonus_helper.php');
- class friend
- {
- //last_time,count,user_id
- private $mParam;
- public function __construct($param)
- {
- $this->mParam = $param;
- }
- public function last_time() {
- return intval($this->mParam['last_time']);
- }
- public function is_poor() {
- return (time() - intval($this->mParam['last_time']) <= shaker_helper::max_poor_period);
- }
- public function can_gain() {
- return (time() - intval($this->mParam['last_time']) >= shaker_helper::max_gain_period);
- }
- public function can_gain_system() {
- return (time() - intval($this->mParam['last_time']) >= shaker_helper::max_gain_period_system);
- }
- public function can_lost() {
- return (time() - intval($this->mParam['last_time']) >= shaker_helper::max_lost_period);
- }
- public function user_id() {
- return intval($this->mParam['user_id']);
- }
- public function count() {
- return intval($this->mParam['count']);
- }
- }
- class gain_policy
- {
- const max_amount = 5;
- private $strength;
- private $total_amount;
- public function __construct($strength,$amount) {
- $this->strength = $strength;
- $this->total_amount = $amount;
- }
- public function calculate()
- {
- $scale = $this->scale();
- if($scale == false) {
- return false;
- }
- $cur_value = intval($this->total_amount) >= self::max_amount ? self::max_amount : $this->total_amount;
- $value = intval($this->scale() * $cur_value * 100 + 0.5);
- if($value == 0) {
- return false;
- }
- else {
- return $value / 100;
- }
- }
- private function scale()
- {
- if($this->strength == 5) {
- $start = 0.4;
- $end = 1.0;
- }
- elseif ($this->strength == 4) {
- $start = 0.3;
- $end = 0.8;
- }
- elseif ($this->strength == 3) {
- $start = 0.2;
- $end = 0.6;
- }
- elseif ($this->strength == 2) {
- $start = 0.1;
- $end = 0.4;
- }
- elseif ($this->strength == 1) {
- $start = 0.0;
- $end = 0.2;
- }
- else {
- return false;
- }
- $start = intval($start * 100 + 0.5);
- $end = intval($end * 100 + 0.5);
- $ret = mt_rand($start,$end);
- return $ret / 100;
- }
- }
- class shaker_helper
- {
- const max_gain_period = 12 * 3600;
- const max_poor_period = 1800;
- const max_gain_period_system = 1;//3 * 3600;
- const max_lost_period = 1800;
- const err_msg = '您此次么有抢到好友红包...邀请更多好友,更多机会哦~';
- const shake_expire = 5;
- const direct_gain = 0;
- const direct_lost = 1;
- const max_strength = 5;
- private $strength = 1;
- private function gain(&$err)
- {
- $friends = $this->gain_friends();
- $usable_amount = 0.00;
- $user_id = $this->find_gain_friend($friends, $usable_amount);
- if ($user_id != false)
- {
- $policy = new gain_policy($this->strength, $usable_amount);
- $value = $policy->calculate();
- if($value == false) {
- $err = array('code' => errcode::ErrShake, 'msg' => self::err_msg);
- return false;
- }
- Log::record(__METHOD__ . " gain amount={$value}",Log::DEBUG);
- $bonus = account_helper::gain_bonus($user_id, $_SESSION['member_id'], $value);
- if($bonus != false) {
- $this->add_gained_friend(array($user_id));
- } else {
- $err = array('code' => errcode::ErrShake, 'msg' => self::err_msg);
- }
- return $bonus;
- }
- else
- {
- if ($this->gained_system() == false) {
- $err = array('code' => errcode::ErrShake, 'msg' => self::err_msg);
- return false;
- }
- else
- {
- $policy = new gain_policy($this->strength, 10);
- $value = $policy->calculate();
- if($value == false) {
- $err = array('code' => errcode::ErrShake, 'msg' => self::err_msg);
- return false;
- }
- Log::record(__METHOD__ . " lost amount={$value}",Log::DEBUG);
- $bonus = account_helper::gain_system($_SESSION['member_id'], $value);
- if($bonus != false) {
- $this->save_gained_system();
- } else {
- $err = array('code' => errcode::ErrShake, 'msg' => self::err_msg);
- }
- return $bonus;
- }
- }
- }
- private function lost(&$err)
- {
- $pred = new bonus\account($_SESSION['member_id']);
- $total_amount = $pred->total_bonus();
- if(intval($total_amount * 100 + 0.5) <= 0) {
- $err = array('code' => errcode::ErrShake,'msg' => self::err_msg);
- return false;
- }
- $friends = $this->lost_friends();
- $user_id = $this->find_lost_friend($friends);
- if($user_id == false) {
- $err = array('code' => errcode::ErrShake, 'msg' => self::err_msg);
- return false;
- }
- $policy = new gain_policy($this->strength, $total_amount);
- $value = $policy->calculate();
- if($value == false) {
- $err = array('code' => errcode::ErrShake, 'msg' => self::err_msg);
- return false;
- }
- $bonus = account_helper::lost_bonus($_SESSION['member_id'],$user_id, $value);
- if($bonus != false) {
- $this->add_lost_friend(array($user_id));
- } else {
- $err = array('code' => errcode::ErrShake, 'msg' => self::err_msg);
- return false;
- }
- return $bonus;
- }
- private function find_lost_friend($friends)
- {
- while (true)
- {
- $count = count($friends);
- if($count == 0) {
- return false;
- }
- $pos = mt_rand(0,$count -1);
- return $friends[$pos];
- }
- }
- private function find_gain_friend($friends, &$usable_amount)
- {
- while (true)
- {
- $count = count($friends);
- if($count == 0) return false;
- $pos = mt_rand(0,$count -1);
- $mod_bonus = Model('user_bonus');
- $usable_amount = $mod_bonus->getThiefableSum($friends[$pos]);
- if(intval($usable_amount * 100 + 0.5) > 0) {
- return $friends[$pos];
- } else {
- $this->add_poor_friend($friends[$pos]);
- array_splice($friends,$pos,1);
- }
- }
- }
- private function calc_strength($strength)
- {
- while (true)
- {
- $strength = intval($strength + 0.5);
- if($strength >= 10) {
- $strength = intval($strength + 0.5) / 10;
- } else {
- break;
- }
- }
- return $strength;
- }
- public function shake($strength,&$err)
- {
- if($this->can_shake() == false) {
- $period = self::shake_expire;
- $err = array('code' => errcode::ErrShake,'msg' => "摇得太频繁啦,{$period}秒钟只能摇一次~");
- return false;
- }
- $this->strength = $this->calc_strength($strength);
- $direct = $this->direction();
- if($direct == self::direct_gain) {
- $bonuses = $this->gain($err);
- }
- else {
- $bonuses = $this->lost($err);
- }
- if($bonuses != false) {
- $this->save_shake();
- }
- return $bonuses;
- }
- private function save_gained_system()
- {
- if(isset($_SESSION['game_shake']['gained_system'])) {
- $_SESSION['game_shake']['gained_system'] = array();
- }
- $system = &$_SESSION['game_shake']['gained_system'];
- if(empty($system)) {
- $system['user_id'] = $_SESSION['member_id'];
- $system['last_time'] = time();
- $system['count'] = 1;
- } else {
- $system['user_id'] = $_SESSION['member_id'];
- $system['last_time'] = time();
- $system['count'] = intval($system['count']) + 1;
- }
- }
- private function gained_system()
- {
- if(!isset($_SESSION['game_shake']['gained_system'])) {
- return true;
- } else {
- $param = $_SESSION['game_shake']['gained_system'];
- $friend = new friend($param);
- return $friend->can_gain_system();
- }
- }
- private function add_gained_friend($user_ids)
- {
- if(!isset($_SESSION['game_shake']['gained_friends'])) {
- $_SESSION['game_shake']['gained_friends'] = array();
- }
- $friends = &$_SESSION['game_shake']['gained_friends'];
- foreach ($user_ids as $user_id)
- {
- if(isset($friends[$user_id])) {
- $friends[$user_id]['user_id'] = $user_id;
- $friends[$user_id]['last_time'] = time();
- $friends[$user_id]['count'] = intval($friends[$user_id]['count']) + 1;
- } else {
- $friends[$user_id]['user_id'] = $user_id;
- $friends[$user_id]['last_time'] = time();
- $friends[$user_id]['count'] = 1;
- }
- }
- }
- private function add_poor_friend($user_id)
- {
- if(!isset($_SESSION['game_shake']['poor_friends'])) {
- $_SESSION['game_shake']['poor_friends'] = array();
- }
- $friends = &$_SESSION['game_shake']['poor_friends'];
- if(isset($friends[$user_id])) {
- $friends[$user_id]['user_id'] = $user_id;
- $friends[$user_id]['last_time'] = time();
- $friends[$user_id]['count'] = intval($friends[$user_id]['count']) + 1;
- } else {
- $friends[$user_id]['user_id'] = $user_id;
- $friends[$user_id]['last_time'] = time();
- $friends[$user_id]['count'] = 1;
- }
- }
- private function del_poor_friend($user_id)
- {
- if(!isset($_SESSION['game_shake']['poor_friends'])) {
- $_SESSION['game_shake']['poor_friends'] = array();
- }
- $friends = &$_SESSION['game_shake']['poor_friends'];
- if(isset($friends[$user_id])) {
- unset($friends[$user_id]);
- }
- }
- private function add_lost_friend($user_ids)
- {
- if(!isset($_SESSION['game_shake']['losted_friends'])) {
- $_SESSION['game_shake']['losted_friends'] = array();
- }
- $friends = &$_SESSION['game_shake']['losted_friends'];
- foreach ($user_ids as $user_id)
- {
- if(isset($friends[$user_id])) {
- $friends[$user_id]['user_id'] = $user_id;
- $friends[$user_id]['last_time'] = time();
- $friends[$user_id]['count'] = intval($friends[$user_id]['count']) + 1;
- } else {
- $friends[$user_id]['user_id'] = $user_id;
- $friends[$user_id]['last_time'] = time();
- $friends[$user_id]['count'] = 1;
- }
- }
- }
- //可以偷的好友
- private function gain_friends()
- {
- if(!isset($_SESSION['game_shake']['gained_friends'])) {
- $_SESSION['game_shake']['gained_friends'] = array();
- }
- $friends = $_SESSION['game_shake']['gained_friends'];
- $exids = [];
- foreach ($friends as $uid => $param)
- {
- $friend = new friend($param);
- if($friend->can_gain() == false) {
- $exids[] = $friend->user_id();
- }
- }
- if(!isset($_SESSION['game_shake']['poor_friends'])) {
- $_SESSION['game_shake']['poor_friends'] = array();
- }
- $poor_friends = $_SESSION['game_shake']['poor_friends'];
- foreach ($poor_friends as $uid => $param)
- {
- $friend = new friend($param);
- if($friend->is_poor()) {
- $exids[] = $friend->user_id();
- } else {
- $this->del_poor_friend($friend->user_id());
- }
- }
- $all_friends = relation_helper::friends(session_helper::memberid());
- sort($all_friends,SORT_NUMERIC);
- $all_friends = array_merge(array_unique($all_friends,SORT_NUMERIC),[]);
- sort($all_friends);
- foreach ($exids as $uid)
- {
- $pos = algorithm::bsearch($uid,$all_friends);
- if($pos != -1) {
- array_splice($all_friends,$pos,1);
- }
- }
- return $all_friends;
- }
- private function lost_friends()
- {
- if(!isset($_SESSION['game_shake']['losted_friends'])) {
- $_SESSION['game_shake']['losted_friends'] = array();
- }
- $friends = $_SESSION['game_shake']['losted_friends'];
- $exids = array();
- foreach ($friends as $param)
- {
- $friend = new friend($param);
- if($friend->can_lost() == false) {
- array_push($exids,$friend->user_id());
- }
- }
- $subscriber = relation_helper::subscriber($_SESSION['member_id']);
- $follower = relation_helper::follower($_SESSION['member_id']);
- $all_friends = array_merge($subscriber,$follower);
- sort($all_friends,SORT_NUMERIC);
- $all_friends = array_merge(array_unique($all_friends,SORT_NUMERIC),[]);
- sort($all_friends,SORT_NUMERIC);
- foreach ($exids as $uid)
- {
- $pos = algorithm::bsearch($uid,$all_friends);
- if($pos != -1) {
- array_splice($all_friends,$pos,1);
- }
- }
- return $all_friends;
- }
- private function can_shake()
- {
- if(!isset($_SESSION['game_shake'])) {
- $_SESSION['game_shake'] = array();
- }
- if(isset($_SESSION['game_shake']['last_time']))
- {
- $last_time = intval($_SESSION['game_shake']['last_time']);
- if($last_time + self::shake_expire >= time()) {
- return false;
- } else {
- return true;
- }
- }
- else {
- return true;
- }
- }
- private function save_shake()
- {
- if(!isset($_SESSION['game_shake'])) {
- $_SESSION['game_shake'] = array();
- }
- $_SESSION['game_shake']['last_time'] = time();
- }
- private function direction()
- {
- if(!isset($_SESSION['game_shake'])) {
- $_SESSION['game_shake'] = array();
- }
- if(!isset($_SESSION['game_shake']['direction'])) {
- $_SESSION['game_shake']['direction'] = self::direct_gain;
- }
- else
- {
- $rand = mt_rand(1,100);
- $direct = ($rand % 5) == 0 ? self::direct_lost : self::direct_gain;
- $_SESSION['game_shake']['direction'] = $direct;
- }
- return $_SESSION['game_shake']['direction'];
- }
- }
|