游戏-游戏配置-优化样式,增强验证

This commit is contained in:
2026-04-03 17:49:58 +08:00
parent 28bd9f1a09
commit 427024171e
7 changed files with 242 additions and 37 deletions

View File

@@ -35,7 +35,7 @@ class Config extends Backend
*/
protected bool $dataLimitFieldAutoFill = false;
protected string|array $defaultSortField = 'group,desc';
protected string|array $defaultSortField = 'channel_id,desc';
protected array $withJoinTable = ['channel'];
@@ -43,9 +43,12 @@ class Config extends Backend
protected string|array $quickSearchField = ['ID'];
/** 权重之和必须100 的配置标识 */
/** default_tier_weight / default_kill_score_weight每项≤100 且各项之和必须=100 */
private const WEIGHT_SUM_100_NAMES = ['default_tier_weight', 'default_kill_score_weight'];
/** default_bigwin_weight仅校验每项整数、0100005/30 固定 10000不参与「和≤100」 */
private const BIGWIN_WEIGHT_NAME = 'default_bigwin_weight';
protected function initController(WebmanRequest $request): ?Response
{
$this->model = new \app\common\model\GameConfig();
@@ -162,6 +165,13 @@ class Config extends Backend
$data['group'] = $row['group'];
$data['name'] = $row['name'];
$data['title'] = $row['title'];
} elseif (!isset($data['name']) || $data['name'] === '' || $data['name'] === null) {
// JSON/表单未带 name 时回退库值避免走「每项≤10000」
$data['name'] = $row['name'] ?? '';
}
// 超管编辑时若未传 group用库值参与 game_weight 校验(与 name 回退一致)
if ($this->auth->isSuperAdmin() && trim((string) ($data['group'] ?? '')) === '') {
$data['group'] = (string) ($row['group'] ?? '');
}
$err = $this->validateGameWeightPayload($data, $row['value'] ?? null);
@@ -201,27 +211,93 @@ class Config extends Backend
}
/**
* game_weight:校验数值、键不可改(编辑)、和为 100特定 name
* 与前端 gameWeightFixed.normalizeGameWeightConfigName 一致trim、去括号后说明、小写
*/
private function normalizeGameWeightConfigName(string $raw): string
{
$s = trim($raw);
if (preg_match('/^([^(]+)/u', $s, $m)) {
$s = trim($m[1]);
}
return strtolower($s);
}
/**
* 解析 game_weight JSON 里每一项的权重:支持 int/float、数字字符串与前端 JSON.stringify 一致常为字符串)
*/
private function parseGameWeightScalarToFloat(mixed $v): ?float
{
if (is_int($v) || is_float($v)) {
return (float) $v;
}
if (is_bool($v) || is_array($v) || $v === null) {
return null;
}
$s = trim((string) $v);
if ($s === '') {
return null;
}
$f = filter_var($s, FILTER_VALIDATE_FLOAT);
if ($f !== false) {
return $f;
}
$i = filter_var($s, FILTER_VALIDATE_INT);
if ($i !== false) {
return (float) $i;
}
return null;
}
/**
* game_weighttier/kill 每项≤100 且和必须=100bigwin 每项 010000编辑时键不可改
*
* @param array<string, mixed> $data
*/
private function validateGameWeightPayload(array $data, ?string $originalValue): ?string
private function validateGameWeightPayload(array &$data, ?string $originalValue): ?string
{
$group = $data['group'] ?? '';
$group = strtolower(trim((string) ($data['group'] ?? '')));
if ($group !== 'game_weight') {
return null;
}
$name = $data['name'] ?? '';
$value = $data['value'] ?? '';
if (!is_string($value)) {
$rawName = (string) ($data['name'] ?? '');
$rawName = preg_replace('/[\x{200B}-\x{200D}\x{FEFF}]/u', '', $rawName);
$name = $this->normalizeGameWeightConfigName($rawName);
$rawValue = $data['value'] ?? null;
$valueWasArray = false;
if (is_array($rawValue)) {
$decoded = $rawValue;
$valueWasArray = true;
} elseif (is_string($rawValue)) {
$s = trim($rawValue);
if (str_starts_with($s, "\xEF\xBB\xBF")) {
$s = substr($s, 3);
}
if ($s === '') {
return __('Parameter error');
}
$decoded = json_decode($s, true);
// 双重 JSON 字符串(外层已解析为字符串时)
if (is_string($decoded)) {
$decoded = json_decode(trim($decoded), true);
}
} else {
return __('Parameter error');
}
$decoded = json_decode($value, true);
if (!is_array($decoded)) {
return __('Parameter error');
}
// 骰子键 530 结构一律按大奖 010000 校验(避免库中 name 与 JSON 不一致时误走 tier/kill 单项上限)
$diceBigwin = $this->gameWeightDecodedIsBigwinDiceKeys($decoded);
// 归一化后的 name + 原始串包含 default_bigwin_weight兼容说明后缀、异常空格避免误走单项上限
$rawLower = strtolower($rawName);
$useBigwinRules = $diceBigwin
|| ($name === self::BIGWIN_WEIGHT_NAME)
|| str_contains($rawLower, 'default_bigwin_weight');
$keys = [];
$numbers = [];
foreach ($decoded as $item) {
@@ -229,13 +305,23 @@ class Config extends Backend
return __('Parameter error');
}
foreach ($item as $k => $v) {
$keys[] = $k;
if (!is_numeric($v)) {
$keys[] = (string) $k;
$num = $this->parseGameWeightScalarToFloat($v);
if ($num === null) {
return __('Game config weight value must be numeric');
}
$num = (float) $v;
if ($num > 100) {
return __('Game config weight each value must not exceed 100');
if ($useBigwinRules) {
if ($num < 0 || $num > 10000) {
return __('Game config bigwin weight each 0 10000');
}
$ks = strval($k);
if (($ks === '5' || $ks === '30') && abs($num - 10000.0) > 0.000001) {
return __('Game config bigwin weight locked 5 30');
}
} else {
if ($num > 100) {
return __('Game config weight each value must not exceed 100');
}
}
$numbers[] = $num;
}
@@ -252,16 +338,51 @@ class Config extends Backend
}
}
if (in_array($name, self::WEIGHT_SUM_100_NAMES, true)) {
if (!$useBigwinRules && in_array($name, self::WEIGHT_SUM_100_NAMES, true)) {
$sum = array_sum($numbers);
if (abs($sum - 100.0) > 0.000001) {
return __('Game config weight sum must equal 100');
}
}
if ($valueWasArray) {
$data['value'] = json_encode($decoded, JSON_UNESCAPED_UNICODE);
}
return null;
}
/**
* 是否为 default_bigwin_weight 的固定骰子键集合(与前端 BIGWIN_WEIGHT_KEYS 一致)
*
* @param array<mixed> $decoded
*/
private function gameWeightDecodedIsBigwinDiceKeys(array $decoded): bool
{
$keys = [];
foreach ($decoded as $item) {
if (!is_array($item)) {
return false;
}
foreach (array_keys($item) as $k) {
$keys[] = trim((string) $k);
}
}
if (count($keys) !== 6) {
return false;
}
$ints = [];
foreach ($keys as $k) {
if ($k === '' || !ctype_digit($k)) {
return false;
}
$ints[] = (int) $k;
}
sort($ints, SORT_NUMERIC);
return $ints === [5, 10, 15, 20, 25, 30];
}
/**
* @return list<string>
*/
@@ -277,7 +398,7 @@ class Config extends Backend
continue;
}
foreach ($item as $k => $_) {
$keys[] = $k;
$keys[] = (string) $k;
}
}
return $keys;

