优化ai小班长

This commit is contained in:
2026-04-12 22:25:18 +08:00
parent f8d5a3b250
commit ef407a8c6e
7 changed files with 608 additions and 91 deletions
+19 -1
View File
@@ -20,11 +20,15 @@ use App\Jobs\SaveMessageJob;
use App\Models\Autoact;
use App\Models\Sysparam;
use App\Models\User;
use App\Services\AiFinanceService;
use App\Services\ChatStateService;
use App\Services\UserCurrencyService;
use App\Services\VipService;
use Illuminate\Console\Command;
/**
* 定时模拟 AI小班长心跳,并同步维护其常规存款理财行为。
*/
class AiHeartbeatCommand extends Command
{
/**
@@ -37,14 +41,21 @@ class AiHeartbeatCommand extends Command
*/
protected $description = '模拟 AI 小班长客户端心跳,触发经验金币与随机事件';
/**
* 注入聊天室状态、VIP、积分与 AI 资金调度服务。
*/
public function __construct(
private readonly ChatStateService $chatState,
private readonly VipService $vipService,
private readonly UserCurrencyService $currencyService,
private readonly AiFinanceService $aiFinance,
) {
parent::__construct();
}
/**
* 执行 AI小班长单次心跳,并处理奖励、随机事件与资金调度。
*/
public function handle(): int
{
// 1. 检查总开关
@@ -58,6 +69,9 @@ class AiHeartbeatCommand extends Command
return Command::SUCCESS;
}
// 心跳开始前,若手上金币已高于 100 万,则先把超出的部分转入银行。
$this->aiFinance->bankExcessGold($user);
// 3. 常规心跳经验与金币发放
// (模拟前端每30-60秒发一次心跳的过程,此处每分钟跑一次,发放单人心跳奖励)
$expGain = $this->parseRewardValue(Sysparam::getValue('exp_per_heartbeat', '1'));
@@ -133,7 +147,8 @@ class AiHeartbeatCommand extends Command
$fishingChance = (int) Sysparam::getValue('chatbot_fishing_chance', '100'); // 默认 5% 概率
if ($fishingEnabled && $fishingChance > 0 && rand(1, 100) <= $fishingChance && \App\Models\GameConfig::isEnabled('fishing')) {
$cost = (int) (\App\Models\GameConfig::param('fishing', 'fishing_cost') ?? Sysparam::getValue('fishing_cost', '5'));
if ($user->jjb >= $cost) {
// 常规小游戏只使用当前手上金币,不再自动从银行补到 100 万。
if ($this->aiFinance->prepareSpend($user, $cost)) {
// 先扣除费用
$this->currencyService->change(
$user, 'gold', -$cost,
@@ -155,6 +170,9 @@ class AiHeartbeatCommand extends Command
}
}
// 心跳结束后,若新增金币让手上金额超过 100 万,则把超出的部分重新转回银行。
$this->aiFinance->bankExcessGold($user);
return Command::SUCCESS;
}