feat(zy): 实现比赛报名和自动分组功能

- 新增比赛报名逻辑,支持单双打和团队赛
- 实现自动分组算法,根据比赛规则生成对阵表
- 添加下一轮对阵安排功能,支持淘汰赛制
- 优化比赛结果处理,自动计算排名和得分
- 新增参赛人员列表接口,支持多种查询条件
This commit is contained in:
2025-05-30 16:29:57 +08:00
parent 6e655c6121
commit be7ee40690
8 changed files with 328 additions and 135 deletions

View File

@@ -12,6 +12,7 @@ use app\admin\model\shopro\Category;
use app\admin\model\zy\game\GameJoin;
use think\exception\ValidateException;
use app\admin\model\shopro\goods\Goods;
use app\admin\model\zy\game\Participant;
use addons\shopro\service\order\OrderCreate;
use app\admin\controller\shopro\traits\SkuPrice;
@@ -206,6 +207,32 @@ class Activity extends Base
if (GameJoin::get(['game_id' => $game['id'], 'act_id' => $game['act_id'], 'user_id' => $this->auth->id])) {
$this->error('您已报名此活动');
}
$order = GameJoin::where('game_id', $game['id'])->count();
$gender = ['man' => 0, 'woman' => 0];
$participant = [];
foreach ($params['users'] as $u) {
$order++;
$participant[] = [
'user_id' => $u['user_id'],
'gender' => $u['gender'],
'avatar' => $u['avatar'],
'name' => $u['nickname'],
'game_id' => $game['id'],
'signin' => 0,
'order' => $order,
'status' => 1,
];
if ($u['gender'] == 0) {
$gender['woman'] += 1;
} else {
$gender['man'] += 1;
}
}
foreach ($gender as $k => $v) {
if ($params['goods_list'][$k]['goods_num'] != $v) {
$this->error('报名人数错误');
}
}
$orderCreate = new OrderCreate($params);
$result = $orderCreate->calc('create');
$order = $orderCreate->create($result);
@@ -215,9 +242,13 @@ class Activity extends Base
'game_id' => $game['id'],
'user_id' => $this->auth->id,
'orer_id' => $order['id'],
'status' => 1,
'status' => 0,//待支付
'users' => json_encode($params['users'] ?? [])
]);
foreach ($participant as &$p) {
$p['game_join_id'] = $join->id;
}
(new Participant)->insertAll($participant);
Db::commit();
} catch (ValidateException | PDOException | Exception $e) {

View File

@@ -227,50 +227,49 @@ class Game extends Base
$this->success('Success', $describe);
}
//参与人员列表
public function participant()
{
$params = $this->request->param();
$query = Participant::where('game_id', $params['game_id']);
if (isset($params['game_join_id'])) {
$query->where('game_join_id', $params['game_join_id']);
}
if (isset($params['status'])) {
$query->where('status', $params['status']);
}
if (isset($params['gender'])) {
$query->where('gender', $params['gender']);
}
if (isset($params['order'])) {
$query->order($params['order'], $params['sort'] ?? NULL);
}
$this->success('Success', $query->select());
}
// 获取比赛匹配列表
public function macthList()
{
$params = $this->request->param();
Db::startTrans();
try {
$game = $this->model->get($params['id'] ?? NULL);
$game = $this->model->get($params['game_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('比赛时间已结束');
// }
$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('比赛时间已结束');
}
$matchs = GameMatch::where('game_id', $game['id'])->select();
if (empty($matchs)) {
$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_decode($j['users'], true);
foreach ($users as $u) {
$participant[] = [
'user_id' => $u['user_id'],
'gender' => $u['gender'],
'avatar' => $u['avatar'],
'name' => $u['name'],
'mark' => $u['mark'], // 备注
'game_join_id' => $j['id'],
'game_id' => $game['id'],
'order' => $order,
'status' => 1,
];
$order++;
}
}
(new Participant)->insertAll($participant);
}
$gameClass = 'format\\Game' . $game['team_type'] . $game['rule_type'];
if (!class_exists($gameClass)) {
throw new Exception("赛制文件不存在,请联系管理人员: {$gameClass}");
@@ -281,15 +280,19 @@ class Game extends Base
}
$matchs = $format->match($game, $participant);
$res = (new GameMatch)->insertAll($matchs);
$matchs = GameMatch::where('game_id', $game['id'])->select();
}
$matchs = GameMatch::where('game_id', $game['id'])->select();
Db::commit();
} catch (ValidateException | PDOException | Exception $e) {
Db::rollback();
$this->error($e->getMessage(), $e);
}
foreach ($matchs as &$m) {
foreach ($matchs as $k => &$m) {
if (!empty($params['level']) && $params['level'] != $m['level']) {
unset($matchs[$k]);
continue;
}
$m['teamA'] = json_decode($m['teamA'], true);
$m['teamB'] = json_decode($m['teamB'], true);
}
@@ -374,6 +377,23 @@ class Game extends Base
$u->net_score = $params['scoreA']; //净得分
$u->save();
}
$undone = GameMatch::where('level', $match['level'])->where('winner', null)->count();
df($undone);
if ($undone == 0) { //所有比赛完成,开启下一轮比赛
$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}");
}
$done = GameMatch::where('level', $match['level'])->where('winner', '!=', '')->select();
$matchs = $format->nextLevel($game, $done, $match['level'] + 1);
if (!empty($matchs)) {
(new GameMatch)->insertAll($matchs);
}
}
Db::commit();
} catch (ValidateException | PDOException | Exception $e) {
Db::rollback();