86 lines
3.1 KiB
C#
86 lines
3.1 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
using System;
|
|
using System.Linq;
|
|
using System.IO;
|
|
using System.Collections.Generic;
|
|
|
|
public class HighscoresManager : MonoBehaviour
|
|
{
|
|
public static HighscoresManager Current { get; private set; }
|
|
private string filename;
|
|
public List<HighscoreEntry> Entries;
|
|
|
|
// Use this for initialization
|
|
void Start()
|
|
{
|
|
filename = Application.streamingAssetsPath + "/highscores.json";
|
|
Current = this;
|
|
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, 30, 30) });
|
|
Entries.Add(new HighscoreEntry { rank = 2, pseudo = "Tata", transport = 0, time = new TimeSpan(0, 0, 0, 30, 30) });
|
|
Entries.Add(new HighscoreEntry { rank = 3, pseudo = "Titi", transport = 0, time = new TimeSpan(0, 0, 0, 30, 30) });
|
|
Entries.Add(new HighscoreEntry { rank = 4, pseudo = "Toto", transport = 0, time = new TimeSpan(0, 0, 0, 30, 30) });
|
|
Entries.Add(new HighscoreEntry { rank = 5, pseudo = "Toto", transport = 0, time = new TimeSpan(0, 0, 0, 30, 30) });
|
|
Entries.Add(new HighscoreEntry { rank = 6, pseudo = "Toto", transport = 0, time = new TimeSpan(0, 0, 0, 30, 30) });
|
|
Entries.Add(new HighscoreEntry { rank = 7, pseudo = "Toto", transport = 0, time = new TimeSpan(0, 0, 0, 30, 30) });
|
|
Entries.Add(new HighscoreEntry { rank = 8, pseudo = "Toto", transport = 0, time = new TimeSpan(0, 0, 0, 30, 30) });
|
|
Entries.Add(new HighscoreEntry { rank = 9, pseudo = "Toto", transport = 0, time = new TimeSpan(0, 0, 0, 30, 30) });
|
|
Entries.Add(new HighscoreEntry { rank = 10, pseudo = "Toto", transport = 0, time = new TimeSpan(0, 0, 0, 30, 30) });
|
|
WriteFile();
|
|
LoadFile();
|
|
}
|
|
|
|
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);
|
|
Entries = Entries.OrderBy(e => e.rank).ToList();
|
|
}
|
|
}
|
|
|
|
private void WriteFile()
|
|
{
|
|
var entries = JsonHelper.ToJson(Entries.ToArray());
|
|
using (var sr = new StreamWriter(filename))
|
|
sr.WriteLine(entries);
|
|
}
|
|
}
|
|
[Serializable]
|
|
public class HighscoreEntry
|
|
{
|
|
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;
|
|
|
|
}
|