using Assets.Scripts; using Assets.Scripts.Apis; using Assets.Scripts.Apis.Models; 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 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; } public override void InjectController(CyclingController controller) { cyclingController = controller; //渲染比赛界面 panel = Instantiate(Resources.Load("UI/Prefab/Match/CompetitonPanel"), transform); panel.transform.SetAsFirstSibling(); PFUIPanel mainPanel = panel.GetComponent(); PFUIPanel modelPanel = transform.Find("ModalPanel").GetComponent(); CompetitionDesc = panel.transform.Find("CompetitionDesc").GetComponent(); SponsorLogoImage = panel.transform.Find("SponsorTitle/Sponsor").GetComponent(); 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; if (count == 0 || applyCount == 0) { UpdateCompetition(); } //1.报名按钮 Apply.SetActive(_canJoin); //2.观看按钮 Watch.SetActive(_canWatch); //3.开始按钮 rideNow.gameObject.SetActive(_canStart); //4.详情按钮 Detail.SetActive(!_canStart); //5.取消报名按钮 (比赛开始前可以取消报名) CancelJoin.SetActive(_canCancelApply); if (_canCancelApply) { CompetitionStatus.text = "Registered"; } else { CompetitionStatus.text = competition.StatusVlaue.ToString(); } } #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; } //构建赛事界面 protected override void InitGameObject() { base.InitGameObject(); CompetitionDesc = panel.transform.Find("CompetitionDesc").GetComponent(); StartTime = panel.transform.Find("Time").GetComponent(); Contestant = panel.transform.Find("Contestant").GetComponent(); 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(); ReadMore = panel.transform.Find("ReadMore").gameObject; GetReady = panel.transform.Find("GetReady").GetComponent(); GetReadyTitle = panel.transform.Find("GetReadyTitle").GetComponent(); 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); } private bool _canCancelApply = false; private bool _raceStart = false; //查询赛事相关数据 protected override void InitGameObjectData() { var route = cyclingController.mapRoute; competition = cyclingController.competition; //赛事状态按钮控制 _canJoin = competition.CanJoin; _canStart = competition.CanStart; _canCancelApply = competition.CanCancelJoin;//可以取消预约 _raceStart = competition.Status == 3; _canWatch = competition.CanWatch; #region 赛事基本信息 //赛事名称 mapName.text = competition.Title; //距离 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(); //比赛状态 CompetitionStatus.text = competition.StatusVlaue.ToString(); //参赛选手 Contestant.text = competition.UserList.Count.ToString() + "Riders"; ShowPlayers(competition.UserList); //路书描述 mapDescText.text = competition.Dec; ReadMore.SetActive(!string.IsNullOrEmpty(competition.Dec)); //主办方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.DOFade(1, 1); //} //else { //异步请求mapbox画出当前路书路线图 DrawMapRouteAsync(route.Id, 1); } AddProcess(30); #endregion StartCoroutine(HiddenLoading()); } private int maxShowCout = 9; private void ShowPlayers(List list) { var watcher = Resources.Load("UI/Prefab/Match/Watcher"); var moreHead = Resources.Load("UI/Prefab/Match/MoreHead"); int index = 0; foreach (var item in list) { index++; if (index >= maxShowCout) { Instantiate(moreHead, PlayersPanel); break; } var head = Instantiate(watcher, PlayersPanel); var headiamge = head.GetComponent(); 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; } base.StartRide(baseEvent); } //观察 protected void StartWatch(BaseEventData baseEvent) { 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; } } protected void ApplyRace(BaseEventData baseEvent) { UIManager.ShowConfirm("Apply", "Are your want to Apply this race?", 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); UIManager.CloseConfirm(); }); } //取消预约 protected void CancelReserve(BaseEventData baseEvent) { try { UIManager.ShowConfirm("Canel Application", "Are you want to cancel this application?", async () => { MapCompetitionApi api = ConfigHelper.mapCompetitionApi; var data = api.CancelMapCompetition(cyclingController.competition.Id); if (data.result) { UpdateCompetition(); 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 } }