功能:自动存点增加金币奖励 + VIP 加成
- heartbeat 增加金币奖励逻辑,读取 jjb_per_heartbeat 配置
- 支持固定值('5')和范围('1-10')两种奖励配置格式
- VIP 会员自动应用经验和金币加成倍率
- 前端手动存点显示金币余额和本次获得的奖励增量
- 新增迁移文件插入 jjb_per_heartbeat 配置项(默认 1-3)
- 更新 exp_per_heartbeat 描述说明支持范围格式
This commit is contained in:
@@ -149,17 +149,29 @@ class ChatController extends Controller
|
||||
return response()->json(['status' => 'error'], 401);
|
||||
}
|
||||
|
||||
// 1. 心跳经验:通过 Redis 限制最小间隔(默认30秒),防止频繁点击刷经验
|
||||
$expCooldownKey = "heartbeat_exp:{$user->id}";
|
||||
$canGainExp = ! Redis::exists($expCooldownKey);
|
||||
// 1. 心跳奖励:通过 Redis 限制最小间隔(默认30秒),防止频繁点击
|
||||
$cooldownKey = "heartbeat_exp:{$user->id}";
|
||||
$canGainReward = ! Redis::exists($cooldownKey);
|
||||
$actualExpGain = 0;
|
||||
$actualJjbGain = 0;
|
||||
|
||||
if ($canGainExp) {
|
||||
$expGain = (int) Sysparam::getValue('exp_per_heartbeat', '1');
|
||||
if ($canGainReward) {
|
||||
// 经验奖励(支持固定值 "1" 或范围 "1-10")
|
||||
$expGain = $this->parseRewardValue(Sysparam::getValue('exp_per_heartbeat', '1'));
|
||||
$expMultiplier = $this->vipService->getExpMultiplier($user);
|
||||
$user->exp_num += (int) round($expGain * $expMultiplier);
|
||||
$actualExpGain = (int) round($expGain * $expMultiplier);
|
||||
$user->exp_num += $actualExpGain;
|
||||
|
||||
// 设置冷却(30秒内不再给经验)
|
||||
Redis::setex($expCooldownKey, 30, 1);
|
||||
// 金币奖励(支持固定值 "1" 或范围 "1-5")
|
||||
$jjbGain = $this->parseRewardValue(Sysparam::getValue('jjb_per_heartbeat', '0'));
|
||||
if ($jjbGain > 0) {
|
||||
$jjbMultiplier = $this->vipService->getJjbMultiplier($user);
|
||||
$actualJjbGain = (int) round($jjbGain * $jjbMultiplier);
|
||||
$user->jjb = ($user->jjb ?? 0) + $actualJjbGain;
|
||||
}
|
||||
|
||||
// 设置冷却(30秒内不再给奖励)
|
||||
Redis::setex($cooldownKey, 30, 1);
|
||||
}
|
||||
|
||||
// 2. 使用 sysparam 表中可配置的等级-经验阈值计算等级
|
||||
@@ -263,6 +275,9 @@ class ChatController extends Controller
|
||||
'status' => 'success',
|
||||
'data' => [
|
||||
'exp_num' => $user->exp_num,
|
||||
'jjb' => $user->jjb ?? 0,
|
||||
'exp_gain' => $actualExpGain,
|
||||
'jjb_gain' => $actualJjbGain,
|
||||
'user_level' => $user->user_level,
|
||||
'leveled_up' => $leveledUp,
|
||||
'is_max_level' => $user->user_level >= $superLevel,
|
||||
@@ -408,4 +423,31 @@ class ChatController extends Controller
|
||||
'announcement' => $room->announcement,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析奖励数值配置(支持固定值或范围格式)
|
||||
*
|
||||
* 支持格式:
|
||||
* "5" → 固定返回 5
|
||||
* "1-10" → 随机返回 1~10 之间的整数
|
||||
* "0" → 返回 0(关闭该奖励)
|
||||
*
|
||||
* @param string $value 配置值
|
||||
* @return int 解析后的奖励数值
|
||||
*/
|
||||
private function parseRewardValue(string $value): int
|
||||
{
|
||||
$value = trim($value);
|
||||
|
||||
// 支持范围格式 "min-max"
|
||||
if (str_contains($value, '-')) {
|
||||
$parts = explode('-', $value, 2);
|
||||
$min = max(0, (int) $parts[0]);
|
||||
$max = max($min, (int) $parts[1]);
|
||||
|
||||
return rand($min, $max);
|
||||
}
|
||||
|
||||
return max(0, (int) $value);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user