www_fpvone_cn/application/index/service/RankingService.php

1172 lines
44 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* @Created by PhpStorm.
* @Author:Soar
* @Time:2023/11/15 16:28
*/
namespace app\index\service;
use app\admin\model\MatchContestant;
use app\common\model\MatchRanking;
use app\common\model\Players;
use app\common\model\User;
use JetBrains\PhpStorm\Deprecated;
class RankingService extends Service
{
const PROMOTED = 1;
public function __construct()
{
$this->rankingModel = new MatchRanking();
}
public function getRank($course = 32, $match_id)
{
switch ($course) {
case 32:
$limit = 64;
break;
case 16:
$limit = 32;
break;
case 8:
$limit = 8;
break;
case 4:
$limit = 4;
break;
case 1:
$limit = 99;
break;
}
$all_ranking_bast = $this->rankingModel
->where("match_id" , "eq", $match_id)
->where("course", "eq", $course)
->where("bast_performance", 1)
->field("player_name, course, match_id, promoted, fly_num, times, grouping, course, player_id, custom_sorting, group_score")
//->order("round_score")
->orderRaw("CASE fly_num
WHEN '3' THEN 1
WHEN '2' THEN 2
WHEN '1' THEN 3
WHEN 'DNF' THEN 4
ELSE 5
END")
->order('datetime_value', 'asc')
->limit($limit)
->select();
if (!empty($all_ranking_bast[0]->custom_sorting)) {
$last_ages = array_column($all_ranking_bast,'custom_sorting');
array_multisort($last_ages ,SORT_ASC,$all_ranking_bast);
}
return $all_ranking_bast;
}
public function getAllRank ($course = 32, $match_id)
{
$list = $this->rankingModel->where([
'course' => $course,
'match_id' => $match_id,
'promoted' => self::PROMOTED
])->field("player_name, course, match_id, promoted, fly_num, times, grouping, course, player_id, custom_sorting, group_score")
->order("group_score", "ASC")
->select();
if (!empty($list[0]->custom_sorting)) {
$last_ages = array_column($list,'custom_sorting');
array_multisort($last_ages ,SORT_ASC,$list);
}
return $list;
}
/**
* 根据赛事ID和赛程获取所有选手成绩 已废弃
* @Author:Soar
* @Time:2023/12/13 9:28
* @param $course 赛程
* @param $match_id 赛事id
* @return bool|\PDOStatement|string|\think\Collection
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
#[Deprecated]
public function getRankAll($course = 32, $match_id = 180)
{
if (empty($course)) {
$course = 32;
}
$row = $this->rankingModel->where([
'course' => $course,
'match_id' => $match_id,
])->field("player_name, course, match_id, promoted, fly_num, times, grouping, course, player_id, integral, finals_round, id")
->order("group_score", "ASC")
->select();
$players = new Players();
$user = new User();
foreach ($row as $vals) {
$user_info = $user->where(['member_number' => $vals->player_id])->find();
if (!empty($user_info)) {
$player_info = $players->where(['member_id' => $user_info->id])->find();
if (!empty( $player_info)) {
$vals->style_photo = $player_info->style_photo;
$vals->province = $player_info->province;
$vals->age = $player_info->age;
}
}
}
return $row;
}
/**
* 替换上面的接口
* 根据赛程信息和赛事ID 获取所有选手的信息
* @Author:Soar
* @Time:2023/12/13 9:27
* @param int $course 赛程
* @param int $match_id 赛事id
* @return array
*/
public function getRoundAllPlayer(int $course, int $match_id)
{
$data = $this->rankingModel
->Distinct(true)
->where('match_id', $match_id)
->where("course", $course)
->field('player_id, grouping, player_name')
->select();
$data = collection($data)->toArray();
if ($data) {
$players = new Players();
$user = new User();
foreach ($data as $key => $value) {
$user_info = $user->where(['member_number' => $value['player_id']])->find();
if ($user_info) {
$player_info = $players->where(['member_id' => $user_info->id])->find();
if ($player_info) {
$data[$key]['style_photo'] = $player_info->style_photo;
$data[$key]['province'] = $player_info->province;
$data[$key]['age'] = $player_info->age;
$data[$key]['player_name'] = $player_info->real_name;
}
}
}
}
return $data;
}
public function getMatchAll($match_id, $course = 32)
{
if (empty($course)) {
$course = 32;
}
return $this->rankingModel->where([
'course' => $course,
'match_id' => $match_id,
])->field("player_name, course, match_id, promoted, fly_num, times, grouping, course, player_id")
->select();
}
public function getSemiFinals($match_id)
{
$row = $this->rankingModel->where([
'course' => 4,
'match_id' => 180,
'promoted' => self::PROMOTED
])->field("player_name, course, match_id, promoted, fly_num, times, grouping, course, player_id")
->select();
$players = new Players();
$user = new User();
foreach ($row as $vals) {
$user_info = $user->where(['member_number' => $vals->player_id])->find();
$player_info = $players->where(['member_id' => $user_info->id])->find();
$vals->style_photo = $player_info->style_photo;
$vals->province = $player_info->province;
$vals->age = $player_info->age;
// 获取64进32强数据
$vals->achievement_list = $this->rankingModel->where([
'match_id' => $vals->match_id,
'player_id' => $vals->player_id,
'player_name' => $vals->player_name
])->field("course, times")->select();
}
return $row;
}
/**
* 获取分组选手信息以及成绩列表
* @Author:Soar
* @Time:2023/11/20 16:25
* @param $param
* @return bool|\PDOStatement|string|\think\Collection
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function getRankGroup($param)
{
$list = $this->rankingModel->where([
'course' => $param['course'],
'match_id' => $param['match_id'],
'grouping' => $param['group'],
'bast_performance' => 1
])->field("player_name, course, match_id, promoted, fly_num, times, grouping, player_id, other_round")
->select();
$players = new Players();
$user = new User();
foreach ($list as $val) {
$user_info = $user->where(['member_number' => $val->player_id])->find();
if (!empty($user_info)) {
$player_info = $players->where(['member_id' => $user_info->id])->find();
if (!empty($player_info)) {
if (!empty($player_info->style_photo)) {
$val->style_photo = $player_info->style_photo;
} else {
$val->style_photo = $player_info->player_pic;
}
$val->age = $player_info->age;
$val->province = $player_info->province;
} else {
$val->style_photo = null;
$val->age = null;
$val->province = null;
}
}
$val->achievement_list = $this->rankingModel->where([
'match_id' => $val->match_id,
'player_id' => $val->player_id,
'player_name' => $val->player_name,
])->field("course, times, fly_num, integral, other_round")->select();
}
$list_['items'] = $list;
$list_['round'] = $this->rankingModel->where("match_id", "eq", $param['match_id'])
->where("course", "eq", $param['course'])
->max("other_round");
return $list_;
}
public function getRankFinals($match_id,$name = 0)
{
$collections = $this->rankingModel->where('match_id', 'eq', $match_id)
->where("course", "eq", 4)
->where("bast_performance", 1)
->field("player_name, course, match_id, promoted, fly_num, times, grouping, course, player_id, other_round")
->orderRaw("CASE fly_num
WHEN '3' THEN 1
WHEN '2' THEN 2
WHEN '1' THEN 3
WHEN 'DNF' THEN 4
ELSE 5
END")
->order('datetime_value', 'asc')
->limit(4)
->select();
if($name == 1){
//名字加*号
foreach ($collections as &$v){
$str = $v['player_name'];
$strlen = mb_strlen($str,'UTF8');
if($strlen == 2){
$real_name=mb_substr($str,0,1,'UTF8').'*';
}
if($strlen > 2){
$tmp_str=mb_substr($str,0,$strlen-2,'UTF8').'*';
$real_name=$tmp_str.mb_substr($str,-1,1,'UTF8');
}
$v['player_name']= $real_name;
}
}
// exit;
// var_dump($collections);exit;
$player_ids = array_column($collections, "player_id");
if(empty($collections)) {
return [];
}
$players = new Players();
$user = new User();
foreach ($collections as $val) {
// 获取选手所有积分
$val->integral_sum = $this->rankingModel->where('match_id', 'eq', $match_id)
->where("course", "eq", 1)
->where("player_id", "eq", $val->player_id)
->where("other_round", "neq", "加赛")
->sum("integral");
$user_info = $user->where(['member_number' => $val->player_id])->find();
if (!empty($user_info)) {
$player_info = $players->where(['member_id' => $user_info->id])->find();
if (!empty($player_info)) {
if (!empty($player_info->style_photo)) {
$val->style_photo = $player_info->style_photo;
} else {
$val->style_photo = $player_info->player_pic;
}
$val->age = $player_info->age;
$val->province = $player_info->province;
} else {
$val->style_photo = null;
$val->age = null;
$val->province = null;
}
}
$val->achievement_list = $this->rankingModel->where([
'match_id' => $val->match_id,
'player_id' => $val->player_id,
'bast_performance' => 1
])->where("course", "neq", 1)
->field("player_name, fly_num, times, integral, finals_round, course, led_color, channel, grouping, other_round")
->order("other_round", "asc")
->select();
}
$sort = array_column($collections, "integral_sum");
array_multisort($sort, SORT_DESC, $collections);
// var_dump($collections[0]->toArray());exit;
foreach ($collections as $key => $values) {
// 根据积分总和把加赛的人分到一个数组里
// var_dump($values->player_id);exit;
$extra_series_arr[$values->integral_sum][$values->player_id] = $values;
}
// var_dump($extra_series_arr);exit;
// 如果数组不为空,如果没有加赛,那么肯定一个人一条数据
// 如果积分相同的两个人肯定数组的长度就会小于4就会进行下列的查询
if (!empty($extra_series_arr) && count($extra_series_arr) < 4) {
$extra_series_ranking = 0;
// 循环需要判断加赛的数组
foreach ($extra_series_arr as $value) {
// 如果该数组里只有一条数组,那么就说明该选手没有参与加赛
if (count($value) > 1) {
unset($fly_three);
unset($fly_two);
unset($fly_one);
unset($fly_dnf);
// 对此数组进行排列 查询该几人的加赛数据
$extra_series_player_ids = array_column($value, "player_id");
// 查询加赛的数据
$extra_series_list = $this->rankingModel->where('match_id', 'eq', $match_id)
->whereIn("player_id", $extra_series_player_ids)
->where("course", "eq", 1)
->where("other_round", "eq", "加赛")
->order("achievement", "ASC")
->select();
$player_num = 0;
// 因为当时没有用到 CASE fly_num
// WHEN '3' THEN 1
// WHEN '2' THEN 2
// WHEN '1' THEN 3
// WHEN 'DNF' THEN 4
// ELSE 5
// END 所以只能循环判断圈数,毕竟他们只上传最优的加赛成绩
// 把3>2>1>DNF的数据取出来
if (!empty($extra_series_list)) {
foreach ($extra_series_list as $val) {
if ($val->fly_num == 3) {
$fly_three[] = $val;
$player_num++;
} elseif ($val->fly_num == 2) {
$fly_two[] = $val;
$player_num++;
} elseif ($val->fly_num == 2) {
$fly_one[] = $val;
$player_num++;
} elseif ($val->fly_num == "DNF") {
$fly_dnf[] = $val;
$player_num++;
}
}
$winKey = 0;
// 下面取出每个圈数的人,根据 achievement 进行排名
// achievement 当初的计算方式 03:01.00
// 03 * 60 * 100;
// 01 * 100;
// 按照从小到大的排序
if (!empty($fly_three)) {
$sort = array_column($fly_three, "achievement");
array_multisort($sort, SORT_ASC, $fly_three);
foreach ($fly_three as $val) {
if ($winKey == 0) {
$value[$val->player_id]->win_extra_series = 1;
}
$new_collections[$extra_series_ranking] = $value[$val->player_id];
$extra_series_ranking++;
$winKey++;
}
}
if (!empty($fly_two)) {
$sort = array_column($fly_two, "achievement");
array_multisort($sort, SORT_ASC, $fly_two);
foreach ($fly_two as $val) {
if ($winKey == 0) {
$value[$val->player_id]->win_extra_series = 1;
}
$new_collections[$extra_series_ranking] = $value[$val->player_id];
$extra_series_ranking++;
$winKey++;
}
}
if (!empty($fly_one)) {
$sort = array_column($fly_one, "achievement");
array_multisort($sort, SORT_ASC, $fly_one);
foreach ($fly_one as $val) {
if ($winKey == 0) {
$value[$val->player_id]->win_extra_series = 1;
}
$new_collections[$extra_series_ranking] = $value[$val->player_id];
$extra_series_ranking++;
$winKey++;
}
}
if (!empty($fly_dnf)) {
$sort = array_column($fly_dnf, "achievement");
array_multisort($sort, SORT_ASC, $fly_dnf);
foreach ($fly_dnf as $val) {
if ($winKey == 0) {
$value[$val->player_id]->win_extra_series = 1;
}
$new_collections[$extra_series_ranking] = $value[$val->player_id];
$extra_series_ranking++;
$winKey++;
}
}
// 如果为空的成绩,那么就会走这里
if (count($value) != $player_num) {
foreach ($value as $val) {
if (!in_array($val,$new_collections)) {
array_push($new_collections, $val);
$extra_series_ranking++;
}
}
}
} else {
// 如果不存在加赛数据,那么就按照顺序排序
foreach ($value as $val) {
$new_collections[$extra_series_ranking] = $value[$val->player_id];
$extra_series_ranking++;
}
}
} else {
// 走到这里,说明他没有加赛,按照积分从高到低排序的顺序排序
$new_collections[$extra_series_ranking] = array_shift($value);
$extra_series_ranking++;
}
}
$collections = $new_collections;
}
// 获取所有选手的决赛记录
$collections['integral_list'] = $this->rankingModel->where('match_id', 'eq', $match_id)
->where("course", "eq", 1)
->whereIn("player_id", $player_ids)
->field("player_name, player_id, match_id, integral, finals_round, fly_num, times, other_round, other_round as finals_round")
->order("other_round")
->select();
// var_dump($collections['integral_list']);exit;
foreach ($collections['integral_list'] as $k => &$v){
// $v['player_name'] = '金志浩';
// var_dump($v['player_name']);
}
return $collections;
}
public function getRankFinalsold($match_id)
{
$collection = $this->rankingModel->where('match_id', 'eq', $match_id)
->where("course", "eq", 1)
->field("player_name, player_id, match_id, sum(integral) as integrals")
->order("integrals", "DESC")
->group("player_name")
->select();
if (count($collection) < 4) {
foreach ($collection as $value) {
$finalsId[] = $value->player_id;
}
$collections = $this->rankingModel->where('match_id', 'eq', $match_id)
->where("course", "eq", 4)
->whereNotIn("player_id", $finalsId)
->field("player_name, player_id, match_id, sum(integral) as integrals")
->order("integrals", "DESC")
->group("player_name")
->select();
$collection = array_merge($collection, $collections);
}
$players = new Players();
$user = new User();
foreach ($collection as $values) {
$user_info = $user->where(['member_number' => $values->player_id])->find();
if (!empty($user_info)) {
$player_info = $players->where(['member_id' => $user_info->id])->find();
if (!empty($player_info)) {
if (!empty($player_info->style_photo)) {
$values->style_photo = $player_info->style_photo;
} else {
$values->style_photo = $player_info->player_pic;
}
$values->age = $player_info->age;
$values->province = $player_info->province;
} else {
$values->style_photo = null;
$values->age = null;
$values->province = null;
}
}
$values->achievement_list = $this->rankingModel->where([
'match_id' => $values->match_id,
'player_id' => $values->player_id,
'player_name' => $values->player_name,
])
->order("finals_round", "ASC")
->field("player_name, fly_num, times, integral, finals_round")->select();
}
return $collection;
}
/**
* 获取单个飞手的本次比赛所有记录
* @Author:Soar
* @Time:2023/11/23 10:45
* @param $match_id 赛事ID
* @param $player_id 飞手ID
* @return void
*/
public function getPlayerRanking($match_id, $player_id)
{
// 获取决赛记录
$collection = $this->rankingModel->where('match_id', 'eq', $match_id)
->where("player_id", "eq", $player_id)
->where("course", "eq", 1)
->field("player_name, player_id, match_id, sum(integral) as integrals, other_round")
->order("integrals", "DESC")
->group("player_name")
->select();
// 如果没参加决赛 就获取所有参赛记录
$players = new Players();
$user = new User();
if (empty($collection)) {
$user_info = $user->where(['member_number' => $player_id])->find();
if (!empty($user_info)) {
$player_info = $players->where(['member_id' => $user_info->id])->find();
if (empty($player_info)) {
return null;
}
$playerInfo['player_id'] = $user_info->member_number;
if (!empty($player_info->style_photo)) {
$playerInfo['style_photo'] = $player_info->style_photo;
} else {
$playerInfo['style_photo'] = $player_info->player_pic;
}
$playerInfo['player_name'] = $player_info->real_name;
$playerInfo['age'] = $player_info->age;
$playerInfo['province'] = $player_info->province;
$playerInfo['achievement_list'] = $this->rankingModel->where([
'match_id' => $match_id,
'player_id' => $user_info->member_number,
])->order("finals_round", "ASC")
->field("player_name, fly_num, times, integral, finals_round, course, other_round")->select();
return $playerInfo;
}
}
$user_info = $user->where(['member_number' => $player_id])->find();
if (!empty($user_info)) {
foreach ($collection as $val) {
$player_info = $players->where(['member_id' => $user_info->id])->find();
if (empty($player_info)) {
return null;
}
if (!empty($player_info->style_photo)) {
$val->style_photo = $player_info->style_photo;
} else {
$val->style_photo = $player_info->player_pic;
}
$val->age = $player_info->age;
$val->province = $player_info->province;
$val->achievement_list = $this->rankingModel->where([
'match_id' => $match_id,
'player_id' => $user_info->member_number,
])->where("course", "<>", '1')
->order("finals_round", "ASC")
->field("player_name, fly_num, times, integral, finals_round, course")->select();
return $val;
}
}
}
/**
* 根据飞手ID、赛事ID、赛程获取飞手比赛记录
* @Author:Soar
* @Time:2023/12/12 13:19
* @param int $match_id 赛事id
* @param int $course 赛程
* @param int $player_id 飞手id
* @return void
*/
public function getRoundPlayer(int $match_id, int $course, int $player_id)
{
$res['other'] = $this->rankingModel
->where("match_id", $match_id)
->where("course", $course)
->where("player_id", $player_id)
//->orderRaw("CASE fly_num
// WHEN '3' THEN 1
// WHEN '2' THEN 2
// WHEN '1' THEN 3
// WHEN 'DNF' THEN 4
// ELSE 5
// END")
->order('other_round', 'asc')
->field("player_name, fly_num, times, integral, finals_round, course, other_round")
->select();
$res['best'] = $this->rankingModel
->where("match_id", $match_id)
->where("course", $course)
->where("player_id", $player_id)
->where("bast_performance", 1)
->field("player_name, fly_num, times, integral, finals_round, course")
->find();
$players = new Players();
$user = new User();
$user_info = $user->where(['member_number' => $player_id])->find();
if (!empty($user_info)) {
$player_info = $players->where(['member_id' => $user_info->id])->find();
if (!empty($player_info)) {
$res['player_info']['age'] = $player_info->age;
$res['player_info']['province'] = $player_info->province;
}
}
return $res;
}
/**
* 首页报名查询飞手所有成绩接口
* @Author:Soar
* @Time:2023/12/18 11:17
* @param $match_id
* @param $course
* @return void
*/
public function getIndexRanking($match_id, $course)
{
$list = $this->rankingModel
->where("match_id", $match_id)
->where("course", $course)
->where("bast_performance", 1)
->field("player_name, fly_num, times, integral, finals_round, course, custom_sorting, group_score, grouping, player_id")
->select();
if (!empty($list[0]->custom_sorting)) {
$last_ages = array_column($list,'custom_sorting');
array_multisort($last_ages ,SORT_ASC,$list);
}
return $list;
}
public function getIndexRankingScore($match_id, $course)
{
$list = $this->rankingModel
->where("match_id", $match_id)
->where("course", $course)
->where("bast_performance", 1)
->field("player_name, fly_num, times, integral, finals_round, course, custom_sorting, group_score, grouping, player_id")
->order("group_score", "ASC")
->select();
if (!empty($list[0]->custom_sorting)) {
$last_ages = array_column($list,'custom_sorting');
array_multisort($last_ages ,SORT_ASC,$list);
}
return $list;
}
public function getIndexPlayerRanking ($match_id, $player_id)
{
// 获取决赛记录
$collection = $this->rankingModel->where('match_id', 'eq', $match_id)
->where("player_id", "eq", $player_id)
->where("course", "eq", 1)
->field("player_name, player_id, match_id, sum(integral) as integrals, other_round")
->order("integrals", "DESC")
->group("player_name")
->select();
// 如果没参加决赛 就获取所有参赛记录
$players = new Players();
$user = new User();
if (empty($collection)) {
$user_info = $user->where(['member_number' => $player_id])->find();
if (!empty($user_info)) {
$player_info = $players->where(['member_id' => $user_info->id])->find();
if (empty($player_info)) {
return null;
}
$playerInfo['player_id'] = $user_info->member_number;
if (!empty($player_info->style_photo)) {
$playerInfo['style_photo'] = $player_info->style_photo;
} else {
$playerInfo['style_photo'] = $player_info->player_pic;
}
$playerInfo['player_name'] = $player_info->real_name;
$playerInfo['age'] = $player_info->age;
$playerInfo['province'] = $player_info->province;
$playerInfo['achievement_list'] = $this->rankingModel->where([
'match_id' => $match_id,
'player_id' => $user_info->member_number,
])->order("finals_round", "ASC")
->field("player_name, fly_num, times, integral, finals_round, course, other_round")->select();
return $playerInfo;
}
}
$user_info = $user->where(['member_number' => $player_id])->find();
if (!empty($user_info)) {
foreach ($collection as $val) {
$player_info = $players->where(['member_id' => $user_info->id])->find();
if (empty($player_info)) {
return null;
}
if (!empty($player_info->style_photo)) {
$val->style_photo = $player_info->style_photo;
} else {
$val->style_photo = $player_info->player_pic;
}
$val->age = $player_info->age;
$val->province = $player_info->province;
$val->achievement_list = $this->rankingModel->where([
'match_id' => $match_id,
'player_id' => $user_info->member_number,
])->where("course", "<>", '1')->where("bast_performance", 1)
->order("finals_round", "ASC")
->field("player_name, fly_num, times, integral, finals_round, course")->select();
return $val;
}
}
}
public function getOneRanking(int $match_id, int $player_id, int $course)
{
return $this->rankingModel
->where('match_id', 'eq', $match_id)
->where("course", $course)
->where("player_id", $player_id)
->where("bast_performance", 1)
->field("player_id, match_id, player_name, grouping, times, fly_num")
->find();
}
public function getFinalsIntegral(int $match_id, int $player_id)
{
return $this->rankingModel
->where('match_id', 'eq', $match_id)
->where("course", 1)
->where("player_id", $player_id)
->field("sum(integral) as sum_integral, player_id, match_id, player_name, grouping")
->group("player_id")
->find();
}
public function getPlayerInfo(int $player_id)
{
$user = new User();
$user_info = $user
->alias("a")
->where("a.member_number", $player_id)
->join("peewee_players b", "a.id = b.member_id", "LEFT")
->find()->toArray();
$player['age'] = $user_info['age'];
$player['player_id'] = $user_info['member_number'] ? $user_info['member_number'] : " ";
$player['province'] = $user_info['province'];
$player['city'] = $user_info['city'];
$player['district'] = $user_info['district'];
$player['birthday'] = $user_info['birthday'];
$player['name'] = $user_info['real_name'];
$player['gender'] = $user_info['gender'];
$player['member_id'] = $user_info['member_id'];
$player['player_pic'] = $user_info['style_photo'] ? $user_info['style_photo'] : $user_info['player_pic'];
// if (!empty($player['player_pic'])) {
// $player['player_pic'] = config("upload.cdnurl") ? config("upload.cdnurl").$player['player_pic'] : config("upload.uploadurl").$player['player_pic'];
// }
return $player;
}
public function getSortRanking ($player_ids, $match_id, $player_id, $sort, $course)
{
if ($course == 1) {
$sort_list = $this->rankingModel
->where("match_id", $match_id)
->where("course", $course)
->whereIn("player_id", $player_ids)
->group("player_id")
->field("player_id, match_id, player_name, led_color, channel, sum(integral) as sum_integral, grouping")
->order("sum_integral", "DESC")
->select();
} else {
$sort_list = $this->rankingModel
->where("match_id", $match_id)
->where("bast_performance", 1)
->where("course", $course)
->whereIn("player_id", $player_ids)
->orderRaw("CASE fly_num
WHEN '3' THEN 1
WHEN '2' THEN 2
WHEN '1' THEN 3
WHEN 'DNF' THEN 4
ELSE 5
END")
->order('datetime_value', 'asc')
->select();
}
$sort_list = collection($sort_list)->toArray();
$ids = array_column($sort_list, "player_id");
if (in_array($player_id, $ids)) {
foreach ($sort_list as $value) {
if ($player_id != $value['player_id']) {
++$sort;
} else {
return ++$sort;
}
}
} else {
foreach ($player_ids as $key => $val) {
foreach ($ids as $value) {
if ($val == $value) {
unset($player_ids[$key]);
}
}
}
sort($player_ids);
$sort = count($ids) + $sort;
switch ($course) {
case 1:
$course = 4;
break;
case 4:
$course = 8;
break;
case 8:
$course = 16;
break;
case 16:
$course = 32;
break;
}
return $this->getSortRanking($player_ids, $match_id, $player_id, $sort, $course);
}
}
/**
* 获取最优数据
* @Author:Soar
* @Time:2023/12/22 15:20
* @param int $match_id
* @param int $player_id
* @return array
*/
public function getPlayerBast(int $match_id, int $player_id, $user)
{
// 获取参赛所有选手
$player_ids = $this->rankingModel
->where('match_id', 'eq', $match_id)
->where("course", 32)
->where("bast_performance", 1)
->field("player_id")
->select();
// 先判断是否参与比赛
$isNull = $this->rankingModel->whereNull("times")
->where("course", 32)
->where('match_id', 'eq', $match_id)
->where("bast_performance", 1)
->where("player_id", $player_id)
->find();
if ($isNull) {
$bast_info['sort'] = 0;
return $bast_info;
}
$player_ids = collection($player_ids)->toArray();
$ids = array_column($player_ids, "player_id");
// 获取决赛的数据
$sort = $this->getSortRanking($ids, $match_id, $player_id, 0, 1);
// $finals_info = collection($this->getRankFinals($match_id))->toArray();
// unset($finals_info['integral_list']);
// 排名之前查询是否有加赛
// $bast_info['course'] = "决赛";
// $bast_info['sort'] = array_search($player_id, array_column($finals_info, 'player_id')) + 1;
$bast_info['sort'] = $sort;
//$array_column = array_column($finals_info, "player_id");
/* if (!in_array($player_id, $array_column)) {
$bast = $this->rankingModel
->where('match_id', 'eq', $match_id)
->where("player_id", $player_id)
->where("bast_performance", 1)
->order('id', "DESC")
->find();
$bast_info['course'] = $bast->course;
$bast_info['sort'] = $bast->custom_sorting ? $bast->custom_sorting : $bast->group_score;
}
*/
// 查询是否属于青年组
$today = date('Y-m-d'); // 当前日期
$previousDate = date('Y-01-01', strtotime('-15 years', strtotime($today))); // 十五年前的日期
if ($previousDate <= $user['birthday']) {
// 查询青年组符合年龄的
$bastYoung = $this->getYoungSort($previousDate, $match_id, $player_id, true);
$bast_info['young_sort'] = $bastYoung;
}
return $bast_info;
}
public function getYoungSort($date, $match_id, $player_id, $isApi = false)
{
// 获取所有这个年龄段的飞手
$players = new Players();
// 按照生日日期获取所有参赛选手
$players_list = $players
->alias("a")
->join('user users', 'users.id = a.member_id', 'LEFT')
->where("a.birthday", ">=", $date)
->field("a.id, users.member_number, a.birthday")
->order("a.birthday")
->select();
$ids = array_column($players_list, "id");
// 获取所有参赛选手
$matchContestant = new \app\common\model\MatchContestant();
// 去报名表查询所有参赛的飞手
$match_list = $matchContestant->whereIn("player_id", $ids)
->where("match_id", "eq", $match_id)
->select();
// 根据参赛选手 加上player_member_id
foreach ($match_list as $vals) {
foreach ($players_list as $values) {
if ($vals->player_id == $values->id) {
$vals->player_member_id = $values->member_number;
break;
}
}
}
$member_number_ids = array_column($match_list, "player_member_id");
// 根据User表中的 member_number 获取比赛信息 权重 决赛 > 半决赛 > 8强 > 16强 > 32强 > 预选赛
// 决赛数据
$rankingService = new \app\index\service\RankingService();
$achievement['finals'] = $rankingService->getRankFinals($match_id);
if (empty($achievement['finals'])) {
if ($isApi) {
return [];
}
$this->error("决赛暂未结束");
}
unset($achievement['finals']['integral_list']);
// 遍历查询是否有
foreach ($achievement['finals'] as $key => $item) {
if (!in_array($item->player_id, $member_number_ids)) {
unset($achievement['finals'][$key]);
} else {
unset($member_number_ids[array_search($item->player_id, $member_number_ids)]);
}
}
$toArray = collection($achievement['finals'])->toArray();
$array_column = array_column($toArray, "player_id");
if (in_array($player_id,$array_column )) {
return array_search($player_id, array_column($toArray, 'player_id')) + 1;
}
// 初始化model
$matchRanking = new MatchRanking();
// 查询 进入半决赛的成绩
$achievement['semifinal'] = $matchRanking->whereIn("player_id", $member_number_ids)
->where("match_id", "eq", $match_id)
->where("bast_performance", 1)
->where("course", "eq", 4)
->order("group_score")
->select();
$toArray = collection($achievement['semifinal'])->toArray();
$array_column = array_column($toArray, "player_id");
if (in_array($player_id,$array_column )) {
return array_search($player_id, array_column($toArray, 'player_id')) + 1;
}
foreach ($achievement['semifinal'] as $val) {
unset($member_number_ids[array_search($val->player_id, $member_number_ids)]);
}
// 查询 进入8强的成绩
$achievement['final_eight'] = $matchRanking->whereIn("player_id", $member_number_ids)
->where("match_id", "eq", $match_id)
->where("bast_performance", 1)
->where("course", "eq", 8)
->order("group_score")
->select();
$toArray = collection($achievement['final_eight'])->toArray();
$array_column = array_column($toArray, "player_id");
if (in_array($player_id,$array_column )) {
return array_search($player_id, array_column($toArray, 'player_id')) + 1;
}
foreach ($achievement['final_eight'] as $val) {
unset($member_number_ids[array_search($val->player_id, $member_number_ids)]);
}
// 查询 进入16强数据
$achievement['final_sixteen'] = $matchRanking->whereIn("player_id", $member_number_ids)
->where("match_id", "eq", $match_id)
->where("bast_performance", 1)
->where("course", "eq", 16)
->order("group_score")
->select();
$toArray = collection($achievement['final_sixteen'])->toArray();
$array_column = array_column($toArray, "player_id");
if (in_array($player_id,$array_column )) {
return array_search($player_id, array_column($toArray, 'player_id')) + 1;
}
foreach ($achievement['final_sixteen'] as $val) {
unset($member_number_ids[array_search($val->player_id, $member_number_ids)]);
}
// 查询 进入32强数据
$achievement['final_thirty_two'] = $matchRanking->whereIn("player_id", $member_number_ids)
->where("match_id", "eq", $match_id)
->where("bast_performance", 1)
->where("course", "eq", 32)
->order("group_score")
->select();
$toArray = collection($achievement['final_thirty_two'])->toArray();
$array_column = array_column($toArray, "player_id");
if (in_array($player_id,$array_column )) {
return array_search($player_id, array_column($toArray, 'player_id')) + 1;
}
foreach ($achievement['final_thirty_two'] as $val) {
unset($member_number_ids[array_search($val->player_id, $member_number_ids)]);
}
return 0;
}
/**
* 计时换算成毫秒
* @Created by PhpStorm.
* @Author:Soar
* @Time:2023/11/15 15:11
* @param $times
* @return float|int|mixed|string
*/
private function getAllMs($times)
{
$minute_ms = 0;
$second_ms = 0;
$ms = 0;
// 截取最后的毫秒
$allTime = explode(".", $times);
if (count($allTime) != 2) {
$this->error("成绩格式有误!");
}
$ms = $allTime[1];
// 截取分秒
$minute = explode(":", $allTime[0]);
if (count($minute) != 2) {
$this->error("成绩格式有误!");
}
if ($minute[0] != 00) {
// 如果第一位不是 00 则代表每分钟
$minute_ms = $minute[0] * 60 * 100;
}
if ($minute[1] != 00) {
$second_ms = $minute[1] * 100;
}
return $ms + $second_ms + $minute_ms;
}
}