380 lines
14 KiB
C#
380 lines
14 KiB
C#
using Assets.Scenes.Ride.Scripts.Model.RiderModels;
|
||
using Assets.Scripts;
|
||
using Assets.Scripts.Apis;
|
||
using Assets.Scripts.Apis.Models;
|
||
using Mapbox.Utils;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace Assets.Scenes.Ride.Scripts.Model.CyclingModels
|
||
{
|
||
public class CompetitionModel : BaseCycling
|
||
{
|
||
private Vector2d firstPoint { get; set; }
|
||
public CompetitionRankingModel competitionRankingModel { get; set; }
|
||
|
||
private MapCompetitionApi mapCompetitionApi;
|
||
|
||
public MapCompetition mapCompetition;
|
||
|
||
private List<CompetitionRankingResultModel> mapWorkoutRecordRankings = new List<CompetitionRankingResultModel>();
|
||
/// <summary>
|
||
/// 第一个到终点的时间
|
||
/// </summary>
|
||
private DateTime? FirstEndTime { get; set; }
|
||
|
||
public override bool CanPause => false;
|
||
|
||
public override string ShowTime
|
||
{
|
||
get
|
||
{
|
||
//if (CanStart())
|
||
{
|
||
var second = Math.Ceiling((Now - mapCompetition.StartTime.ToLocalTime()).TotalSeconds);
|
||
|
||
var ts = TimeSpan.FromSeconds(second);
|
||
return $"{ (int)ts.TotalHours }:{ts:mm}:{ts:ss}";
|
||
}
|
||
return "00:00:00";
|
||
}
|
||
}
|
||
AbstractPlayer currentPlayer;
|
||
public CompetitionModel(Route route1, RouteResultParam param,AbstractPlayer player)
|
||
: base(route1, CyclingModel.Competition)
|
||
{
|
||
currentPlayer = player;//当前选中用户
|
||
mapCompetitionApi = new MapCompetitionApi();
|
||
//param.EndDistance = 18;
|
||
recorderData = new RecorderDataModel()
|
||
{
|
||
CurrentUser = App.CurrentUser,
|
||
//BelongUserId = PubCommData.CurrentUser.Id,
|
||
Competitionid = param.CompetitionId??0,
|
||
ContinueIndex = param.ContinueIndex == null ? 0 : (int)param.ContinueIndex + 1,
|
||
ContinueMark = string.IsNullOrEmpty(param.ContinueMark) ? Guid.NewGuid().ToString() : param.ContinueMark,
|
||
EndDistance = param.EndDistance == 0 ? 0 : param.EndDistance,
|
||
IsCompleted = false,
|
||
IsNeedRanking = true,
|
||
CyclingState = CyclingStateEnum.Prepare,
|
||
//ManufacturerId = ManufacturerId,
|
||
CurrentRoute = route,
|
||
CurrentRouteStartDistance = param.EndDistance == 0 ? 0 : param.EndDistance,
|
||
//DeviceNumber = DeviceNumber,
|
||
//AntModelId = antModelId
|
||
};
|
||
|
||
IsRecord = true;
|
||
|
||
riders = new List<BaseRider>();
|
||
var rider = new OnlineRiderModel()
|
||
{
|
||
NickName = App.CurrentUser.Nickname,
|
||
UserId = App.CurrentUser.Id,
|
||
WxHeadImg = App.CurrentUser.WxHeadImg,
|
||
EndDistance = param.EndDistance == 0 ? 0 : param.EndDistance
|
||
};
|
||
if (recorderData.CurrentRouteStartDistance > 0)
|
||
{
|
||
rider.Point = _turfHelper.Along(param.EndDistance);
|
||
}
|
||
else
|
||
{
|
||
rider.Point = new GeoJSON.Net.Geometry.GeographicPosition(this.route.RouteInstance.Point[0], this.route.RouteInstance.Point[1]);
|
||
}
|
||
//riders.Add(rider);//剔除自己
|
||
|
||
var result = mapCompetitionApi.GetById(param.CompetitionId??0);
|
||
if (result.result == false || result.data == null)
|
||
{
|
||
return;
|
||
}
|
||
mapCompetition = result.data;
|
||
FirstEndTime = mapCompetition.FirstEndTime;
|
||
}
|
||
|
||
protected override void BeforeRunAfterPakcData()
|
||
{
|
||
//#region 设置第一名的坐标
|
||
//if (!FirstEndTime.HasValue)
|
||
//{
|
||
// var firstItem = base.realRiders.OrderByDescending(w => w.NextDistance).FirstOrDefault();
|
||
// if (firstItem != null)
|
||
// {
|
||
// firstPoint = new Vector2d(firstItem.Point.Latitude, firstItem.Point.Longitude);
|
||
// }
|
||
//}
|
||
//#endregion
|
||
|
||
var userList = MapUDPService.GetCompetitionOnlineUsers(recorderData.Competitionid).OrderByDescending(u => u.IsCompleted).ThenByDescending(d => d.EndDistance);
|
||
riders.RemoveAll(r => r.IsSelf == false);
|
||
int index = 0;
|
||
List<CompetitionRankingSortModel> sort = new List<CompetitionRankingSortModel>();
|
||
|
||
if (userList.Any())
|
||
{
|
||
var currentDistance = currentPlayer.TotalDistance;
|
||
foreach (var item in userList)
|
||
{
|
||
#region 记录第一个冲线的时间
|
||
if (item.IsCompleted && !FirstEndTime.HasValue)
|
||
{
|
||
FirstEndTime = Now;
|
||
}
|
||
#endregion
|
||
|
||
if (item.IsSelf == false)
|
||
{
|
||
riders.Add(new OnlineRiderModel
|
||
{
|
||
NickName = item.Name,
|
||
UserId = item.Id,
|
||
WxHeadImg = item.HeadImage,
|
||
Point = new GeoJSON.Net.Geometry.GeographicPosition(item.Point[0], item.Point[1]),
|
||
EndDistance = item.EndDistance,
|
||
PreDistance = item.PreDistance,
|
||
Power = item.Power,
|
||
HeartRate = item.HeartRate,
|
||
Cadence = item.Cadence,
|
||
TotalTicks = item.TotalTicks,
|
||
});
|
||
}
|
||
|
||
index++;
|
||
double near = Math.Round(item.EndDistance - currentDistance,2); //Math.Round(TurfHelper.GetDistances(point, item.Point), 2);
|
||
|
||
sort.Add(new CompetitionRankingSortModel
|
||
{
|
||
Index = index,
|
||
Name = item.Name,
|
||
Near = near,
|
||
KGWeight = Math.Round(item.WeightKg, 2).ToString(),
|
||
Speed = Math.Round(item.Speed, 1),
|
||
CountryImg = item.Country,//ConfigHelper.Host + $"User/GetCountryImg?userid={ item.Id }",
|
||
UserId = item.Id,
|
||
IsSelf = item.IsSelf,
|
||
Headimage =item.HeadImage
|
||
});
|
||
|
||
}
|
||
}
|
||
|
||
#region 骑行结束,比赛结果展示数据
|
||
var results = new List<CompetitionResultModel>();
|
||
if (recorderData.IsCompleted || recorderData.Saved || mapCompetition.HasRecord)
|
||
{
|
||
var users1 = userList.Where(d => d.IsCompleted || d.Saved);
|
||
if ((users1.Any() && users1.Any(d => mapWorkoutRecordRankings.All(u => u.UserId != d.Id)))
|
||
|| mapWorkoutRecordRankings.All(u => u.UserId != recorderData.BelongUserId))
|
||
{
|
||
var rrr = mapCompetitionApi.GetRankingList(recorderData.Competitionid);
|
||
if (rrr.result)
|
||
{
|
||
mapWorkoutRecordRankings = rrr.data;
|
||
}
|
||
}
|
||
foreach (var ranking in mapWorkoutRecordRankings)
|
||
{
|
||
//var ranking = mapWorkoutRecordRankings.SingleOrDefault(r => r.UserId == item.Id);
|
||
|
||
var createTime = "";
|
||
if (ranking != null)
|
||
{
|
||
createTime = ranking.CreateTime; //.ToString("yyyy-MM-dd HH:mm:ss");
|
||
}
|
||
results.Add(new CompetitionResultModel
|
||
{
|
||
Index = ranking.Index,
|
||
Name = ranking.NickName,
|
||
UserId = ranking.UserId,
|
||
Weight = ranking?.Weight ?? 0,
|
||
WeightKg = ranking?.WeightKg ?? 0,
|
||
//Time = ranking?.TotalTime ?? "",
|
||
Power = ranking?.AveragePower ?? 0,
|
||
BicycleWeight = ranking?.BicycleWeight ?? 0,
|
||
HeartRate = ranking?.AverageHeartRate ?? 0,
|
||
DeviceType = ranking?.DeviceType ?? "",
|
||
CreateTime = createTime,
|
||
TripTime = ranking?.TripTime ?? "",
|
||
CompleteTime = ranking?.CompleteTime,
|
||
Gap = ranking?.Gap ?? "",
|
||
Country = ranking.Country,
|
||
WxHeadImg = ranking.WxHeadImg
|
||
});
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 骑行实时排名&比赛结果列表 取前六名
|
||
|
||
List<CompetitionTopModel> CompetitionTopList = new List<CompetitionTopModel>();
|
||
|
||
var resultList =results.Take(6);
|
||
var count = 6 - resultList.Count();
|
||
//先取比赛结果
|
||
foreach (var item in resultList)
|
||
{
|
||
CompetitionTopList.Add(new CompetitionTopModel
|
||
{
|
||
Index = item.Index,
|
||
TripTime = item.TripTime,
|
||
Country = item.Country,
|
||
Name = item.Name,
|
||
UserId = item.UserId,
|
||
Head = item.WxHeadImg,
|
||
});
|
||
}
|
||
|
||
if (count > 0)
|
||
{
|
||
var ss = sort.Where(c => !resultList.Select(p => p.UserId).Contains(c.UserId)).Take(count);
|
||
foreach (var item in ss)
|
||
{
|
||
CompetitionTopList.Add(new CompetitionTopModel
|
||
{
|
||
Index = item.Index,
|
||
TripTime = "———",
|
||
Country = item.CountryImg,
|
||
Name = item.Name,
|
||
UserId = item.UserId,
|
||
Head = item.Headimage,
|
||
});
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
competitionRankingModel = new CompetitionRankingModel
|
||
{
|
||
Sorts = sort,//骑行实时排名
|
||
MyRanking = sort.FindIndex(n => n.UserId == currentPlayer.UserId) + 1,//切换人物显示对应人物的排名
|
||
Total = sort.Count,//总人数
|
||
Results = results,//比赛结果列表,
|
||
TopList = CompetitionTopList,
|
||
};
|
||
}
|
||
|
||
/// <summary>
|
||
/// 赛事模式有继续骑行,重写执行方法
|
||
/// </summary>
|
||
/// <param name="targetData"></param>
|
||
public override void Run(TargetData targetData)
|
||
{
|
||
|
||
#region 获取当前路线在线用户
|
||
riders.RemoveAll(r => r.IsSelf == false);
|
||
var onlineUsers = MapUDPService.GetCompetitionOnlineUsers(mapCompetition.Id).Where(d => d.IsSelf == false)
|
||
.ToList();
|
||
foreach (var item in onlineUsers)
|
||
{
|
||
riders.Add(new OnlineRiderModel()
|
||
{
|
||
NickName = item.Name,
|
||
UserId = item.Id,
|
||
WxHeadImg = item.HeadImage,
|
||
Point = new GeoJSON.Net.Geometry.GeographicPosition(item.Point[0], item.Point[1]),
|
||
EndDistance = item.EndDistance,
|
||
PreDistance = item.PreDistance,
|
||
WeightKg = item.WeightKg,
|
||
});
|
||
}
|
||
|
||
#endregion
|
||
//TODO:接力赛
|
||
////base.BeforeRun(targetData);
|
||
//if (recorderData.CyclingState == CyclingStateEnum.Prepare)
|
||
//{
|
||
// if (recorderData.EndDistance != 0)
|
||
// {
|
||
// currentDistance = recorderData.EndDistance;
|
||
// }
|
||
// else
|
||
// {
|
||
// currentDistance = 0;
|
||
// }
|
||
//}
|
||
//else if (recorderData.CyclingState == CyclingStateEnum.Cycling)
|
||
//{
|
||
// currentDistance = recorderData.EndDistance + SpeedToDistance(targetData._Speed);
|
||
//}
|
||
base.Run(targetData);
|
||
}
|
||
|
||
//显示结果当前用户保存或者产生第一名
|
||
public bool CanShowResult()
|
||
{
|
||
return mapCompetition?.HasRecord ?? false || mapCompetition ?.FirstEndTime != null;
|
||
}
|
||
//关门时间到了退出骑行场景
|
||
public bool CanQuit()
|
||
{
|
||
if (mapCompetition?.FirstEndTime.HasValue??false)
|
||
{
|
||
var closeDateTime = mapCompetition?.FirstEndTime.Value.ToLocalTime().AddMinutes(mapCompetition?.CloseTime??0 );
|
||
return Now >= closeDateTime;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
public bool CanStart()
|
||
{
|
||
if (mapCompetition == null)
|
||
{
|
||
return false;
|
||
}
|
||
//if (mapCompetition.CanStart)
|
||
//{
|
||
// return true;
|
||
//}
|
||
|
||
if (mapCompetition.HasRecord)
|
||
{
|
||
return false;
|
||
}
|
||
|
||
//已结束
|
||
if (mapCompetition.Status == 4)
|
||
{
|
||
return false;
|
||
}
|
||
if (mapCompetition.StartTime.ToLocalTime() <= Now && mapCompetition.EndTime.ToLocalTime() > Now)
|
||
{
|
||
return true;
|
||
}
|
||
|
||
return false;
|
||
}
|
||
|
||
public int StartCountdown()
|
||
{
|
||
if (CanStart()) return 0;
|
||
return (int)Math.Floor((mapCompetition.StartTime.ToLocalTime() - Now).TotalSeconds);
|
||
}
|
||
/// <summary>
|
||
/// 结束倒计时,bool值为true说明倒计时是关门时间
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public (int, bool) EndCountDown()
|
||
{
|
||
//if (CanStart() == false) return (0, false);
|
||
//if (recorderData.IsCompleted) return 0;
|
||
var end1 = (int)Math.Floor((mapCompetition.EndTime.ToLocalTime() - Now).TotalSeconds);
|
||
if (!FirstEndTime.HasValue)
|
||
{
|
||
return (end1, false);
|
||
}
|
||
//var end2 = (int)Math.Floor((FirstEndTime.Value - Now).TotalSeconds);
|
||
var end2 = (int)Math.Floor((FirstEndTime.Value.ToLocalTime().AddMinutes(mapCompetition.CloseTime) - Now).TotalSeconds);
|
||
if (end1 < end2)
|
||
{
|
||
return (end1, false);
|
||
}
|
||
//return Math.Min(end1, end2);
|
||
return (end2, true);
|
||
}
|
||
}
|
||
}
|