feat(shopro): 优化比赛模块功能和流程

- 新增赛制说明接口和页面
- 实现比赛列表和对阵详情页面
- 添加比赛计分功能
- 优化比赛报名和参赛者分配逻辑
- 重构赛制配置和对阵安排方法
This commit is contained in:
2025-05-27 08:51:27 +08:00
parent b7023e7ab3
commit 94a540ac96
4 changed files with 207 additions and 78 deletions

View File

@@ -199,68 +199,7 @@ class Game extends Base
$this->success('Success');
}
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);
}
// 赛制说明
public function describe()
{
$params = $this->request->param();
@@ -287,4 +226,160 @@ class Game extends Base
$this->success('Success', $describe);
}
public function macthList()
{
$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('比赛时间已结束');
// }
$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}");
}
$format = new $gameClass;
if (!$format instanceof \format\GameInterface) {
throw new Exception("赛制配置错误,请联系管理人员: {$gameClass}");
}
$matchs = $format->match($game, $participant);
$res = (new GameMatch)->insertAll($matchs);
}
$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) {
$m['teamA'] = json_decode($m['teamA'], true);
$m['teamB'] = json_decode($m['teamB'], true);
}
$this->success('Success', $matchs);
}
public function getMacth()
{
$params = $this->request->param();
$m = GameMatch::get($params['id'] ?? NULL);
if (empty($m)) {
$this->error('对阵记录不存在');
}
$m['teamA'] = json_decode($m['teamA'] ?? '[]', true);
$m['teamB'] = json_decode($m['teamB'] ?? '[]', true);
$m['round1'] = json_decode($m['round1'] ?? '[]', true);
$m['round2'] = json_decode($m['round2'] ?? '[]', true);
$m['round3'] = json_decode($m['round3'] ?? '[]', true);
$m['winner'] = json_decode($m['winner'] ?? '[]', true);
return $this->success('Success', $m);
}
public function scoring()
{
$params = $this->request->param();
Db::startTrans();
try {
$match = GameMatch::get($params['id'] ?? NULL);
if (empty($match)) {
$this->error('对阵记录不存在');
}
$game = $this->model->get($match['game_id']);
$referee = explode(',', $game['referee']);
if (empty($referee)) {
$this->error('请先设置裁判');
}
if (!in_array($this->auth->id, $referee)) {
$this->error('不是裁判无权计分');
}
if (empty($params['round']) || ($params['round'] != 1 && $params['round'] != 2 && $params['round'] != 3)) {
$this->error('回合(局)数错误');
}
$round = 'round' . $params['round'];
if (!empty($match->$round)) {
$this->error('请勿重复提交');
}
$match->$round = json_encode([
'addedA' => $params['addedA'], //队伍B让分
'addedB' => $params['addedB'], //队伍A让分
'scoreA' => $params['scoreA'], //队伍A得分
'scoreB' => $params['scoreB'], //队伍B得分
]);
$totalA = $params['scoreA'] + $params['addedA'];
$totalB = $params['scoreB'] + $params['addedB'];
if ($totalA > $totalB) {
$match->scoreA = $match->scoreA + 1; //队伍A回合得分加1
} else if ($totalA < $totalB) {
$match->scoreB = $match->scoreB + 1; //队伍B回合得分加1
} else {
$this->error('回合(局)内分数不能相等');
}
if (!empty($match->round1) && !empty($match->round2) && !empty($match->round3)) { //3局结束
$match->winner = $match->scoreA > $match->scoreB ? $match->teamA : $match->teamB;
}
$match->save();
// 更新用户得分
$teamA = json_decode($match->teamA, true);
$teamAuser = Participant::where('game_id', $game['id'])->where('user_id', 'IN', array_column($teamA['user'], 'user_id'))->select();
// dd($teamAuser);
foreach ($teamAuser as $u) {
$u->team = $teamA['name']; //队伍名
$u->score = $totalA; //得分
$u->net_score = $params['scoreA']; //净得分
$u->save();
}
$teamB = json_decode($match->teamB, true);
$teamBuser = Participant::where('game_id', $game['id'])->where('user_id', 'IN', array_column($teamB['user'], 'user_id'))->select();
foreach ($teamBuser as $u) {
$u->team = $teamB['name']; //队伍名
$u->score = $totalA; //得分
$u->net_score = $params['scoreA']; //净得分
$u->save();
}
Db::commit();
} catch (ValidateException | PDOException | Exception $e) {
Db::rollback();
$this->error($e->getMessage(), $e);
}
$this->success('Success');
}
}

