151 lines
5.5 KiB
PHP
151 lines
5.5 KiB
PHP
<?php
|
||
declare(strict_types=1);
|
||
|
||
namespace app\api\controller;
|
||
|
||
use support\Request;
|
||
use support\Response;
|
||
use app\api\logic\UserLogic;
|
||
use app\api\logic\GameLogic;
|
||
use app\api\logic\PlayStartLogic;
|
||
use app\api\util\ReturnCode;
|
||
use app\dice\model\play_record\DicePlayRecord;
|
||
use app\dice\model\player\DicePlayer;
|
||
use app\dice\model\reward_config\DiceRewardConfig;
|
||
use plugin\saiadmin\basic\OpenController;
|
||
use plugin\saiadmin\exception\ApiException;
|
||
|
||
/**
|
||
* 游戏相关接口(购买抽奖券等)
|
||
*/
|
||
class GameController extends OpenController
|
||
{
|
||
/**
|
||
* 购买抽奖券
|
||
* POST /api/game/buyLotteryTickets
|
||
* header: auth-token, user-token
|
||
* body: count = 1 | 5 | 10(1次/100coin, 5次/500coin, 10次/1000coin)
|
||
* 记录钱包流水,并更新缓存中玩家的 total_draw_count、paid_draw_count、free_draw_count、coin
|
||
*/
|
||
public function buyLotteryTickets(Request $request): Response
|
||
{
|
||
$token = $request->header('user-token');
|
||
if (empty($token)) {
|
||
$auth = $request->header('authorization');
|
||
if ($auth && stripos($auth, 'Bearer ') === 0) {
|
||
$token = trim(substr($auth, 7));
|
||
}
|
||
}
|
||
if (empty($token)) {
|
||
return $this->fail('请携带 user-token', ReturnCode::MISSING_TOKEN);
|
||
}
|
||
$userId = UserLogic::getUserIdFromToken($token);
|
||
if ($userId === null) {
|
||
return $this->fail('user-token 无效或已过期', ReturnCode::TOKEN_TIMEOUT);
|
||
}
|
||
|
||
$count = (int) $request->post('count', 0);
|
||
if (!in_array($count, [1, 5, 10], true)) {
|
||
return $this->fail('购买抽奖券错误', ReturnCode::EMPTY_PARAMS);
|
||
}
|
||
|
||
try {
|
||
$logic = new GameLogic();
|
||
$data = $logic->buyLotteryTickets($userId, $count);
|
||
return $this->success($data);
|
||
} catch (ApiException $e) {
|
||
$msg = $e->getMessage();
|
||
if ($msg === '平台币不足') {
|
||
$player = DicePlayer::find($userId);
|
||
$coin = $player ? (float) $player->coin : 0;
|
||
return $this->success(['coin' => $coin], $msg);
|
||
}
|
||
return $this->fail($msg, ReturnCode::EMPTY_PARAMS);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取彩金池(中奖配置表)
|
||
* GET /api/game/lotteryPool
|
||
* header: auth-token
|
||
* 返回 DiceRewardConfig 列表(彩金池/中奖配置)
|
||
*/
|
||
public function lotteryPool(Request $request): Response
|
||
{
|
||
$list = DiceRewardConfig::order('id', 'asc')->select()->toArray();
|
||
return $this->success($list);
|
||
}
|
||
|
||
/**
|
||
* 开始游戏(抽奖一局)
|
||
* POST /api/game/playStart
|
||
* header: auth-token, user-token
|
||
* body: rediction 必传,0=无 1=中奖
|
||
* 余额不足时返回 code=200、message=玩家当前余额不足无法开启对局;超时返回 code=200、message=服务超时,并记录 status=0
|
||
*/
|
||
public function playStart(Request $request): Response
|
||
{
|
||
$token = $request->header('user-token');
|
||
if (empty($token)) {
|
||
$auth = $request->header('authorization');
|
||
if ($auth && stripos($auth, 'Bearer ') === 0) {
|
||
$token = trim(substr($auth, 7));
|
||
}
|
||
}
|
||
if (empty($token)) {
|
||
return $this->fail('请携带 user-token', ReturnCode::MISSING_TOKEN);
|
||
}
|
||
$userId = UserLogic::getUserIdFromToken($token);
|
||
if ($userId === null) {
|
||
return $this->fail('user-token 无效或已过期', ReturnCode::TOKEN_TIMEOUT);
|
||
}
|
||
|
||
$rediction = $request->post('rediction');
|
||
if ($rediction === '' || $rediction === null) {
|
||
return $this->fail('请传递 rediction 参数', ReturnCode::EMPTY_PARAMS);
|
||
}
|
||
$direction = (int) $rediction;
|
||
if (!in_array($direction, [0, 1], true)) {
|
||
return $this->fail('rediction 必须为 0 或 1', ReturnCode::EMPTY_PARAMS);
|
||
}
|
||
|
||
$player = DicePlayer::find($userId);
|
||
if (!$player) {
|
||
return $this->fail('用户不存在', ReturnCode::EMPTY_PARAMS);
|
||
}
|
||
$minEv = (float) DiceRewardConfig::min('real_ev');
|
||
$minCoin = abs($minEv + 100);
|
||
$coin = (float) $player->coin;
|
||
if ($coin < $minCoin) {
|
||
return $this->success([], '当前玩家余额小于DiceRewardConfigMin.real_ev+100无法继续游戏');
|
||
}
|
||
|
||
try {
|
||
$logic = new PlayStartLogic();
|
||
$data = $logic->run($userId, $direction);
|
||
return $this->success($data);
|
||
} catch (ApiException $e) {
|
||
return $this->fail($e->getMessage(), ReturnCode::EMPTY_PARAMS);
|
||
} catch (\Throwable $e) {
|
||
$timeoutRecord = null;
|
||
try {
|
||
$timeoutRecord = DicePlayRecord::create([
|
||
'player_id' => $userId,
|
||
'lottery_config_id' => 0,
|
||
'lottery_type' => 0,
|
||
'win_coin' => 0,
|
||
'direction' => $direction,
|
||
'reward_config_id' => 0,
|
||
'start_index' => 0,
|
||
'target_index' => 0,
|
||
'roll_array' => '[]',
|
||
'status' => PlayStartLogic::RECORD_STATUS_TIMEOUT,
|
||
]);
|
||
} catch (\Throwable $_) {
|
||
}
|
||
$payload = $timeoutRecord ? ['record' => $timeoutRecord->toArray()] : [];
|
||
return $this->success($payload, '服务超时');
|
||
}
|
||
}
|
||
}
|