Home > Backend Development > PHP Tutorial > PHP lottery program code for time periods

PHP lottery program code for time periods

WBOY
Release: 2016-07-25 08:58:42
Original
1302 people have browsed it
  1. require_once (APP_ROOT . "./class/class_lib.php");
  2. require_once libfile ( 'class/StoreProcs' );
  3. pc_base::load_app_class('table_sub','',0 );
  4. /**
  5. * Big carousel
  6. * @author Administrator
  7. *
  8. */
  9. class lib_dzp_lottery extends app_lib {
  10. /**
  11. * Prize List
  12. */
  13. public $lottery = 'activity_lyq_lottery';
  14. /**
  15. * Prize Log Sheet
  16. */
  17. public $lottery_log = ' activity_lyq_lottery_log';
  18. /**
  19. * n prizes
  20. */
  21. private $level = array();
  22. /**
  23. * Prize information
  24. */
  25. private $lotteryArray = array();
  26. /***
  27. * Random number ending digits
  28. */
  29. private $max = 30000;
  30. /**
  31. * Category true: Default false: Palm Su
  32. */
  33. private $type=true;
  34. /**
  35. * Winning professional account
  36. */
  37. private $tel_zy=false;
  38. /**
  39. * Read configuration
  40. */
  41. public function load_setting () {
  42. $data=getcache('lyqdzp_setting');
  43. define('TIME', time()); //Current time
  44. define('L_START', strtotime($data['l_start'])); / /Registration start time
  45. define('L_END', strtotime($data['l_end'])); //Registration end time, -1 means yesterday 23:59:59
  46. define('L_DAY_START',$data['l_day_start ']); //Start time of each day
  47. define('L_DAY_END', $data['l_day_end']); //End time of each day
  48. define('L_DAY_CISHU', $data['l_day_cishu']); //Get every day Number of prizes
  49. define('L_CISHU', $data['l_cishu']);//Total number of prizes obtained
  50. define('L_MIN', 20);//Minimum LIFB coins
  51. define('L_LFB', $data[ 'l_lfb']);//Reduce Lifb coins
  52. define('L_ZY', $data['l_zy']); //Professional accounts
  53. define('L_QUANSHU', $data['l_quanshu']);//Circle Count, the number of rounds the big wheel starts to roll
  54. define('L_KONGJIANG', intval($data['l_kongjiang'])); //Probability of empty prize
  55. $host='www.****.com'; //Existing network
  56. if($_SERVER['HTTP_HOST']=='221.****0'){
  57. $host='221.*****'; //Test machine
  58. }elseif($_SERVER['HTTP_HOST ']=='125.com'){
  59. $host='125life.com';//Local
  60. }elseif(strpos($_SERVER['HTTP_HOST'],'192.168.')==0){
  61. $ host=$_SERVER['HTTP_HOST'].'/125e.com';//Local
  62. }
  63. define('L_SITE', 'http://'.$host.'/'); //Add / at the end of the domain name
  64. define('L_URL', L_SITE.'index.php');//Access address
  65. //Can only be accessed through flash address
  66. $s=L_SITE.'flash/dzp/i.swf';
  67. if(preg_replace ('/swf.*$/','swf',$_SERVER['HTTP_REFERER'])!=$s&&$this->type){
  68. exit('Dude, you have got enough, please pass the formal Way to win!');
  69. }
  70. $this->load();
  71. }
  72. /**
  73. * Blacklist
  74. */
  75. private $heimingdan = array();
  76. /**
  77. * Read prize configuration
  78. */
  79. public function load() {
  80. $this->setlottery ();
  81. $this->heimingdan = getcache ( 'tel', 'blanklist' );
  82. }
  83. private function setlottery() {
  84. $time=strtotime(date ('Y-m-d',TIME));
  85. $sql = "select u.*";
  86. $sql .= ", (select COUNT(l.lotteryid) from " . DB::table ( $this-> lottery_log ) . " AS l where u.lotteryid=l.lotteryid and l.iswinning='1')as num ";
  87. $sql .= " from " . DB::table ( $this->lottery ) . " AS u ";
  88. $sql .= " where 1 ";
  89. $sql .= " and u.isdisable='0' ";
  90. $sql .= " and u.time_start<='$time' ";
  91. $ sql .= " and u.time_end>='$time' ";
  92. $sql .= " ORDER BY u.listorder ";
  93. $d = DB::fetch_all ( $sql );
  94. $sum = 0;
  95. foreach ( $d as $r ) {
  96. $r ['sum_over'] = intval ( $r ['sum'] ) - intval ( $r ['num'] );
  97. if ($r ['sum'] > ; 0) {
  98. $sum += $r ['sum'];
  99. }
  100. $r['msg']=str_replace('{name}', $r['name'],$r['msg' ]);
  101. $this->lotteryArray [$r ['lotteryid']] = $r;
  102. }
  103. if($sum<100) $sum=1000;
  104. $m = 1;
  105. $probability = 0;
  106. $newsum=0;
  107. //80% empty prize + 20% prize
  108. $this->max = $sum + $sum * (L_KONGJIANG/100) / ((100-L_KONGJIANG)/100);
  109. foreach ( $this->lotteryArray as $r ) {
  110. if ($r ['sum'] > 0) {
  111. $newsum=$r ['sum'];
  112. if($r['probability']> 0){
  113. $probability=0;
  114. $probability=ceil($r ['sum']*100/$r['probability']);
  115. if($probability>0){
  116. $newsum=$probability;
  117. }
  118. }
  119. $this->level [$r ['lotteryid']] ['start'] = $m;
  120. $this->level [$r ['lotteryid']] ['end'] = $m + $newsum;
  121. $m = $m + $r ['sum'];
  122. }
  123. }
  124. }
  125. /**
  126. * Check whether the user has won the prize 0 Thank you for participating, others return prize data
  127. * @param array $user array(uid,tel,ip)
  128. */
  129. public function check($user) {
  130. if (intval ( $user ['uid'] ) < 1) {
  131. //Not logged in
  132. return 0;
  133. }
  134. if(intval(TIME)//You are here early, the event is still going on No start!
  135. return -21;
  136. }
  137. if(intval(TIME)>intval(L_END)){
  138. //The event time has passed, I am very sorry, thank you!
  139. return -22;
  140. }
  141. if ($this->get_user_lfb ( $user ['uid'] ) < L_MIN) {
  142. //$this->credit ( $user ['uid'], -11 , $logid );
  143. return -11; //Your LIF balance is insufficient
  144. }
  145. $data = array(
  146. 'uid' => $user ['uid'],
  147. 'tel' => $user ['tel'],
  148. 'ip' => $user ['ip'],
  149. 'addtime' => time ()
  150. );
  151. $logid = DB::insert ( $this-> lottery_log, $data, true );
  152. //Check the number of prizes the user has won every day
  153. if ($this->get_lottery ( $user ['uid'], strtotime ( date ( 'Y-m-d', TIME ) ), strtotime ( "+1day", strtotime ( date ( 'Y-m-d', TIME ) ) ), true ) >= L_DAY_CISHU) {
  154. $this->credit ( $user ['uid'], -12 , $logid );
  155. //Unfortunately, you didn’t draw anything this time!
  156. return -12;
  157. }
  158. //Check the number of prizes won by the user
  159. if ($this->get_lottery ( $user ['uid'], 0, 0, true ) > L_CISHU&&L_CISHU>0) {
  160. $ this->credit ( $user ['uid'], -12, $logid );
  161. //Unfortunately, you didn’t draw anything this time!
  162. return -15;
  163. }
  164. //Detect blacklist, set probability
  165. if ($this->checkhei ( $user ['tel'] ) > 0) {
  166. $this->credit ( $user [ 'uid'], -13, $logid );
  167. //Unfortunately, you didn't draw anything this time!
  168. return -13;
  169. }
  170. $lotteryid = $this->choujiang();
  171. $this->credit ( $user ['uid'], $lotteryid<1?$lotteryid:0, $logid, $ lotteryid ,$this->hao);
  172. if ($lotteryid > 0) {
  173. return $this->lotteryArray [$lotteryid];
  174. } else {
  175. return -4;
  176. }
  177. //-1 prize Already distributed
  178. //-2 Prizes for this period have been played
  179. //-3 Prizes not distributed
  180. //-4 There is a problem with the prizes
  181. //-11 Your Laifu coin balance is insufficient
  182. //-12 Unfortunately, you Nothing was drawn this time!
  183. //-In the blacklist of 13, unfortunately, you didn’t draw anything this time!
  184. //-14 Winning professional accounts are prohibited from talking about high prizes.
  185. //-15 has exceeded the total number of prizes in yizhong.
  186. //-16 The mobile number sent by Zhangsu is wrong.
  187. //-21 You are early and the event has not started yet!
  188. //-22 has passed the event time, I am very sorry, thank you!
  189. //-23Sorry, it is not the event time now, the event time is every day
  190. }
  191. /**
  192. * Show
  193. * @param unknown_type $data
  194. */
  195. public function show($data){
  196. $ok=0;//3 Winning
  197. $zz= 0;//Roulette degree
  198. $result='';//Prize name
  199. $lotteryid=0;//Prize ID
  200. if(is_array($data)){
  201. $ok=3;
  202. $result=$data[ 'msg'];
  203. $zz=$data['angle']+L_QUANSHU*360;
  204. $lotteryid=$data['lotteryid'];
  205. }else{
  206. $m='';
  207. switch($data) {
  208. case 0:{
  209. $m= 'Please log in first! ';
  210. }break;
  211. case -11:{
  212. $m= 'Your Raffles balance is insufficient! ';
  213. }break;
  214. case -12:{
  215. $m= 'Unfortunately, you didn't draw anything this time! ';
  216. }break;
  217. case -21:{
  218. $m= 'You are early and the event has not started yet! ';
  219. }break;
  220. case -22:{
  221. $m= 'The event time has passed, I am very sorry, thank you! ';
  222. }break;
  223. case -23:{
  224. $m= 'Sorry, it is not the event time now, the event time is every day'.L_DAY_START.':00-'.L_DAY_END.':00';
  225. }break;
  226. default:{
  227. $m= 'Unfortunately, you didn't draw anything this time!';
  228. }break;
  229. }
  230. $result=$m;
  231. $ok=3;//3中奖
  232. $zz=360+L_QUANSHU*360;//再接再厉
  233. }
  234. header("Expires:Mon,26Jul199705:00:00GMT");
  235. header("Cache-Control:no-cache,must-revalidate");
  236. header("Pragma:no-cache");
  237. header("Content-type:text/html;charset=utf-8");
  238. if(!$this->type){
  239. echo json_encode(array('return'=>$lotteryid,
  240. 'msg'=>$result));
  241. }else{
  242. echo ''."n";
  243. echo ''."n";
  244. echo ''."n";
  245. echo ''."n";
  246. }
  247. exit;
  248. }
  249. /**
  250. * Lucky draw number
  251. */
  252. private $hao=0;
  253. /**
  254. *The draw begins
  255. */
  256. private function choujiang() {
  257. $j = 0;
  258. $lotteryid = 0;
  259. $j = mt_rand ( 1, $this->max );
  260. $this->hao=$j;
  261. foreach ( $this->level as $k => $r ) {
  262. if ($r ['start'] <= $j && $r ['end'] > $j) {
  263. $lotteryid = $k;
  264. break;
  265. }
  266. }
  267. $d = array(
  268. 1 => array(
  269. 'time' => strtotime ( date ( 'Y-m-d 00:00:00', TIME ) ),
  270. 'time_end' => strtotime ( date ( 'Y-m-d 07:59:59', TIME ) )
  271. )
  272. );
  273. for ($i=2;$i<=17;$i++){
  274. $d[$i]=array(
  275. 'time' => strtotime ( date ( 'Y-m-d '.($i+6).':00:00', TIME ) ),
  276. 'time_end' => strtotime ( date ( 'Y-m-d '.($i+6).':59:59', TIME ) )
  277. );
  278. }
  279. if($lotteryid>0){
  280. $l=$this->lotteryArray [$lotteryid];
  281. $sql = "select count(*) m from " . DB::table ( $this->lottery_log );
  282. $sql .= " where lotteryid='$lotteryid' ";
  283. $s= intval ( DB::result_first ( $sql ) );
  284. $sum_over=$l['sum']-$s;
  285. if($sum_over>0){
  286. //本时段 奖品数
  287. $sum=0;
  288. foreach ($d as $k=>$r){
  289. if(TIME>=$r['time']&&TIME<=$r['time_end']){
  290. if($l['time_'.$k]>0){//个数限制
  291. $sum=$this->get_lottery_id(0,$r['time'],$r['time_end']+1,$lotteryid);
  292. if($sum>=$l['time_'.$k]){
  293. //本时段奖品已玩
  294. $lotteryid=-2;
  295. break;
  296. }
  297. }elseif ($l['time_'.$k]==0){
  298. //不发奖品
  299. $lotteryid=-3;
  300. break;
  301. }else{
  302. //-1 不限制
  303. }
  304. }
  305. }
  306. if($this->tel_zy && $lotteryid>0&&$lotteryid<4){
  307. $lotteryid=-14;
  308. }
  309. }else{
  310. //奖品已发完
  311. $lotteryid=-1;
  312. }
  313. }else{
  314. //奖品有问题
  315. $lotteryid=-4;
  316. }
  317. return $lotteryid;
  318. }
  319. /**
  320. * Detect blacklist and ordinary users, probability
  321. * @param string $tel
  322. * @return greater than 1 means that drawing is prohibited this time
  323. */
  324. private function checkhei($tel) {
  325. $n = mt_rand ( 1, 100 );
  326. if (in_array ( $tel, $this->heimingdan ['tel'] ) && L_ZY > 0) {
  327. $this->tel_zy=true;
  328. if ($n > L_ZY) {
  329. return 1;
  330. }
  331. }else{
  332. //号码检测
  333. if (!self::check_tel($tel)) {
  334. return 1;
  335. }
  336. }
  337. return 0;
  338. }
  339. /**
  340. * Total number of prizes
  341. * @param intval $uid
  342. * @param intval $start
  343. * @param intval $end
  344. * @param bool $bool true Number of prizes obtained false No statistics
  345. * @return number
  346. */
  347. private function get_lottery($uid = 0, $start = 0, $end = 0, $bool = false) {
  348. $sql = "select count(*) m from " . DB::table ( $this->lottery_log );
  349. $sql .= " where 1 ";
  350. if (intval ( $start ) > 0) {
  351. $sql .= " and addtime>='$start' ";
  352. }
  353. if (intval ( $end ) > 0) {
  354. $sql .= " and addtime<'$end' ";
  355. }
  356. if ($bool) {
  357. $sql .= " and lotteryid>0 ";
  358. }
  359. if (intval ( $uid ) > 0) {
  360. $sql .= " and uid='$uid' ";
  361. }
  362. return intval ( DB::result_first ( $sql ) );
  363. }
  364. /**
  365. * Total number of prizes
  366. * @param intval $uid
  367. * @param intval $start
  368. * @param intval $end
  369. * @param intval $bool prize id
  370. * @return number
  371. */
  372. private function get_lottery_id($uid = 0, $start = 0, $end = 0, $lotteryid = 0) {
  373. $sql = "select count(*) m from " . DB::table ( $this->lottery_log );
  374. $sql .= " where 1 ";
  375. if (intval ( $start ) > 0) {
  376. $sql .= " and addtime>='$start' ";
  377. }
  378. if (intval ( $end ) > 0) {
  379. $sql .= " and addtime<'$end' ";
  380. }
  381. if ($lotteryid>=0) {
  382. $sql .= " and lotteryid='$lotteryid' ";
  383. }
  384. if (intval ( $uid ) > 0) {
  385. $sql .= " and uid='$uid' ";
  386. }
  387. return intval ( DB::result_first ( $sql ) );
  388. }
  389. /**
  390. * Get the user’s Rafflecoins
  391. * @param intval $uid
  392. * @return number
  393. */
  394. public function get_user_lfb($uid) {
  395. $sql = "select extcredits2 from sz_common_member_count ";
  396. $sql .= " where 1 ";
  397. $sql .= " and uid='$uid' ";
  398. return intval ( DB::result_first ( $sql ) );
  399. }
  400. /**
  401. * Record logs and update user LIF coins
  402. * @param intval $uid
  403. * @param intval $r return value
  404. * @param intval $logid
  405. * @param intval $lotteryid
  406. */
  407. private function credit($uid, $result, $logid = 0, $lotteryid = 0,$hao=0) {
  408. $data = array();
  409. if ($lotteryid > 0) {
  410. $data ['lotteryid'] = $lotteryid;
  411. $data ['iswinning'] = 1;
  412. }
  413. $data ['result'] = $result;
  414. $data ['hao'] = $hao;
  415. $data ['HTTP_REFERER'] = $_SERVER['HTTP_REFERER'];
  416. DB::Update ( $this->lottery_log, $data, " logid='$logid' " );
  417. if (L_LFB > 0) {
  418. $sql = 'UPDATE sz_common_member_count SET extcredits2=extcredits2-' . L_LFB . ' WHERE uid=' . $uid;
  419. DB::query ( $sql );
  420. $this->credit_log ( $uid, 'reduce', L_LFB, 1, '积分清零活动' );
  421. }
  422. }
  423. /**
  424. *Mobile number check
  425. */
  426. private function check_tel($tel){
  427. $different_network=table_sub::get_different_network();
  428. if (!preg_match("/$different_network/",$tel)) {
  429. return false;
  430. }
  431. return true;
  432. }
  433. /**
  434. * Stored procedure link
  435. */
  436. private $StoreProcs;
  437. /**
  438. * Laifu Coin log stored procedure
  439. * @param intval $uid
  440. * @param string $op add to increase reduce consumption
  441. * @param intval $creditvalue Laifu Coin
  442. * @param intval $credittype 1 Laifu Coin 2 Laifu Coin Click
  443. * @param string $oreason description
  444. */
  445. private function credit_log($uid, $op, $creditvalue, $credittype, $oreason) {
  446. if (! $this->StoreProcs) {
  447. $this->StoreProcs = new StoreProcs ();
  448. }
  449. //调用存储过程
  450. $this->StoreProcs->query ( "call app_credit_log($uid,'$op','$credittype',1,$creditvalue,'$oreason')" );
  451. }
  452. }
复制代码

三,调用:

  1. $dzp=new lib_dzp_lottery();
  2. $u=array('uid'=>$_G ['uid'],'tel'=>$_G ['tel'],'ip'=> $_G ['clientip']);
  3. $dzp->load_setting();
  4. $dzp->show($dzp->check($u));
  5. ?>
复制代码


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template