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

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' => '渠道已创建,但复制默认游戏配置失败',
];