这是一个简单的经典头奖系统。 我的偏好是在数据库中随机选择所选用户是否具有空值。 如果获胜者具有任何值但不是null(user_id),则不要随意选择并赢得该用户。 如果winner_id具有空值,请选择随机。但如果您有预定义的winner_id(非空(例如:winner_id = 152)),则获得152个用户。
ClassicController.php:
public function getWinners()
{
$us = $this->usersGame();
$lastBet = Bet::where('game_id', $this->game->id)->orderBy('to', 'desc')->first();
$winTicket = round($this->game->rand_number * $lastBet->to);
if($winTicket == 0) {
$winTicket = 1;
}
$winningBet = Bet::where('game_id', $this->game->id)->where('from', '<=', $winTicket)->where('to', '>=', $winTicket)->first();
$userWin = User::find($winningBet->user_id);
$this->redis->del('last.ticket.' . $this->game->id);
$this->game->winner_id = $winningBet->user_id;
$this->game->status = 3;
$this->game->finished_at = Carbon::now();
$this->game->won_items = json_encode($this->sendItems($this->game->bets, $userWin));
$this->game->chance = $this->userChance($userWin, $this->game);
$this->game->save();
foreach ($us as $user) {
$user->itemsCount = $this->userItem($user, $this->game);
$user->itemPrice = $this->userPrice($user, $this->game);
$user->percent = $this->userChance($user, $this->game);
}
$winner = [
'randNumber' => $this->game->rand_number,
'avatar' => $userWin->avatar,
'GameSecret' => $this->game->secret,
'name' => $userWin->username,
'TicketWin' => $winTicket
];
$returnValue = [
'differenceDelay' => 0,
'endtimestamp' => Carbon::now()->getTimestamp(),
'prize' => $this->game->price,
'userList' => [
'users' => $us
],
'winner' => $winner
];
return response()->json($returnValue);
}
Datebase SQL
CREATE TABLE `games` (
`id` int(10) UNSIGNED NOT NULL,
`winner_id` int(10) UNSIGNED DEFAULT NULL,
`status` int(11) NOT NULL DEFAULT '0',
`items` int(11) NOT NULL DEFAULT '0',
`price` double(8,2) NOT NULL DEFAULT '0.00',
`started_at` timestamp NULL DEFAULT NULL,
`finished_at` timestamp NULL DEFAULT NULL,
`won_items` text COLLATE utf8_unicode_ci NOT NULL,
`status_prize` int(11) NOT NULL DEFAULT '0',
`chance` double(8,2) NOT NULL DEFAULT '0.00',
`secret` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`rand_number` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;