AR文件更新流程&AR网络延时问题

This commit is contained in:
lishuo 2023-05-18 19:06:40 +08:00
parent 16cd2fd26b
commit 74ba5790f9
18 changed files with 867 additions and 508 deletions

194
Assets/AR/ARDownloader.cs Normal file
View File

@ -0,0 +1,194 @@
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Assets.Scenes.Ride.Scripts;
using Assets.Scripts.Apis.Models;
using Cysharp.Threading.Tasks;
using UnityEngine.Networking;
namespace Assets.AR
{
public class ARDownloader
{
private MapRoute _route;
private readonly string _localARDataPath;
private readonly string _localARRoutePath;
private readonly string _localVideoPath;
private readonly string _serverARDataPath;
private readonly string _serverARRoutePath;
private readonly string _serverVideoPath;
public bool IsVideoDownloaded { get; private set; }
public bool IsARDataDownloaded { get; private set; }
public bool IsARRouteDownloaded { get; private set; }
public float Total { get; private set; } = 0;
public ARDownloader(MapRoute route)
{
this._route = route;
var dataPath = $"{PFConstants.ARFolder}/{_route.Id}";
var path = PFConstants.VideoFolder;
Helper.CreateDirectoryIfNotExsit(dataPath);
_localARDataPath = $"{dataPath}/{_route.Id}.json";
_localARRoutePath = $"{dataPath}/route-{_route.Id}.json";
_localVideoPath = $"{path}/{_route.FileName}";
_serverARDataPath = _route.ARConfig;
_serverARRoutePath = _route.VideoRoute;
_serverVideoPath = _route.Url;
}
public async UniTask<UnityWebRequest> DownLoadVideoAsync(IProgress<float> progress = null,
CancellationTokenSource cancellation = default(CancellationTokenSource))
{
return await DownloadToFileAsync(_serverVideoPath, _localVideoPath, progress, cancellation);
}
public async UniTask<UnityWebRequest> DownLoadARRouteAsync(IProgress<float> progress = null,
CancellationTokenSource cancellation = default(CancellationTokenSource))
{
return await DownloadToFileAsync(_serverARRoutePath, _localARRoutePath, progress, cancellation);
}
public async UniTask<UnityWebRequest> DownLoadARDataAsync(IProgress<float> progress = null,
CancellationTokenSource cancellation = default(CancellationTokenSource))
{
return await DownloadToFileAsync(_serverARDataPath, _localARDataPath, progress, cancellation);
}
public async Task<bool> CheckAllAsync()
{
IsARRouteDownloaded = await CheckARRouteAsync();
IsARDataDownloaded = await CheckARDataAsync();
IsVideoDownloaded = await CheckVideoAsync();
if (IsVideoDownloaded)
Total += 1f;
if (IsARRouteDownloaded)
Total += 1f;
if (IsARDataDownloaded)
Total += 1f;
return IsVideoDownloaded || IsARRouteDownloaded || IsARDataDownloaded;
}
/// <summary>
/// return true if the video is needed to download
/// </summary>
/// <returns></returns>
public async Task<bool> CheckVideoAsync()
{
try
{
if (!File.Exists(_localVideoPath)) return true;
var result = await HeadFileAsync(_serverVideoPath, _localVideoPath);
return result.responseCode != 304;
}
catch (Exception e)
{
return false;
}
}
/// <summary>
/// return true if the video is needed to download
/// </summary>
/// <returns></returns>
public async Task<bool> CheckARRouteAsync()
{
if (!File.Exists(_localARRoutePath)) return true;
var result = await HeadFileAsync(_serverARRoutePath, _localARRoutePath);
return result.responseCode != 304;
}
/// <summary>
/// return true if the video is needed to download
/// </summary>
/// <returns></returns>
public async Task<bool> CheckARDataAsync()
{
if (!File.Exists(_localARDataPath)) return true;
var result = await HeadFileAsync(_serverARDataPath, _localARDataPath);
return result.responseCode != 304;
}
/// <summary>
/// 下载文件到本地(持续存入磁盘减少内存占用)
/// </summary>
/// <param name="downloadUrl">文件连接</param>
/// <param name="fullPath">本地文件全路径</param>
/// <param name="progress">下载进度</param>
public static async UniTask<UnityWebRequest> DownloadToFileAsync(string downloadUrl, string fullPath,
IProgress<float> progress = null, CancellationTokenSource cancellation = default(CancellationTokenSource))
{
var dh = new DownloadHandlerFile(fullPath)
{
removeFileOnAbort = true
};
var request = UnityWebRequest.Get(downloadUrl);
request.downloadHandler = dh;
request.method = UnityWebRequest.kHttpVerbGET;
var result = await request.SendWebRequest().ToUniTask(progress, PlayerLoopTiming.Update,
(cancellation?.Token ?? default(CancellationToken)));
var etag = result.GetResponseHeader("ETag");
var path = fullPath + ".etag";
if (!File.Exists(path))
{
File.Create(path);
}
File.WriteAllText(path, etag);
return result;
}
/// <summary>
/// head request for file meta info such as etag.
/// </summary>
/// <param name="downloadUrl">download url</param>
/// <param name="fullPath">local path for storage</param>
/// <returns></returns>
public static async UniTask<UnityWebRequest> HeadFileAsync(string downloadUrl, string fullPath,
IProgress<float> progress = null, CancellationTokenSource cancellation = default(CancellationTokenSource))
{
try
{
var etagFile = fullPath + ".etag";
var etag = string.Empty;
if (File.Exists(etagFile))
{
etag = File.ReadAllText(etagFile);
}
var request = UnityWebRequest.Head(downloadUrl);
if (!string.IsNullOrEmpty(etag))
request.SetRequestHeader("If-None-Match", etag);
var result =
await request
.SendWebRequest().ToUniTask(progress, PlayerLoopTiming.Update,
(cancellation?.Token ?? default(CancellationToken)));
if (result.responseCode != 304)
{
etag = result.GetResponseHeader("ETag");
File.WriteAllText(fullPath + ".etag", etag);
}
return result;
}
catch (Exception e)
{
return null;
}
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 84a219eba2114884a2e246578ade9ab9
timeCreated: 1684373675

View File

@ -39,13 +39,16 @@ namespace Assets.AR
public int TestPower { get; set; }
public float ZOffset { get; set; }
protected void Start()
{
this.Lane = route.Lane;//轨迹上的距离
}
//重置碰撞检测参数
public void ResetCollisionDetectionParameters()
{
this.StartPosition = 0;//开始位置
this.Lane = 0.0f;//轨迹上的距离
this.Lane = route.Lane;//轨迹上的距离
this.DeltaLane = 0.0f;//差距
this.LaneWidth = 0.0f;//轨迹宽度
this.LaneCamera = 0.0f;//轨迹上摄像头的位置
@ -59,10 +62,10 @@ namespace Assets.AR
{
PositionOffset = Vector3.up * route.CameraHeight * (-1);
var cameraRotation = this.GetCameraRotation(this.frame - this.FrameIndexDistanceCorrection);
var normalizeCameraRotation = Quaternion.Euler(new Vector3(cameraRotation.eulerAngles.x, cameraRotation.eulerAngles.y, cameraRotation.eulerAngles.z+ZOffset));
var normalizeCameraRotation = Quaternion.Euler(new Vector3(cameraRotation.eulerAngles.x+route.CameraRotationsOffset.x, cameraRotation.eulerAngles.y+route.CameraRotationsOffset.y, cameraRotation.eulerAngles.z+route.CameraRotationsOffset.z));
var filteredCameraPosition = this.GetFilteredCameraPosition(this.frame - this.FrameIndexDistanceCorrection);
var vector3 = Vector3.left * (this.route.LeftHanded ? -1f : 1f) * (this.LaneWidth * (this.Lane + this.LaneCamera) - this.BaseOffset);
var vector3 = Vector3.left * (this.route.LeftHanded ? -1f : 1f) * (this.LaneWidth * (this.Lane + this.LaneCamera + this.route.Lane) - this.BaseOffset);
var targetPos = filteredCameraPosition + normalizeCameraRotation * (vector3 + this.PositionOffset);
this.transform.position = targetPos;

View File

@ -8,7 +8,7 @@ namespace Assets.AR
{
protected const float LaneWidth = 0.7f;
protected const float StartRegionDistance = 200f;
private const float StartRowsGap = 2.2f;
private const float StartRowsGap = 1.0f;
private const float StartRowsRandomDistance = 0.4f;
private const float StartRowsRandomWidth = 0.15f;
private const float MinCollisionDistance = 1.33f;
@ -34,7 +34,7 @@ namespace Assets.AR
{
var frame = 100f;
this.StartRegionRouteWidth = this.GetRouteLeftOffset(frame) + this.GetRouteRightOffset(frame);
this.RiderCountInStartRow = Mathf.Max(2, Mathf.CeilToInt(this.StartRegionRouteWidth / 0.7f));
this.RiderCountInStartRow = Mathf.Max(1, Mathf.CeilToInt(this.StartRegionRouteWidth / LaneWidth));
}
protected override void UpdateGameObjects(
@ -96,9 +96,9 @@ namespace Assets.AR
this.CalculateOvertakingParameters();
foreach (ARLaneGameObject collision in this.collisionList)
{
collision.LaneWidth = 0.7f;
collision.LaneWidth = LaneWidth;
var num6 = this.Route.LeftHanded ? this.GetRouteLeftOffset(collision.Frame) : this.GetRouteRightOffset(collision.Frame);
var num7 = Mathf.Lerp(this.GetStartOffset(collision.StartPosition).x, 0.0f, collision.Distance / 200f);
var num7 = Mathf.Lerp(this.GetStartOffset(collision.StartPosition).x, 0.0f, collision.Distance / StartRegionDistance);
collision.BaseOffset = num6 + num7;
}
this.CalculateCameraAvoidance(cameraDistance);
@ -224,22 +224,19 @@ namespace Assets.AR
Vector2 startOffset;
if (this.randomizedStartOffsets.TryGetValue(atIndex, out startOffset))
return startOffset;
startOffset = new Vector2(UnityEngine.Random.Range(-0.15f, 0.15f), UnityEngine.Random.Range(-0.4f, 0.4f));
startOffset = new Vector2(UnityEngine.Random.Range(-StartRowsRandomWidth, StartRowsRandomWidth), UnityEngine.Random.Range(-StartRowsRandomDistance,StartRowsRandomDistance));
this.randomizedStartOffsets.Add(atIndex, startOffset);
return startOffset;
}
public float GetRouteDistance(int startPosition, float routeDistance)
{
if (!this.AllowStartOrder || (double)routeDistance >= 200.0)
if (!this.AllowStartOrder || (double)routeDistance >= StartRegionDistance)
return routeDistance;
var start = (float)((RiderCountInStartRow == 0 ? 0 : (startPosition - 1) / this.RiderCountInStartRow) * 2.2000000476837158 + 6.0) + this.GetStartOffset(startPosition).y;
var num = Mathf.Lerp(start , 0.0f, routeDistance / 200f);
float num = Mathf.Lerp((float) ((double) ((startPosition - 1) / this.RiderCountInStartRow) * StartRowsGap + 6.0) + this.GetStartOffset(startPosition).y, 0.0f, routeDistance / StartRegionDistance);
return routeDistance + num;
}
//计算碰撞距离
protected static float GetCollisionParameter(
float distSource,

View File

@ -59,8 +59,6 @@ namespace Assets.AR
public int VideoFrameOffset { get; set; }
public float CameraHeight { get; set; }
public float ZOffset { get; set; }
public float RiderScale { get; set; } = 1f;
@ -73,10 +71,15 @@ namespace Assets.AR
public int FrameCount => this.CameraPositions.Length;
public double Lenght => this.FrameDistances[this.FrameCount - 1];
public VectorData CameraRotationsOffset { get; set; }
public float Lane { get; set; }
private float DefaultLeftSideOffset => !this.LeftHanded ? 4f : 0.0f;
private float DefaultRightSideOffset => !this.LeftHanded ? 0.0f : 4f;
public int GetVisibilityAt(int frame)
{
@ -431,17 +434,9 @@ namespace Assets.AR
public void LoadData(ARRouteData data)
{
// //todo:修改rotation z
// foreach (var rotation in data.CameraRotations)
// {
// rotation.z += 4f;
// }
//
// var str = JsonConvert.SerializeObject(data);
this.Visibility = data.Visibility;
this.VideoFrameOffset = data.VideoFrameOffset;
this.LeftHanded = data.LeftHanded;
this.ZOffset = data.Zoffset;
this.SlamSegments = data.SlamSegments;
this.CameraHeight = data.CameraHeight;
this.RiderScale = (double)data.RiderScale != 0.0 ? data.RiderScale : 1f;
@ -454,6 +449,8 @@ namespace Assets.AR
this.LeftSideOffsets = data.LeftSideOffsets;
this.RightSideOffsetFrames = data.RightSideOffsetFrames;
this.RightSideOffsets = data.RightSideOffsets;
this.CameraRotationsOffset = data.CameraRotationsOffset;
this.Lane = data.Lane;
if (data.LightRotationFrames == null || data.LightRotationFrames.Length == 0)
{
this.LightRotationFrames = new float[0];

View File

@ -248,11 +248,12 @@ namespace Assets.AR
public int VideoFrameOffsetMac = -1;
public float CameraHeight;
public float RiderScale = 1f;
public float Zoffset = 0f;
public int[] LeftSideOffsetFrames;
public float[] LeftSideOffsets;
public int[] RightSideOffsetFrames;
public float[] RightSideOffsets;
public VectorData CameraRotationsOffset;
public float Lane;
}
public class VectorData
{

View File

@ -91,17 +91,10 @@ namespace Assets.AR
}
private string LastAnimatorState { get; set; }
protected override void Start()
{
base.Start();
SetZOffset();
}
protected override void Update()
{
if (this.route == null)
return;
SetMainRiderLane();
base.Update();
var num = this.Speed;
this.Paused = this.Speed == 0;
@ -168,24 +161,6 @@ namespace Assets.AR
this.lastVisibilitylevel = this.VisibilityLevel;
}
private void SetZOffset()
{
var manager = FindObjectOfType<VideoGameManager>();
var map = manager.GetMapRoute();
this.ZOffset = map.Id == 6296 ? -4f : 0f;
}
private void SetMainRiderLane()
{
if (IsMain)
{
var manager = FindObjectOfType<VideoGameManager>();
var map = manager.GetMapRoute();
if(map.Id == 6296)//TODO:这里写死溧阳AR骑行
Lane = -2f;
}
}
private void FixedUpdate()
{
//if (this.Paused || (double)this.Speed <= 0.0)

View File

@ -213,7 +213,8 @@ public static class App
{
InitLanguage();
#if !UNITY_EDITOR
Host = "http://pf.juze.pro/";
Host = "http://192.168.0.98:6662/";
//Host = "http://pf.juze.pro/";
UdpAddress = new IPEndPoint(IPAddress.Parse("47.97.84.8"), 21000);
TcpAddress = new IPEndPoint(IPAddress.Parse("47.97.84.8"), 21001);
//线上

View File

@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
@ -14,13 +13,12 @@ using Assets.Scenes.Ride.Scripts.Model;
using Assets.AR;
using System.Threading.Tasks;
using System.Threading;
using Cysharp.Threading.Tasks;
namespace Assets.Scripts.Scenes.VideoRide
{
public class VideoLoading : MonoBehaviour, IProgress<float>
public class VideoLoading : MonoBehaviour
{
VideoGameManager manager { get; set; }
private Text mapName;
private RawImage mapRouteImage;
private CanvasGroup canvasGroup;
@ -35,26 +33,27 @@ namespace Assets.Scripts.Scenes.VideoRide
private Text slope;
private GameObject panel;
private GameObject download;
private GameObject watch;
protected GameObject loadingPanel { get; set; }
private GameObject watch ;
protected GameObject loadingPanel;
private Text level;
private Text rideNum;
private Text uploadByUserName;
private RawImage head;
private RawImage country;
private RawImage mapCountry;
private RawImage altitudeGraph { get; set; }
private RawImage altitudeGraph;
private Transform mapRanking;
private Text mapId;
private Text downloadText { get; set; }
private Text downloadText;
private Transform ui;
private MapRoute route;
protected float process = 0;//0-100 %
private VideoGameManager manager { get; set; }
private MapRoute route;
private ARDownloader downloader;
private CancellationTokenSource cancelToken { get; set; }
private bool startBtnLock = false;
private bool downloading = false;
private float total = 1f;
private void Awake()
{
@ -104,31 +103,12 @@ namespace Assets.Scripts.Scenes.VideoRide
processText.text = $"{(f).ToString("#0")}%";
});
download = transform.Find("Panel/Download").gameObject;
watch = transform.Find("Panel/Watch").gameObject;
UIManager.AddEvent(watch, UnityEngine.EventSystems.EventTriggerType.PointerClick, WatchHandler);
canvasGroup = transform.GetComponent<CanvasGroup>();
var fileName = route.FileName;// "12067924_720p.mp4";
var url = route.Url; //@"http://192.168.0.97:6031/12067924_720p.mp4";
var path = PFConstants.VideoFolder;
var filepath = path + "/" + fileName;
if (File.Exists(filepath))
{
watch.SetActive(true);
slider.value = 100;
download.gameObject.SetActive(false);
manager.SetMedia(filepath);
}
else
{
watch.SetActive(false);
//检查本地文件是否存在 如果存在直接设置mediaPlayer 否则显示下载按钮
UIManager.AddEvent(download.gameObject, UnityEngine.EventSystems.EventTriggerType.PointerClick, DownloadAsync);
}
#region
level = panel.transform.Find("level/Text").GetComponent<Text>();
rideNum = panel.transform.Find("RideNum").GetComponent<Text>();
@ -177,9 +157,35 @@ namespace Assets.Scripts.Scenes.VideoRide
{
panel.transform.Find("RideTip").GetComponent<CanvasGroup>().DOFade(0, 0.5f);
});
UIManager.AddEvent(download.gameObject, EventTriggerType.PointerClick, DownloadAsync);
#endregion
downloadText = rideNow.GetComponentInChildren<Text>();//视频下载进度text
CheckUpdate();
}
private async void CheckUpdate()
{
downloader = new ARDownloader(route);
var showDownload = await downloader.CheckAllAsync();
var isContinue = App.routeResult != null;
watch.SetActive(!showDownload && !isContinue);
download.gameObject.SetActive(showDownload);
if (!showDownload)
{
InitializeVideo();
}
}
private void InitializeVideo()
{
var path = PFConstants.VideoFolder + "/" + route.FileName;
slider.value = 100;
manager.SetMedia(path);
}
protected void InitGameObjectData()
@ -267,18 +273,14 @@ namespace Assets.Scripts.Scenes.VideoRide
},false,true);
}
private bool downloading = false;
private CancellationTokenSource cancelToken;
private async void DownloadAsync(BaseEventData baseEvent)
{
if (downloading)
return;
beforeDownload();
await startDownloading();
if (downloading) return;
ResetDownload();
await StartDownloading();
}
private void beforeDownload()
private void ResetDownload()
{
downloading = true;
download.SetActive(false);
@ -287,45 +289,47 @@ namespace Assets.Scripts.Scenes.VideoRide
downloadText.text = "0%";
}
private async Task startDownloading()
private async Task StartDownloading()
{
var route = manager.mapRoute;
var path = PFConstants.VideoFolder;
slider.maxValue = downloader.Total * 100;
if(downloader.IsVideoDownloaded)
await downloader.DownLoadVideoAsync(Progress.Create<float>(HandleProgress));
if(downloader.IsARRouteDownloaded)
await downloader.DownLoadARRouteAsync(Progress.Create<float>(HandleProgress));
if(downloader.IsARDataDownloaded)
await downloader.DownLoadARDataAsync(Progress.Create<float>(HandleProgress));
var localPath = PFConstants.ARFolder;
var arDataPath = route.ARConfig;
var videoRoute = route.VideoRoute;
var dataPath = $"{localPath}/{route.Id}";
Helper.CreateDirectoryIfNotExsit(dataPath);
Helper.CreateDirectoryIfNotExsit(dataPath);
cancelToken = new CancellationTokenSource();
await Loom.DownloadToFileAsync(arDataPath, $"{dataPath}/{route.Id}.json");
await Loom.DownloadToFileAsync(videoRoute, $"{dataPath}/route-{route.Id}.json");
var res = await Loom.DownloadToFileAsync(route.Url, $"{path}/{route.FileName}",this,cancelToken);
if (res.isDone)
{
var filepath = $"{PFConstants.VideoFolder}/{route.FileName}";
manager.SetMedia(filepath);
rideNow.enabled = true;
rideNow.interactable = true;
downloadText.text = App.GetLocalString("Ride Now");
downloading = false;
}
//cancelToken = new CancellationTokenSource();
//CompleteDownLoad();
}
public void Report(float value)
private void HandleProgress(float value)
{
if (cancelToken.IsCancellationRequested)
if (cancelToken != null && cancelToken.IsCancellationRequested)
return;
slider.value += (float)Math.Round(value * 100, 0);
Debug.Log(value);
slider.value = value < 1 ? (float)Math.Round(value * 100, 0) : 100;
downloadText.text = slider.value.ToString(CultureInfo.InvariantCulture) + "%";
var progress = slider.value / slider.maxValue * 100;
downloadText.text = progress.ToString(CultureInfo.InvariantCulture) + "%";
var isComplete = slider.value == slider.maxValue;
if (isComplete)
CompleteDownLoad();
}
private void CompleteDownLoad()
{
var filepath = $"{PFConstants.VideoFolder}/{route.FileName}";
manager.SetMedia(filepath);
watch.GetComponent<Button>().enabled = true;
rideNow.enabled = true;
rideNow.interactable = true;
downloadText.text = App.GetLocalString("Ride Now");
downloading = false;
}
//进入观察模式
private void WatchHandler(BaseEventData data)
{
@ -360,20 +364,27 @@ namespace Assets.Scripts.Scenes.VideoRide
return;
}
UIManager.SetModalPanel(transform.Find("ModalPanel").GetComponent<PFUIPanel>());
UIManager.ShowConfirm("Tips", "CancelDownloadConfirm",
() =>
{
cancelToken?.Cancel();
SceneManager.LoadSceneAsync("MainScene");
UIManager.CloseConfirm();
},
2,
() =>
{
UIManager.CloseConfirm();
});
CancelDownload();
}
private void CancelDownload()
{
UIManager.SetModalPanel(transform.Find("ModalPanel").GetComponent<PFUIPanel>());
UIManager.ShowConfirm("Tips", "CancelDownloadConfirm",
() =>
{
cancelToken?.Cancel();
SceneManager.LoadSceneAsync("MainScene");
UIManager.CloseConfirm();
},
2,
() =>
{
UIManager.CloseConfirm();
});
}
private async void DrawMapRouteAsync(int routeId, int type = 0)
{
var result = await ConfigHelper.mapApi.GetMapLoadingCoverageUrl(routeId, type);
@ -391,6 +402,7 @@ namespace Assets.Scripts.Scenes.VideoRide
Utils.DisplayImageAysnc(StartCoroutine, mapRouteImage, url, DowloadCallBack);
}
}
private void DowloadCallBack(string url, RawImage rawImage)
{
if (!App.TextureCache.ContainsKey(url))

View File

@ -68,7 +68,7 @@ namespace Assets.Scripts.Scenes.VideoRide
private void Update()
{
if (manager != null)
if (manager != null && manager.GetMapRoute() != null)
{
mapName.text = manager.GetMapRoute().Name;
if (manager.CurrentPlayer != null)

View File

@ -62,7 +62,6 @@ namespace Assets.Scripts.Scenes.VideoRide
public GameObject videoPlayer;
public GameObject target;
public int CurrentUserId { get; set; }
protected override void Awake()
{
@ -119,34 +118,49 @@ namespace Assets.Scripts.Scenes.VideoRide
yield return null;
videoLoading.Init();//初始化loading页面
}
protected override void Update()
private float tcpTimer = 0f;
private void FixedUpdate()
{
timer -= Time.deltaTime;
while (timer <= 0)
if (cyclingController == null)
return;
try
{
if (cyclingController == null)
return;
try
timer -= Time.deltaTime;
tcpTimer -= Time.deltaTime;
while (timer <= 0)
{
cyclingController.Run(null);//发送数据到tcp
var onlineRiders = cyclingController.riders;
CreateOnlineUser(onlineRiders); //解析tcp返回数据
if (isStart)
{
ComputePlayerRuntimeData();
ticks++;
}
TickTimer();
}
catch (Exception e)
while (tcpTimer <= 0)
{
Debug.Log(e.Message);
MillTimer();
}
timer += 1f;
}
catch (Exception e)
{
Debug.Log(e.Message);
}
}
private void TickTimer()
{
if (isStart)
{
ComputePlayerRuntimeData();
ticks++;
}
timer +=1f;
}
private void MillTimer()
{
cyclingController.Run(null);//发送数据到tcp
var onlineRiders = cyclingController.riders;
CreateOnlineUser(onlineRiders); //解析tcp返回数据
tcpTimer += 0.2f;
}
//计算骑手的数据
@ -302,7 +316,6 @@ namespace Assets.Scripts.Scenes.VideoRide
//移除退出的选手
private void RemovePlayers(List<BaseRider> list)
{
var players = FindObjectsOfType<OnlineVideoPlayer>();
foreach (var item in players)
{
@ -371,10 +384,7 @@ namespace Assets.Scripts.Scenes.VideoRide
//开始游戏
public void StartGame()
{
//if (startTime != DateTime.MinValue)
{
startTime = UIManager.Now.GetDateTime();
}
startTime = UIManager.Now.GetDateTime();
isStart = true;
}
//骑行是否开始
@ -445,7 +455,6 @@ namespace Assets.Scripts.Scenes.VideoRide
}
return mapRoute;
}
public void ShowUpRemainTime(double remain)
{
@ -477,18 +486,22 @@ namespace Assets.Scripts.Scenes.VideoRide
RankingId = cyclingController.recorderData.SaveWithLocalRecordAysnc(cyclingModel, selectParamModel, imageFileName, recordId, path);
}
public void AddEvent(GameObject sender, EventTriggerType eventType, UnityAction<BaseEventData> unityAction)
{
UIManager.AddEvent(sender, eventType, unityAction);
}
public Texture GetCountryImageByCode(string code)
{
return UIManager.Instance.loginRegOptions.GetCountryImage(code);
}
public Texture GetCountryImageByName(string name)
{
return UIManager.Instance.loginRegOptions.GetCountryImageByName(name);
}
public Vector2d Along(double endDistance)
{
if (mapData != null)
@ -504,6 +517,7 @@ namespace Assets.Scripts.Scenes.VideoRide
return new Vector2d(0, 0);
}
}
private void OnDestroy()
{
App.gameRoomDetail = null;

View File

@ -17,48 +17,55 @@ namespace Assets.Scenes.Ride.Scripts.Model
//private readonly MapWorkoutService service = new MapWorkoutService();
public UserResultModel CurrentUser { get; set; }
/// <summary>
/// 所属人ID
/// </summary>
public int BelongUserId
{
get
{
return App.CurrentUser.Id;
}
get { return App.CurrentUser.Id; }
}
/// <summary>
/// 记录是否完毕
/// </summary>
public bool IsCompleted { get; set; }
/// <summary>
/// 是否需要计算排名
/// </summary>
public bool IsNeedRanking { get; set; }
/// <summary>
/// 骑行记录索引(用于中断骑行,继续骑行)
/// </summary>
public int ContinueIndex { get; set; }
/// <summary>
/// 骑行状态
/// </summary>
public CyclingStateEnum CyclingState { get; set; }
/// <summary>
/// 赛事Id
/// </summary>
public int Competitionid { get; set; }
/// <summary>
/// 当前已经骑行的距离(表示用户现在在的距离,并不是实际骑的距离)
/// </summary>
public double EndDistance { get; set; }
/// <summary>
/// 上次骑行的距离
/// </summary>
public double PreDistance { get; set; }
/// <summary>
/// 继续骑行记录标志
/// </summary>
public string ContinueMark { get; set; }
/// <summary>
/// 厂商Id
/// </summary>
@ -67,14 +74,17 @@ namespace Assets.Scenes.Ride.Scripts.Model
public string ManufacturerName { get; set; }
public int? AntModelId { get; set; }
/// <summary>
/// 骑行数据
/// </summary>
public List<TargetData> RiderDatas = new List<TargetData>();
/// <summary>
/// 当前路线
/// </summary>
public Route CurrentRoute { get; set; }
/// <summary>
/// 骑行记录Id
/// </summary>
@ -88,14 +98,17 @@ namespace Assets.Scenes.Ride.Scripts.Model
/// 当前给骑行台发送数据的模式
/// </summary>
public SendDataMode SendDataMode { get; set; }
/// <summary>
/// 临时使用的坡度
/// </summary>
public double SlopeGrade { get; set; }
/// <summary>
/// 临时使用的海拔
/// </summary>
public double Elevation { get; set; }
/// <summary>
/// 当前路书开始的距离
/// </summary>
@ -108,6 +121,7 @@ namespace Assets.Scenes.Ride.Scripts.Model
public int? LastFrame { get; set; }
public int RoomId { get; set; }
/// <summary>
/// 是否已经保存成功
/// </summary>
@ -123,25 +137,25 @@ namespace Assets.Scenes.Ride.Scripts.Model
public RouteResultParam selectParam { get; set; }
public int SaveWithLocalRecordAysnc(CyclingModel cyclingModel, RouteResultParam selectParam, string imageName,string recordId,string path)
public int SaveWithLocalRecordAysnc(CyclingModel cyclingModel, RouteResultParam selectParam, string imageName,
string recordId, string path)
{
Saved = true;
Dictionary<MapInterruptRecord, List<string>> recordData = new Dictionary<MapInterruptRecord, List<string>>();
var recordData = new Dictionary<MapInterruptRecord, List<string>>();
if (RiderDatas.Count <= 0)
{
return -1;
}
string newFileName = Guid.NewGuid().ToString();
int FTP = Helper.GetFtp();
double NP = Helper.GetNP(RiderDatas);
var newFileName = Guid.NewGuid().ToString();
var FTP = Helper.GetFtp();
var NP = Helper.GetNP(RiderDatas);
//强度
double IF = NP / FTP;
var IF = NP / FTP;
//训练量
double TSS = (RiderDatas.Count * NP * IF) / (FTP * 3600) * 100;
var TSS = (RiderDatas.Count * NP * IF) / (FTP * 3600) * 100;
var averagePower = Helper.AveragePower(RiderDatas);
// AntManufacturer manufacturer = service.GetAntManufacturer(ManufacturerId).data;
//var recordId = Guid.NewGuid().ToString();
var ticks = RiderDatas.Last().Ticks;
var interruptRecord = new MapInterruptRecord
{
@ -151,17 +165,15 @@ namespace Assets.Scenes.Ride.Scripts.Model
TotalDistance = CurrentRoute.RouteInstance.Distance,
UserId = BelongUserId,
RecordFileName = newFileName + ".txt",
Ftp = FTP,//FTP设置
Ftp = FTP,
IF = Math.Round(IF, 2),
Kj = RiderDatas.Sum(a => a._Power) / 1000,//消耗
Kj = RiderDatas.Sum(a => a._Power) / 1000, //消耗
Tss = Math.Round(TSS, 2),
EndDistance = EndDistance,
IsCompleted = IsCompleted,
//TrainingTime = TimeHelper.FormatSeconds(RiderDatas.Last().Ticks),//训练时间
NormalizedPower = Math.Floor(NP),//标准化功率
AveragePower = averagePower,//平均功率
MaxPower = RiderDatas.Max(a => a._Power),//最大功率
NormalizedPower = Math.Floor(NP), //标准化功率
AveragePower = averagePower, //平均功率
MaxPower = RiderDatas.Max(a => a._Power), //最大功率
WeightKg = Math.Round(averagePower / App.CurrentUser.Weight, 2),
Weight = App.CurrentUser.Weight,
BicycleWeight = App.CurrentUser.BicycleWeight,
@ -169,14 +181,14 @@ namespace Assets.Scenes.Ride.Scripts.Model
ContinueIndex = ContinueIndex,
IsDelete = false,
MapCompetitionId = Competitionid,
ManufacturerName = ManufacturerName,//manufacturer == null ? "" : manufacturer.Name,
ManufacturerName = ManufacturerName,
DeviceNumber = DeviceNumber,
IsRanking = IsNeedRanking,
CurrentRouteStartDistance = CurrentRouteStartDistance,
ManufacturerId = ManufacturerId,
AntModelId = AntModelId,
StartTime = StartTime,
CreateTime = EndTime, //StartTime.AddSeconds(ticks),
CreateTime = EndTime,
Ticks = ticks,
Mode = cyclingModel.ToString(),
Param = Newtonsoft.Json.JsonConvert.SerializeObject(selectParam),
@ -185,10 +197,10 @@ namespace Assets.Scenes.Ride.Scripts.Model
LastFrame = LastFrame,
RoomId = RoomId
};
//var range = new MapSpeedRange().GetSpeedRange(RiderDatas, CurrentRoute.RouteInstance.Distance);
interruptRecord.SpeedRange = null; //JsonConvert.SerializeObject(range);
double process = Math.Round((EndDistance - interruptRecord.CurrentRouteStartDistance) / CurrentRoute.RouteInstance.Distance, 2);
interruptRecord.SpeedRange = null;
var process = Math.Round((EndDistance - interruptRecord.CurrentRouteStartDistance) / CurrentRoute.RouteInstance.Distance, 2);
interruptRecord.Progress = process > 1 ? 1 : process;
var cadences = RiderDatas.Where(a => a._Cadence.HasValue && a._Cadence.Value > 0);
@ -196,19 +208,16 @@ namespace Assets.Scenes.Ride.Scripts.Model
{
interruptRecord.AverageCadence = Math.Round(cadences.Average(a => a._Cadence.GetValueOrDefault(0)));
}
interruptRecord.MaxCadence = Math.Round(RiderDatas.Max(a => a._Cadence.GetValueOrDefault(0)));
interruptRecord.AverageHeartRate = Math.Round(RiderDatas.Average(a => a._HeartRate.GetValueOrDefault(0)));
interruptRecord.MaxHeartRate = RiderDatas.Max(a => a._HeartRate.GetValueOrDefault(0));
//service.CreateRecordCyclingData(interruptRecord);
//保存骑行记录txt
//var path = Helper.GetDataDir("MapWorkoutRecords" + "/" + interruptRecord.Id);
//var path = PFConstants.MapWorkoutRecordFolder + "/" + interruptRecord.Id;
var files = new List<string>();
try
{
var fname = path + "/" + newFileName + ".txt";
using (var fs = new FileInfo(fname).OpenWrite())
var fileName = path + "/" + newFileName + ".txt";
using (var fs = new FileInfo(fileName).OpenWrite())
{
var stream = new StreamWriter(fs);
stream.BaseStream.Seek(0, SeekOrigin.End);
@ -218,13 +227,15 @@ namespace Assets.Scenes.Ride.Scripts.Model
}
stream.Flush();
stream.Close();
files.Add(fname);
files.Add(fileName);
}
//图片
if (!string.IsNullOrEmpty(imageName))
{
files.Add(imageName);
}
//持久化序列化对象
var recordString = JsonConvert.SerializeObject(interruptRecord);
var recordFilePath = path + "/" + "record" + ".txt";
@ -235,48 +246,44 @@ namespace Assets.Scenes.Ride.Scripts.Model
Helper.DelectDir(path);
Debug.Log(ex.Message);
}
recordData.Add(interruptRecord, files);
return SaveDataAysnc(recordData, path);
}
/// <summary>
/// 保存数据
/// </summary>
/// <param name="base64Image"></param>
/// <returns></returns>
public int SaveDataAysnc(Dictionary<MapInterruptRecord, List<string>> data,string path)
public int SaveDataAysnc(Dictionary<MapInterruptRecord, List<string>> data, string path)
{
if (data != null && data.Count > 0)
if (data == null || data.Count <= 0) return -1;
try
{
//var path = Helper.GetDataDir("MapWorkoutRecords");
//Task.Run(() => {
try
{
var record = data.FirstOrDefault();
MapInterruptRecordApi service = new MapInterruptRecordApi();
var result = service.Add(record.Key, record.Value);
var record = data.FirstOrDefault();
var service = new MapInterruptRecordApi();
var result = service.Add(record.Key, record.Value);
if (result.result)
if (result.result)
{
if (Directory.Exists(path))
{
//删除文件
if (Directory.Exists(path))
{
Helper.DelectDir(path);
}
//});
return result.data.RankingId;
Helper.DelectDir(path);
}
}
catch (Exception e)
{
Debug.Log(e.Message);
return result.data.RankingId;
}
}
catch (Exception e)
{
Debug.Log(e.Message);
}
return -1;
}
}
public class TempRecordData
{
public int RoomId { get; set; }

View File

@ -26,6 +26,18 @@ namespace Assets.Scenes.Ride.Scripts
}
return path;
}
public static string CreateFileIfNotExsit(string path)
{
if (!File.Exists(path))
{
File.CreateText(path);
}
return path;
}
public double Distance(Vector2d from, Vector2d to)
{
var pt1 = Turf.Point(new double[] { from.x, from.y });

View File

@ -1,14 +1,6 @@
using Assets.Scenes.Ride.Scripts;
using Assets.Scripts;
using Assets.Scripts.Apis.Models;
using Cysharp.Threading.Tasks;
using PolyAndCode.UI;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using Assets.AR;
using Assets.Core;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
@ -36,24 +28,49 @@ class GameRoomDownLoad : MonoBehaviour
public string FileName { get; set; }
public string RouteName { get; set; }
private string FileUrl { get; set; }
private float _videoProgress;
public float VideoProgress
{
get => _videoProgress;
set
{
_videoProgress = value;
ComputeProgress();
}
}
private float _dataProgress = 0f;
public float DataProgress
{
get => _dataProgress;
set
{
_dataProgress = value;
ComputeProgress();
}
}
private float _routeProgress = 0f;
public float RouteProgress
{
get => _routeProgress;
set
{
_routeProgress = value;
ComputeProgress();
}
}
private void Start()
{
UIManager.AddEvent(downloadBtn, EventTriggerType.PointerClick, DownloadClick);
UIManager.AddEvent(downloadBtn, EventTriggerType.PointerClick, Download_Click);
UIManager.AddEvent(backBtn, EventTriggerType.PointerClick, CancelClick);
UIManager.AddEvent(runInbackBtn, EventTriggerType.PointerClick, (d) =>
{
gameObject.SetActive(false);
downLoadList.SetActive(true);
//var content = downLoadList.transform.Find("Viewport/Content");
//var list = FindObjectsOfType<GameRoomDownloadTask>();
////新增
//var obj = list.Where(c => c.RoomId == RoomId).FirstOrDefault();
//if (obj == null)
//{
// var newtask = Instantiate(downLoadTask, content);
// newtask.GetComponent<GameRoomDownloadTask>().Init(RoomId, FileName,gameObject);
//}
});
UIManager.AddEvent(enterBtn, EventTriggerType.PointerClick, (ee) =>
@ -69,22 +86,17 @@ class GameRoomDownLoad : MonoBehaviour
});
}
private void Update()
{
//if (!string.IsNullOrEmpty(FileName) && Loom.DownloadStack.ContainsKey(FileName))
//{
// processing(Loom.DownloadStack[FileName]);
//}
}
public void Init(int Id, string fileName, string url, GameRoomListController gameRoomListController, string routeName)
private ARDownloader _downloader;
public void Init(int Id, string fileName, string url, GameRoomListController gameRoomListController, string routeName,ARDownloader downloader)
{
RoomId = Id;
FileName = fileName;
RouteName = routeName;
FileUrl = url;
manager = gameRoomListController;
_downloader = downloader;
}
public void ComeIntoStep2(string fileName, string routeName)
{
FileName = fileName;
@ -101,42 +113,47 @@ class GameRoomDownLoad : MonoBehaviour
step3.SetActive(false);
downloadSlider.value = 0f;
}
private void CancelClick(BaseEventData data)
{
Reset();
gameObject.SetActive(false);
}
private void processing(float p)
{
downloadSlider.value = p;
if (!Loom.DownloadStack.ContainsKey(FileName) )
{
Loom.DownloadStack.Add(FileName, new Assets.Core.DownloadInfo(FileName, RouteName, p));
}
Loom.DownloadStack[FileName].Process = p;
if (p >= 1)
{
Loom.DownloadStack.Remove(FileName);
}
}
private async void DownloadClick(BaseEventData baseEventData)
private async void Download_Click(BaseEventData baseEventData)
{
step2.SetActive(true);
step2.transform.Find("RouteName").GetComponent<Text>().text = RouteName;
var filepath = PFConstants.VideoFolder + "/" + FileName;
await Loom.DownloadToFileAsync(FileUrl, filepath, Progress.Create<float>(x => {
processing(x);
if (x >= 1)
{
downloadSlider.value = 100;
step3.SetActive(true);
step3.transform.Find("RouteName").GetComponent<Text>().text = RouteName;
}
}));
downloadSlider.maxValue = _downloader.Total * 100;
if(_downloader.IsVideoDownloaded)
await _downloader.DownLoadVideoAsync(Progress.Create<float>(x => VideoProgress = x));
if(_downloader.IsARRouteDownloaded)
await _downloader.DownLoadARRouteAsync(Progress.Create<float>(x=> RouteProgress = x));
if(_downloader.IsARDataDownloaded)
await _downloader.DownLoadARDataAsync(Progress.Create<float>(x=> DataProgress = x));
}
private void ComputeProgress()
{
//update progress
var progress = (_videoProgress + _routeProgress + _dataProgress) / _downloader.Total;
downloadSlider.value = progress * 100;
//update download info
if (!Loom.DownloadStack.ContainsKey(FileName) )
{
Loom.DownloadStack.Add(FileName, new DownloadInfo(FileName, RouteName,progress));
}
Loom.DownloadStack[FileName].Process = progress;
if (progress >= 1)
{
Loom.DownloadStack.Remove(FileName);
downloadSlider.value = 100;
step3.SetActive(true);
step3.transform.Find("RouteName").GetComponent<Text>().text = RouteName;
}
}
}

View File

@ -1,25 +1,25 @@
using Assets.AR;
using Assets.Scenes.Ride.Scripts;
using System;
using Assets.Scripts;
using Assets.Scripts.Apis;
using Assets.Scripts.Apis.Models;
using Cysharp.Threading.Tasks;
using DG.Tweening;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Assets.AR;
using Assets.Core;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameRoomMapItem : MonoBehaviour, IPointerExitHandler, IPointerEnterHandler, IPointerUpHandler
{
private GameObject downloadBtn;
private GameObject downloading;
private GameObject downloadTxt;
public class PropNames
{
public static List<string> icons = new List<string> { "icon1", "icon2", "icon3" };
@ -29,27 +29,66 @@ public class GameRoomMapItem : MonoBehaviour, IPointerExitHandler, IPointerEnter
{ true,"#ffffff"},{false,"#5c5c6e" }
};
}
public bool isModal = false;
float width;
float height;
GameRoomListController listController;
void Awake()
{
width = this.GetComponent<RectTransform>().rect.width;
height = this.GetComponent<RectTransform>().rect.height;
}
void Start()
{
listController = FindObjectOfType<GameRoomListController>();
}
float? localY = null;
MapRoute map;
Dictionary<string, Texture> caches;
private MapRouteAreaItem area = null;
Slider slider;
GameRoomListController listController;
NearRouteModel model { get; set; }
private ARDownloader downloader { get; set; }
private float _videoProgress;
public float VideoProgress
{
get => _videoProgress;
set
{
_videoProgress = value;
ComputeProgress();
}
}
private float _dataProgress = 0f;
public float DataProgress
{
get => _dataProgress;
set
{
_dataProgress = value;
ComputeProgress();
}
}
private float _routeProgress = 0f;
public float RouteProgress
{
get => _routeProgress;
set
{
_routeProgress = value;
ComputeProgress();
}
}
private void Awake()
{
width = this.GetComponent<RectTransform>().rect.width;
height = this.GetComponent<RectTransform>().rect.height;
}
private void Start()
{
listController = FindObjectOfType<GameRoomListController>();
}
public void Initial(MapRoute myMap, Dictionary<string, Texture> caches, MapRouteAreaItem area = null)
{
if (caches != null)
@ -63,30 +102,14 @@ public class GameRoomMapItem : MonoBehaviour, IPointerExitHandler, IPointerEnter
transform.GetComponent<Button>().onClick.RemoveAllListeners();
transform.GetComponent<Button>().onClick.AddListener(Ride);
}
var downloadBtn = transform.Find("DownLoadModal/DownLoad").gameObject;
var downloading = transform.Find("DownLoadModal/Downloading").gameObject;
var downloadTxt = transform.Find("DownLoadModal/Text").gameObject;
downloadBtn = transform.Find("DownLoadModal/DownLoad").gameObject;
downloading = transform.Find("DownLoadModal/Downloading").gameObject;
downloadTxt = transform.Find("DownLoadModal/Text").gameObject;
slider = transform.Find("DownLoadModal/Slider").GetComponent<Slider>();
var fileName = map.FileName;
var path = PFConstants.VideoFolder;
var filepath = path + "/" + fileName;
transform.Find("DownLoadModal").gameObject.SetActive(map.EnableAR && !File.Exists(filepath));
UIManager.AddEvent(downloadBtn, EventTriggerType.PointerClick, async (e) =>
{
downloadBtn.SetActive(false);
downloading.SetActive(true);
downloadTxt.SetActive(false);
slider.gameObject.SetActive(true);
var progress = Progress.Create<float>(x =>
{
Loom.DownloadStack[map.FileName].Process = x;
slider.value = x;
LayoutRebuilder.ForceRebuildLayoutImmediate((RectTransform)this.slider.transform);
transform.Find("DownLoadModal").gameObject.SetActive(x!=1);
});
await Loom.DownloadLoadARDataAndVideo(map, progress);
});
ShowDownloadModal();
UIManager.AddEvent(downloadBtn, EventTriggerType.PointerClick,Download_Click);
transform.Find("Name").GetComponent<Text>().text = myMap.Name;
transform.Find("IdContainer/Text").GetComponent<Text>().text = $"#{myMap.Id}";
@ -104,16 +127,7 @@ public class GameRoomMapItem : MonoBehaviour, IPointerExitHandler, IPointerEnter
tabContainer.Find("3d").gameObject.SetActive(myMap.Enable3D && !myMap.EnableAR);
tabContainer.Find("AR").gameObject?.SetActive(myMap.EnableAR);
tabContainer.Find("Country").GetComponent<RawImage>().texture = UIManager.Instance.loginRegOptions.GetCountryImage(myMap.CountryCode);
//if (!isModal)
//{
// transform.Find("CollectImg").GetComponent<Button>().onClick.RemoveAllListeners();
// transform.Find("CollectImg").GetComponent<Button>().onClick.AddListener(Collect);
//}
//transform.Find("CollectImg").Find("Image").GetComponent<Image>().sprite =
// UIManager.Instance.collectDict[myMap.IsFavorite];
#if !(UNITY_ANDROID || UNITY_IOS)
//transform.Find("CollectImg").gameObject.SetActive(false);
#endif
transform.Find("BtnInfo").GetComponent<Button>().onClick.AddListener(Info);
transform.Find("BtnRide").GetComponent<Button>().onClick.AddListener(Ride);
@ -126,7 +140,45 @@ public class GameRoomMapItem : MonoBehaviour, IPointerExitHandler, IPointerEnter
transform.Find("BtnContinue").gameObject.SetActive(isRecent);
transform.Find("BtnReride").gameObject.SetActive(isRecent);
transform.Find("MapHBImg").gameObject.SetActive(!isRecent);
}
private async void Download_Click(BaseEventData data)
{
downloadBtn.SetActive(false);
downloading.SetActive(true);
downloadTxt.SetActive(false);
slider.gameObject.SetActive(true);
slider.maxValue = downloader.Total * 100;
if(downloader.IsVideoDownloaded)
await downloader.DownLoadVideoAsync(Progress.Create<float>(x => VideoProgress = x));
if(downloader.IsARRouteDownloaded)
await downloader.DownLoadARRouteAsync(Progress.Create<float>(x=> RouteProgress = x));
if(downloader.IsARDataDownloaded)
await downloader.DownLoadARDataAsync(Progress.Create<float>(x=> DataProgress = x));
}
private void ComputeProgress()
{
//update progress
var progress = (_videoProgress + _routeProgress + _dataProgress) / downloader.Total;
slider.value = progress * 100;
//update download info
if(!Loom.DownloadStack.ContainsKey(map.FileName))
Loom.DownloadStack.Add(map.FileName, new DownloadInfo(map.FileName,map.Name,0, map.Url));
Loom.DownloadStack[map.FileName].Process = progress;
//refresh slider
LayoutRebuilder.ForceRebuildLayoutImmediate((RectTransform)this.slider.transform);
transform.Find("DownLoadModal").gameObject.SetActive(progress != 1);
}
//显示下载面板
private async void ShowDownloadModal()
{
if(!map.EnableAR) return;
downloader = new ARDownloader(map);
var showDownload = await downloader.CheckAllAsync();
transform.Find("DownLoadModal").gameObject.SetActive(showDownload);
}
private void Ride()
@ -206,7 +258,7 @@ public class GameRoomMapItem : MonoBehaviour, IPointerExitHandler, IPointerEnter
transform.Find("Shadow").GetComponent<Image>().DOFade(1, 0.3f);
#endif
}
void SetActive4Button(bool b)
private void SetActive4Button(bool b)
{
transform.Find("MapHBImg").gameObject.SetActive(!b);
transform.Find("BtnInfo").gameObject.SetActive(b);
@ -228,7 +280,7 @@ public class GameRoomMapItem : MonoBehaviour, IPointerExitHandler, IPointerEnter
transform.Find("MapHBImg").GetComponent<RawImage>().color = Utils.HexToColorHtml(
PropNames.colorDict[isFav]);
}
async void Collect()
private async void Collect()
{
JsonResult<object> r;
//transform.Find("CollectImg").GetComponent<Button>().enabled = false;
@ -256,7 +308,6 @@ public class GameRoomMapItem : MonoBehaviour, IPointerExitHandler, IPointerEnter
UIManager.ShowAlert("WARNING", r.errMsg);
}
}
NearRouteModel model { get; set; }
public void Show(Vector3 position, NearRouteModel model)
{

View File

@ -9,8 +9,9 @@ using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using Assets.Scenes.Ride.Scripts.Model;
using UnityEngine.EventSystems;
public class GameRoomDetailController : PFUIPanel
public class GameRoomDetailController : PFUIPanel,IProgress<float>
{
public Text versionText;
public RawImage Avatar;
@ -27,12 +28,10 @@ public class GameRoomDetailController : PFUIPanel
public Text bestTabName;
public Text bestTabTimer;
public RawImage bestTabHead;
public GameObject readyBtn;
public GameObject startBtn;
public GameObject quitBtn;
public GameObject cancelBtn;
public GameObject d2;
public GameObject d3;
public GameObject ar;
@ -40,68 +39,80 @@ public class GameRoomDetailController : PFUIPanel
public GameRoomModel GameRoom { get; set; }
public DateTime? StartTime { get; set; }
public int Status { get; set; }
private bool isQuit = false;
private bool DataSourceChanged { get; set; }
float timer = 0f;
private bool isOwner { get; set; } = true;
private MapRoute route { get; set; }
protected override void Start()
{
base.Start();
UIManager.AddBackHandler((e) =>
{
MapUDPService.SendGameRoomKick(GameRoom.RoomId,App.CurrentUser.Id,App.CurrentUser.Id);
App.gameRoomDetail = null;
});
UIManager.AddBackHandler(QuitGameRoom);
transform.Find("MainNav").GetComponent<MainNav>().ShowBack();
Utils.DisplayHead(Avatar, App.CurrentUser.WxHeadImg);
UIManager.AddEvent(startBtn, UnityEngine.EventSystems.EventTriggerType.PointerClick, (e) =>
UIManager.AddEvent(startBtn, UnityEngine.EventSystems.EventTriggerType.PointerClick,StartRide);
UIManager.AddEvent(readyBtn, UnityEngine.EventSystems.EventTriggerType.PointerClick,Ready);
UIManager.AddEvent(cancelBtn, UnityEngine.EventSystems.EventTriggerType.PointerClick,CancelReady);
UIManager.AddEvent(quitBtn, UnityEngine.EventSystems.EventTriggerType.PointerClick,LeaveGameRoom);
}
private void LeaveGameRoom(BaseEventData data)
{
if (Status == 1)
{
var list = GameRoom.RoomPlayerList;
var notReady = list.Where(c => c.Status == 0 && c.IsOwner == false).Any();
if (notReady)
{
Utils.showToast(gameObject, App.GetLocalString("have to wait for all ready!"));
}
else
{
MapUDPService.SendGameRoomStartTime(App.gameRoomDetail.RoomId, App.CurrentUser.Id);
}
});
//准备
UIManager.AddEvent(readyBtn, UnityEngine.EventSystems.EventTriggerType.PointerClick, (e) =>
Utils.showToast(gameObject, App.GetLocalString("CancelReadyToQuit"));
return;
}
App.gameRoomDetail = null;
isQuit = true;
MapUDPService.SendGameRoomKick(GameRoom.RoomId, App.CurrentUser.Id, App.CurrentUser.Id);
UIManager.ShowGameRoomListPanel();
}
private void CancelReady(BaseEventData data)
{
Status = 0;
readyBtn.SetActive(true);
cancelBtn.SetActive(false);
MapUDPService.SendGameRoomReadyStatus(App.gameRoomDetail.RoomId, App.CurrentUser.Id, Status);
}
private void Ready(BaseEventData data)
{
Status = 1;
readyBtn.SetActive(false);
cancelBtn.SetActive(true);
MapUDPService.SendGameRoomReadyStatus(App.gameRoomDetail.RoomId, App.CurrentUser.Id, Status);
}
private void StartRide(BaseEventData data)
{
var list = GameRoom.RoomPlayerList;
var notReady = list.Any(c => c.Status == 0 && c.IsOwner == false);
if (notReady)
{
Status = 1;
readyBtn.SetActive(false);
cancelBtn.SetActive(true);
MapUDPService.SendGameRoomReadyStatus(App.gameRoomDetail.RoomId, App.CurrentUser.Id, Status);
});
//取消准备
UIManager.AddEvent(cancelBtn, UnityEngine.EventSystems.EventTriggerType.PointerClick, (e) =>
Utils.showToast(gameObject, App.GetLocalString("have to wait for all ready!"));
}
else
{
Status = 0;
readyBtn.SetActive(true);
cancelBtn.SetActive(false);
MapUDPService.SendGameRoomReadyStatus(App.gameRoomDetail.RoomId, App.CurrentUser.Id, Status);
});
//退出
UIManager.AddEvent(quitBtn, UnityEngine.EventSystems.EventTriggerType.PointerClick, (e) =>
{
if (Status == 1)
{
Utils.showToast(gameObject, App.GetLocalString("CancelReadyToQuit"));
return;
}
App.gameRoomDetail = null;
isQuit = true;
MapUDPService.SendGameRoomKick(GameRoom.RoomId, App.CurrentUser.Id, App.CurrentUser.Id);
UIManager.ShowGameRoomListPanel();
});
MapUDPService.SendGameRoomStartTime(App.gameRoomDetail.RoomId, App.CurrentUser.Id);
}
}
private void QuitGameRoom(string e)
{
MapUDPService.SendGameRoomKick(GameRoom.RoomId,App.CurrentUser.Id,App.CurrentUser.Id);
App.gameRoomDetail = null;
}
public override void Show()
{
base.Show();
Init();
Initialize();
}
private bool DataSourceChanged { get; set; }
private void ListenerHandler(List<ReceiveMsgModel> message)
{
var detail = message.FirstOrDefault();
@ -113,22 +124,24 @@ public class GameRoomDetailController : PFUIPanel
GameRoom = null;
return;
}
GameRoom = detail.RoomList.Where(c => c.RoomId == GameRoom.RoomId).FirstOrDefault();
GameRoom = detail.RoomList.FirstOrDefault(c => c.RoomId == GameRoom.RoomId);
if (GameRoom != null)
{
DataSourceChanged = true;
}
}
float timer = 0f;
private void Update()
{
TcpHandler();
}
private void Init()
private void Initialize()
{
DataSourceChanged = false;
App.Model = "GameRoom";
versionText.text = "V"+App.AppVersion.ToString();
versionText.text = "V"+App.AppVersion;
MapUDPService.MessageListener = ListenerHandler;
GameRoom = App.gameRoomDetail;
@ -150,17 +163,13 @@ public class GameRoomDetailController : PFUIPanel
distanceText.text = $"{Math.Round(GameRoom.Distance, 1)}KM";
eleText.text = $"{Math.Round(GameRoom.TotalClimb, 0)}M";
slopeText.text = $"{Math.Round(GameRoom.AverageGrade, 1)}%";
//查询某线路最佳
var result = ConfigHelper.mapApi.GetMapBestInfo(GameRoom.MapRouteId);
if (result.result && !string.IsNullOrEmpty(result.data.BestNickName))
{
bestTab.SetActive(true);
bestTabName.text = result.data.BestNickName;
bestTabTimer.text = result.data.BestTotalTime;
Utils.DisplayHead(bestTabHead, result.data.BestWxHeadImg);
}
Utils.DisplayHead(altitudeGraph, GameRoom.AltitudeGraph);
RenderBestPlayer();
GetMapRoute();
Utils.DisplayHead(altitudeGraph, GameRoom.AltitudeGraph);
var playerList = FindObjectsOfType<GameRoomPlayerPanel>();
foreach (var item in playerList)
{
@ -174,94 +183,126 @@ public class GameRoomDetailController : PFUIPanel
}
}
}
private bool isOwner { get; set; } = true;
private void TcpHandler()
private void GetMapRoute()
{
if (DataSourceChanged)
var result = ConfigHelper.mapApi.GetById(GameRoom.MapRouteId);
if (result != null && result.result)
{
//房间被销毁了返回房间列表
if (GameRoom == null)
{
Debug.Log("GameRoom == null");
UIManager.ShowGameRoomListPanel();
return;
}
var playerList = FindObjectsOfType<GameRoomPlayerPanel>();
var list = GameRoom.RoomPlayerList;
//自己是否是房主
var mine = list.Where(c => c.UserId == App.CurrentUser.Id).FirstOrDefault();
if (mine != null && isOwner != mine.IsOwner)
{
isOwner = mine.IsOwner;
readyBtn.SetActive(!isOwner);
startBtn.SetActive(isOwner);
cancelBtn.SetActive(!isOwner);
}
//新增/更新
foreach (var item in list)
{
var userName =item.Name;
var headurl = item.WxHeadImage;
var ftp = item.FTP;
var weight = item.Weight;
var sex = item.Sex;
var current = playerList.Where(c => c.UserId == item.UserId).FirstOrDefault();
if (current == null)
{
var s = playerList.Where(c => c.UserId == 0 && c.NotUse == false).OrderBy(c => c.sort).FirstOrDefault();
if (s != null)
{
s.Init(item.UserId, userName, headurl, weight, ftp, item.IsOwner, item.Status,sex);
}
}
else
{
current.UpdatePlayer(item.UserId, userName, headurl, weight, ftp, item.IsOwner, item.Status,sex);
}
}
//删除
foreach (var item in playerList)
{
if (item.UserId != 0)
{
var current = list.Where(c => c.UserId == item.UserId).FirstOrDefault();
if (current == null)
{
item.ShowInviteModal();
}
var myself = list.Where(c => c.UserId == App.CurrentUser.Id).FirstOrDefault();
//被移除房间/掉线
if (myself == null)
{
if (!isQuit)
{
Utils.showToast(gameObject, App.GetLocalString("you have been kicked out of the room"));
UIManager.ShowGameRoomListPanel();
}
}
}
}
//收到服务器开始命令进入 loading页面
if (GameRoom.Status == 1)
{
//路线
App.RouteIdParam = GameRoom.MapRouteId;
App.MainSceneParam["Name"] = "GameRoomList";
if (GameRoom.EnableAR)
{
SceneManager.LoadScene("VideoPlay");
}
else
{
SceneManager.LoadScene("Ride");
}
}
DataSourceChanged = false;
this.route = result.data;
}
}
//查询某线路最佳
private void RenderBestPlayer()
{
var result = ConfigHelper.mapApi.GetMapBestInfo(GameRoom.MapRouteId);
if (result.result && !string.IsNullOrEmpty(result.data.BestNickName))
{
bestTab.SetActive(true);
bestTabName.text = result.data.BestNickName;
bestTabTimer.text = result.data.BestTotalTime;
Utils.DisplayHead(bestTabHead, result.data.BestWxHeadImg);
}
}
private void TcpHandler()
{
if (!DataSourceChanged) return;
//房间被销毁了返回房间列表
if (GameRoom == null)
{
UIManager.ShowGameRoomListPanel();
return;
}
var playerList = FindObjectsOfType<GameRoomPlayerPanel>();
SetRoomOwner();
CreatePlayers(playerList);
RemovePlayers(playerList);
LoadScene();
DataSourceChanged = false;
}
//自己是否是房主
private void SetRoomOwner()
{
var mine = GameRoom.RoomPlayerList.FirstOrDefault(c => c.UserId == App.CurrentUser.Id);
if (mine != null && isOwner != mine.IsOwner)
{
isOwner = mine.IsOwner;
readyBtn.SetActive(!isOwner);
startBtn.SetActive(isOwner);
cancelBtn.SetActive(!isOwner);
}
}
//收到服务器开始命令进入 loading页面
private void LoadScene()
{
if (GameRoom.Status != 1) return;
App.RouteIdParam = GameRoom.MapRouteId;
App.MainSceneParam["Name"] = "GameRoomList";
SceneManager.LoadScene(GameRoom.EnableAR?"VideoPlay":"Ride");
}
private void CreatePlayers(GameRoomPlayerPanel[] playerList)
{
foreach (var item in GameRoom.RoomPlayerList)
{
var userName = item.Name;
var headurl = item.WxHeadImage;
var ftp = item.FTP;
var weight = item.Weight;
var sex = item.Sex;
var current = playerList.Where(c => c.UserId == item.UserId).FirstOrDefault();
if (current == null)
{
var s = playerList.Where(c => c.UserId == 0 && c.NotUse == false).OrderBy(c => c.sort).FirstOrDefault();
if (s != null)
{
s.Init(item.UserId, userName, headurl, weight, ftp, item.IsOwner, item.Status,sex);
}
}
else
{
current.UpdatePlayer(item.UserId, userName, headurl, weight, ftp, item.IsOwner, item.Status,sex);
}
}
}
private void RemovePlayers(GameRoomPlayerPanel[] playerList)
{
foreach (var item in playerList)
{
if (item.UserId == 0) continue;
var current = GameRoom.RoomPlayerList.FirstOrDefault(c => c.UserId == item.UserId);
if (current == null)
{
item.ShowInviteModal();
}
//被移除房间/掉线
var myself = GameRoom.RoomPlayerList.FirstOrDefault(c => c.UserId == App.CurrentUser.Id);
if (myself == null && !isQuit )
{
Utils.showToast(gameObject, App.GetLocalString("you have been kicked out of the room"));
UIManager.ShowGameRoomListPanel();
}
}
}
protected override void OnDisable()
{
App.Model = "";
}
public void Report(float value)
{
Debug.Log(value);
}
}

