147 lines
6.6 KiB
C#
147 lines
6.6 KiB
C#
using Assets.Scenes.Ride.Scripts.Model;
|
||
using Assets.Scenes.Ride.Scripts.Model.CyclingModels;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using UnityEngine;
|
||
using UnityEngine.EventSystems;
|
||
using UnityEngine.UI;
|
||
|
||
namespace Assets.Scenes.Ride.Scripts
|
||
{
|
||
public class CompetitionRankingFactory : BaseListFactory
|
||
{
|
||
private bool isInit = false;
|
||
private bool isnext = false;
|
||
private List<CompetitionRankingSortModel> rankingList {get;set;}
|
||
private AbstractPlayer currentPlayer { get; set; }
|
||
private Text Title { get; set; }
|
||
private Text OnlineUserNum { get; set; }
|
||
private Text Rank { get; set; }
|
||
private GameObject PreBtn { get; set; }
|
||
private GameObject NextBtn { get; set; }
|
||
|
||
protected override void Awake()
|
||
{
|
||
//base.Awake();
|
||
scroll = transform.Find("List").gameObject;
|
||
if (scroll != null)
|
||
{
|
||
UIManager.AddEvent(scroll, UnityEngine.EventSystems.EventTriggerType.EndDrag, OnEndDrag);
|
||
}
|
||
parent = scroll.transform.Find("Viewport/Content");
|
||
cyclingController = FindObjectOfType<CyclingController>();
|
||
playerController = FindObjectOfType<PlayerController>();
|
||
currentPlayer = cyclingController.currentPlayer;
|
||
nearByItem = Resources.Load<GameObject>("UI/Prefab/Match/CompetitionRankingItem");
|
||
nearByMajorItem = Resources.Load<GameObject>("UI/Prefab/Match/CompetitionRankingMajorItem");
|
||
Title = transform.Find("Title").GetComponent<Text>();
|
||
OnlineUserNum = transform.Find("Head/OnlineUserNum").GetComponent<Text>();
|
||
PreBtn = transform.Find("Pre").gameObject;//上一页
|
||
NextBtn = transform.Find("Next").gameObject;//下一页
|
||
Rank = transform.Find("Rank").GetComponent<Text>();
|
||
UIManager.AddEvent(PreBtn, UnityEngine.EventSystems.EventTriggerType.PointerClick, PrePage);
|
||
UIManager.AddEvent(NextBtn, UnityEngine.EventSystems.EventTriggerType.PointerClick, NextPage);
|
||
}
|
||
private void PrePage(BaseEventData baseEventData)
|
||
{
|
||
SetPreIndex();
|
||
isInit = true;
|
||
isnext = false;
|
||
CreateList();
|
||
}
|
||
private void NextPage(BaseEventData baseEventData)
|
||
{
|
||
SetNextIndex();
|
||
isInit = true;
|
||
isnext = true;
|
||
CreateList();
|
||
}
|
||
protected override void CreateList()
|
||
{
|
||
CompetitionModel s = cyclingController.cyclingController as CompetitionModel;
|
||
var ss = s?.competitionRankingModel;
|
||
rankingList = ss?.Sorts;
|
||
|
||
if (rankingList == null) return;
|
||
|
||
rankingList = rankingList.OrderByDescending(u => u.IsCompleted).ThenBy(d => d.CreateTime).ThenByDescending(d => d.EndDistance).ToList();
|
||
//当前参赛总人数
|
||
var totalCount = rankingList.Count();
|
||
OnlineUserNum.text = " "+totalCount.ToString();
|
||
//当前用户排名
|
||
var currentIndex = rankingList.Where(c => c.UserId == cyclingController.currentPlayer.UserId).FirstOrDefault()?.Index ?? 0;
|
||
totalPages = (pageSize>0?totalCount / pageSize:0) + (totalCount % pageSize> 0? 1:0);
|
||
if (!isInit)
|
||
{
|
||
//当前用户在第几页
|
||
pageIndex = (pageSize > 0 ? currentIndex / pageSize : 1) + (currentIndex % pageSize > 0 ? 1 : 0);
|
||
}
|
||
Rank.text = $"{pageIndex}/{totalPages}";
|
||
var pagedList = rankingList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
|
||
//删除
|
||
var currentList = FindObjectsOfType<CompetitionRankingItem>();
|
||
foreach (var item in currentList)
|
||
{
|
||
var e = pagedList.Where(c => c.UserId == item.UserId).FirstOrDefault();
|
||
if (e == null)
|
||
{
|
||
item.transform.gameObject.SetActive(false);//TODO:这里暂时不删除,删除对应的图片和脚本依赖这个对象;脚本提供一个dispose
|
||
bufferSize--;
|
||
}
|
||
}
|
||
//创建或者新增
|
||
for (int i = 0; i < pagedList.Count(); i++)
|
||
{
|
||
CreateOrUpdate(pagedList[i]);
|
||
}
|
||
|
||
}
|
||
private void CreateOrUpdate(CompetitionRankingSortModel item)
|
||
{
|
||
var rankingList = FindObjectsOfType<CompetitionRankingItem>();
|
||
foreach (var o in rankingList)
|
||
{
|
||
o.transform.SetAsFirstSibling();
|
||
}
|
||
var competitionRankingItem = rankingList.Where(c => c.UserId == item.UserId).FirstOrDefault();
|
||
//如果当前有这个用户则更新没有则创建并setslibingindex
|
||
if (competitionRankingItem == null)
|
||
{
|
||
//创建
|
||
if (bufferSize < pageSize)
|
||
{
|
||
//情况一:还有足够的buffer,直接创建
|
||
GameObject prefabItem = item.IsSelf ? nearByMajorItem : nearByItem;
|
||
GameObject newObj = (GameObject)Instantiate(prefabItem, parent);
|
||
competitionRankingItem = newObj.GetComponent<CompetitionRankingItem>();
|
||
bufferSize++;
|
||
}
|
||
else
|
||
{
|
||
//情况二:没有足够的buffer,找到要被淘汰的buffer()最后一名
|
||
if (isnext) {
|
||
competitionRankingItem = rankingList.OrderBy(c => c.rank).FirstOrDefault();
|
||
}
|
||
else
|
||
{
|
||
competitionRankingItem = rankingList.OrderByDescending(c => c.rank).FirstOrDefault();
|
||
}
|
||
}
|
||
}
|
||
|
||
competitionRankingItem.setUserId(item.UserId);
|
||
competitionRankingItem.SetRank(item.Index);
|
||
competitionRankingItem.setName(item.Name);
|
||
competitionRankingItem.setRatio(item.KGWeight + "W/KG");
|
||
competitionRankingItem.setSpeed(item.Speed.ToString() + "KM/H");
|
||
competitionRankingItem.setHead(item.Headimage);
|
||
var countryTexture = cyclingController.GetCountryImageByName(item.CountryImg);
|
||
competitionRankingItem.setCountry(countryTexture);
|
||
var near = Math.Round((item.EndDistance - cyclingController.currentPlayer.TotalDistance)*1000);
|
||
competitionRankingItem.setDistance(cyclingController.currentPlayer.UserId == item.UserId?"0M": near.ToString() + "M");
|
||
competitionRankingItem.transform.SetSiblingIndex(item.Index);
|
||
}
|
||
}
|
||
}
|