41 lines
1.1 KiB
C#
41 lines
1.1 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Newtonsoft.Json;
|
|
using UnityEngine;
|
|
|
|
namespace Code.Records
|
|
{
|
|
public static class RecordsService
|
|
{
|
|
private const string Key = "RaceRecords";
|
|
|
|
public static List<Record> Load()
|
|
{
|
|
var json = PlayerPrefs.GetString(Key, "[]");
|
|
try
|
|
{
|
|
return JsonConvert.DeserializeObject<List<Record>>(json) ?? new List<Record>();
|
|
}
|
|
catch
|
|
{
|
|
return new List<Record>();
|
|
}
|
|
}
|
|
|
|
private static void Save(List<Record> records)
|
|
{
|
|
var json = JsonConvert.SerializeObject(records);
|
|
PlayerPrefs.SetString(Key, json);
|
|
PlayerPrefs.Save();
|
|
}
|
|
|
|
public static void AddRecord(string name, long totalMs)
|
|
{
|
|
var records = Load();
|
|
records.Add(new Record(name, totalMs));
|
|
records = records.OrderBy(r => r.totalMilliseconds).ToList();
|
|
records = records.Take(5).ToList();
|
|
Save(records);
|
|
}
|
|
}
|
|
} |