192 lines
6.7 KiB
PHP
192 lines
6.7 KiB
PHP
<?php
|
|
|
|
/**
|
|
* 文件功能:游戏配置后台管理控制器
|
|
*
|
|
* 管理员可在此页面统一管理所有娱乐游戏的开关状态和核心参数。
|
|
* 每个游戏的参数说明通过前端渲染,后台只做通用 JSON 存储。
|
|
*
|
|
* @author ChatRoom Laravel
|
|
*
|
|
* @version 1.0.0
|
|
*/
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\UpdateGameConfigParamsRequest;
|
|
use App\Models\GameConfig;
|
|
use App\Models\LotteryIssue;
|
|
use App\Models\Room;
|
|
use App\Services\GameRoomScopeService;
|
|
use App\Services\LotteryService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\View\View;
|
|
|
|
/**
|
|
* 类功能:统一处理后台游戏开关、参数保存与手动操作入口。
|
|
*/
|
|
class GameConfigController extends Controller
|
|
{
|
|
/**
|
|
* 游戏管理总览页面。
|
|
*/
|
|
public function index(): View
|
|
{
|
|
$games = GameConfig::orderBy('id')->get();
|
|
$availableRooms = Room::query()->orderBy('id')->get();
|
|
|
|
return view('admin.game-configs.index', compact('games', 'availableRooms'));
|
|
}
|
|
|
|
/**
|
|
* 切换游戏开启/关闭状态。
|
|
*/
|
|
public function toggle(GameConfig $gameConfig): JsonResponse
|
|
{
|
|
$gameConfig->update(['enabled' => ! $gameConfig->enabled]);
|
|
$gameConfig->clearCache();
|
|
|
|
return response()->json([
|
|
'ok' => true,
|
|
'enabled' => $gameConfig->enabled,
|
|
'message' => $gameConfig->enabled
|
|
? "「{$gameConfig->name}」已开启"
|
|
: "「{$gameConfig->name}」已关闭",
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 保存游戏核心参数。
|
|
*
|
|
* 接收前端提交的 params JSON 对象并合并至现有配置。
|
|
*/
|
|
public function updateParams(UpdateGameConfigParamsRequest $request, GameConfig $gameConfig, GameRoomScopeService $roomScopeService): RedirectResponse
|
|
{
|
|
// 合并参数,保留已有键,只更新传入的键
|
|
$current = $gameConfig->params ?? [];
|
|
// 这里不能只读取 validated('params')。
|
|
// 当前请求类只对公共房间字段做了显式规则约束,像 fishing_cooldown 这类普通游戏参数
|
|
// 在 validated 数据中会被裁掉,导致后台提示成功但实际没有写入数据库。
|
|
$validatedParams = (array) $request->input('params', []);
|
|
$updated = array_merge($current, $validatedParams);
|
|
|
|
$scopeConfig = $roomScopeService->getScopeConfigForParams($validatedParams);
|
|
$updated['room_scope_mode'] = $scopeConfig['room_scope_mode'];
|
|
$updated['room_ids'] = $scopeConfig['room_ids'];
|
|
|
|
if ($gameConfig->game_key === 'mystery_box') {
|
|
$legacyMap = [
|
|
'min_reward' => 'normal_reward_min',
|
|
'max_reward' => 'normal_reward_max',
|
|
'rare_min_reward' => 'rare_reward_min',
|
|
'rare_max_reward' => 'rare_reward_max',
|
|
];
|
|
|
|
foreach ($legacyMap as $legacyKey => $newKey) {
|
|
if (! array_key_exists($newKey, $updated) && array_key_exists($legacyKey, $updated)) {
|
|
$updated[$newKey] = $updated[$legacyKey];
|
|
}
|
|
|
|
unset($updated[$legacyKey]);
|
|
}
|
|
}
|
|
|
|
$gameConfig->update(['params' => $updated]);
|
|
$gameConfig->clearCache();
|
|
|
|
return back()->with('success', "「{$gameConfig->name}」参数已保存!");
|
|
}
|
|
|
|
/**
|
|
* 管理员手动投放神秘箱子。
|
|
*
|
|
* 立即分发 DropMysteryBoxJob 到队列,由 Horizon 执行箱子投放和公屏广播。
|
|
*/
|
|
public function dropMysteryBox(Request $request): JsonResponse
|
|
{
|
|
if (! \App\Models\GameConfig::isEnabled('mystery_box')) {
|
|
return response()->json(['ok' => false, 'message' => '神秘箱子功能未开放,请先开启。']);
|
|
}
|
|
|
|
$boxType = $request->input('box_type', 'normal');
|
|
|
|
if (! in_array($boxType, ['normal', 'rare', 'trap'], true)) {
|
|
return response()->json(['ok' => false, 'message' => '无效的箱子类型。']);
|
|
}
|
|
|
|
// 检查是否有正在开放的箱子(避免同时多个)
|
|
$targetRoomId = app(GameRoomScopeService::class)->getPrimaryRoomIdForGame('mystery_box');
|
|
|
|
if (\App\Models\MysteryBox::currentOpenBox($targetRoomId)) {
|
|
return response()->json(['ok' => false, 'message' => '当前已有一个神秘箱子正在等待领取,请等它结束后再投放。']);
|
|
}
|
|
|
|
\App\Jobs\DropMysteryBoxJob::dispatch($boxType, $targetRoomId, null, (int) auth()->id());
|
|
|
|
$typeNames = ['normal' => '普通箱', 'rare' => '稀有箱', 'trap' => '黑化箱'];
|
|
|
|
return response()->json([
|
|
'ok' => true,
|
|
'message' => "✅ 已投放「{$typeNames[$boxType]}」到 #{$targetRoomId} 房间,暗号将实时发送到公屏!",
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 手动开启新一期双色球彩票。
|
|
*
|
|
* 仅在当前无进行中期次时生效,防止重复开期。
|
|
*/
|
|
public function openLotteryIssue(GameRoomScopeService $roomScopeService): JsonResponse
|
|
{
|
|
if (! GameConfig::isEnabled('lottery')) {
|
|
return response()->json(['ok' => false, 'message' => '双色球彩票未开启,请先开启游戏。']);
|
|
}
|
|
|
|
$openedRoomIds = [];
|
|
|
|
foreach ($roomScopeService->getScopedRoomIdsForGame('lottery') as $roomId) {
|
|
if (LotteryIssue::currentIssue($roomId)) {
|
|
continue;
|
|
}
|
|
|
|
\App\Jobs\OpenLotteryIssueJob::dispatch($roomId);
|
|
$openedRoomIds[] = $roomId;
|
|
}
|
|
|
|
if ($openedRoomIds === []) {
|
|
return response()->json(['ok' => false, 'message' => '目标房间当前已有进行中的期次,无需重复开期。']);
|
|
}
|
|
|
|
return response()->json([
|
|
'ok' => true,
|
|
'message' => '✅ 已排队开期任务,目标房间:#'.implode('、#', $openedRoomIds),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 管理员强制立即开奖(测试专用)。
|
|
*
|
|
* 将当前 open 或 closed 期次直接投入开奖队列。
|
|
*/
|
|
public function forceLotteryDraw(LotteryService $lottery): JsonResponse
|
|
{
|
|
$issue = LotteryIssue::query()
|
|
->whereIn('status', ['open', 'closed'])
|
|
->latest()
|
|
->first();
|
|
|
|
if (! $issue) {
|
|
return response()->json(['ok' => false, 'message' => '当前无可开奖的期次。']);
|
|
}
|
|
|
|
// 强制将状态改为 closed
|
|
$issue->update(['status' => 'closed']);
|
|
\App\Jobs\DrawLotteryJob::dispatch($issue->fresh());
|
|
|
|
return response()->json(['ok' => true, 'message' => "✅ 开奖任务已入队,第 {$issue->issue_no} 期将就绪开奖!"]);
|
|
}
|
|
}
|