409 lines
17 KiB
C#
409 lines
17 KiB
C#
using Assets.Scripts;
|
|
using Assets.Scripts.Apis;
|
|
using Assets.Scripts.Apis.Models;
|
|
using DG.Tweening;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Assets.Scenes.Ride.Scripts
|
|
{
|
|
public class CompetitionLoadingController : AbstratctLoadingController
|
|
{
|
|
private GameObject Watch { get; set; }
|
|
private bool _canJoin = false;
|
|
private bool _canStart = false;
|
|
private bool _canWatch = false;
|
|
private bool _canEnter = false;
|
|
private RawImage CompetitionDesc { get; set; }
|
|
private RawImage SponsorLogoImage { get; set; }
|
|
private Text StartTime { get; set; }
|
|
private Text Contestant { get; set; }
|
|
private Text CompetitionStatus { get; set; }
|
|
private Text GetReady { get; set; }
|
|
private Text GetReadyTitle { get; set; }
|
|
private Transform PlayersPanel {get;set;}
|
|
private GameObject LeftBack { get; set; }
|
|
private GameObject MiddleBack { get; set; }
|
|
private GameObject CancelJoin { get; set; }
|
|
private GameObject ReadMore { get; set; }
|
|
|
|
CompetitionUIManager competitionUI { get; set; }
|
|
|
|
public override void InjectController(CyclingController controller)
|
|
{
|
|
cyclingController = controller;
|
|
//渲染比赛界面
|
|
#if UNITY_IOS || UNITY_ANDROID
|
|
panel = Instantiate(Resources.Load<GameObject>("UI/Prefab/Match/Mobile/CompetitonPanel"), transform);
|
|
#else
|
|
panel = Instantiate(Resources.Load<GameObject>("UI/Prefab/Match/CompetitonPanel"), transform);
|
|
#endif
|
|
panel.transform.SetAsFirstSibling();
|
|
PFUIPanel mainPanel = panel.GetComponent<PFUIPanel>();
|
|
PFUIPanel modelPanel = transform.Find("ModalPanel").GetComponent<PFUIPanel>();
|
|
CompetitionDesc = panel.transform.Find("CompetitionDesc").GetComponent<RawImage>();
|
|
SponsorLogoImage = panel.transform.Find("SponsorTitle/Sponsor").GetComponent<RawImage>();
|
|
cyclingController.SetUIManager(mainPanel, modelPanel);
|
|
}
|
|
protected override void Start()
|
|
{
|
|
base.Start();
|
|
}
|
|
MapCompetition competition { get; set; }
|
|
protected override void Update()
|
|
{
|
|
base.Update();
|
|
#region 倒计时状态
|
|
if (competition != null)
|
|
{
|
|
var count = (int)(competition.StartTime.ToLocalTime() - UIManager.Now.GetDateTime()).TotalSeconds;
|
|
if (count > 0)
|
|
{
|
|
GetReady.text = Helper.FormatTicks(count);
|
|
GetReadyTitle.gameObject.SetActive(true);
|
|
GetReady.gameObject.SetActive(true);
|
|
}
|
|
else
|
|
{
|
|
GetReadyTitle.gameObject.SetActive(false);
|
|
GetReady.gameObject.SetActive(false);
|
|
}
|
|
var applyCount = (int)(competition.StartApplyTime.ToLocalTime() - UIManager.Now.GetDateTime()).TotalSeconds;
|
|
var endApplyCount = (int)(competition.EndApplyTime.ToLocalTime() - UIManager.Now.GetDateTime()).TotalSeconds;
|
|
|
|
if (count == 0 || applyCount == 0 || endApplyCount == 0)
|
|
{
|
|
UpdateCompetition();
|
|
}
|
|
//1.报名按钮
|
|
Apply.SetActive(_canJoin);
|
|
//2.观看按钮
|
|
Watch.SetActive(_canWatch);
|
|
//3.开始按钮
|
|
rideNow.gameObject.SetActive(_canEnter);
|
|
//4.详情按钮
|
|
Detail.SetActive(!_canEnter && !_canWatch);
|
|
//5.取消报名按钮 (比赛开始前可以取消报名)
|
|
CancelJoin.SetActive(_canCancelApply);
|
|
|
|
if (_canCancelApply)
|
|
{
|
|
CompetitionStatus.text = App.GetLocalString("Registered");
|
|
}
|
|
else
|
|
{
|
|
CompetitionStatus.text = competition.StatusVlaue.ToString();
|
|
}
|
|
|
|
//Watch.GetComponent<Button>().enabled = process>=100;
|
|
Watch.GetComponent<Button>().interactable = process >= 100;
|
|
}
|
|
#endregion
|
|
}
|
|
protected override void ShowLoading()
|
|
{
|
|
loadingPanel = panel.transform.Find("LoadingPanel").gameObject;
|
|
loadingPanel.showLoadingExtension();
|
|
}
|
|
//初始化
|
|
public override void Init()
|
|
{
|
|
InitGameObject();
|
|
InitGameObjectData();
|
|
}
|
|
GameObject Detail { get; set; }
|
|
GameObject Apply { get; set; }
|
|
GameObject Watcher { get; set; }
|
|
GameObject MoreHead { get; set; }
|
|
Text CompetitionTitle { get; set; }
|
|
Text EndTime { get; set; }
|
|
Text MapId { get; set; }
|
|
//构建赛事界面
|
|
protected override void InitGameObject()
|
|
{
|
|
base.InitGameObject();
|
|
CompetitionTitle = panel.transform.Find("CompetitionName").GetComponent<Text>();
|
|
CompetitionDesc = panel.transform.Find("CompetitionDesc").GetComponent<RawImage>();
|
|
StartTime = panel.transform.Find("Time").GetComponent<Text>();
|
|
//EndTime = panel.transform.Find("EndTime").GetComponent<Text>();
|
|
Contestant = panel.transform.Find("ContestantTile").GetComponent<Text>();
|
|
Watch = panel.transform.Find("Watch").gameObject;
|
|
CancelJoin = panel.transform.Find("CancelJoin").gameObject;
|
|
Detail = panel.transform.Find("Detail").gameObject;
|
|
Apply = panel.transform.Find("Apply").gameObject;
|
|
PlayersPanel = panel.transform.Find("PlayersPanel/List/Viewport/Content");
|
|
LeftBack = panel.transform.Find("LeftBack").gameObject;
|
|
MiddleBack = panel.transform.Find("MiddleBack").gameObject;
|
|
CompetitionStatus = panel.transform.Find("CompetitionStatus").GetComponent<Text>();
|
|
ReadMore = panel.transform.Find("ReadMore").gameObject;
|
|
GetReady = panel.transform.Find("GetReady").GetComponent<Text>();
|
|
GetReadyTitle = panel.transform.Find("GetReadyTitle").GetComponent<Text>();
|
|
MapId = panel.transform.Find("MapId").GetComponent<Text>();
|
|
#if UNITY_IOS || UNITY_ANDROID
|
|
Watcher = Resources.Load<GameObject>("UI/Prefab/Match/Mobile/Watcher");
|
|
MoreHead = Resources.Load<GameObject>("UI/Prefab/Match/Mobile/MoreHead");
|
|
#else
|
|
Watcher = Resources.Load<GameObject>("UI/Prefab/Match/Watcher");
|
|
MoreHead = Resources.Load<GameObject>("UI/Prefab/Match/MoreHead");
|
|
#endif
|
|
//Watch.GetComponent<Button>().enabled = false;
|
|
|
|
Watch.GetComponent<Button>().interactable = false;
|
|
|
|
UIManager.AddEvent(ReadMore, EventTriggerType.PointerClick, ReadCompetitionPreview);
|
|
UIManager.AddEvent(Watch, EventTriggerType.PointerClick, StartWatch);
|
|
UIManager.AddEvent(Apply, EventTriggerType.PointerClick, ApplyRace);
|
|
UIManager.AddEvent(CancelJoin, EventTriggerType.PointerClick, CancelReserve);
|
|
UIManager.AddEvent(Detail, EventTriggerType.PointerClick, GoDetail);
|
|
#if UNITY_STANDALONE_WIN
|
|
UIManager.RemoveEvent(distance.gameObject);
|
|
UIManager.RemoveEvent(slope.gameObject);
|
|
UIManager.RemoveEvent(elevaction.gameObject);
|
|
UIManager.AddEvent(distance.gameObject, UnityEngine.EventSystems.EventTriggerType.PointerEnter, b =>
|
|
{
|
|
panel.transform.Find("DistanceTip").GetComponent<CanvasGroup>().DOFade(1, 0.5f);
|
|
});
|
|
UIManager.AddEvent(distance.gameObject, UnityEngine.EventSystems.EventTriggerType.PointerExit, b =>
|
|
{
|
|
panel.transform.Find("DistanceTip").GetComponent<CanvasGroup>().DOFade(0, 0.5f);
|
|
});
|
|
|
|
UIManager.AddEvent(slope.gameObject, UnityEngine.EventSystems.EventTriggerType.PointerEnter, b =>
|
|
{
|
|
panel.transform.Find("SlopeTip").GetComponent<CanvasGroup>().DOFade(1, 0.5f);
|
|
});
|
|
UIManager.AddEvent(slope.gameObject, UnityEngine.EventSystems.EventTriggerType.PointerExit, b =>
|
|
{
|
|
panel.transform.Find("SlopeTip").GetComponent<CanvasGroup>().DOFade(0, 0.5f);
|
|
});
|
|
UIManager.AddEvent(elevaction.gameObject, UnityEngine.EventSystems.EventTriggerType.PointerEnter, b =>
|
|
{
|
|
panel.transform.Find("ElevactionTip").GetComponent<CanvasGroup>().DOFade(1, 0.5f);
|
|
});
|
|
UIManager.AddEvent(elevaction.gameObject, UnityEngine.EventSystems.EventTriggerType.PointerExit, b =>
|
|
{
|
|
panel.transform.Find("ElevactionTip").GetComponent<CanvasGroup>().DOFade(0, 0.5f);
|
|
});
|
|
#endif
|
|
}
|
|
|
|
private bool _canCancelApply = false;
|
|
private bool _raceStart = false;
|
|
//查询赛事相关数据
|
|
protected override void InitGameObjectData()
|
|
{
|
|
var route = cyclingController.mapRoute;
|
|
competition = cyclingController.competition;
|
|
//赛事状态按钮控制
|
|
_canJoin = competition.CanJoin;
|
|
_canStart = competition.CanExit;
|
|
_canCancelApply = competition.CanCancelJoin;//可以取消预约
|
|
_raceStart = competition.Status == 3;
|
|
_canWatch = competition.CanWatch;
|
|
_canEnter = competition.CanEnter;
|
|
#region 赛事基本信息
|
|
//赛事名称
|
|
CompetitionTitle.text = GetMaxString(competition.Title, 35);
|
|
//线路名称
|
|
mapName.text = GetMaxString(route.Name, 45);
|
|
//距离
|
|
distance.text = route.Distance.ToString("f1") + "KM";
|
|
//海拔
|
|
elevaction.text = Math.Round(route.TotalClimb.GetValueOrDefault(0), 2).ToString() + "M";
|
|
//平均破度
|
|
slope.text = Math.Round(route.AverageGrade, 2).ToString() + "%";
|
|
//比赛开始时间
|
|
StartTime.text = competition.StartTime.ToLocalTime().ToString();//+" TO "+ competition.EndTime.ToLocalTime().ToString();
|
|
//比赛结束时间
|
|
// EndTime.text = competition.EndTime.ToLocalTime().ToString();
|
|
//比赛状态
|
|
CompetitionStatus.text = competition.StatusVlaue.ToString();
|
|
//参赛选手
|
|
Contestant.text = competition.UserList.Count.ToString() + App.GetLocalString("Riders");
|
|
ShowPlayers(competition.UserList);
|
|
//路书描述
|
|
mapDescText.text = GetMaxString(competition.Dec, 400);
|
|
//线路编号
|
|
MapId.text = $"#{route.Id.ToString()}";
|
|
|
|
ReadMore.SetActive(!string.IsNullOrEmpty(competition.CompetitionPreviewUrl));
|
|
//主办方logo
|
|
if (!string.IsNullOrEmpty(competition.SponsorCoverage))
|
|
{
|
|
Utils.DisplayImage(SponsorLogoImage, competition.SponsorCoverage, true);
|
|
}
|
|
//赛事简介
|
|
if (!string.IsNullOrEmpty(competition.ShortPreview))
|
|
{
|
|
LeftBack.SetActive(false);
|
|
MiddleBack.SetActive(false);
|
|
Utils.DisplayImage(CompetitionDesc, competition.ShortPreview, true);
|
|
var canvasGroup = CompetitionDesc.GetComponent<CanvasGroup>();
|
|
canvasGroup.DOFade(1, 1);
|
|
AddProcess(10);
|
|
}
|
|
else
|
|
{
|
|
//异步请求mapbox画出当前路书路线图
|
|
DrawMapRouteAsync(route.Id, 1);
|
|
}
|
|
AddProcess(20);
|
|
#endregion
|
|
//StartCoroutine(HiddenLoading());
|
|
}
|
|
private int maxShowCout = 9;
|
|
private void ShowPlayers(List<CompetitionPlayer> list)
|
|
{
|
|
int index = 0;
|
|
Utils.DestroyChildren(PlayersPanel);
|
|
foreach (var item in list)
|
|
{
|
|
index++;
|
|
if (index >= maxShowCout)
|
|
{
|
|
Instantiate(MoreHead, PlayersPanel);
|
|
break;
|
|
}
|
|
var head = Instantiate(Watcher, PlayersPanel);
|
|
var headiamge = head.GetComponent<RawImage>();
|
|
if (!string.IsNullOrEmpty(item.WxHeadImg))
|
|
{
|
|
Utils.DisplayImage(headiamge, item.WxHeadImg, true);
|
|
}
|
|
}
|
|
}
|
|
#region 事件
|
|
|
|
//查看赛事详情
|
|
protected void ReadCompetitionPreview(BaseEventData baseEvent)
|
|
{
|
|
Application.OpenURL(App.CurrentUser.WebHost + $"Mine/MatchPreview?id={cyclingController.competitionId}&Token={App.CurrentUser.cookie}");
|
|
}
|
|
protected override void StartRide(BaseEventData baseEvent)
|
|
{
|
|
UpdateCompetition();
|
|
if (competition.Status == 4)
|
|
{
|
|
Utils.showToast(gameObject, "Race close");
|
|
return;
|
|
}
|
|
competitionUI = cyclingController.singleUIManager as CompetitionUIManager;
|
|
competitionUI?.SetMobileViewButton(false);
|
|
base.StartRide(baseEvent);
|
|
}
|
|
//观察
|
|
protected void StartWatch(BaseEventData baseEvent)
|
|
{
|
|
if (!Watch.GetComponent<Button>().interactable)
|
|
return;
|
|
UpdateCompetition();
|
|
if (competition.Status == 4)
|
|
{
|
|
Utils.showToast(gameObject, "Race close");
|
|
return;
|
|
}
|
|
//隐藏当前用户
|
|
cyclingController.isWatch = true;
|
|
cyclingController.player.SetActive(false);
|
|
base.StartRide(baseEvent);
|
|
}
|
|
protected void GoDetail(BaseEventData baseEvent)
|
|
{
|
|
Application.OpenURL(App.CurrentUser.WebHost + $"Mine/MatchPreview?id={cyclingController.competitionId}&Token={App.CurrentUser.cookie}");
|
|
}
|
|
|
|
private void UpdateCompetition()
|
|
{
|
|
MapCompetitionApi s = ConfigHelper.mapCompetitionApi;
|
|
var result = s.GetById(cyclingController.competitionId);
|
|
if (result.result == false || result.data == null)
|
|
{
|
|
Utils.showToast(gameObject, "error when get competition information");
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
competition = result.data;
|
|
_canStart = competition.CanStart;
|
|
_canJoin = competition.CanJoin;
|
|
_canCancelApply = competition.CanCancelJoin;
|
|
_canWatch = competition.CanWatch;
|
|
_raceStart = competition.Status == 3;
|
|
_canEnter = competition.CanEnter;
|
|
|
|
Contestant.text = competition.UserList.Count.ToString() + App.GetLocalString("Riders");
|
|
ShowPlayers(competition.UserList);
|
|
}
|
|
}
|
|
protected void ApplyRace(BaseEventData baseEvent)
|
|
{
|
|
UIManager.ShowConfirm("Join", "Do you want to join?", async () =>
|
|
{
|
|
MapCompetitionApi s = ConfigHelper.mapCompetitionApi;
|
|
var rrr = s.ApplyMapCompetition(cyclingController.competitionId);
|
|
if (rrr.result)
|
|
{
|
|
UpdateCompetition();
|
|
UIManager.UpdateJoinCompetition();
|
|
Utils.showToast(gameObject, "Success", 2, 1);
|
|
}
|
|
else
|
|
{
|
|
Utils.showToast(gameObject, rrr.errMsg);
|
|
}
|
|
rideNow.gameObject.SetActive(_canStart);
|
|
Detail.SetActive(!_canStart && !_canWatch);
|
|
UIManager.CloseConfirm();
|
|
|
|
});
|
|
|
|
}
|
|
//取消预约
|
|
protected void CancelReserve(BaseEventData baseEvent)
|
|
{
|
|
try
|
|
{
|
|
UIManager.ShowConfirm("Cancel Reservation", "Do you want to cancel reservation?", async () =>
|
|
{
|
|
MapCompetitionApi api = ConfigHelper.mapCompetitionApi;
|
|
var data = api.CancelMapCompetition(cyclingController.competition.Id);
|
|
if (data.result)
|
|
{
|
|
UpdateCompetition();
|
|
App.CompetitionIdList.Remove(cyclingController.competition.Id);
|
|
UIManager.UpdateJoinCompetition();
|
|
Utils.showToast(gameObject, "Success", 2, 1);
|
|
}
|
|
else
|
|
{
|
|
Utils.showToast(gameObject, data.errMsg);
|
|
}
|
|
UIManager.CloseConfirm();
|
|
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.Log(ex);
|
|
}
|
|
}
|
|
|
|
protected override void Cancel(BaseEventData baseEvent)
|
|
{
|
|
if (App.MainSceneParam.ContainsKey("Name"))
|
|
{
|
|
App.MainSceneParam["Name"] = "RaceHomePanel";
|
|
}
|
|
else
|
|
{
|
|
App.MainSceneParam.Add("Name", "RaceHomePanel");
|
|
}
|
|
base.Cancel(baseEvent);
|
|
}
|
|
#endregion
|
|
}
|
|
}
|