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();

View File

@@ -35,49 +35,49 @@ return [
'Club.name' => '俱乐部名称',
'User.username' => '用户名',
"Is_public0" => "",
"Is_public1" => "",
'Is_public0' => '否',
'Is_public1' => '是',
"Is_bring0" => "",
"Is_bring1" => "",
'Is_bring0' => '否',
'Is_bring1' => '是',
"Team_type1" => "双打",
"Team_type2" => "单打",
"Team_type3" => "团队",
'Team_type1' => '双打',
'Team_type2' => '单打',
'Team_type3' => '团队',
"Rule_type1" => "八人转",
"Rule_type2" => "超八转",
"Rule_type3" => "混双转",
"Rule_type4" => "固搭转",
"Rule_type5" => "固定擂",
"Rule_type6" => "活动擂",
"Rule_type7" => "转转",
"Rule_type8" => "分区转",
"Rule_type9" => "擂台赛",
"Rule_type10" => "守擂赛",
"Rule_type11" => "追分赛",
"Rule_type12" => "固搭追分赛",
"Rule_type13" => "大循环群内赛",
"Rule_type14" => "两队PK赛",
"Rule_type15" => "战队淘汰赛",
"Rule_type16" => "单项淘汰赛",
"Rule_type17" => "分区循环淘汰赛",
'Rule_type1' => '八人转',
'Rule_type2' => '超八转',
'Rule_type3' => '混双转',
'Rule_type4' => '固搭转',
'Rule_type5' => '固定擂',
'Rule_type6' => '活动擂',
'Rule_type7' => '转转',
'Rule_type8' => '分区转',
'Rule_type9' => '擂台赛',
'Rule_type10' => '守擂赛',
'Rule_type11' => '追分赛',
'Rule_type12' => '固搭追分赛',
'Rule_type13' => '大循环群内赛',
'Rule_type14' => '两队PK赛',
'Rule_type15' => '战队淘汰赛',
'Rule_type16' => '单项淘汰赛',
'Rule_type17' => '分区循环淘汰赛',
"Type0" => "一次性",
"Type1" => "周期性",
'Type0' => '一次性',
'Type1' => '周期性',
"Status0" => "发布",
"Status1" => "报名中",
"Status2" => "比赛中",
"Status3" => "结束",
'Status0' => '发布',
'Status1' => '报名中',
'Status2' => '比赛中',
'Status3' => '结束',
"Week0" => "周日",
"Week1" => "周一",
"Week2" => "周二",
"Week3" => "周三",
"Week4" => "周四",
"Week5" => "周五",
"Week6" => "周六",
'Week0' => '周日',
'Week1' => '周一',
'Week2' => '周二',
'Week3' => '周三',
'Week4' => '周四',
'Week5' => '周五',
'Week6' => '周六',
];

View File

