Unity,C#,字典,计算比赛中各队的得分

Unity,C#,字典,计算比赛中各队的得分,c#,dictionary,unity3d,C#,Dictionary,Unity3d,好吧,这对我来说很难理解,因为我会尽可能清楚地解释我的问题是什么 首先,我正在为我制作的fps游戏创建一个计分系统。 现在分数系统使用了一个双字典结构,即在字典中有一个字典 playerScores = new Dictionary<string, Dictionary<string, int> > (); 现在它正在存储玩家的死亡和死亡信息,然后按照死亡的降序将它们显示在一个领导板上,并将它们分组到他们所在的团队中,这一切都很好 我遇到的问题是试图找出一种方法来计算每

好吧,这对我来说很难理解,因为我会尽可能清楚地解释我的问题是什么

首先,我正在为我制作的fps游戏创建一个计分系统。 现在分数系统使用了一个双字典结构,即在字典中有一个字典

playerScores = new Dictionary<string, Dictionary<string, int> > ();
现在它正在存储玩家的死亡和死亡信息,然后按照死亡的降序将它们显示在一个领导板上,并将它们分组到他们所在的团队中,这一切都很好

我遇到的问题是试图找出一种方法来计算每个团队的当前得分。还要计算出哪个队得分最高。我就是想不起来

这是一个处理这些数据的类(不是排行榜代码,而是评分等)

