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 quit() { $model = $this->model->get($this->request->param('id')); if (empty($model)) { $this->error(__('No rows were found')); } if ($model['status'] > 1) { $this->error('活动已开始或结束,不能退出'); } if ($model['status'] == -1) { $this->error('活动已取消'); } $join = GameJoin::where('user_id', $this->auth->id)->find(); if (empty($join) || $join['status'] == -1) { $this->error('未报名或已取消'); } if (date('Y-m-d H:i:s') >= $model['quit_time']) { $this->error('已超过免费退出时间'); } Db::startTrans(); try { $join->save(['status' => -1]); if ($join['status'] == 1) { $order = $this->model->paid()->where('id', $join->order_id)->lock(true)->find(); if (!$order) { $this->error('订单不存在或不可退款'); } $orderRefund = new OrderRefund($order); $orderRefund->fullRefund(NULL, [ 'refund_type' => 'back', 'remark' => '用户自行退出活动' ]); } (new Message())->save([ 'type' => 1, 'name' => '系统消息', 'avatar' => '', 'from_id' => 0, 'target_id' => $join->user_id, 'content' => json_encode([ 'topic' => '退出', 'content' => '已退出 ' . $model['name'] . ' 活动', 'game_id' => $model->id ]) ]); Db::commit(); } catch (ValidateException | PDOException | Exception $e) { Db::rollback(); $this->error($e->getMessage()); } $this->success('Success'); } // 取消活动 public function cancle() { $model = $this->model->get($this->request->param('id')); if (empty($model)) { $this->error(__('No rows were found')); } $member = Menber::get(['club_id' => $model->club_id, 'user_id' => $this->auth->id]); if (empty($member) || $member->role < 2) { $this->error('无权取消活动'); } if ($model['pid'] != 0) { $this->error('不能取消子活动'); } if ($model['status'] > 1) { $this->error('活动已开始或结束,不能取消'); } if ($model['status'] == -1) { $this->error('活动已取消'); } Db::startTrans(); try { $model->save(['status' => -1]); (new \app\admin\model\zy\game\Game)->where('pid', $model->id)->update(['status' => -1]); //取消成功,进入退款流程并通知用户 $join = GameJoin::where('game_id', $model['id'])->select(); $msgs = []; foreach ($join as $j) { $j->save(['status' => -1]); if ($j['status'] == 1) { $order = $this->model->paid()->where('id', $j->order_id)->lock(true)->find(); if (!$order) { $this->error('订单不存在或不可退款'); } $orderRefund = new OrderRefund($order); $orderRefund->fullRefund(NULL, [ 'refund_type' => 'back', 'remark' => '活动取消,全额退款' ]); } $msgs[] = [ 'type' => 1, 'name' => '系统消息', 'avatar' => '', 'from_id' => 0, 'target_id' => $j->user_id, 'content' => json_encode([ 'topic' => '评论', 'content' => $model['name'] . ' 活动已取消', 'game_id' => $model->id ]) ]; } (new Message())->insertAll($msgs);; Db::commit(); } catch (ValidateException | PDOException | Exception $e) { Db::rollback(); $this->error($e->getMessage()); } $this->success('Success'); } // 赛制说明 public function describe() { $params = $this->request->param(); Db::startTrans(); try { $game = $this->model->get($params['id'] ?? NULL); if (empty($game)) { $this->error('比赛不存在'); } $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}"); } $describe = $format->describe(); Db::commit(); } catch (ValidateException | PDOException | Exception $e) { Db::rollback(); $this->error($e->getMessage(), $e); } $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']); } else { $query->where('status', 1); } 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 setUser() { $params = $this->request->param(); $user = Participant::get($params['id'] ?? NULL); if (empty($user)) { $this->error('用户不存在'); } $game = $this->model->get($user['game_id']); $referee = explode(',', $game['referee']); //裁判 $menber = Menber::where(['club_id' => $game['club_id'], 'user_id' => $this->auth->id])->where('role', '>', 1)->find(); if (empty($menber) && !in_array($this->auth->id, $referee)) { $this->error('您没有权限'); } $update['status'] = intval($params['status']); if ($update['status'] > 1 || $update['status'] < -1) { $this->error('status错误'); } if (!empty($params['mark'])) { $update['mark'] = $params['mark']; } $user->save($update); $this->success('修改成功'); } // 获取比赛匹配列表 public function macthList() { $params = $this->request->param(); Db::startTrans(); try { $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('比赛时间已结束'); } $matchs = GameMatch::where('game_id', $game['id'])->select(); if (empty($matchs)) { $participant = Participant::where('game_id', $game['id'])->where('status', 1)->select(); $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 $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); } $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')) ->where('status', '>', -1)->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(); } $undone = GameMatch::where('level', $match['level'])->where('winner', null)->count(); 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(); $this->error($e->getMessage(), $e); } $this->success('Success'); } // 团队排名 public function teamRank() { $params = $this->request->param(); $game = $this->model->get($params['game_id']) ?? null; if (empty($game)) { $this->error('比赛不存在'); } $match = GameMatch::field('*,GREATEST(scoreA,scoreB) as score')->where('game_id', $game['id'])->order(['level' => 'desc', 'score' => 'desc'])->select(); $teams = []; $rank = 1; foreach ($match as $m) { $win = json_decode($m['winner'] ?? '', true); if (empty($win)) { // 未决出胜负的按暂时得分 高的队伍排名靠前 $teamA = json_decode($m['teamA'] ?? '', true); $teamB = json_decode($m['teamB'] ?? '', true); if (($m['scoreA'] >= $m['scoreB'])) { $teamA['rank'] = $rank; $teamB['rank'] = $rank + 1; $teams[] = $teamA; $teams[] = $teamB; $rank += 2; } else { $teamB['rank'] = $rank; $teamA['rank'] = $rank + 1; $teams[] = $teamB; $teams[] = $teamA; $rank += 2; } } else { $win['rank'] = $rank; $rank++; $teams[] = $win; } } $this->success('Success', $teams); } // 个人排名 public function rank() { $params = $this->request->param(); $game = $this->model->get($params['game_id']) ?? null; if (empty($game)) { $this->error('比赛不存在'); } $list = Participant::where('game_id', $game['id'])->where('status', '>', -1)->order('score', 'desc')->select(); $rank = 1; foreach ($list as &$l) { $l['rank'] = $rank; $rank += 1; } $this->success('Success', $list); } }