metahunt/Assets/Scripts/LeaderboardTDM.cs
2024-02-11 23:23:12 +05:30

55 lines
2.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
public class LeaderboardTDM : MonoBehaviour
{
public Dictionary<string, List<PlayerNetwork>> Teams;
public Transform[] TeamParents;
public void Populate(PlayerNetwork[] list){
//Teamify
Teams = new Dictionary<string, List<PlayerNetwork>>();
foreach(PlayerNetwork player in list){
if(Teams.ContainsKey(player.partyname)){
Teams[player.partyname].Add(player);
}else{
Teams.Add(player.partyname, new List<PlayerNetwork>(){player});
}
}
for(int i=0; i < TeamParents.Length; i++){
bool withinRange = (Teams.Keys.Count > i);
TeamParents[i].gameObject.SetActive(withinRange);
if(withinRange){
TeamParents[i].GetChild(0).GetComponent<TMP_Text>().text = Teams.Keys.ElementAt(i);
int score =0;
for(int ii = 0; ii < TeamParents[i].GetChild(2).childCount; ii++){
bool playerItemsWithinRange = Teams.Values.ElementAt(i).Count > ii;
Transform playerItemTransform = TeamParents[i].GetChild(2).GetChild(ii);
playerItemTransform.gameObject.SetActive(playerItemsWithinRange);
if(!playerItemsWithinRange){continue;}
PlayerNetwork player = Teams.Values.ElementAt(i)[ii];
playerItemTransform.GetChild(0).GetComponent<TMP_Text>().text = player.username;
playerItemTransform.GetChild(1).GetComponent<TMP_Text>().text = player.kills.ToString();
playerItemTransform.GetChild(2).GetComponent<TMP_Text>().text = player.deaths.ToString();
playerItemTransform.GetChild(3).GetComponent<TMP_Text>().text = player.assists.ToString();
score+= player.kills;
}
TeamParents[i].GetChild(1).GetComponent<TMP_Text>().text = (score * 10).ToString("n0");
}
}
}
}