added results
This commit is contained in:
1 parent
586c476851
commit
bae0e2218e
11 files changed
+617
-5
No files matched your search
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
|
||||
namespace Code.Records
|
||||
{
|
||||
[Serializable]
|
||||
public class Record
|
||||
{
|
||||
public string name;
|
||||
public long totalMilliseconds;
|
||||
|
||||
public Record(string name, long ms)
|
||||
{
|
||||
this.name = name;
|
||||
totalMilliseconds = ms;
|
||||
}
|
||||
|
||||
public string formattedTime => FormatTime(totalMilliseconds);
|
||||
|
||||
private string FormatTime(long ms)
|
||||
{
|
||||
var minutes = ms / 60000;
|
||||
var seconds = ms % 60000 / 1000;
|
||||
var milliseconds = ms % 1000;
|
||||
return $"{minutes:D2}:{seconds:D2}:{milliseconds:D2}";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8f08515aa1b1499d99937107933f7a9b
|
||||
timeCreated: 1769530713
|
||||
@@ -0,0 +1,41 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Newtonsoft.Json;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Records
|
||||
{
|
||||
public 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>();
|
||||
}
|
||||
}
|
||||
|
||||
public 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c8fc6666dae743c5abb11a5fba8b495b
|
||||
timeCreated: 1769530671
|
||||
Reference in new issue
Block a user