閱讀231 返回首頁    go iPhone_iPad_Mac_apple


項目4:抽獎程序 分時間段(按時段設置的獎品數為概率)

數據庫先

activity_lyq_lottery	獎品表	
lotteryid		
name	禮品名稱	
sum	禮品總數	
time_start	開始時間	
time_end	結束時間	
addtime	時間	
probability	中獎概率	大於0:表示概率    0:表示獎品數為概率
isdisable	是否開啟	0開啟1關閉
listorder	排序	默認正序
msg	發送短信或顯示信息	
content	備注	
angle	轉盤角度	獎品所在的角度
time_1	時間段個數限製	個數限製,-1表示不限製,0表示不發
time_2		
time_3		
time_4		
time_5		
time_6		
time_7		
time_8		
time_9		
time_10		
time_11		
time_12		
time_13		
time_14		
time_15		
time_16		
time_17		
		
		
		
activity_lyq_lottery_log	抽獎日誌	
logid	ID	
uid	用戶ID	
tel	手機號	
addtime	抽獎時間	
ip	抽獎IP	
lotteryid	獎品ID	
iswinning	是否抽中	1抽中0無
result	返狀態回值	
hao	隨機號	

主程序

<?php
require_once (APP_ROOT . "./class/class_lib.php");
require_once libfile ( 'class/StoreProcs' );
pc_base::load_app_class('table_sub','',0);
/**
 * 大轉盤
 * @author Administrator
 *
 */
