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 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 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); 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(); } protected override void ShowLoading() { loadingPanel = panel.transform.Find("LoadingPanel").gameObject; loadingPanel.showLoadingExtension(); } //初始化 public override void Init() { InitGameObject(); InitGameObjectData(); } //构建赛事界面 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; 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; UIManager.AddEvent(ReadMore, EventTriggerType.PointerClick, ReadCompetitionPreview); UIManager.AddEvent(Watch, EventTriggerType.PointerClick, StartWatch); UIManager.AddEvent(CancelJoin, EventTriggerType.PointerClick, CancelReserve); } private bool _canCancelApply = false; //查询赛事相关数据 protected override void InitGameObjectData() { var route = cyclingController.mapRoute; var competition = cyclingController.competition; //赛事状态按钮控制 _canJoin = competition.CanJoin; _canStart = competition.CanStart; _canCancelApply = competition.applyed;//可以取消预约 //报名或者开始比赛 rideNowText.text = _canJoin ? "Apply" : "RideNow"; //观战 var showWatch = !_canJoin && !_canStart && !_canCancelApply; Watch.SetActive(showWatch); rideNow.gameObject.SetActive(!showWatch); //取消预约 CancelJoin.SetActive(_canCancelApply); //比赛结束 if (competition.Status == 4) { rideNow.gameObject.SetActive(false); Watch.SetActive(false); CancelJoin.SetActive(false); } #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.ToLongTimeString(); //比赛状态 CompetitionStatus.text = GetENStatus(competition.StatusVlaue.ToString()); //参赛选手 Contestant.text = competition.UserList.Count.ToString() + "Players"; ShowPlayers(competition.UserList); //路书描述 mapDescText.text = route.Dec; //主办方logo 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); } } } //显示比赛详情图片:如果没有比赛详情图则显示地图 private void ShowCompetitionDescImage() { } private string GetENStatus(string status) { switch (status) { case "已结束": return "DONE"; case "未开始": return "APPLIED"; case "报名中": return "APPLLYING"; case "比赛中": return "RACING"; } return ""; } #region 事件 //先报名后开始 protected override void StartRide(BaseEventData baseEvent) { try { //先报名 if (_canJoin) { MapCompetitionApi s = new MapCompetitionApi(); var rrr = s.ApplyMapCompetition(cyclingController.competitionId); if (rrr.result) { //var competition = s.GetById(cyclingController.competitionId).data; //_canStart = competition.CanStart; _canJoin = false; //取消预约 CancelJoin.SetActive(true); } //if (_canStart) { rideNowText.text = "RideNow"; } } else { base.StartRide(baseEvent); } } catch (Exception e) { Debug.LogError(e); } } //查看赛事详情 protected void ReadCompetitionPreview(BaseEventData baseEvent) { Application.OpenURL(App.CurrentUser.WebHost + $"#/Mine/MatchPreview?id={cyclingController.competitionId}&Token={App.CurrentUser.cookie}"); } //观察 protected void StartWatch(BaseEventData baseEvent) { //隐藏当前用户 cyclingController.isWatch = true; cyclingController.player.SetActive(false); base.StartRide(baseEvent); } //取消预约 protected void CancelReserve(BaseEventData baseEvent) { try { MapCompetitionApi api = new MapCompetitionApi(); var result = api.CancelMapCompetition(cyclingController.competition.Id); if (result.result) { rideNowText.text = "Apply"; CancelJoin.SetActive(false); _canJoin = true; Debug.Log(result); //SHOW Alert } else { Debug.Log(result); //SHOW Alert } } catch (Exception ex) { Debug.Log(ex); } } #endregion } }