Files
webman-buildadmin/app/admin/controller/game/RewardConfig.php

278 lines
8.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare(strict_types=1);
namespace app\admin\controller\game;
use Throwable;
use app\common\controller\Backend;
use app\common\library\GameRewardConfigTemplate;
use app\common\library\GameRewardTierBoardGenerator;
use app\common\library\GameRewardWeightSeeder;
use app\common\model\GameRewardConfig;
use app\common\validate\GameRewardConfig as GameRewardConfigValidate;
use support\think\Db;
use support\Response;
use Webman\Http\Request as WebmanRequest;
/**
* 游戏奖励配置(渠道表单页,路由 /admin/game/rewardConfig
* 约定game_channel_id = 0 为超管维护的「全渠道默认模板」,新建渠道时优先从此行复制到新渠道
*/
class RewardConfig extends Backend
{
/** 默认模板渠道主键(非真实渠道,仅存库一行) */
private const DEFAULT_TEMPLATE_CHANNEL_ID = 0;
public function index(WebmanRequest $request): Response
{
$response = $this->initializeBackend($request);
if ($response !== null) {
return $response;
}
[$channelId, $err] = $this->resolveTargetChannelId($request, false);
if ($err !== null) {
return $err;
}
$row = GameRewardConfig::where('game_channel_id', $channelId)->find();
if ($row) {
return $this->success('', [
'row' => $row->toArray(),
]);
}
$defaults = GameRewardConfigTemplate::getDefaultJsonColumns();
return $this->success('', [
'row' => [
'id' => null,
'game_channel_id' => $channelId,
'tier_reward_form' => $defaults['tier_reward_form'],
'bigwin_form' => $defaults['bigwin_form'],
],
]);
}
public function save(WebmanRequest $request): Response
{
$response = $this->initializeBackend($request);
if ($response !== null) {
return $response;
}
if ($request->method() !== 'POST') {
return $this->error(__('Parameter error'));
}
$data = $request->post();
if (!$data) {
return $this->error(__('Parameter %s can not be empty', ['']));
}
[$channelId, $err] = $this->resolveTargetChannelId($request, true);
if ($err !== null) {
return $err;
}
if (!$this->channelExists($channelId)) {
return $this->error(__('Record not found'));
}
$tier = $data['tier_reward_form'] ?? null;
$big = $data['bigwin_form'] ?? null;
if (!is_string($tier) || !is_string($big)) {
return $this->error(__('Parameter error'));
}
try {
$validate = new GameRewardConfigValidate();
$validate->scene('channel_form')->check([
'tier_reward_form' => $tier,
'bigwin_form' => $big,
]);
} catch (Throwable $e) {
return $this->error($e->getMessage());
}
$existing = GameRewardConfig::where('game_channel_id', $channelId)->find();
try {
if ($existing) {
$existing->save([
'tier_reward_form' => $tier,
'bigwin_form' => $big,
]);
} else {
$m = new GameRewardConfig();
$m->save([
'game_channel_id' => $channelId,
'tier_reward_form' => $tier,
'bigwin_form' => $big,
]);
}
} catch (Throwable $e) {
return $this->error($e->getMessage());
}
return $this->success(__('Update successful'));
}
/**
* 按条数与结算标准生成 26 格档位奖励并保存(保留当前 bigwin_form
*/
public function generateTierBoard(WebmanRequest $request): Response
{
$response = $this->initializeBackend($request);
if ($response !== null) {
return $response;
}
if ($request->method() !== 'POST') {
return $this->error(__('Parameter error'));
}
$data = $request->post();
if (!$data || !is_array($data)) {
return $this->error(__('Parameter %s can not be empty', ['']));
}
[$channelId, $err] = $this->resolveTargetChannelId($request, true);
if ($err !== null) {
return $err;
}
if (!$this->channelExists($channelId)) {
return $this->error(__('Record not found'));
}
try {
$out = GameRewardTierBoardGenerator::generate($data);
} catch (Throwable $e) {
return $this->error($e->getMessage());
}
$tier = $out['tier_reward_form'];
$existing = GameRewardConfig::where('game_channel_id', $channelId)->find();
if ($existing && is_string($existing->bigwin_form) && trim($existing->bigwin_form) !== '') {
$big = $existing->bigwin_form;
} else {
$defaults = GameRewardConfigTemplate::getDefaultJsonColumns();
$big = $defaults['bigwin_form'];
}
try {
$validate = new GameRewardConfigValidate();
$validate->scene('channel_form')->check([
'tier_reward_form' => $tier,
'bigwin_form' => $big,
]);
} catch (Throwable $e) {
return $this->error($e->getMessage());
}
try {
if ($existing) {
$existing->save([
'tier_reward_form' => $tier,
'bigwin_form' => $big,
]);
} else {
$m = new GameRewardConfig();
$m->save([
'game_channel_id' => $channelId,
'tier_reward_form' => $tier,
'bigwin_form' => $big,
]);
}
} catch (Throwable $e) {
return $this->error($e->getMessage());
}
return $this->success(__('Update successful'));
}
/**
* 根据当前档位奖励 JSON 生成 game_reward_weight先清空该渠道再写入 52 条)
*/
public function generateRewardWeight(WebmanRequest $request): Response
{
$response = $this->initializeBackend($request);
if ($response !== null) {
return $response;
}
if ($request->method() !== 'POST') {
return $this->error(__('Parameter error'));
}
[$channelId, $err] = $this->resolveTargetChannelId($request, true);
if ($err !== null) {
return $err;
}
if (!$this->channelExists($channelId)) {
return $this->error(__('Record not found'));
}
$row = GameRewardConfig::where('game_channel_id', $channelId)->find();
if (!$row || !is_string($row->tier_reward_form) || trim($row->tier_reward_form) === '') {
return $this->error('请先保存档位奖励配置');
}
try {
GameRewardWeightSeeder::syncFromTierRewardForm($channelId, $row->tier_reward_form);
} catch (Throwable $e) {
return $this->error($e->getMessage());
}
return $this->success(__('Update successful'));
}
/**
* @return array{0: int, 1: Response|null}
*/
private function resolveTargetChannelId(WebmanRequest $request, bool $isPost): array
{
if ($this->auth->isSuperAdmin()) {
$raw = $isPost ? ($request->post('game_channel_id') ?? $request->get('game_channel_id')) : $request->get('game_channel_id');
if ($raw === null || $raw === '') {
return [self::DEFAULT_TEMPLATE_CHANNEL_ID, null];
}
if (!is_numeric($raw)) {
return [self::DEFAULT_TEMPLATE_CHANNEL_ID, $this->error(__('Parameter error'))];
}
$cid = intval(strval($raw));
if ($cid < 0) {
return [self::DEFAULT_TEMPLATE_CHANNEL_ID, $this->error(__('Parameter error'))];
}
if ($cid === self::DEFAULT_TEMPLATE_CHANNEL_ID) {
return [self::DEFAULT_TEMPLATE_CHANNEL_ID, null];
}
return [$cid, null];
}
$ids = Db::name('game_channel')->where('admin_id', $this->auth->id)->order('id', 'asc')->column('id');
if ($ids === []) {
return [self::DEFAULT_TEMPLATE_CHANNEL_ID, $this->error(__('Record not found'))];
}
return [intval(strval($ids[0])), null];
}
private function channelExists(int $channelId): bool
{
if ($channelId === self::DEFAULT_TEMPLATE_CHANNEL_ID) {
return $this->auth->isSuperAdmin();
}
if ($this->auth->isSuperAdmin()) {
return Db::name('game_channel')->where('id', $channelId)->count() > 0;
}
return Db::name('game_channel')->where('id', $channelId)->where('admin_id', $this->auth->id)->count() > 0;
}
}