325 lines
12 KiB
C#
Raw Normal View History

using Assets.Scenes.Ride.Scripts.Model.RiderModels;
using Assets.Scripts;
using Assets.Scripts.Apis;
using Assets.Scripts.Apis.Models;
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 MapCompetitionApi mapCompetitionApi;
private MapCompetition mapCompetition;
private List<CompetitionRankingResultModel> mapWorkoutRecordRankings = new List<CompetitionRankingResultModel>();
/// <summary>
/// 第一个到终点的时间
/// </summary>
private DateTime? FirstEndTime;
public override bool CanPause => false;
protected override string ShowTime
{
get
{
if (CanStart())
{
var second = Math.Ceiling((Now - mapCompetition.StartTime).TotalSeconds);
var ts = TimeSpan.FromSeconds(second);
return $"{ (int)ts.TotalHours }:{ts:mm}:{ts:ss}";
}
return "00:00:00";
}
}
public CompetitionModel(Route route1, RouteResultParam param)
: base(route1, CyclingModel.Competition)
{
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 = param.ContinueMark == null ? 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)
{
return;
}
FirstEndTime = mapCompetition.FirstEndTime;
}
protected override void BeforeRunAfterPakcData(OutModel outData)
{
outData.CanStart = CanStart();
outData.StartCountDown = StartCountdown();
var endCountDownResult = EndCountDown();
outData.EndCountDown = endCountDownResult.Item1;
outData.IsClosed = endCountDownResult.Item2;
#region
if (!FirstEndTime.HasValue)
{
var firstItem = base.realRiders.OrderByDescending(w => w.NextDistance).FirstOrDefault();
if (firstItem != null)
{
outData.FirstPoint = firstItem.Point;
}
}
#endregion
////如果骑行中,但是已经到结束时间
//if (outData.EndCountDown <= 0 && recorderData.CyclingState == CyclingStateEnum.Cycling)
//{
// base.CacheAnimate();
//}
var userList = MapUDPService.GetOnlineUsers(recorderData.CurrentRoute.RouteInstance.Id)
.Where(d => d.CompetitionId == 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>();
//标识后面的用户都是比我慢的用户
var slowerThenMe = false;
if (userList.Any())
{
var point = new double[] { outData.targetData._Lat, outData.targetData._Lon };
var self = userList.SingleOrDefault(r => r.IsSelf);
if (self != null)
{
point = self.Point;
}
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
});
}
index++;
double near = Math.Round(TurfHelper.GetDistances(point, item.Point), 2);
if (slowerThenMe)
{
near = near * -1;
}
if (item.IsSelf)
{
near = 0;
slowerThenMe = true;
}
//else if (index == 1)
//{
// near = near * -1;
//}
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 = ConfigHelper.Host + $"User/GetCountryImg?userid={ item.Id }",
UserId = item.Id,
IsSelf = item.IsSelf
});
}
}
#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,
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 ?? ""
});
}
}
#endregion
outData.CompetitionRanking = new CompetitionRankingModel
{
Sorts = sort,
MyRanking = sort.FindIndex(n => n.UserId == recorderData.BelongUserId) + 1,
Total = sort.Count,
Results = results
};
}
/// <summary>
/// 赛事模式有继续骑行,重写执行方法
/// </summary>
/// <param name="targetData"></param>
public override void Run(TargetData targetData)
{
//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 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 <= Now && mapCompetition.EndTime > Now)
{
return true;
}
return false;
}
public int StartCountdown()
{
if (CanStart()) return 0;
return (int)Math.Floor((mapCompetition.StartTime - 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 - Now).TotalSeconds);
if (!FirstEndTime.HasValue)
{
return (end1, false);
}
//var end2 = (int)Math.Floor((FirstEndTime.Value - Now).TotalSeconds);
var end2 = (int)Math.Floor((FirstEndTime.Value.AddMinutes(mapCompetition.CloseTime) - Now).TotalSeconds);
if (end1 < end2)
{
return (end1, false);
}
//return Math.Min(end1, end2);
return (end2, true);
}
}
}