View File

@@ -15,15 +15,18 @@ class Game17 implements GameInterface
由前几名按比例瓜分 (可调 Default40%/30%/20%/10%)';
}
// 对阵安排
// 首轮对阵安排
public function match($game, $users): array
{
$math = $team = [];
$utotal = count($users);
//队伍分配
for ($i = 0; $i < $utotal; $i++) {
for ($j = $i + 1; $j < $utotal; $j++) {
$team[] = json_encode([
'name' => 'team' . $i . $j,
$teamName = 'team' . $i . $j;
// $team[] = json_encode([
$team[] = ([
'name' => $teamName,
'user' => [
[
'user_id' => $users[$i]['user_id'],
@@ -39,28 +42,44 @@ class Game17 implements GameInterface
]
]
]);
$users[$i]['team'] = $users[$j]['team'] = $teamName;
}
}
// dd($team);
$ttotal = count($users);
// 对阵分配
$ttotal = count($team);
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;//一个人不能同时在两边队伍
}
$math[] = [
'gym_id' => $game['gym_id'],
'club_id' => $game['club_id'],
'act_id' => $game['act_id'],
'game_id' => $game['id'],
'level' => 1,
'turn' => $n,
'teamA' => $team[$i],
'teamB' => $team[$j],
'level' => 1, //轮次
'turn' => $n, //场次
'teamA' => json_encode($team[$i]), //队伍A
'teamB' => json_encode($team[$j]), //队伍B
];
// $ta =array_column($team[$i]['user'],'user_id');
// $tb =array_column($team[$j]['user'],'user_id');
// df("第 $n 场:".$ta[0].'/'.$ta[1].' VS '.$tb[0].'/'.$tb[1]);
$n++;
}
}
// dd($math);
$this->next($game, $math);
return $math;
}
// 下一轮对阵安排
public function next($game, $math): array
{
$math = [];
return $math;
}

View File

@@ -8,16 +8,20 @@ namespace format;
2. 规则类型1八人转2超八转3混双转4固搭转5固定擂6活动擂7转转8分区转9擂台赛10守擂赛11追分赛12固搭追分赛13大循环群内赛14两队PK赛15战队淘汰赛16单项淘汰赛17分区循环淘汰赛
3. 需要引入新赛制请创建新的类文件,类文件需要实现GameInterface接口,并且文件名为`Game + 团队类型代码 + 规则类型代码.php`。
如:赛制为双打八人则文件为format/Game11.php。
赛制为单打擂台赛则文件为format/Game29.php。
如:赛制为双打+转则文件为format/Game17.php。
赛制为单打+擂台赛则文件为format/Game29.php。
*/
interface GameInterface
{
// 赛制说明
public function describe(): string;
// 对阵安排
public function match($game,$users): array;
// 首轮对阵安排
public function match($game, $users): array;
// 下一轮对阵安排
public function next($game, $math): array;
// 奖金分配
public function prize($rank): array;

View File

@@ -587,24 +587,35 @@ if (!function_exists('collection')) {
}
}
}
if (!function_exists('dd')) {
/**
* 调试打印
*/
function dd(...$params)
{
echo '
';
foreach ($params as $p) {
print_r($p);
echo '
';
echo PHP_EOL;
}
// echo '</pre>';
die;
}
}
if (!function_exists('df')) {
/**
* 调试打印
*/
function df(...$params)
{
foreach ($params as $p) {
print_r($p);
echo PHP_EOL;
}
}
}
if (!function_exists('getsql')) {
/**
* 调试打印