powerfun-unity/Assets/Scripts/Scenes/Ride/Scripts/Competiton/CompetitionRankingFactory.cs

65 lines
2.7 KiB
C#
Raw Normal View History

using Assets.Scenes.Ride.Scripts.Model;
using Assets.Scenes.Ride.Scripts.Model.CyclingModels;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace Assets.Scenes.Ride.Scripts
{
public class CompetitionRankingFactory : BaseListFactory
{
private bool isInit = false;
private List<CompetitionRankingSortModel> rankingList {get;set;}
private AbstractPlayer currentPlayer { get; set; }
protected override void Awake()
{
base.Awake();
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");
}
protected override void CreateList()
{
//获取实时排名
CompetitionModel s = cyclingController.cyclingController as CompetitionModel;
var ss = s?.competitionRankingModel;
rankingList = ss?.Sorts;
if (rankingList == null) return;
totalPages = rankingList.Count();
//TODO 计算当前用户排名
if (!isInit)
{
//当前用户排名
var currentIndex = rankingList.Where(c => c.UserId == currentPlayer.UserId).FirstOrDefault()?.Index??0;
//当前用户在第几页
pageIndex = currentIndex / pageSize;
}
var pagedList = rankingList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
for (int i = 0; i < pagedList.Count(); i++)
{
Create(pagedList[i]);
}
}
private void Create(CompetitionRankingSortModel item)
{
//创建
GameObject prefabItem = item.IsSelf ? nearByMajorItem : nearByItem;
GameObject newObj = (GameObject)Instantiate(prefabItem, transform);
var competitionRankingItem = newObj.GetComponent<CompetitionRankingItem>();
competitionRankingItem.setUserId(item.UserId);
competitionRankingItem.setName(item.Name);
competitionRankingItem.setRatio(item.KGWeight+"W/KG");
competitionRankingItem.setSpeed(item.Speed.ToString() + "KM/H");
//competitionRankingItem.setHead(item.Headimage);
//competitionRankingItem.setCountry(cyclingController.GetCountryImageByName(item.));
competitionRankingItem.setDistance(item.Near.ToString() + "KM");
bufferSize++;
}
}
}