class lib_dzp_lottery extends app_lib {
	/**
	 * 獎品表
	 */
	public $lottery = 'activity_lyq_lottery';
	/**
	 * 獎品日誌表
	 */
	public $lottery_log = 'activity_lyq_lottery_log';
	/**
	 * n等獎品
	 */
	private $level = array();
	/**
	 * 獎品信息
	 */
	private $lotteryArray = array();
	/***
	 * 隨機數結束位數
	 */
	private $max = 30000;
	/**
	 * 中獎專業戶
	 */
	private $tel_zy=false;
	/**
	 *增加時段時間
	 */
	private $second=0;
	/**
	 *時段時間
	 */
	private $time=array();
	/**
	 * 讀取配置
	 */
	public function load_setting() {
		$data=getcache('lyqdzp_setting');
		define('TIME', time());									//當前時間
		define('L_START', strtotime($data['l_start']));			//報名開始時間
		define('L_END', strtotime($data['l_end']));				//報名結束時間,-1意思昨天 23:59:59
		define('L_DAY_START',$data['l_day_start']);				//每天開始時間
		define('L_DAY_END', $data['l_day_end']);				//每天結束時間
		define('L_DAY_CISHU', $data['l_day_cishu']);			//每天獲得獎品次數
		define('L_CISHU', $data['l_cishu']);					//共計獲得獎品次數
		define('L_MIN', 20);									//最少來福幣
		define('L_LFB', $data['l_lfb']);						//減少來福幣
		define('L_ZY', $data['l_zy']);							//專業戶
		define('L_QUANSHU', $data['l_quanshu']);				//圈數,大轉盤開始圈數
		define('L_KONGJIANG', intval($data['l_kongjiang']));			//空獎概率
		$host='www.*****.com';								//現網
		if($_SERVER['HTTP_HOST']=='221.******'){
			$host='221.*******';						//測試機
		}elseif($_SERVER['HTTP_HOST']=='125.com'){
			$host='125life.com';								//本地
		}elseif(strpos($_SERVER['HTTP_HOST'],'192.168.')==0){
			//$host=$_SERVER['HTTP_HOST'].'/125.com';			//本地
		}
		define('L_SITE', 'https://'.$host.'/');					//域名最後加/
		define('L_URL', L_SITE.'index.php');	//訪問地址
		
		//隻能通過flash地址訪問
		$s=L_SITE.'flash/zhuanpan/i.swf';
		if(preg_replace('/swf.*$/','swf',$_SERVER['HTTP_REFERER'])!=$s){
			exit('老兄你已經得到得夠多了,請通過正規途徑搖獎!');
		}
		$this->load();
	}
	/**
	 * 黑名單
	 */
	private $heimingdan = array();
	/**
	 * 讀取獎品配置
	 */
	public function load() {
		$this->setlottery ();
		$this->heimingdan = getcache ( 'tel', 'blanklist' );
	}
	private function setlottery() {
		$time=strtotime(date('Y-m-d',TIME));
		$sql = "select u.* ";
		$sql .= " from " . DB::table ( $this->lottery ) . " AS u ";
		$sql .= " where 1 ";
		$sql .= " and u.isdisable='0' ";
		$sql .= " and u.time_start<='$time' ";
		$sql .= " and u.time_end>='$time' ";
		$sql .= " ORDER BY u.listorder ";
		$d = DB::fetch_all ( $sql );
		
		foreach ( $d as $r ) {
			$sql = "select count(*) m from " . DB::table ( $this->lottery_log );
			$sql .= " where lotteryid='".$r ['lotteryid']."' and iswinning='1' ";
			$r ['num']= intval ( DB::result_first ( $sql ) );
			$r ['sum_over'] = intval ( $r ['sum'] ) - intval ( $r ['num'] );
			$r['msg']=str_replace('{name}', $r['name'],$r['msg']);
			$this->lotteryArray [$r ['lotteryid']] = $r;
		}

		
		//時間段的獎品數為概率
		$sum = 0;
		$this->second=$second=45*60;
		$d = array(
				1 => array(
						'time' => strtotime ( date ( 'Y-m-d 00:00:00', TIME ) ),
						'time_end' => strtotime ( date ( 'Y-m-d 07:59:59', TIME ) )
				)
		);
		for ($i=2;$i<=17;$i++){
			$d[$i]=array(
					'time' => strtotime ( date ( 'Y-m-d '.($i+6).':00:00', TIME ) )+$second,
					'time_end' => strtotime ( date ( 'Y-m-d '.($i+6).':59:59', TIME ) )+$second
			);
		}
		
		foreach ($d as $k=>$t){
			if(TIME>=$t['time']&&TIME<=$t['time_end']){
				foreach ( $this->lotteryArray as $r ) {
					if ($r ['sum_over'] > 0) {
						if($r ['time_'.$k]>0){
							$sum += $r ['time_'.$k];
						}
					}
				}
			}
		}
		
		$this->time=$d;
		//本時段 獎品數
		foreach ($d as $k=>$t){
			if(TIME>=$t['time']&&TIME<=$t['time_end']){
				$m = 1;
				$probability = 0;
				$newsum=0;
				//80%空獎+20%獎
				$this->max = $sum + $sum * (L_KONGJIANG/100) / ((100-L_KONGJIANG)/100);
				
				foreach ( $this->lotteryArray as $r ) {
					$newsum=0;
					if ($r ['sum_over'] > 0) {
						if($r ['time_'.$k]>0){
							$newsum=$r ['time_'.$k];
							if($r['probability']>0){
								$probability=0;
								$probability=ceil($newsum*100/$r['probability']);
								if($probability>0){
									$newsum=$probability;
								}
							}
							$this->level [$r ['lotteryid']] ['start'] = $m;
							$this->level [$r ['lotteryid']] ['end'] = $m + $newsum;
							$m = $m + $newsum;
						}elseif ($r ['time_'.$k]<0){
							$newsum=$r ['sum_over'];
							if($r['probability']>0){
								$probability=0;
								$probability=ceil($newsum*100/$r['probability']);
								if($probability>0){
									$newsum=$probability;
								}
							}
							$this->level [$r ['lotteryid']] ['start'] = $m;
							$this->level [$r ['lotteryid']] ['end'] = $m + $newsum;
							$m = $m + $newsum;
						}
					}
				}
			}
		}
	}
	/**
	 * 檢測用戶是否中獎 0 謝謝參與,其他 返回獎品data數據
	 * @param array $user array(uid,tel,ip)
	 */
	public function check($user) {
		if (intval ( $user ['uid'] ) < 1) {
			//沒有登錄
			return 0;
		}
		if(intval(TIME)<intval(L_START)){
			//您來早了,活動還沒有開始!
			return -21;
		}
		if(intval(TIME)>intval(L_END)){
			//已經過了活動時間,非常遺憾,謝謝!
			return -22;
		}
		if ($this->get_user_lfb ( $user ['uid'] ) < L_MIN) {
			//$this->credit ( $user ['uid'], -11, $logid );
			return -11; //您的來福幣餘額不足
		}
		
		$data = array(
				'uid' => $user ['uid'],
				'tel' => $user ['tel'],
				'ip' => $user ['ip'],
				'addtime' => time ()
		);
		$logid = DB::insert ( $this->lottery_log, $data, true );

		
		//檢查用戶  每天 已中獎品數
		if ($this->get_lottery ( $user ['uid'], strtotime ( date ( 'Y-m-d', TIME ) ), strtotime ( "+1day", strtotime ( date ( 'Y-m-d', TIME ) ) ), true ) >= L_DAY_CISHU) {
			$this->credit ( $user ['uid'], -12, $logid );
			//很遺憾,你本次什麼也沒有抽到!
			return -12;
		}
		//檢查用戶  全部 已中獎品數
		if ($this->get_lottery ( $user ['uid'], 0, 0, true ) >= L_CISHU&&L_CISHU>0) {
			$this->credit ( $user ['uid'], -12, $logid );
			//很遺憾,你本次什麼也沒有抽到!
			return -15;
		}
		//檢測黑名單,設置概率
		if ($this->checkhei ( $user ['tel'] ) > 0) {
			$this->credit ( $user ['uid'], -13, $logid );
			//很遺憾,你本次什麼也沒有抽到!
			return -13;
		}
		$lotteryid = $this->choujiang();
		$this->credit ( $user ['uid'], $lotteryid<1?$lotteryid:0, $logid, $lotteryid ,$this->hao);
		if ($lotteryid > 0) {
			return $this->lotteryArray [$lotteryid];
		} else {
			return -4;
		}
		//-1獎品已發完
		//-2本時段獎品已玩
		//-3不發獎品
		//-4獎品有問題
		//-11您的來福幣餘額不足
		//-12很遺憾,你本次什麼也沒有抽到!
		//-13黑名單裏麵,很遺憾,你本次什麼也沒有抽到!
		//-14中獎專業戶禁止中高講高獎品
		//-15已超過yizhong總獎品數
		//-21您來早了,活動還沒有開始!
		//-22已經過了活動時間,非常遺憾,謝謝!
		//-23對不起,現在不是活動時間,活動時間為每天
	}
	/**
	 * 顯示
	 * @param unknown_type $data
	 */
	public function show($data){
		$ok=0;//3中獎
		$zz=0;//轉盤度數
		$result='';//獎品名稱
		$lotteryid=0;//獎品ID
		if(is_array($data)){
			$ok=3;
			$result=$data['msg'];
			$zz=$data['angle']+L_QUANSHU*360;
			$lotteryid=$data['lotteryid'];
		}else{
			$m='';
			switch($data){
				case 0:{
					$m= '請先登錄!';
				}break;
				case -11:{
					$m= '您的來福幣餘額不足!';
				}break;
				case -12:{
					$m= '很遺憾,你本次什麼也沒有抽到!';
				}break;
				case -21:{
					$m= '您來早了,活動還沒有開始!';
				}break;
				case -22:{
					$m= '已經過了活動時間,非常遺憾,謝謝!';
				}break;
				case -23:{
					$m= '對不起,現在不是活動時間,活動時間為每天'.L_DAY_START.':00-'.L_DAY_END.':00';
				}break;
				default:{
					$m= '很遺憾,你本次什麼也沒有抽到!';
				}break;
			}
			$result=$m;
			$ok=3;//3中獎
			$zz=360+L_QUANSHU*360;//再接再厲
		}
		header("Expires:Mon,26Jul199705:00:00GMT");
		header("Cache-Control:no-cache,must-revalidate");
		header("Pragma:no-cache");
		header("Content-type:text/html;charset=utf-8");
		echo '<?xml version="1.0" encoding="UTF-8"?>'."\n";
		echo '<gift>'."\n";
		echo '<shift ok="'.$ok.'" zz="'.$zz.'" add="9" down="0" l result="'.$result.'"/>'."\n";
		echo '</gift>'."\n";
		exit;
	}
	/**
	 * 抽獎的號
	 */
	private $hao=0;
	/**
	 * 抽獎開始
	 */
	private function choujiang() {
		$j = 0;
		$lotteryid = 0;
		$j = mt_rand ( 1, $this->max );
		$this->hao=$j;
		foreach ( $this->level as $k => $r ) {
			if ($r ['start'] <= $j && $j< $r ['end'] ) {
				$lotteryid = $k;
				break;
			}
		}
		$d = $this->time;
		if($lotteryid>0){
			$l=$this->lotteryArray [$lotteryid];
			$sql = "select count(*) m from " . DB::table ( $this->lottery_log );
			$sql .= " where lotteryid='$lotteryid' ";
			$s= intval ( DB::result_first ( $sql ) );
			$sum_over=$l['sum']-$s;
			if($sum_over>0){
				//本時段 獎品數
				$sum=0;
				foreach ($d as $k=>$r){
					if(TIME>=$r['time']&&TIME<=$r['time_end']){
						if($l['time_'.$k]>0){//個數限製
							$sum=$this->get_lottery_id(0,$r['time'],$r['time_end']+1,$lotteryid);
							if($sum>=$l['time_'.$k]){
								//本時段獎品已玩
								$lotteryid=-2;
								break;
							}
						}elseif ($l['time_'.$k]==0){
							//不發獎品
							$lotteryid=-3;
							break;
						}else{
							//-1 不限製
						}
						
					}
				}
				if($this->tel_zy && $lotteryid>0&&$lotteryid<4){
					$lotteryid=-14;
				}
			}else{
				//獎品已發完
				$lotteryid=-1;
			}
		}else{
			//獎品有問題
			$lotteryid=-4;
		}
		return $lotteryid;
	
	}
	/**
	 * 檢測黑名單和普通用戶,概率
	 * @param string $tel
	 * @return 大於1 表示本次禁止抽
	 */
	private function checkhei($tel) {
		$n = mt_rand ( 1, 100 );
		if (in_array ( $tel, $this->heimingdan ['tel'] ) && L_ZY > 0) {
			$this->tel_zy=true;
			if ($n > L_ZY) {
				return 1;
			}
		}else{
			//號碼檢測
 			if (!self::check_tel($tel)) {
 				return 1;
 			}
		}
		return 0;
	}
	/**
	 * 合計獎品數
	 * @param intval $uid
	 * @param intval $start
	 * @param intval $end
	 * @param bool $bool true 獲得獎品數 false 不統計
	 * @return number
	 */
	private function get_lottery($uid = 0, $start = 0, $end = 0, $bool = false) {
		$sql = "select count(*) m from " . DB::table ( $this->lottery_log );
		$sql .= " where 1 ";
		if (intval ( $start ) > 0) {
			$sql .= " and addtime>='$start' ";
		}
		if (intval ( $end ) > 0) {
			$sql .= " and addtime<'$end' ";
		}
		if ($bool) {
			$sql .= " and lotteryid>0 ";
		}
		if (intval ( $uid ) > 0) {
			$sql .= " and uid='$uid' ";
		}
		return intval ( DB::result_first ( $sql ) );
	}
	/**
	 * 合計獎品數
	 * @param intval $uid
	 * @param intval $start
	 * @param intval $end
	 * @param intval $bool 獎品id
	 * @return number
	 */
	private function get_lottery_id($uid = 0, $start = 0, $end = 0, $lotteryid = 0) {
	
		$sql = "select count(*) m from " . DB::table ( $this->lottery_log );
		$sql .= " where 1 ";
		if (intval ( $start ) > 0) {
			$sql .= " and addtime>='$start' ";
		}
		if (intval ( $end ) > 0) {
			$sql .= " and addtime<'$end' ";
		}
		if ($lotteryid>=0) {
			$sql .= " and lotteryid='$lotteryid' ";
		}
		if (intval ( $uid ) > 0) {
			$sql .= " and uid='$uid' ";
		}
		return intval ( DB::result_first ( $sql ) );
	}
	/**
	 * 獲取用戶來福幣
	 * @param intval $uid
	 * @return number
	 */
	public function get_user_lfb($uid) {
		$sql = "select extcredits2 from sz_common_member_count ";
		$sql .= " where 1 ";
		$sql .= " and uid='$uid' ";
		$s=intval ( DB::result_first ( $sql ) );
		return $s>0?$s:0;
	}
	/**
	 * 記錄日誌和更新用戶來福幣
	 * @param intval $uid
	 * @param intval $r  返回值
	 * @param intval $logid
	 * @param intval $lotteryid
	 */
	private function credit($uid, $result, $logid = 0, $lotteryid = 0,$hao=0) {
		$data = array();
		if ($lotteryid > 0) {
			$data ['lotteryid'] = $lotteryid;
			$data ['iswinning'] = 1;
		}
		$data ['result'] = $result;
		$data ['hao'] = $hao;
		$data ['HTTP_REFERER'] = $_SERVER['HTTP_REFERER'];
		$data['139sz']=$this->type?0:1;
		DB::Update ( $this->lottery_log, $data, " logid='$logid' " );
		if (L_LFB > 0) {
			$sql = "UPDATE sz_common_member_count SET extcredits2=extcredits2-'". L_LFB ."' WHERE extcredits2>'0' and extcredits2>='".L_LFB."' and uid='$uid' ";
			DB::query ( $sql );
			$this->credit_log ( $uid, 'reduce', L_LFB, 1, '積分清零活動' );
		}
	}
	
