Files
yusheng-php/application/common/library/LotteryGiftLua.php
2025-12-21 16:04:34 +08:00

135 lines
5.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\common\library;
class LotteryGiftLua
{
// 获取Lua脚本
public static function getLotteryLuaScript()
{
return <<<LUA
-- 接收参数send_uid, recv_uid, gift_gold, small_trigger_times, big_threshold, small_round, big_round, big_total_gold
local send_uid = ARGV[1]
local recv_uid = ARGV[2]
local gift_gold = tonumber(ARGV[3])
local small_trigger_times = tonumber(ARGV[4])
local big_threshold = tonumber(ARGV[5])
local small_round = tonumber(ARGV[6]) -- 小奖池当前轮次
local big_round = tonumber(ARGV[7]) -- 大奖池当前轮次
local big_total_gold = tonumber(ARGV[8]) -- 大奖池当前轮次金额
-- 核心约束:强制小轮次 ≥ 大轮次
if small_round < big_round then
small_round = big_round
end
-- 1. 基础金额拆分
local recv_gold = gift_gold * 0.5
local small_pool_add = gift_gold * 0.5
-- 2. Redis键定义
local small_round_key = "lottery:small_pool:round"
local small_total_times_key = "lottery:small_pool:total_times"
local small_total_gold_key = "lottery:small_pool:total_gold"
local big_round_key = "lottery:big_pool:round"
local big_total_gold_key = "lottery:big_pool:total_gold"
-- 初始化轮次确保Redis与入参一致
redis.call('set', small_round_key, small_round)
redis.call('set', big_round_key, big_round)
-- 初始化大奖池金额
if big_total_gold == 0 or big_total_gold == nil then
big_total_gold = tonumber(redis.call('get', big_total_gold_key) or 0)
end
-- 3. 小奖池累计更新
local small_total_times = tonumber(redis.call('incr', small_total_times_key))
local small_total_gold = tonumber(redis.call('get', small_total_gold_key) or 0)
small_total_gold = math.floor((small_total_gold + small_pool_add) * 100) / 100
redis.call('set', small_total_gold_key, small_total_gold)
-- 4. 返回结果初始化(区分大小轮次)
local result = {
send_uid = send_uid,
recv_uid = recv_uid,
gift_gold = gift_gold,
recv_gold = recv_gold,
small_pool_add = small_pool_add,
small_total_times = small_total_times,
small_total_gold = small_total_gold,
is_small_prize = 0,
small_prize_amount = 0,
small_remain_amount = 0,
is_big_prize = 0,
big_prize_amount = 0,
big_release_amount = 0,
small_round = small_round, -- 小奖池轮次
big_round = big_round, -- 大奖池轮次
big_total_gold = big_total_gold,
-- 新增:记录要划入下一轮的小奖开奖金额
small_prize_to_big_next_round = 0
}
-- 5. 小奖池开奖判断(小轮次+1
if small_total_times >= small_trigger_times then
result.is_small_prize = 1
-- 小奖随机比例
local small_ratio = math.random(2, 99)
result.small_prize_amount = math.floor(small_total_gold * small_ratio / 100 * 100) / 100
-- 小奖剩余金额(划入大奖池当前轮次)
result.small_remain_amount = math.floor((small_total_gold - result.small_prize_amount) * 100) / 100
-- 重置小奖池,小轮次+1
redis.call('set', small_total_times_key, 0)
redis.call('set', small_total_gold_key, 0)
small_round = small_round + 1
redis.call('set', small_round_key, small_round)
result.small_round = small_round
-- 6. 小奖剩余划入大奖池当前轮次
big_total_gold = math.floor((big_total_gold + result.small_remain_amount) * 100) / 100
redis.call('set', big_total_gold_key, big_total_gold)
result.big_total_gold = big_total_gold
-- 7. 大奖池开奖判断(大轮次+1
if big_total_gold >= big_threshold then
result.is_big_prize = 1
-- 大奖比例权重
local weight_sum = 20 + 50 + 30
local random_weight = math.random(1, weight_sum)
local big_ratio = random_weight <= 20 and 60 or (random_weight <= 70 and 70 or 80)
-- 大奖金额
result.big_prize_amount = math.floor(big_total_gold * big_ratio / 100 * 100) / 100
result.big_release_amount = math.floor((big_total_gold - result.big_prize_amount) * 100) / 100
-- 原有逻辑:重置大奖池,大轮次+1
redis.call('set', big_total_gold_key, 0)
big_round = big_round + 1
redis.call('set', big_round_key, big_round)
-- 强制保证小轮次≥大轮次
if small_round < big_round then
small_round = big_round
redis.call('set', small_round_key, small_round)
result.small_round = small_round
end
result.big_round = big_round
result.big_total_gold = 0
-- ===================== 新增核心逻辑 =====================
-- 小奖开奖金额累加到大奖池下一轮次新的big_round
result.small_prize_to_big_next_round = result.small_prize_amount
-- 原子性更新大奖池下一轮次金额
local new_big_total_gold = math.floor(result.small_prize_amount * 100) / 100
redis.call('set', big_total_gold_key, new_big_total_gold)
result.big_total_gold = new_big_total_gold
-- ======================================================
end
end
-- 返回结果
return cjson.encode(result)
LUA;
}
}