122 lines
3.8 KiB
C#
122 lines
3.8 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
using System;
|
|
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<HighscoreEntry> Entries;
|
|
|
|
public HighscoresManager()
|
|
{
|
|
filename = Application.streamingAssetsPath + "/highscores.json";
|
|
Entries = new List<HighscoreEntry>();
|
|
LoadFile();
|
|
//Init();
|
|
}
|
|
|
|
|
|
private void Init()
|
|
{
|
|
Entries.Clear();
|
|
Entries.Add(new HighscoreEntry { rank = 1, pseudo = "Toto", transport = 0, time = new TimeSpan(0, 0, 0, 50, 30) });
|
|
Entries.Add(new HighscoreEntry { rank = 2, pseudo = "Tata", transport = 0, time = new TimeSpan(0, 0, 0, 55, 30) });
|
|
Entries.Add(new HighscoreEntry { rank = 3, pseudo = "Titi", transport = 0, time = new TimeSpan(0, 0, 0, 52, 30) });
|
|
Entries.Add(new HighscoreEntry { rank = 4, pseudo = "Tutu", transport = 0, time = new TimeSpan(0, 0, 0, 42, 30) });
|
|
Entries.Add(new HighscoreEntry { rank = 5, pseudo = "Popo", transport = 0, time = new TimeSpan(0, 0, 0, 40, 30) });
|
|
Entries.Add(new HighscoreEntry { rank = 6, pseudo = "Papa", transport = 0, time = new TimeSpan(0, 0, 0, 50, 0) });
|
|
Entries.Add(new HighscoreEntry { rank = 7, pseudo = "Mama", transport = 0, time = new TimeSpan(0, 0, 1, 30, 30) });
|
|
Entries.Add(new HighscoreEntry { rank = 8, pseudo = "Mimi", transport = 0, time = new TimeSpan(0, 0, 1, 50, 10) });
|
|
Entries.Add(new HighscoreEntry { rank = 9, pseudo = "Nana", transport = 0, time = new TimeSpan(0, 0, 1, 55, 0) });
|
|
Entries.Add(new HighscoreEntry { rank = 10, pseudo = "Dada", transport = 0, time = new TimeSpan(0, 0, 1, 30, 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.Any(e => e.time > highscore.time);
|
|
}
|
|
|
|
private void LoadFile()
|
|
{
|
|
HighscoreEntry[] entries;
|
|
using (var sr = new StreamReader(filename))
|
|
entries = JsonHelper.FromJson<HighscoreEntry>(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;
|
|
|
|
}
|