	/**
	 *手機號檢查
	 */
	private function check_tel($tel){
		$different_network=table_sub::get_different_network();
		if (!preg_match("/$different_network/",$tel)) {
			return false;
		}
		return true;
	}
	/**
	 * 存儲過程鏈接
	 */
	private $StoreProcs;
	/**
	 * 來福幣日誌 存儲過程
	 * @param intval $uid
	 * @param string $op add增加  reduce 消耗
	 * @param intval $creditvalue 來福幣
	 * @param intval $credittype 1來福幣 2來福點
	 * @param string $oreason  說明
	 */
	private function credit_log($uid, $op, $creditvalue, $credittype, $oreason) {
		if (! $this->StoreProcs) {
			$this->StoreProcs = new StoreProcs ();
		}
		//調用存儲過程
		$this->StoreProcs->query ( "call app_credit_log($uid,'$op','$credittype',1,$creditvalue,'$oreason')" );
	}

}
調用:
$dzp=new lib_dzp_lottery();
$u=array('uid'=>$_G ['uid'],'tel'=>$_G ['tel'],'ip'=> $_G ['clientip']);
$dzp->load_setting();
$dzp->show($dzp->check($u));


最後更新:2017-04-02 06:52:19

  上一篇:go 搜索關鍵字飛入飛出效果
  下一篇:go JAVA 十六進製與字符串的轉換