View File

@@ -34,6 +34,7 @@ return [
'The uploaded file is too large (%sMiB), Maximum file size:%sMiB' => 'The uploaded file is too large (%sMiB), maximum file size:%sMiB',
'No files have been uploaded or the file size exceeds the upload limit of the server' => 'No files have been uploaded or the file size exceeds the server upload limit.',
'Unknown' => 'Unknown',
'Global default' => 'Global default (channel_id=0)',
'Super administrator' => 'Super administrator',
'No permission' => 'No permission',
'%first% etc. %count% items' => '%first% etc. %count% items',
@@ -97,6 +98,14 @@ return [
'Group Name Arr' => 'Group Name Arr',
'Game config weight keys cannot be modified' => 'Weight config keys cannot be modified',
'Game config weight value must be numeric' => 'Weight values must be numeric',
'Game config weight each value must not exceed 100' => 'Each weight value must not exceed 100',
'Game config weight each value must not exceed 100' => 'Weight must not exceed 10000',
'Game config weight each value must not exceed 10000' => 'Weight must not exceed 10000',
'Game config weight sum must equal 100' => 'The sum of weights for default_tier_weight / default_kill_score_weight must equal 100',
'Game config weight sum must not exceed 100' => 'The sum of weights for default_tier_weight / default_kill_score_weight must not exceed 100',
'Game config bigwin weight must be integer' => 'Each default big win weight must be an integer',
'Game config bigwin weight each 0 10000' => 'Each default big win weight must be between 0 and 10000',
'Game config bigwin weight locked 5 30' => 'Dice totals 5 and 30 are guaranteed leopard (豹子); weight is fixed at 10000 and cannot be changed',
'Game user username exists in channel' => 'This username already exists in the selected channel',
'Game channel delete need confirm related' => 'Please confirm deletion of related game configuration and user data first',
'Game channel copy default config failed' => 'Channel was created, but copying default game configuration failed',
];

View File

@@ -117,6 +117,14 @@ return [
'Group Name Arr' => '分组名称数组',
'Game config weight keys cannot be modified' => '权重配置的键不可修改',
'Game config weight value must be numeric' => '权重值必须为数字',
'Game config weight each value must not exceed 100' => '每项权重不能超过100',
'Game config weight each value must not exceed 100' => '权重不能超过10000',
'Game config weight each value must not exceed 10000' => '权重不能超过10000',
'Game config weight sum must equal 100' => 'default_tier_weight / default_kill_score_weight 的权重之和必须等于100',
'Game config weight sum must not exceed 100' => 'default_tier_weight / default_kill_score_weight 的权重之和不能超过100',
'Game config bigwin weight must be integer' => '默认大奖权重每项必须为整数',
'Game config bigwin weight each 0 10000' => '默认大奖权重每项须在 010000 之间',
'Game config bigwin weight locked 5 30' => '点数 5 与 30 为豹子号必中,权重固定为 10000不可修改',
'Game user username exists in channel' => '该渠道下已存在相同用户名的用户',
'Game channel delete need confirm related' => '请先确认是否同时删除关联的游戏配置与用户数据',
'Game channel copy default config failed' => '渠道已创建,但复制默认游戏配置失败',
];

View File

@@ -246,11 +246,21 @@ Route::get('/admin/security/dataRecycleLog/index', [\app\admin\controller\securi
Route::post('/admin/security/dataRecycleLog/restore', [\app\admin\controller\security\DataRecycleLog::class, 'restore']);
Route::get('/admin/security/dataRecycleLog/info', [\app\admin\controller\security\DataRecycleLog::class, 'info']);
// ==================== 显式路由(优先级高,避免动态路由误匹配) ====================
Route::add(['GET', 'POST', 'HEAD'], '/admin/game.User/defaultWeightPresets', [\app\admin\controller\game\User::class, 'defaultWeightPresets']);
Route::add(['GET', 'POST', 'HEAD'], '/admin/game.User/defaultWeightByChannel', [\app\admin\controller\game\User::class, 'defaultWeightByChannel']);
Route::add(['GET', 'POST', 'HEAD'], '/admin/game.user/defaultWeightPresets', [\app\admin\controller\game\User::class, 'defaultWeightPresets']);
Route::add(['GET', 'POST', 'HEAD'], '/admin/game.user/defaultWeightByChannel', [\app\admin\controller\game\User::class, 'defaultWeightByChannel']);
// 游戏渠道:删除前统计关联数据(显式路由,避免动态路由 404
Route::add(['GET', 'POST', 'HEAD'], '/admin/game.Channel/deleteRelatedCounts', [\app\admin\controller\game\Channel::class, 'deleteRelatedCounts']);
Route::add(['GET', 'POST', 'HEAD'], '/admin/game.channel/deleteRelatedCounts', [\app\admin\controller\game\Channel::class, 'deleteRelatedCounts']);
// ==================== 兼容 ThinkPHP 风格 URLmodule.Controller/action ====================
// 前端使用 /admin/user.Rule/index 格式,需转换为控制器调用
Route::add(
['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD'],
'/admin/{controllerPart:[a-zA-Z]+\\.[a-zA-Z0-9]+}/{action}',
'/admin/{controllerPart:[a-zA-Z]+\\.[a-zA-Z0-9]+}/{action:[^/]+}',
function (\Webman\Http\Request $request, string $controllerPart, string $action) {
$pos = strpos($controllerPart, '.');
if ($pos === false) {
@@ -258,8 +268,21 @@ Route::add(
}
$module = substr($controllerPart, 0, $pos);
$controller = substr($controllerPart, $pos + 1);
$class = '\\app\\admin\\controller\\' . strtolower($module) . '\\' . $controller;
if (!class_exists($class)) {
// game.user / game.User 等:小写控制器名需解析为 User.phpPSR-4 类名 StudlyCase
$class = null;
$candidates = array_unique([
$controller,
ucfirst($controller),
ucfirst(strtolower($controller)),
]);
foreach ($candidates as $base) {
$tryClass = '\\app\\admin\\controller\\' . strtolower($module) . '\\' . $base;
if (class_exists($tryClass)) {
$class = $tryClass;
break;
}
}
if ($class === null) {
return new Response(404, ['Content-Type' => 'application/json'], json_encode(['code' => 404, 'msg' => '404 Not Found', 'data' => []], JSON_UNESCAPED_UNICODE));
}
if (!method_exists($class, $action)) {
@@ -281,6 +304,26 @@ Route::add(
}
);
// 兜底closure 显式调用,避免路由绑定在某些情况下仍落到 404
Route::add(['GET', 'POST', 'HEAD'], '/admin/game.User/defaultWeightByChannel/', function (\Webman\Http\Request $request) {
return (new \app\admin\controller\game\User())->defaultWeightByChannel($request);
});
Route::add(['GET', 'POST', 'HEAD'], '/admin/game.user/defaultWeightByChannel/', function (\Webman\Http\Request $request) {
return (new \app\admin\controller\game\User())->defaultWeightByChannel($request);
});
Route::add(['GET', 'POST', 'HEAD'], '/admin/game.User/defaultWeightPresets/', function (\Webman\Http\Request $request) {
return (new \app\admin\controller\game\User())->defaultWeightPresets($request);
});
Route::add(['GET', 'POST', 'HEAD'], '/admin/game.user/defaultWeightPresets/', function (\Webman\Http\Request $request) {
return (new \app\admin\controller\game\User())->defaultWeightPresets($request);
});
Route::add(['GET', 'POST', 'HEAD'], '/admin/game.Channel/deleteRelatedCounts/', function (\Webman\Http\Request $request) {
return (new \app\admin\controller\game\Channel())->deleteRelatedCounts($request);
});
Route::add(['GET', 'POST', 'HEAD'], '/admin/game.channel/deleteRelatedCounts/', function (\Webman\Http\Request $request) {
return (new \app\admin\controller\game\Channel())->deleteRelatedCounts($request);
});
// ==================== CORS 预检OPTIONS ====================
// 放在最后注册;显式加上前端会请求的路径,再加固通配
Route::add('OPTIONS', '/api/index/index', [\app\common\middleware\AllowCrossDomain::class, 'optionsResponse']);

View File

@@ -9,12 +9,24 @@ export default {
'weight key': 'Key',
'weight value': 'Value',
'weight sum must 100': 'The sum of weights for default_tier_weight / default_kill_score_weight must equal 100',
'weight sum max 100': 'The sum of weights for default_tier_weight / default_kill_score_weight must not exceed 100',
'weight each max 100': 'Each weight value must not exceed 100',
'weight each max 10000': 'Weight must not exceed 10000',
'bigwin weight must integer': 'Each big win weight must be an integer',
'name opt game_rule': 'game_rule',
'name opt game_rule_en': 'game_rule_en',
'name opt default_tier_weight': 'default_tier_weight',
'name opt default_kill_score_weight': 'default_kill_score_weight',
'name opt default_bigwin_weight': 'default_bigwin_weight',
default_bigwin_weight_help:
'Big win weight range is 010000: 0 means never, 10000 means 100%. Totals 5 and 30 are guaranteed leopard (豹子); fixed at 10000 and cannot be changed.',
'bigwin weight each 0 10000': 'Each weight (except fixed keys) must be between 0 and 10000',
'bigwin weight locked 5 30': 'Weights for totals 5 and 30 are fixed at 10000',
'weight value numeric': 'Weight values must be valid numbers',
sort: 'sort',
instantiation: 'instantiation',
'instantiation 0': 'instantiation 0',
'instantiation 1': 'instantiation 1',
'instantiation 0': '---',
'instantiation 1': 'YES',
create_time: 'create_time',
update_time: 'update_time',
'quick Search Fields': 'ID',

View File

@@ -9,11 +9,23 @@ export default {
'weight key': '键',
'weight value': '数值',
'weight sum must 100': 'default_tier_weight / default_kill_score_weight 的权重之和必须等于 100',
'weight sum max 100': 'default_tier_weight / default_kill_score_weight 的权重之和不能超过 100',
'weight each max 100': '每项权重不能超过 100',
'weight each max 10000': '权重不能超过10000',
'bigwin weight must integer': '大奖权重每项必须为整数',
'name opt game_rule': 'game_rule游戏规则',
'name opt game_rule_en': 'game_rule_en游戏规则英文',
'name opt default_tier_weight': 'default_tier_weight默认档位权重',
'name opt default_kill_score_weight': 'default_kill_score_weight默认击杀分权重',
'name opt default_bigwin_weight': 'default_bigwin_weight默认大奖权重',
default_bigwin_weight_help:
'大奖权重区间为 0100000 表示不可能出现10000 表示 100% 出现。点数 5 与 30 为豹子号必中组合,权重固定为 10000不可修改。',
'bigwin weight each 0 10000': '除固定项外,每项大奖权重须在 010000 之间',
'bigwin weight locked 5 30': '点数 5 与 30 权重固定为 10000不可修改',
'weight value numeric': '权重值必须为有效数字',
sort: '排序',
instantiation: '实例化',
'instantiation 0': '不需要',
'instantiation 0': '---',
'instantiation 1': '需要',
create_time: '创建时间',
update_time: '更新时间',

View File

@@ -48,22 +48,21 @@ const baTable = new baTableClass(
column: [
{ type: 'selection', align: 'center', operator: false },
{ label: t('game.config.ID'), prop: 'ID', align: 'center', width: 70, operator: 'RANGE', sortable: 'custom' },
// {
// label: t('game.config.channel_id'),
// prop: 'channel_id',
// align: 'center',
// show: false,
// enableColumnDisplayControl: false,
// operatorPlaceholder: t('Fuzzy query'),
// render: 'tags',
// operator: 'LIKE',
// comSearchRender: 'string',
// },
{
label: t('game.config.channel_id'),
prop: 'channel_id',
align: 'center',
show: false,
width: 88,
operator: 'RANGE',
sortable: 'custom',
},
{
label: t('game.config.channel__name'),
prop: 'channel.name',
align: 'center',
minWidth: 100,
sortable: 'true',
operatorPlaceholder: t('Fuzzy query'),
render: 'tags',
operator: 'LIKE',
@@ -111,6 +110,7 @@ const baTable = new baTableClass(
label: t('game.config.instantiation'),
prop: 'instantiation',
align: 'center',
custom: { 0: 'error', 1: 'primary' },
operator: 'RANGE',
sortable: false,
render: 'tag',
@@ -142,7 +142,7 @@ const baTable = new baTableClass(
{ label: t('Operate'), align: 'center', width: 100, render: 'buttons', buttons: optButtons, operator: false },
],
dblClickNotEditColumn: [undefined, 'instantiation'],
defaultOrder: { prop: 'group', order: 'desc' },
defaultOrder: { prop: 'channel_id', order: 'desc' },
},
{
defaultItems: { sort: 100 },