@@ -40,51 +40,51 @@ return [
'User.username' => '用户名',
"Is_public0" => "",
"Is_public1" => "",
'Is_public0' => '否',
'Is_public1' => '是',
"Is_bring0" => "",
"Is_bring1" => "",
'Is_bring0' => '否',
'Is_bring1' => '是',
"Team_type1" => "双打",
"Team_type2" => "单打",
"Team_type3" => "团队",
'Team_type1' => '双打',
'Team_type2' => '单打',
'Team_type3' => '团队',
"Rule_type1" => "八人转",
"Rule_type2" => "超八转",
"Rule_type3" => "混双转",
"Rule_type4" => "固搭转",
"Rule_type5" => "固定擂",
"Rule_type6" => "活动擂",
"Rule_type7" => "转转",
"Rule_type8" => "分区转",
"Rule_type9" => "擂台赛",
"Rule_type10" => "守擂赛",
"Rule_type11" => "追分赛",
"Rule_type12" => "固搭追分赛",
"Rule_type13" => "大循环群内赛",
"Rule_type14" => "两队PK赛",
"Rule_type15" => "战队淘汰赛",
"Rule_type16" => "单项淘汰赛",
"Rule_type17" => "分区循环淘汰赛",
'Rule_type1' => '八人转',
'Rule_type2' => '超八转',
'Rule_type3' => '混双转',
'Rule_type4' => '固搭转',
'Rule_type5' => '固定擂',
'Rule_type6' => '活动擂',
'Rule_type7' => '转转',
'Rule_type8' => '分区转',
'Rule_type9' => '擂台赛',
'Rule_type10' => '守擂赛',
'Rule_type11' => '追分赛',
'Rule_type12' => '固搭追分赛',
'Rule_type13' => '大循环群内赛',
'Rule_type14' => '两队PK赛',
'Rule_type15' => '战队淘汰赛',
'Rule_type16' => '单项淘汰赛',
'Rule_type17' => '分区循环淘汰赛',
"Type0" => "一次性",
"Type1" => "周期性",
'Type0' => '一次性',
'Type1' => '周期性',
"Status-1" => "取消",
"Status0" => "未开始",
"Status1" => "报名中",
"Status2" => "进行中",
"Status3" => "已结束",
'Status-1' => '取消',
'Status0' => '未开始',
'Status1' => '报名中',
'Status2' => '进行中',
'Status3' => '已结束',
"Week0" => "周日",
"Week1" => "周一",
"Week2" => "周二",
"Week3" => "周三",
"Week4" => "周四",
"Week5" => "周五",
"Week6" => "周六",
'Week0' => '周日',
'Week1' => '周一',
'Week2' => '周二',
'Week3' => '周三',
'Week4' => '周四',
'Week5' => '周五',
'Week6' => '周六',
];

View File

@@ -14,7 +14,7 @@ return [
'User.username' => '用户名',
'Order.order_sn' => '订单号',
"Status-1" => "取消",
"Status0" => "待支付",
"Status1" => "已支付",
'Status-1' => '取消',
'Status0' => '待支付',
'Status1' => '已支付',
];

View File

@@ -21,10 +21,10 @@ return [
'User.username' => '用户名',
'Game.name' => '赛事名称',
"Status0" => "候补",
"Status1" => "已报名",
"Status2" => "退坑",
'Status-1' => '退坑',
'Status0' => '候补',
'Status1' => '正常',
"Signin0" => "未签到",
"Signin1" => "已签到",
'Signin0' => '未签到',
'Signin1' => '已签到',
];

View File