View File

@ -7,10 +7,9 @@ using Assets.Scripts.UI.Control;
using Assets.Scripts.UI.Prefab.MapList;
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Assets.AR;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
@ -54,7 +53,6 @@ public class GameRoomListController : PFUIPanel
private GameObject right;
private GameObject right2;
private GameObject btn30;
private GameObject btn40;
private GameObject btn50;
@ -70,7 +68,6 @@ public class GameRoomListController : PFUIPanel
private Transform enterRoomModal;
private Transform cycingRoomModal;
private Transform resultRoomModal;
private GameObject RoomRankItem;
private string RoomName;
@ -100,10 +97,8 @@ public class GameRoomListController : PFUIPanel
GameObject _avatar;
private GameObject RoomCell { get; set; }
//Dummy data List
private List<GameRoomModel> _list = new List<GameRoomModel>();
private string seachName = "";
private bool ListChanged = false;
private bool CreateRoomSuccessed = false;
@ -162,7 +157,6 @@ public class GameRoomListController : PFUIPanel
var g = Instantiate(RoomCell, _rectTransform);
g.GetComponent<GameRoomCell>().ConfigureCell(item, index);
}
}
List<GameRoomModel> list;
private int Total { get; set; }
@ -268,7 +262,13 @@ public class GameRoomListController : PFUIPanel
CreateClicked = false;
UIManager.ShowGameRoomDetailPanel();
}
//LOOM中取数据渲染下载当前下载进度
RenderDownloadTask();
}
//LOOM中取数据渲染下载当前下载进度
private void RenderDownloadTask()
{
var downLoadList = transform.Find("DownLoadList").gameObject;
var list = Loom.DownloadStack.Where(c => c.Value.Process < 1);
if (list.Count() > 0)
@ -736,24 +736,30 @@ public class GameRoomListController : PFUIPanel
enterRoomModal.gameObject.SetActive(true);
}
//如果本地没有视频文件则弹窗下载否则直接进入房间
public void ShowDownLoadConfirm()
public async void ShowDownLoadConfirm()
{
var fileName = GameRoom.FileName;
var path = PFConstants.VideoFolder;
var filepath = path + "/" + fileName;
if(!GameRoom.EnableAR) return ;
var result = ConfigHelper.mapApi.GetById(GameRoom.MapRouteId);
if (!result.result) return;
var route = result.data;
var download = transform.Find("DownloadPanel").gameObject;
if (GameRoom.EnableAR && !File.Exists(filepath))
downloader = new ARDownloader(route);
var showDownload = await downloader.CheckAllAsync();
if (showDownload)
{
//查询fileUrl
GameRoom.FileUrl = ConfigHelper.GameRoomApi.GetMapVideoURL(GameRoom.MapRouteId).data;
GameRoom.FileUrl = route.Url;
download.SetActive(true);
download.GetComponent<GameRoomDownLoad>().Init(RoomId, GameRoom.FileName, GameRoom.FileUrl, this, GameRoom.MapRouteName);
download.GetComponent<GameRoomDownLoad>().Init(RoomId, GameRoom.FileName, GameRoom.FileUrl, this, GameRoom.MapRouteName,downloader);
}
else
{
GetInRoom();
}
}
private ARDownloader downloader;
private GameRoomModel modelGameRoom { get; set; }
//private GameRoomModel modelGameRoom { get; set; }
private Text CyclingTimer { get; set; }
@ -788,7 +794,6 @@ public class GameRoomListController : PFUIPanel
}
}
}
private void ShowStatusModal(Transform tr,GameRoomModel data)
{
tr.Find("Modal/Id").GetComponent<Text>().text = data.RoomId.ToString().PadLeft(7, '0');
@ -818,8 +823,6 @@ public class GameRoomListController : PFUIPanel
UIManager.AddEvent(quit, EventTriggerType.PointerClick, (e) => { tr.gameObject.SetActive(false); });
tr.gameObject.SetActive(true);
}
public void GetInRoom()
{
//1.房间人数已经满了
@ -841,13 +844,11 @@ public class GameRoomListController : PFUIPanel
MapUDPService.SendJoinGameRoom(App.gameRoomDetail.RoomId,App.CurrentUser.Id,UIManager.Now.GetDateTime().ToUniversalTime());
UIManager.ShowGameRoomDetailPanel();
}
public void SelectRoom(GameRoomModel room)
{
GameRoom = room;
RoomId = GameRoom.RoomId;
}
public int GetRoomId()
{
return RoomId;
@ -886,7 +887,6 @@ public class GameRoomListController : PFUIPanel
tabContainer.Find("Country").GetComponent<RawImage>().texture = UIManager.Instance.loginRegOptions.GetCountryImage(map.CountryCode);
}
public void Refresh()
{
content.transform.DestroyChildren();
@ -894,14 +894,12 @@ public class GameRoomListController : PFUIPanel
isEnd = false;
GetList();
}
private void onEndEdit()
{
var t = searchInput.GetComponent<InputField>().text;
ftname = t;
Refresh();
}
private void DisplayMaps(List<MapRoute> list)
{
if (map != null)

View File

@ -7,6 +7,7 @@ using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
@ -171,7 +172,7 @@ public class Loom : MonoBehaviour
}
}
public static Dictionary<string, DownloadInfo> DownloadStack = new Dictionary<string, DownloadInfo>();
public static Dictionary<string, DownloadInfo> DownloadStack { get; set; } = new Dictionary<string, DownloadInfo>();
public static async Task DownloadLoadARDataAndVideo(MapRoute route,IProgress<float> progress)
{
@ -184,7 +185,6 @@ public class Loom : MonoBehaviour
var cancelToken = new CancellationTokenSource();
var dataPath = $"{localPath}/{route.Id}";
Helper.CreateDirectoryIfNotExsit(dataPath);
Helper.CreateDirectoryIfNotExsit(dataPath);
await Loom.DownloadToFileAsync(arDataPath, $"{dataPath}/{route.Id}.json");
@ -206,9 +206,45 @@ public class Loom : MonoBehaviour
};
var request = UnityWebRequest.Get(downloadUrl);
request.method = UnityWebRequest.kHttpVerbGET;
request.downloadHandler = dh;
request.method = UnityWebRequest.kHttpVerbGET;
return await request.SendWebRequest().ToUniTask(progress, PlayerLoopTiming.Update, (cancellation?.Token ?? default(CancellationToken)));
}
/// <summary>
/// head request for file meta info such as etag.
/// </summary>
/// <param name="downloadUrl">download url</param>
/// <param name="fullPath">local path for storage</param>
/// <returns></returns>
public static async UniTask<UnityWebRequest> HeadFileAsync(string downloadUrl, string fullPath, IProgress<float> progress = null,CancellationTokenSource cancellation = default(CancellationTokenSource))
{
try
{ var etagFile = fullPath + ".etag";
var etag = string.Empty;
if (File.Exists(etagFile))
{
etag = File.ReadAllText(etagFile);
}
var request = UnityWebRequest.Head(downloadUrl);
if(!string.IsNullOrEmpty(etag))
request.SetRequestHeader("If-None-Match", etag);
var result = await request.SendWebRequest();//.ToUniTask(progress, PlayerLoopTiming.Update, (cancellation?.Token ?? default(CancellationToken)));
if (result.responseCode != (long)HttpStatusCode.NotModified)
{
etag = result.GetResponseHeader("ETag");
File.WriteAllText(fullPath+".etag",etag);
}
return result;
}
catch (Exception e)
{
return null;
}
}
}