Files
fast/addons/shopro/controller/zy/Game.php
xiadc ac0777e34b feat(游戏模块): 重构比赛逻辑并添加赛制支持
- 移除了原有的比赛时间检查逻辑
- 引入了赛制类文件,实现了比赛自动编排功能
- 新增了 GameMatch 模型用于存储比赛对阵信息
- 优化了数据库事务处理逻辑
- 更新了 Composer 自动加载配置,添加 format 命名空间
2025-05-23 17:37:48 +08:00

142 lines
5.2 KiB
PHP

<?php
namespace addons\shopro\controller\zy;
use think\Db;
use think\Exception;
use app\admin\model\zy\Club;
use app\admin\model\zy\Stadium;
use think\exception\PDOException;
use app\admin\model\zy\game\GameJoin;
use app\admin\model\zy\game\GameMatch;
use think\exception\ValidateException;
use app\admin\model\zy\game\Participant;
class Game extends Base
{
public function __construct()
{
$this->model = new \app\admin\model\zy\game\Game;
parent::__construct();
}
public function index()
{
$params = $this->request->param();
$query = $this->model->alias('g')
->join([Participant::$tableName => 'p'], 'p.game_id=g.id', 'LEFT')
->join([Stadium::$tableName => 's'], 's.id = g.gym_id', 'LEFT')
->join([Club::$tableName => 'c'], 'c.id = g.club_id', 'LEFT')
->field('g.*, s.name as gym_name, c.name as club_name,JSON_ARRAYAGG(p.avatar) as avatar,count(p.id) as join_num');
if (isset($params['name'])) {
$query->where('g.name', 'like', '%' . $params['name'] . '%');
}
if (isset($params['club_id'])) {
$query->where('g.club_id', $params['club_id']);
}
if (isset($params['week'])) {
$query->where('g.week', $params['week']);
}
if (isset($params['pid'])) {
$query->where('pid', $params['pid']);
} else {
$query->where('pid', 0);
}
if (isset($params['public_time'])) {
$query->where('public_time', $params['public_time']);
} else {
$query->where('public_time', '<=', date('Y-m-d H:i:s'));
}
if (isset($params['page'])) {
$pageSize = intval($params['pageSize'] ?? 10);
$offeset = (intval($params['page']) - 1) * $pageSize;
$query->limit($offeset, $pageSize);
}
$res = $query->paginate($params['pageSize'] ?? 10);
$list = $res->items();
foreach ($list as &$v) {
$v['cost'] = json_decode($v['cost'] ?? '[]', true);
$v['avatar'] = json_decode($v['avatar'] ?? '[]', true);
}
$this->success('Success', ['list' => $list, 'count' => $res->total()]);
}
public function view()
{
$model = $this->model->get($this->request->param('id'));
if (empty($model)) {
$this->error(__('No rows were found'));
}
$this->model->where('id', $model['id'])->setInc('attention');
$model['cost'] = json_decode($model['cost'] ?? '[]', true);
$model['referee'] = explode(',', $model['referee']);
$this->success('Success', $model);
}
public function getMacth()
{
$params = $this->request->param();
Db::startTrans();
try {
$game = $this->model->get($params['id'] ?? NULL);
if (empty($game)) {
$this->error('比赛不存在');
}
// $dataTime = date('Y-m-d H:i:s');
// $startTime = $game['date'] . ' ' . $game['start_time'];
// if ($dataTime < $startTime) {
// $this->error('比赛时间未开始');
// }
// $endTime = $game['date'] . ' ' . $game['end_time'];
// if ($dataTime > $endTime) {
// $this->error('比赛时间已结束');
// }
$participant = Participant::where('game_id', $game['id'])->where('status', 1)->select();
if (empty($participant)) {
$join = GameJoin::where('game_id', $game['id'])->where('status', 1)->select();
$order = 1;
$participant = [];
foreach ($join as $j) {
$users = json($j['users'], true);
foreach ($users as $u) {
$participant[] = [
'user_id' => $u['user_id'],
'gender' => $u['gender'],
'avatar' => $u['avatar'],
'name' => $u['name'],
'game_join_id' => $j['id'],
'game_id' => $game['id'],
'order' => $order,
'status' => 1,
];
$order++;
}
}
$this->model->allowField(true)->save($participant);
}
$gameClass = 'format\\Game' . $game['team_type'] . $game['rule_type'];
if (!class_exists($gameClass)) {
throw new Exception("赛制文件不存在,请联系管理人员: {$gameClass}");
}
$format = new $gameClass;
if (!$format instanceof \format\GameInterface) {
throw new Exception("赛制配置错误,请联系管理人员: {$gameClass}");
}
$matchs = $format->match($game, $participant);
$res = (new GameMatch)->insertAll($matchs);
Db::commit();
} catch (ValidateException | PDOException | Exception $e) {
Db::rollback();
$this->error($e->getMessage(), $e);
}
// print_r($res);
$this->success('Success', $res);
}
}