2021-07-01 18:41:35 +08:00
|
|
|
|
using Assets.Scenes.Ride.Scripts.Model;
|
|
|
|
|
|
using Assets.Scenes.Ride.Scripts.Model.CyclingModels;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using UnityEngine;
|
2021-07-06 18:24:15 +08:00
|
|
|
|
using UnityEngine.EventSystems;
|
|
|
|
|
|
using UnityEngine.UI;
|
2021-07-01 18:41:35 +08:00
|
|
|
|
|
|
|
|
|
|
namespace Assets.Scenes.Ride.Scripts
|
|
|
|
|
|
{
|
|
|
|
|
|
public class CompetitionRankingFactory : BaseListFactory
|
|
|
|
|
|
{
|
|
|
|
|
|
private bool isInit = false;
|
2021-07-09 18:16:50 +08:00
|
|
|
|
private bool isnext = false;
|
2021-07-01 18:41:35 +08:00
|
|
|
|
private List<CompetitionRankingSortModel> rankingList {get;set;}
|
|
|
|
|
|
private AbstractPlayer currentPlayer { get; set; }
|
2021-07-06 18:24:15 +08:00
|
|
|
|
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; }
|
|
|
|
|
|
|
2021-07-01 18:41:35 +08:00
|
|
|
|
protected override void Awake()
|
|
|
|
|
|
{
|
2021-07-06 18:24:15 +08:00
|
|
|
|
//base.Awake();
|
|
|
|
|
|
scroll = transform.Find("List").gameObject;
|
|
|
|
|
|
if (scroll != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
UIManager.AddEvent(scroll, UnityEngine.EventSystems.EventTriggerType.EndDrag, OnEndDrag);
|
|
|
|
|
|
}
|
|
|
|
|
|
parent = scroll.transform.Find("Viewport/Content");
|
2021-07-01 18:41:35 +08:00
|
|
|
|
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");
|
2021-07-06 18:24:15 +08:00
|
|
|
|
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;
|
2021-07-09 18:16:50 +08:00
|
|
|
|
isnext = false;
|
|
|
|
|
|
CreateList();
|
2021-07-06 18:24:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
private void NextPage(BaseEventData baseEventData)
|
|
|
|
|
|
{
|
|
|
|
|
|
SetNextIndex();
|
|
|
|
|
|
isInit = true;
|
2021-07-09 18:16:50 +08:00
|
|
|
|
isnext = true;
|
|
|
|
|
|
CreateList();
|
2021-07-01 18:41:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
protected override void CreateList()
|
|
|
|
|
|
{
|
|
|
|
|
|
CompetitionModel s = cyclingController.cyclingController as CompetitionModel;
|
|
|
|
|
|
var ss = s?.competitionRankingModel;
|
|
|
|
|
|
rankingList = ss?.Sorts;
|
|
|
|
|
|
|
|
|
|
|
|
if (rankingList == null) return;
|
2021-07-06 18:24:15 +08:00
|
|
|
|
//当前参赛总人数
|
|
|
|
|
|
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);
|
2021-07-01 18:41:35 +08:00
|
|
|
|
if (!isInit)
|
|
|
|
|
|
{
|
|
|
|
|
|
//当前用户在第几页
|
2021-07-23 09:04:56 +08:00
|
|
|
|
pageIndex = (pageSize > 0 ? currentIndex / pageSize : 1) + (currentIndex % pageSize > 0 ? 1 : 0);
|
2021-07-01 18:41:35 +08:00
|
|
|
|
}
|
2021-07-06 18:24:15 +08:00
|
|
|
|
Rank.text = $"{pageIndex}/{totalPages}";
|
2021-07-01 18:41:35 +08:00
|
|
|
|
var pagedList = rankingList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
|
2021-07-09 18:16:50 +08:00
|
|
|
|
//删除
|
|
|
|
|
|
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--;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
//创建或者新增
|
2021-07-01 18:41:35 +08:00
|
|
|
|
for (int i = 0; i < pagedList.Count(); i++)
|
|
|
|
|
|
{
|
2021-07-06 18:24:15 +08:00
|
|
|
|
CreateOrUpdate(pagedList[i]);
|
2021-07-01 18:41:35 +08:00
|
|
|
|
}
|
2021-07-09 18:16:50 +08:00
|
|
|
|
|
2021-07-01 18:41:35 +08:00
|
|
|
|
}
|
2021-07-06 18:24:15 +08:00
|
|
|
|
private void CreateOrUpdate(CompetitionRankingSortModel item)
|
2021-07-01 18:41:35 +08:00
|
|
|
|
{
|
2021-07-06 18:24:15 +08:00
|
|
|
|
var rankingList = FindObjectsOfType<CompetitionRankingItem>();
|
|
|
|
|
|
var competitionRankingItem = rankingList.Where(c => c.UserId == item.UserId).FirstOrDefault();
|
|
|
|
|
|
//如果当前有这个用户则更新没有则创建并setslibingindex
|
|
|
|
|
|
if (competitionRankingItem == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
//创建
|
|
|
|
|
|
if (bufferSize < pageSize)
|
|
|
|
|
|
{
|
|
|
|
|
|
//情况一:还有足够的buffer,直接创建
|
2021-07-16 18:37:02 +08:00
|
|
|
|
GameObject prefabItem = item.IsSelf ? nearByMajorItem : nearByItem;
|
2021-07-06 18:24:15 +08:00
|
|
|
|
GameObject newObj = (GameObject)Instantiate(prefabItem, parent);
|
|
|
|
|
|
competitionRankingItem = newObj.GetComponent<CompetitionRankingItem>();
|
|
|
|
|
|
bufferSize++;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
//情况二:没有足够的buffer,找到要被淘汰的buffer()最后一名
|
2021-07-09 18:16:50 +08:00
|
|
|
|
if (isnext) {
|
|
|
|
|
|
competitionRankingItem = rankingList.OrderBy(c => c.rank).FirstOrDefault();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
competitionRankingItem = rankingList.OrderByDescending(c => c.rank).FirstOrDefault();
|
|
|
|
|
|
}
|
2021-07-06 18:24:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-01 18:41:35 +08:00
|
|
|
|
competitionRankingItem.setUserId(item.UserId);
|
2021-07-06 18:24:15 +08:00
|
|
|
|
competitionRankingItem.SetRank(item.Index);
|
2021-07-01 18:41:35 +08:00
|
|
|
|
competitionRankingItem.setName(item.Name);
|
2021-07-06 18:24:15 +08:00
|
|
|
|
competitionRankingItem.setRatio(item.KGWeight + "W/KG");
|
2021-07-01 18:41:35 +08:00
|
|
|
|
competitionRankingItem.setSpeed(item.Speed.ToString() + "KM/H");
|
2021-07-06 18:24:15 +08:00
|
|
|
|
competitionRankingItem.setHead(item.Headimage);
|
|
|
|
|
|
competitionRankingItem.setCountry(item.CountryImg);
|
2021-07-09 18:16:50 +08:00
|
|
|
|
competitionRankingItem.setDistance(currentPlayer.UserId == item.UserId?"0KM": item.Near.ToString() + "KM");
|
2021-07-06 18:24:15 +08:00
|
|
|
|
competitionRankingItem.transform.SetSiblingIndex(item.Index);
|
2021-07-01 18:41:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|