using UnityEngine; using System.Collections; using System; using System.Threading; using System.Linq; using System.IO; using System.Collections.Generic; public class HighscoresManager { private static HighscoresManager current; public static HighscoresManager Current { get { if (current == null) current = new HighscoresManager(); return current; } private set { current = value; } } private string filename; public List Entries; public HighscoresManager() { filename = Application.streamingAssetsPath + "/highscores.json"; Entries = new List(); LoadFile(); //Init(); } private void Init() { Entries.Clear(); Entries.Add(new HighscoreEntry { rank = 1, pseudo = "Fausto", transport = 0, time = new TimeSpan(0, 0, 0, 50, 30) }); Entries.Add(new HighscoreEntry { rank = 2, pseudo = "Raymond", transport = 1, time = new TimeSpan(0, 0, 0, 55, 30) }); Entries.Add(new HighscoreEntry { rank = 3, pseudo = "Louison", transport = 2, time = new TimeSpan(0, 0, 0, 52, 30) }); Entries.Add(new HighscoreEntry { rank = 4, pseudo = "Marco", transport = 2, time = new TimeSpan(0, 0, 0, 42, 30) }); Entries.Add(new HighscoreEntry { rank = 5, pseudo = "Jeannie", transport = 3, time = new TimeSpan(0, 0, 0, 40, 30) }); Entries.Add(new HighscoreEntry { rank = 6, pseudo = "Nathalie", transport = 6, time = new TimeSpan(0, 0, 0, 50, 0) }); Entries.Add(new HighscoreEntry { rank = 7, pseudo = "Eddy", transport = 4, time = new TimeSpan(0, 0, 1, 30, 30) }); Entries.Add(new HighscoreEntry { rank = 8, pseudo = "Bernard", transport = 5, time = new TimeSpan(0, 0, 1, 50, 10) }); Entries.Add(new HighscoreEntry { rank = 9, pseudo = "Jacques", transport = 7, time = new TimeSpan(0, 0, 1, 55, 0) }); Entries.Add(new HighscoreEntry { rank = 10, pseudo = "Laurent", transport = 0, time = new TimeSpan(0, 0, 1, 30, 0) }); Entries.Add(new HighscoreEntry { rank = 11, pseudo = "Miguel", transport = 0, time = new TimeSpan(0, 0, 2, 00, 0) }); Entries.Add(new HighscoreEntry { rank = 12, pseudo = "Harry", transport = 0, time = new TimeSpan(0, 0, 2, 10, 0) }); Entries.Add(new HighscoreEntry { rank = 13, pseudo = "Donatello", transport = 0, time = new TimeSpan(0, 0, 2, 20, 0) }); Entries.Add(new HighscoreEntry { rank = 14, pseudo = "Raphael", transport = 0, time = new TimeSpan(0, 0, 2, 25, 0) }); Entries.Add(new HighscoreEntry { rank = 15, pseudo = "Conan", transport = 0, time = new TimeSpan(0, 0, 2, 35, 0) }); Entries.Add(new HighscoreEntry { rank = 16, pseudo = "ObiWan", transport = 0, time = new TimeSpan(0, 0, 2, 40, 0) }); Entries.Add(new HighscoreEntry { rank = 17, pseudo = "Bilbo", transport = 0, time = new TimeSpan(0, 0, 2, 50, 0) }); Entries.Add(new HighscoreEntry { rank = 18, pseudo = "James", transport = 0, time = new TimeSpan(0, 0, 2, 55, 0) }); Entries.Add(new HighscoreEntry { rank = 19, pseudo = "Indy", transport = 0, time = new TimeSpan(0, 0, 3, 00, 0) }); WriteFile(); LoadFile(); } internal void AddNewEntry(HighscoreEntry entry) { if (entry == null || Entries == null) return; if (IsANewRecord(entry)) { Entries.Add(entry); SortEntries(); Entries.Remove(Entries.Last()); WriteFile(); } } internal bool IsANewRecord(HighscoreEntry highscore) { return !Entries.Contains(highscore) && Entries.Any(e => e.time > highscore.time && e!= highscore); } private void LoadFile() { HighscoreEntry[] entries; using (var sr = new StreamReader(filename)) entries = JsonHelper.FromJson(sr.ReadToEnd()); if (entries != null) { Entries.Clear(); Entries.AddRange(entries); SortEntries(); } } private void SortEntries() { Entries = Entries.OrderBy(e => e.time).ToList(); Entries.ForEach(e => e.rank = Entries.IndexOf(e) + 1); } private void WriteFile() { var entries = JsonHelper.ToJson(Entries.ToArray()); using (var sr = new StreamWriter(filename)) sr.WriteLine(entries); } } [Serializable] public class HighscoreEntry { [NonSerialized] public int rank; public string pseudo; public int transport; public TimeSpan time { get { if (string.IsNullOrEmpty(timeStr)) return default(TimeSpan); var values = timeStr.Split(':'); return new TimeSpan(0, 0, Convert.ToInt32(values[0]), Convert.ToInt32(values[1]), Convert.ToInt32(values[2])); } set { timeStr = time != null ? value.Minutes + ":" + value.Seconds + ":" + value.Milliseconds : string.Empty; } } public string timeStr; }