title: Unity, C#, Dictionary, calculating scores of teams in a match
body: <p>ok its hard for me to wrap my head around this as it is so ill try to explane as clearly as I can what my problem is.</p>
  
  <p>First of all im creating a score system for a fps game that im making.
  now right now the score system uses a dubble Dictionary structure that is it has a Dictionary inside a Dictionary.</p>
  
  <pre><code>playerScores = new Dictionary&lt;string, Dictionary&lt;string, int&gt; &gt; ();
  </code></pre>
  
  <p>the first dictionary is creating a list of user names the 2nd dictionery is used to hold 4 keys (kills, deaths, assists, teamID)</p>
  
  <p>so basicley its</p>
  
  <pre><code>playerScores[username][scoretype][scorevalue]
  </code></pre>
  
  <p>now it currently storing the kills and deaths of players and then is displaying them in a leader board in descending order of kills and are grouped in what team they are in and that's working all fine. </p>
  
  <p>the issue I am having is trying to work out a way to calculate the teams current score for each team. and also work out what team has the highest score. I just cant get my head around it.</p>
  
  <p>this here is the class that handles this data (not the leaderboard code but the scoring and stuff). </p>
  
  <pre><code>using UnityEngine;
  using System.Collections;
  using System.Collections.Generic;
  using System.Linq;
  
  public class MyMatchData : MonoBehaviour
  {
  
  
  
          Dictionary&lt;string, Dictionary&lt;string, int&gt; &gt; 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&lt;Canvas&gt; ();
  
  
  
  
  
                  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&lt;string, Dictionary&lt;string, int&gt; &gt; (); //must tell it that theres a new dictionery else u will get null error
                  //playerScores [playerNameKey] = new Dictionary&lt;string, int&gt; (); // 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&lt;Health&gt; ();
                                  hlscBot.GetComponent&lt;PhotonView&gt; ().RPC ("DeSpawn", PhotonTargets.AllBuffered, "bot", playerName, playerName, playerTeam);
  
  
                          } else {
                                  //player respawn stuff here
                                  Debug.Log ("Host MyMatchData " + playerName);
                                  Health hlsc = GameObject.Find (playerName).GetComponent&lt;Health&gt; ();
                                  hlsc.GetComponent&lt;PhotonView&gt; ().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&lt;string, int&gt; ();// 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 =&gt; 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;
          }
  
  
  }
  </code></pre>
  
  <p>can any one help me work out some code to calculate the teams scores?</p>
  
  <p>also there may be 2 teams or 3 or even 12 teams it just depends on how the match is set up.</p>
  
  <p>also most of the comments in the code are for my understanding of the code when I wrote it.</p>