@@ -2,6 +2,8 @@
namespace format;
use app\admin\model\zy\game\Game;
class Game17 implements GameInterface
{
// 赛制说明
@@ -16,7 +18,7 @@ class Game17 implements GameInterface
}
// 首轮对阵安排
public function match($game, $users): array
public function match(Game $game, array $users): array
{
$math = $team = [];
$utotal = count($users);
@@ -24,7 +26,6 @@ class Game17 implements GameInterface
for ($i = 0; $i < $utotal; $i++) {
for ($j = $i + 1; $j < $utotal; $j++) {
$teamName = 'team' . $i . $j;
// $team[] = json_encode([
$team[] = ([
'name' => $teamName,
'user' => [
@@ -45,15 +46,14 @@ class Game17 implements GameInterface
$users[$i]['team'] = $users[$j]['team'] = $teamName;
}
}
// dd($team);
// 对阵分配
$ttotal = count($team);
df("$utotal 人:");
// df("共 $utotal 人:");
$n = 1;
for ($i = 0; $i < $ttotal; $i++) {
for ($j = $i + 1; $j < $ttotal; $j++) {
if (array_intersect(array_column($team[$i]['user'], 'user_id'), array_column($team[$j]['user'], 'user_id'))) {
continue;//一个人不能同时在两边队伍
continue; //一个人不能同时在两边队伍
}
$math[] = [
'gym_id' => $game['gym_id'],
@@ -71,20 +71,44 @@ class Game17 implements GameInterface
$n++;
}
}
// dd($math);
$this->next($game, $math);
return $math;
}
// 下一轮对阵安排
public function next($game, $math): array
public function nextLevel(Game $game, array $maths, $level): array
{
$math = [];
return $math;
if ($game['rule_type'] < 15) { //15战队淘汰赛16单项淘汰赛17分区循环淘汰赛
return []; //只有淘汰赛才有下一轮对阵安排,其他类型比赛只有一轮
}
$winner = $next = [];
foreach ($maths as $m) {
$winner[] = json_decode($m['winner'], true);
}
$total = count($winner);
$n = 1;
for ($i = 0; $i < $total; $i++) {
for ($j = $i + 1; $j < $total; $j++) {
if (array_intersect(array_column($winner[$i]['user'], 'user_id'), array_column($winner[$j]['user'], 'user_id'))) {
continue;
}
$next[] = [
'gym_id' => $game['gym_id'],
'club_id' => $game['club_id'],
'act_id' => $game['act_id'],
'game_id' => $game['id'],
'level' => $level, //轮次
'turn' => $n, //场次
'teamA' => json_encode($winner[$i]), //队伍A
'teamB' => json_encode($winner[$j]), //队伍B
];
$n++;
}
}
return $next;
}
// 奖金分配
public function prize($rank): array
public function prize(array $rank): array
{
return $rank;
}

View File

@@ -1,6 +1,8 @@
<?php
namespace format;
use app\admin\model\zy\game\Game;
/*
赛制文件
@@ -17,12 +19,128 @@ interface GameInterface
// 赛制说明
public function describe(): string;
// 首轮对阵安排
public function match($game, $users): array;
// 下一轮对阵安排
public function next($game, $math): array;
/**首轮对阵安排
*
入参:
$game = Array
(
[id] => 1 //比赛id
[pid] => 0 //主比赛id
[act_id] => 1 //活动id
[gym_id] => 1 //球馆id
[club_id] => 4 //俱乐部id
[name] => 聚点第八届年终团体赛 //比赛名称
[team_type] => 1 //团队类型1双打2单打3团队
[rule_type] => 7 //规则类型1八人转2超八转3混双转4固搭转5固定擂6活动擂7转转8分区转9擂台赛10守擂赛11追分赛12固搭追分赛13大循环群内赛14两队PK赛15战队淘汰赛16单项淘汰赛17分区循环淘汰赛
[date] => 2025-05-17 //比赛日期
[week] => 2 //周几
[start_time] => 09:00:00 //开始时间
[end_time] => 12:30:00 //结束时间
[public_time] => 2025-05-02 09:00:00 //公布时间
[join_start_time] => 2025-05-03 09:00:00 //报名开始时间
[join_end_time] => 2025-05-04 09:00:00 //报名结束时间
[quit_time] => 2025-05-04 08:00:00 //免费退坑时间
[game_time] => 3.5 //比赛时长
[address] => 泰新路88号 //地址
[field] => 22 //场地号
[position] => //位置
[cost] => {"type":"1","man":"84","woman":"56","extra":"10","server":"1"} //费用
[limit_num] => 30 //报名人数限制
[join_num] => 0 //已报名人数
[attention] => 0 //关注人数
[status] => 0 //状态,-1取消0未开始1报名中2进行中3已结束
[describe] => 活动介绍 //介绍
[img] => /assets/img/qrcode.png //图片
[game_rule] => 比赛规则 //规则
[is_public] => 1 //是否公开0否1是
[bring_num] => 2 //可带人数
[is_bring] => 1 //是否可带人0否1是
[referee] => 1,2 //裁判用户id
[create_time] => 2025-04-30 10:45:58
[update_time] => 2025-05-26 10:34:14
)
$users = Array(
[
[id] => 1 // 比赛参与id
[user_id] => 1 //用户id
[name] => Yam Lai Yan //昵称
[avatar] => /assets/img/qrcode.png //头像
[gender] => 0 //性别0女1男
[game_join_id] => 1 //报名记录id
[game_id] => 1 //比赛id
[team] => //本次比赛团队
[order] => 1 //本次比赛顺序
[rank] => 0 //本次比赛排名
[mark] => //备注
[status] => 1 //状态0候补1正常2退坑
[signin] => 0 //签到状态0未签到1已签到
[score] => 0 //得分
[net_score] => 0 //净得分
[win] => 0 //胜局
[create_time] => 2025-04 - 24 05: 35:00
[update_time] => 2025-05 - 26 15:08: 55
],
)
出参:
return $math = Array(
[
[gym_id] => 1 //球馆id
[club_id] => 4 //俱乐部id
[act_id] => 1 //活动id
[game_id] => 1 //比赛id
[level] => 1 //轮次
[turn] => 1 //场次
[teamA] => {"name":"team01","user":[{"user_id":1,"gender":0,"avatar":"\/assets\/img\/qrcode.png","name":"Yam Lai Yan"},{"user_id":2,"gender":1,"avatar":"\/assets\/img\/qrcode.png","name":"Yuen Wai San"}]} //队伍A
[teamB] => {"name":"team23","user":[{"user_id":3,"gender":0,"avatar":"xjgH6E5VkQ","name":"Mo Ming Sze"},{"user_id":4,"gender":1,"avatar":"hSuH5AZVxt","name":"Chan Ka Keung"}]} //队伍B
],
)
*/
public function match(Game $game, array $users): array;
/**下一轮对阵安排
*
入参:
$maths = Array(
[
[id] => 1
[gym_id] => 1
[club_id] => 4
[act_id] => 1
[game_id] => 1
[level] => 1
[turn] => 1
[teamA] => {"name":"team01","user":[{"user_id":1,"gender":0,"avatar":"\/assets\/img\/qrcode.png","name":"admin1"},{"user_id":2,"gender":1,"avatar":"\/assets\/img\/qrcode.png","name":"admin2"}]}
[teamB] => {"name":"team23","user":[{"user_id":3,"gender":0,"avatar":"\/assets\/img\/qrcode.png","name":"admin3"},{"user_id":4,"gender":1,"avatar":"\/assets\/img\/qrcode.png","name":"admin4"}]}
[round1] => {"addedA":"3","addedB":"0","scoreA":"15","scoreB":"24"}
[round2] => {"addedA":"3","addedB":"0","scoreA":"15","scoreB":"24"}
[round3] => {"addedA":"3","addedB":"0","scoreA":"15","scoreB":"24"}
[scoreA] => 0
[scoreB] => 3
[winner] => {"name":"team23","user":[{"user_id":3,"gender":0,"avatar":"\/assets\/img\/qrcode.png","name":"admin3"},{"user_id":4,"gender":1,"avatar":"\/assets\/img\/qrcode.png","name":"admin4"}]}
[create_time] => 2025-05-30 15:24:28
[update_time] => 2025-05-30 15:54:34
],
)
$level 设定为第几轮数
出参:
return $math = Array(
[
[gym_id] => 1 //球馆id
[club_id] => 4 //俱乐部id
[act_id] => 1 //活动id
[game_id] => 1 //比赛id
[level] => 1 //轮次
[turn] => 1 //场次
[teamA] => {"name":"team01","user":[{"user_id":1,"gender":0,"avatar":"\/assets\/img\/qrcode.png","name":"Yam Lai Yan"},{"user_id":2,"gender":1,"avatar":"\/assets\/img\/qrcode.png","name":"Yuen Wai San"}]} //队伍A
[teamB] => {"name":"team23","user":[{"user_id":3,"gender":0,"avatar":"xjgH6E5VkQ","name":"Mo Ming Sze"},{"user_id":4,"gender":1,"avatar":"hSuH5AZVxt","name":"Chan Ka Keung"}]} //队伍B
],
)
如return $math 为空则比赛结束
*/
public function nextLevel(Game $game, array $maths, int $level): array;
// 奖金分配
public function prize($rank): array;
public function prize(array $rank): array;
}