public function index() { $prizeDb = M('prize'); $prize_arr = $prizeDb->where('id>0')->select(); $this->assign('prize_arr', $prize_arr); if (session('?username') == false || session('?password') == false) { //判断session是否存在用户名和密码 $this->assign('login', 1); //用户名和密码中有一个不存在,显示登录页面 $this->display('scratch'); //print_r($prize_arr); } else { //如果session中存储了用户名和密码,显示抽奖页面 $scratchDb = M('scratch'); $result = $scratchDb->where("username='******'", session('username'))->find(); if (!$result) { $scratch = array('username' => session('username'), 'times' => 2, 'time' => time()); $scratchDb->data($scratch)->add(); } else { $scratch = $result; } $prize = getPrize($prize_arr); $refreshTime = mktime(0, 0, 0, date("m", $scratch['time']), date("d", $scratch['time']) + 1, date("Y", $scratch['time'])); $scratch['time'] = time(); if ($scratch['time'] > $refreshTime) { $scratch['times'] = 2; } if ($scratch['times'] > 0) { $scratch['times'] = $scratch['times'] - 1; $scratchDb->save($scratch); $this->assign('scratch', $scratch); $this->assign('prize', $prize); $this->display('scratch'); $id = $prize['id']; if ($id < 7) { $prize_arr[$id - 1]['v'] = $prize_arr[$id - 1]['v'] - 1; $prizeDb->save($prize_arr[$id - 1]); S($prize['sncode'], $prize['num']); } } else { $this->display('tomorrow'); } } }
/** * It checks if the world is full * * @return bool True, if it is full; false, in the opposite case */ function isFull() { if (count(getStatic()) + count(getDynamic()) + count(getPrize()) < getSizeWorld()['row'] * getSizeWorld()['col']) { return false; } else { return true; } }
//The prize's order on the turn-plate $prize_arr = array($prize[0], $prize[1], $prize[2], $prize[1], $prize[2], $prize[3], $prize[2], $prize[1]); /*According to the prize's value, calculate prize's probability Create random number and get the corresponding prize*/ function getPrize($prize_arr) { $prize_sum = 0; foreach ($prize_arr as $v) { $prize_sum += $v['value']; } foreach ($prize_arr as $k => $v) { $randNum = mt_rand(1, $prize_sum); //random number if ($randNum <= $v['value']) { return array('id' => $k, 'state' => $v); } else { $prize_sum -= $v['value']; } } } /*Send the the data to the front-end $data['prize']: all of prize's name $data['id']: the prize of winning*/ $data = array(); $prize_result = getPrize($prize_arr); $data['id'] = $prize_result['id'] + 1; foreach ($prize_arr as $v) { $data['prize'][] = urlencode($v['prize']); } echo urldecode(json_encode($data)); mysqli_close($conn);