Ejemplo n.º 1
0
        public void AddEntry(ScoreboardEntryData scoreboardEntryData)
        {
            ScoreboardSavedData saved_scores = GetSavedScores();

            bool score_added = false;

            for (int i = 0; i < saved_scores.high_scores.Count; i++)
            {
                if (scoreboardEntryData.entry_score > saved_scores.high_scores[i].entry_score) //if score trying to add is greater than current score checking
                {
                    saved_scores.high_scores.Insert(i, scoreboardEntryData);                   //put into list at i
                    score_added = true;
                    break;
                }
            }
            if (!score_added && saved_scores.high_scores.Count < max_scoreboard_entries)                                               // still space left
            {
                saved_scores.high_scores.Add(scoreboardEntryData);                                                                     //put new score in list
            }
            if (saved_scores.high_scores.Count > max_scoreboard_entries)                                                               //too many entries
            {
                saved_scores.high_scores.RemoveRange(max_scoreboard_entries, saved_scores.high_scores.Count - max_scoreboard_entries); //remove entries from max to count of scores
            }

            UpdateUI(saved_scores);

            SaveScores(saved_scores);
        }
Ejemplo n.º 2
0
        //based on https://stackoverflow.com/questions/4940124/how-can-i-delete-the-first-n-lines-in-a-string-in-c
        private void SaveScores(ScoreboardSavedData scoreboardSavedData) //this will save scores to file
        {
            var first2Lines = "";

            using (StreamReader stream = new StreamReader(save_path)) //using is a good way to stop leakages like leaving a file open
            {
                string   json  = stream.ReadToEnd();
                int      n     = 2;
                string[] lines = json
                                 .Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries) //return collection of items
                                 .Take(n)                                                                         //take takes first n elements from collection. oppposite of
                                 .ToArray();                                                                      //converts collection to an array

                first2Lines = string.Join(Environment.NewLine, lines);

                Debug.Log("Encrypted username and pass (2 lines) : " + first2Lines);
            }

            using (StreamWriter stream = new StreamWriter(save_path))
            {
                string json = JsonUtility.ToJson(scoreboardSavedData, true); //format to true makes it look nice
                stream.Write(first2Lines + Environment.NewLine);
                stream.Write(json);
            }
        }
Ejemplo n.º 3
0
        private string save_path => Application.persistentDataPath + "\\" + Register.EncryptString(login.Username) + ".txt";  //where the project is stored. can be .txt not necessarily json

        private void Start()
        {
            ScoreboardSavedData saved_scores = GetSavedScores();

            UpdateUI(saved_scores);

            SaveScores(saved_scores);
        }
Ejemplo n.º 4
0
 private void SaveScores(ScoreboardSavedData scoreboardSavedData) //this will save scores to file
 {
     using (StreamWriter stream = new StreamWriter(save_path))
     {
         string json = JsonUtility.ToJson(scoreboardSavedData, true); //format to true makes it look nice
         stream.Write(json);
     }
 }
Ejemplo n.º 5
0
 private void UpdateUI(ScoreboardSavedData saved_scores)
 {
     foreach (Transform child in high_scores_holder_transform)
     {
         Destroy(child.gameObject);                                       //if new entry want to destroy old buttons and repopulate with new ones
     }
     foreach (ScoreboardEntryData high_score in saved_scores.high_scores) //loop through each score in high_scores list
     {
         Debug.Log("check if scoreboard entry object null; " + (scoreboard_entry_object == null));
         Debug.Log("check if high_scores_holder_transform object null; " + (high_scores_holder_transform == null));
         Instantiate(scoreboard_entry_object, high_scores_holder_transform). //instantiate means to "spawn in"
         GetComponent <ScoreboardEntryUI>().Initialize(high_score);          //create a new UI entry with this loop's high score info
     }
 }