使用UnityEngine;
使用系统集合;
使用System.Collections.Generic;
使用System.Linq;
公共类MyMatchData:MonoBehavior
{
字典播放器核心;
int changeconter=0;
公共游戏对象记分板;
帆布记分板;
公共浮动myTeamID=0f;
字符串playerNameKey=“blank”;
浮点数=0f;
浮动myKills=0f;
//用于初始化
无效开始()
{
scoreBoardCan=scoreBoard.transform.parent.GetComponent();
死亡人数=0f;
myKills=0f;
}
void Init()
//init只是为了让舒尔知道PlayerCores设置为b4,有人试图使用它
//(当有人首先调用PlayerCores时,他们将调用Init()
{
如果(playerScores!=null){
返回;
}//如果玩家的分数已经设置好,那么返回,这样我们就不会删除分数
playerScores=newdictionary();//必须告诉它有一个新的字典,否则您将得到null错误
//PlayerCores[playerNameKey]=新词典();//agane必须告诉它有一个新的词典,因为这是子词典
//必须为每个玩家创建一个新的子词典。如果尚未创建一个sjub词典,则setScore()将为玩家创建一个新的sjub词典。
}
无效更新()
{
if(Input.GetKeyDown(KeyCode.Tab)){
changeCounter++;
//scoreBoard.SetActive(!scoreBoard.activeSelf);
bool scoreBoardCanIsActive=scoreBoardCan.enabled;
scoreBoardCan.enabled=!scoreBoardCanIsActive;
}
if(Input.GetKeyDown(KeyCode.P)){
变更分数(“天空”,“杀死”,1);
}
}
[RPC]
public void playerDied(布尔wasBot、布尔被某人杀死、字符串被杀死、字符串playerName、int playerTeam)
{
if(PhotonNetwork.isMasterClient){
if(wasBot){
Health-hlscBot=GameObject.Find(playerName.GetComponent();
hlscBot.GetComponent().RPC(“DeSpawn”,PhotonTargets.AllBuffered,“bot”,playerName,playerName,playerTeam);
}否则{
//玩家在这里重生
Log(“主机MyMatchData”+playerName);
Health hlsc=GameObject.Find(playerName.GetComponent();
hlsc.GetComponent().RPC(“DeSpawn”,PhotonTargets.AllBuffered,“player”,playerName,“null”,-1);
}
//你在这里倒东西吗
if(wasKilledBySomeone==true){
ChangeScore(玩家名称,“死亡”,1);//增加玩家死亡
更改分数(killedBy,“kills”,1);//增加killers分数
}否则{
ChangeScore(玩家名称,“死亡”,1);//增加玩家死亡
}
}
}
[RPC]
public void firstSpawn(字符串用户名,int-teamID)
{
如果(!PhotonNetwork.isMasterClient){
返回;
}
SetScore(用户名,“kills”,0);
SetScore(用户名,“死亡”,0);
SetScore(用户名,“协助”,0);
SetScore(用户名,“团队ID”,团队ID);
}
public int GetScore(字符串用户名,字符串scoreType)
{
Init();
如果(playerScores.ContainsKey(username)==false){//如果收到的玩家名称尚未重新确认,则必须先设置
返回0;//将玩家分数返回为0
}
if(playerScores[username].ContainsKey(scoreType)==false){//在这个阶段,我们知道球员的名字已经记录在词汇表中,但是我们需要检查是否已经记录了分数。如果没有,则返回0
返回0;//将玩家分数返回为0
}
return playerScores[用户名][分数类型];
}
public void SetScore(字符串用户名、字符串scoreType、int值)
{
Init();
changeCounter++;
if(playerScores.ContainsKey(username)==false){//如果播放机尚未记录在词典中。
playerScores[username]=newdictionary();//为该播放器创建一个新的词典库(子词典库)。
}
playerScores[username][scoreType]=value;//玩家的分数现在是value的值
}
公共无效更改分数(字符串用户名、字符串分数类型、整数金额)
playerScores[username][scoretype][scorevalue]
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class MyMatchData : MonoBehaviour
{



        Dictionary<string, Dictionary<string, int> > playerScores;
        int changeCounter = 0;
        public GameObject scoreBoard;
        Canvas scoreBoardCan;
        public float myTeamID = 0f;
        string playerNameKey = "blank";
        float myDeaths = 0f;
        float myKills = 0f;




        // Use this for initialization
        void Start ()
        {
                scoreBoardCan = scoreBoard.transform.parent.GetComponent<Canvas> ();





                myDeaths = 0f;
                myKills = 0f;
        }

        void Init ()
        //init is just to make shure that we know playerscores is set b4 some one trys to use it 
        //(when ever some one calls the playerscores first they will call Init())
        {



                if (playerScores != null) {
                        return;
                }//If the player scores has been set then return out so we dont erase the scores



                playerScores = new Dictionary<string, Dictionary<string, int> > (); //must tell it that theres a new dictionery else u will get null error
                //playerScores [playerNameKey] = new Dictionary<string, int> (); // agane must tell it theres a new dictionery because this is the sub dictionery
                //a new sub dictionerey must be created for evey player. setScore() will create a new sjub dictionerey for a player if one dose not yet exsist.

        }

        void Update ()
        {

                if (Input.GetKeyDown (KeyCode.Tab)) {
                        changeCounter ++;


                        //scoreBoard.SetActive ( !scoreBoard.activeSelf);
                        bool scoreBoardCanIsActive = scoreBoardCan.enabled;
                        scoreBoardCan.enabled = !scoreBoardCanIsActive;
                }

                if (Input.GetKeyDown (KeyCode.P)) {

                        ChangeScore ("sky", "kills", 1);
                }
        }

        [RPC]
        public void playerDied (bool wasBot, bool wasKilledBySomeone, string killedBy, string playerName, int playerTeam)
        {
                if (PhotonNetwork.isMasterClient) {
                        if (wasBot) {
                                Health hlscBot = GameObject.Find (playerName).GetComponent<Health> ();
                                hlscBot.GetComponent<PhotonView> ().RPC ("DeSpawn", PhotonTargets.AllBuffered, "bot", playerName, playerName, playerTeam);


                        } else {
                                //player respawn stuff here
                                Debug.Log ("Host MyMatchData " + playerName);
                                Health hlsc = GameObject.Find (playerName).GetComponent<Health> ();
                                hlsc.GetComponent<PhotonView> ().RPC ("DeSpawn", PhotonTargets.AllBuffered, "player", playerName, "null", -1);

                        }
                        //do Score dumping stuff here
                        if (wasKilledBySomeone == true) {
                                ChangeScore (playerName, "deaths", 1);//increase the players deaths
                                ChangeScore (killedBy, "kills", 1);//increase the killers score
                        } else {
                                ChangeScore (playerName, "deaths", 1);//increase the players deaths
                        }

                }
        }

        [RPC]
        public void firstSpawn (string userName, int teamID)
        {

                if (!PhotonNetwork.isMasterClient) {
                        return;
                }


                SetScore (userName, "kills", 0);
                SetScore (userName, "deaths", 0);
                SetScore (userName, "assists", 0);
                SetScore (userName, "teamID", teamID);  

        }

        public int GetScore (string username, string scoreType)
        {
                Init ();
                if (playerScores.ContainsKey (username) == false) { //this just meens if the player name receaved has not reconised then it must not of been set yet
                        return 0;// return the player score as 0
                }
                if (playerScores [username].ContainsKey (scoreType) == false) {// at this stage we know the player name is recorded in the dictionery but we need to check if there score has been recorded yet. if not then return 0
                        return 0;// return the player score as 0
                }
                return playerScores [username] [scoreType];
        }

        public void SetScore (string username, string scoreType, int value)
        {
                Init ();
                changeCounter ++;

                if (playerScores.ContainsKey (username) == false) {//if the player has not been recorded in the dictionery yet.
                        playerScores [username] = new Dictionary<string, int> ();// Creates a new dictinerey(sub dictionery) for that player.
                }

                playerScores [username] [scoreType] = value; // the players score is now the value of value

        }

        public void ChangeScore (string username, string scoreType, int amount)
        {
                Init ();
                int currScore = GetScore (username, scoreType);
                SetScore (username, scoreType, currScore + amount); 
        }

        public string[] GetPlayerNames ()
        {
                Init ();
                return playerScores.Keys.ToArray ();
        }

        public string[] GetPlayerNames (string sortingScoreType)
        {
                Init ();

                string[] names = playerScores.Keys.ToArray (); //have to convert to array because keys dont retun a array but a list of string of keys or somthing 

                return names.OrderByDescending (n => GetScore (n, sortingScoreType)).ToArray (); //this needs to beconverted toArray because of the same issue above
                // ok so the names in this array (names) gets sorted by running the getscore function to find out who has what score. its complacated but works
                //for that to work you need using System.Linq

        }

        public int GetChangeCounter ()
        {
                return changeCounter;
        }


}
float myDeaths = 0f;
float myKills = 0f;
internal class Game
{
    public List<Team> Teams { get; set; } 
    public void firstSpawn(string userName, int teamID)
    {
        if (!PhotonNetwork.isMasterClient)
        {
            return;
        }
        Team team;
        if (Teams.Any(t => t.TeamID == teamID))
        {
            team = Teams.FirstOrDefault(t => t.TeamID == teamID);
        }
        else
        {
            team = new Team(teamID);
            Teams.Add(team);
        }            
        team.Users.Add(new User(userName));
    }


    public void playerDied(bool wasBot, bool wasKilledBySomeone, User killer, User dead, Team playerTeam)
    {
        if (PhotonNetwork.isMasterClient)
        {
            if (wasBot)
            {
                Health hlscBot = GameObject.Find(dead.Name).GetComponent<Health>();
                hlscBot.GetComponent<PhotonView>().RPC("DeSpawn", PhotonTargets.AllBuffered, "bot", dead.Name, dead.Name, playerTeam);
            }
            else
            {
                //player respawn stuff here
                Debug.Log("Host MyMatchData " + dead.Name);
                Health hlsc = GameObject.Find(playerName).GetComponent<Health>();
                hlsc.GetComponent<PhotonView>().RPC("DeSpawn", PhotonTargets.AllBuffered, "player", dead.Name, "null", -1);
            }

            //do Score dumping stuff here
            dead.Death(); // The dead is dead, no matter if suicide or not
            if (wasKilledBySomeone)
            {
                killer.Kill();//increase the killers score
            }
         }
    }

    public bool getTheHigherTeamBy(Types type)
    {
        return Teams.Any(team => team.getTeamScore(type) == Teams.Max(t => t.getTeamScore(type)));
    }
}

internal class Team
{
    public int TeamID { get; set; }
    public List<User> Users { get; set; } 


    public Team(int id = 0)
    {
        TeamID = id;
    }

    public int getTeamScore(Types type)
    {
        return Users.Sum(u => u.Score.getScoreFromType(type));
    }
    public string[] getPlayerNames()
    {
        return Users.Select(u => u.Name).ToArray();
    }

    public string[] GetPlayerNames(Types sortingScoreType)
    {
        return Users.OrderByDescending(u => u.Score.getScoreFromType(sortingScoreType)).Select(u => u.Name).ToArray();
    }

}

public enum Types
{
    Kill,
    Death,
    Assist // Not sure about that one
}
internal class Score
{
    private readonly Dictionary<Types, int> points = new Dictionary<Types, int>();

    public Score()
    {
        // Fill the dictionnary with all the available types
        foreach (Types type in Enum.GetValues(typeof(Types)))
        {
            points.Add(type, 0);
        }
    }
    public int getScoreFromType(Types type)
    {
        return points[type];
    }
    public void incrementScoreForType(Types type, int amount = 1)
    {
        points[type] += amount;
    }

}

internal class User
{
    public string Name { get; set; }
    public Score Score { get; set; }

    public User(string name = "Default")
    {
        Name = name;
        Score = new Score();
    }

    public int getScoreFromType(Types type)
    {
        return Score.getScoreFromType(type);
    }

    public void changeScore(Types type, int amount)
    {
        Score.incrementScoreForType(type, amount);
    }

    public void Death()
    {
        Score.incrementScoreForType(Types.Death);
    }
    public void Kill()
    {
        Score.incrementScoreForType(Types.Kill);
    }
    public void Assist()
    {
        Score.incrementScoreForType(Types.Assist);
    }
}