feat(AI): 新增小班长随机赠送金币福利功能,支持 [ACTION:GIVE_GOLD] 拦截与全服广播
This commit is contained in:
@@ -13,14 +13,17 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Enums\CurrencySource;
|
||||
use App\Events\MessageSent;
|
||||
use App\Jobs\SaveMessageJob;
|
||||
use App\Models\Sysparam;
|
||||
use App\Services\AiChatService;
|
||||
use App\Services\ChatStateService;
|
||||
use App\Services\UserCurrencyService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Redis;
|
||||
|
||||
class ChatBotController extends Controller
|
||||
{
|
||||
@@ -30,6 +33,7 @@ class ChatBotController extends Controller
|
||||
public function __construct(
|
||||
private readonly AiChatService $aiChat,
|
||||
private readonly ChatStateService $chatState,
|
||||
private readonly UserCurrencyService $currencyService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -81,13 +85,57 @@ class ChatBotController extends Controller
|
||||
|
||||
$result = $this->aiChat->chat($user->id, $message, $roomId);
|
||||
|
||||
$reply = $result['reply'];
|
||||
|
||||
// 检查 AI 是否决定给用户发金币
|
||||
if (str_contains($reply, '[ACTION:GIVE_GOLD]')) {
|
||||
$reply = str_replace('[ACTION:GIVE_GOLD]', '', $reply);
|
||||
$reply = trim($reply);
|
||||
|
||||
$redisKey = 'ai_chat:give_gold:'.date('Ymd').':'.$user->id;
|
||||
// 原子操作,防止并发多次领取
|
||||
if (Redis::setnx($redisKey, 1)) {
|
||||
Redis::expire($redisKey, 86400); // 缓存 24 小时
|
||||
|
||||
// 给用户发放随机 100~5000 金币
|
||||
$goldAmount = rand(100, 5000);
|
||||
$this->currencyService->change(
|
||||
$user,
|
||||
'gold',
|
||||
$goldAmount,
|
||||
CurrencySource::AI_GIFT,
|
||||
'AI小班长发善心赠送的金币福利',
|
||||
$roomId
|
||||
);
|
||||
|
||||
// 发送全场大广播
|
||||
$sysMsg = [
|
||||
'id' => $this->chatState->nextMessageId($roomId),
|
||||
'room_id' => $roomId,
|
||||
'from_user' => '系统传音',
|
||||
'to_user' => '大家',
|
||||
'content' => "🤖 听闻小萌新哭穷,AI小班长看【{$user->username}】骨骼惊奇,大方地赏赐了 {$goldAmount} 枚金币福利!",
|
||||
'is_secret' => false,
|
||||
'font_color' => '#d97706', // 橙色醒目
|
||||
'action' => '大声宣告',
|
||||
'sent_at' => now()->toDateTimeString(),
|
||||
];
|
||||
$this->chatState->pushMessage($roomId, $sysMsg);
|
||||
broadcast(new MessageSent($roomId, $sysMsg));
|
||||
SaveMessageJob::dispatch($sysMsg);
|
||||
} else {
|
||||
// 如果已经领过了,修改回复提醒
|
||||
$reply = $reply."\n\n(系统提示:你今天已经领过金币福利啦,每天只能领一次哦!)";
|
||||
}
|
||||
}
|
||||
|
||||
// 广播 AI 回复消息
|
||||
$botMsg = [
|
||||
'id' => $this->chatState->nextMessageId($roomId),
|
||||
'room_id' => $roomId,
|
||||
'from_user' => 'AI小班长',
|
||||
'to_user' => $user->username,
|
||||
'content' => $result['reply'],
|
||||
'content' => $reply,
|
||||
'is_secret' => false,
|
||||
'font_color' => '#16a34a',
|
||||
'action' => '',
|
||||
|
||||
Reference in New Issue
Block a user