diff --git a/Assets/Src/LapStorage.cs b/Assets/Src/LapStorage.cs index ff6f5ba..44d17ba 100644 --- a/Assets/Src/LapStorage.cs +++ b/Assets/Src/LapStorage.cs @@ -8,45 +8,26 @@ using UnityEngine; using UnityEngine.Networking; using UnityEngine.UIElements; -[Serializable] -public class SectorTime { - public int Sector; - public double Seconds; - public override string ToString() => $"{Seconds:0.000}"; -} - -[Serializable] -public class Lap { - public string Map; - public List Sectors; - - // 0:1:39 - public override string ToString() { - return $"{TimeSpan.FromSeconds(TotalTime):m\\:ss\\.fff}"; - } - - public double TotalTime => Sectors.Sum(s => s.Seconds); -} - [Serializable] public class PlayerData { - public Guid Id; + public string Id; } -public class LapStorage { - public string Map = "unset"; - public List Laps = new(); - public int Completions = 0; - public int Position = 0; +public class LapDTO { + public DateTimeOffset Created { get; set; } + public Guid Id { get; set; } + public Guid UserId { get; set; } + public string Map { get; set; } + public double Time { get; set; } } -static class LapStorageService { +public class LapFetchService { static string playerDataPath = Path.Combine(Application.persistentDataPath, "playerData.json"); + string _mapName; PlayerData player; - LapStorage lapStorage; - - static LapStorageService() { + public LapFetchService(string mapName) { + _mapName = mapName; try { string json = File.ReadAllText(playerDataPath); JsonUtility.FromJsonOverwrite(json, player); @@ -56,9 +37,25 @@ static class LapStorageService { } } - public static LapJSON GetPlayerLaps(string mapName) { - // get - return new Lap; + public IEnumerator GetPb() { + string uri = $"localhost:40080/Pb?userId={player.Id}&mapName={_mapName}"; + Debug.Log(uri); + using (UnityWebRequest req = UnityWebRequest.Get(uri)) { + yield return req.SendWebRequest(); + + switch (req.result) { + case UnityWebRequest.Result.ConnectionError: + case UnityWebRequest.Result.DataProcessingError: + Debug.LogError("Error: " + req.error); + break; + case UnityWebRequest.Result.ProtocolError: + Debug.LogError("HTTP Error: " + req.error); + break; + case UnityWebRequest.Result.Success: + Debug.Log("nReceived: " + req.downloadHandler.text); + break; + } + } } // laps.count @@ -72,13 +69,10 @@ static class LapStorageService { } PlayerData CreatePlayerData() { - player = new PlayerData { Id = Guid.NewGuid() } - File.WriteAllText(playerDataPath, JsonUtility.ToJson(data)); + player = new PlayerData { Id = Guid.NewGuid().ToString() }; + Debug.Log(player); + Debug.Log(JsonUtility.ToJson(player)); + File.WriteAllText(playerDataPath, JsonUtility.ToJson(player)); return player; } - - public static Lap PB(string mapName) { - return lapsByMap[mapName].Laps - .MinBy(l => l.TotalTime); - } } diff --git a/Assets/Src/LapStorage.cs.meta b/Assets/Src/LapStorage.cs.meta new file mode 100644 index 0000000..a5b52df --- /dev/null +++ b/Assets/Src/LapStorage.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 00d682b1176e566f9aae5613d818f385 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Src/SpaceRaceShared.cs b/Assets/Src/SpaceRaceShared.cs new file mode 120000 index 0000000..b73b64a --- /dev/null +++ b/Assets/Src/SpaceRaceShared.cs @@ -0,0 +1 @@ +../../Server/Models/SpaceRaceShared.cs \ No newline at end of file diff --git a/Assets/Src/SpaceRaceShared.cs.meta b/Assets/Src/SpaceRaceShared.cs.meta new file mode 100644 index 0000000..887f5a0 --- /dev/null +++ b/Assets/Src/SpaceRaceShared.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 920bca976301d21d2bfd4560944f9803 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Src/Timing.cs b/Assets/Src/Timing.cs index d59ed3c..cae4bfc 100644 --- a/Assets/Src/Timing.cs +++ b/Assets/Src/Timing.cs @@ -25,6 +25,8 @@ public class Timing: MonoBehaviour { IEnumerator noticeLifecycle; + LapFetchService lapFetchService; + void Awake() { ui = GetComponent(); sectorTimes = new List(); @@ -38,6 +40,9 @@ public class Timing: MonoBehaviour { Debug.Log("Track Sector Count - " + sectors.Length); LapStorageService.GetPlayerLaps(mapName); + + lapFetchService = new LapFetchService(mapName); + StartCoroutine(lapFetchService.GetPb()); } void Update() { @@ -129,9 +134,7 @@ public class Timing: MonoBehaviour { ui.HideRecentLap(); } - public Lap PB() { - return LapStorageService.PB(mapName); - } + public Lap PB() => LapStorageService.PB(mapName); public TimingNotice PBSectorDiff(SectorTime s) => PB() switch { Lap pb => pb.Sectors.SingleOrDefault(fls => fls.Sector == s.Sector) switch { diff --git a/Server/Controllers/LapController.cs b/Server/Controllers/LapController.cs index fb91b82..0f2e1c2 100644 --- a/Server/Controllers/LapController.cs +++ b/Server/Controllers/LapController.cs @@ -1,7 +1,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using System.Data.SqlClient; -using Server.Models; +using SpaceRace.Shared; namespace Server.Controllers; diff --git a/Server/Controllers/PbController.cs b/Server/Controllers/PbController.cs index bfcc6d1..9390223 100644 --- a/Server/Controllers/PbController.cs +++ b/Server/Controllers/PbController.cs @@ -1,7 +1,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using System.Data.SqlClient; -using Server.Models; +using SpaceRace.Shared; namespace Server.Controllers; diff --git a/Server/Migrations/20230829030053_Beginninging.Designer.cs b/Server/Migrations/20230829062456_Beginninging.Designer.cs similarity index 74% rename from Server/Migrations/20230829030053_Beginninging.Designer.cs rename to Server/Migrations/20230829062456_Beginninging.Designer.cs index 93930c5..dc1aaac 100644 --- a/Server/Migrations/20230829030053_Beginninging.Designer.cs +++ b/Server/Migrations/20230829062456_Beginninging.Designer.cs @@ -12,7 +12,7 @@ using Server; namespace Server.Migrations { [DbContext(typeof(SpaceRaceDbContext))] - [Migration("20230829030053_Beginninging")] + [Migration("20230829062456_Beginninging")] partial class Beginninging { /// @@ -56,31 +56,31 @@ namespace Server.Migrations b.HasData( new { - Id = new Guid("dd85f455-658c-4e39-b77e-a8f227c3d0cf"), - Created = new DateTimeOffset(new DateTime(2023, 8, 29, 3, 0, 53, 704, DateTimeKind.Unspecified).AddTicks(7826), new TimeSpan(0, 0, 0, 0, 0)), + Id = new Guid("42ee5177-d287-4ea1-91ed-ddd1d596e81f"), + Created = new DateTimeOffset(new DateTime(2023, 8, 29, 6, 24, 56, 272, DateTimeKind.Unspecified).AddTicks(1375), new TimeSpan(0, 0, 0, 0, 0)), Map = "sr_TestTrack", Time = 128.0, - UserId = new Guid("a6e068ac-5aaa-48f2-adf5-ca30963d2f9d") + UserId = new Guid("11111111-1111-1111-1111-111111111111") }, new { - Id = new Guid("be83b30b-15f0-4013-95c1-d4c71ac602f9"), - Created = new DateTimeOffset(new DateTime(2023, 8, 29, 3, 0, 53, 704, DateTimeKind.Unspecified).AddTicks(7873), new TimeSpan(0, 0, 0, 0, 0)), + Id = new Guid("81eb5dcb-1dd4-483b-993e-e73e1b95c5ff"), + Created = new DateTimeOffset(new DateTime(2023, 8, 29, 6, 24, 56, 272, DateTimeKind.Unspecified).AddTicks(1459), new TimeSpan(0, 0, 0, 0, 0)), Map = "sr_TestTrack", Time = 132.0, - UserId = new Guid("3620fbda-a45a-4eec-824b-9a6c77e9dbfd") + UserId = new Guid("22222222-2222-2222-2222-222222222222") }, new { - Id = new Guid("cb42e7cb-02c4-4725-8d41-a62fa3868d67"), - Created = new DateTimeOffset(new DateTime(2023, 8, 29, 3, 0, 53, 704, DateTimeKind.Unspecified).AddTicks(7881), new TimeSpan(0, 0, 0, 0, 0)), + Id = new Guid("69002b76-54e2-455f-99aa-2fdb7f11c130"), + Created = new DateTimeOffset(new DateTime(2023, 8, 29, 6, 24, 56, 272, DateTimeKind.Unspecified).AddTicks(1471), new TimeSpan(0, 0, 0, 0, 0)), Map = "sr_TestTrack", Time = 148.0, - UserId = new Guid("b3a23d2c-ee77-4122-9086-50a884bf49b5") + UserId = new Guid("33333333-3333-3333-3333-333333333333") }); }); - modelBuilder.Entity("Server.Models.PbDTO", b => + modelBuilder.Entity("SpaceRace.Shared.PbDTO", b => { b.Property("Completions") .HasColumnType("integer"); @@ -104,6 +104,8 @@ namespace Server.Migrations .HasColumnType("uuid"); b.ToTable("PbDTO"); + + b.ToSqlQuery("WITH ranked AS (\n SELECT *, rank() OVER (ORDER BY \"Time\" ASC) AS \"Position\", COUNT(\"Id\") OVER () AS \"Completions\"\n FROM \"Laps\"\n)\n\nSELECT * FROM ranked"); }); #pragma warning restore 612, 618 } diff --git a/Server/Migrations/20230829030053_Beginninging.cs b/Server/Migrations/20230829062456_Beginninging.cs similarity index 76% rename from Server/Migrations/20230829030053_Beginninging.cs rename to Server/Migrations/20230829062456_Beginninging.cs index 4116da2..ca2e502 100644 --- a/Server/Migrations/20230829030053_Beginninging.cs +++ b/Server/Migrations/20230829062456_Beginninging.cs @@ -49,9 +49,9 @@ namespace Server.Migrations columns: new[] { "Id", "Created", "Map", "Time", "UserId" }, values: new object[,] { - { new Guid("be83b30b-15f0-4013-95c1-d4c71ac602f9"), new DateTimeOffset(new DateTime(2023, 8, 29, 3, 0, 53, 704, DateTimeKind.Unspecified).AddTicks(7873), new TimeSpan(0, 0, 0, 0, 0)), "sr_TestTrack", 132.0, new Guid("3620fbda-a45a-4eec-824b-9a6c77e9dbfd") }, - { new Guid("cb42e7cb-02c4-4725-8d41-a62fa3868d67"), new DateTimeOffset(new DateTime(2023, 8, 29, 3, 0, 53, 704, DateTimeKind.Unspecified).AddTicks(7881), new TimeSpan(0, 0, 0, 0, 0)), "sr_TestTrack", 148.0, new Guid("b3a23d2c-ee77-4122-9086-50a884bf49b5") }, - { new Guid("dd85f455-658c-4e39-b77e-a8f227c3d0cf"), new DateTimeOffset(new DateTime(2023, 8, 29, 3, 0, 53, 704, DateTimeKind.Unspecified).AddTicks(7826), new TimeSpan(0, 0, 0, 0, 0)), "sr_TestTrack", 128.0, new Guid("a6e068ac-5aaa-48f2-adf5-ca30963d2f9d") } + { new Guid("42ee5177-d287-4ea1-91ed-ddd1d596e81f"), new DateTimeOffset(new DateTime(2023, 8, 29, 6, 24, 56, 272, DateTimeKind.Unspecified).AddTicks(1375), new TimeSpan(0, 0, 0, 0, 0)), "sr_TestTrack", 128.0, new Guid("11111111-1111-1111-1111-111111111111") }, + { new Guid("69002b76-54e2-455f-99aa-2fdb7f11c130"), new DateTimeOffset(new DateTime(2023, 8, 29, 6, 24, 56, 272, DateTimeKind.Unspecified).AddTicks(1471), new TimeSpan(0, 0, 0, 0, 0)), "sr_TestTrack", 148.0, new Guid("33333333-3333-3333-3333-333333333333") }, + { new Guid("81eb5dcb-1dd4-483b-993e-e73e1b95c5ff"), new DateTimeOffset(new DateTime(2023, 8, 29, 6, 24, 56, 272, DateTimeKind.Unspecified).AddTicks(1459), new TimeSpan(0, 0, 0, 0, 0)), "sr_TestTrack", 132.0, new Guid("22222222-2222-2222-2222-222222222222") } }); migrationBuilder.CreateIndex( diff --git a/Server/Migrations/SpaceRaceDbContextModelSnapshot.cs b/Server/Migrations/SpaceRaceDbContextModelSnapshot.cs index 889618c..d6b723a 100644 --- a/Server/Migrations/SpaceRaceDbContextModelSnapshot.cs +++ b/Server/Migrations/SpaceRaceDbContextModelSnapshot.cs @@ -53,31 +53,31 @@ namespace Server.Migrations b.HasData( new { - Id = new Guid("dd85f455-658c-4e39-b77e-a8f227c3d0cf"), - Created = new DateTimeOffset(new DateTime(2023, 8, 29, 3, 0, 53, 704, DateTimeKind.Unspecified).AddTicks(7826), new TimeSpan(0, 0, 0, 0, 0)), + Id = new Guid("42ee5177-d287-4ea1-91ed-ddd1d596e81f"), + Created = new DateTimeOffset(new DateTime(2023, 8, 29, 6, 24, 56, 272, DateTimeKind.Unspecified).AddTicks(1375), new TimeSpan(0, 0, 0, 0, 0)), Map = "sr_TestTrack", Time = 128.0, - UserId = new Guid("a6e068ac-5aaa-48f2-adf5-ca30963d2f9d") + UserId = new Guid("11111111-1111-1111-1111-111111111111") }, new { - Id = new Guid("be83b30b-15f0-4013-95c1-d4c71ac602f9"), - Created = new DateTimeOffset(new DateTime(2023, 8, 29, 3, 0, 53, 704, DateTimeKind.Unspecified).AddTicks(7873), new TimeSpan(0, 0, 0, 0, 0)), + Id = new Guid("81eb5dcb-1dd4-483b-993e-e73e1b95c5ff"), + Created = new DateTimeOffset(new DateTime(2023, 8, 29, 6, 24, 56, 272, DateTimeKind.Unspecified).AddTicks(1459), new TimeSpan(0, 0, 0, 0, 0)), Map = "sr_TestTrack", Time = 132.0, - UserId = new Guid("3620fbda-a45a-4eec-824b-9a6c77e9dbfd") + UserId = new Guid("22222222-2222-2222-2222-222222222222") }, new { - Id = new Guid("cb42e7cb-02c4-4725-8d41-a62fa3868d67"), - Created = new DateTimeOffset(new DateTime(2023, 8, 29, 3, 0, 53, 704, DateTimeKind.Unspecified).AddTicks(7881), new TimeSpan(0, 0, 0, 0, 0)), + Id = new Guid("69002b76-54e2-455f-99aa-2fdb7f11c130"), + Created = new DateTimeOffset(new DateTime(2023, 8, 29, 6, 24, 56, 272, DateTimeKind.Unspecified).AddTicks(1471), new TimeSpan(0, 0, 0, 0, 0)), Map = "sr_TestTrack", Time = 148.0, - UserId = new Guid("b3a23d2c-ee77-4122-9086-50a884bf49b5") + UserId = new Guid("33333333-3333-3333-3333-333333333333") }); }); - modelBuilder.Entity("Server.Models.PbDTO", b => + modelBuilder.Entity("SpaceRace.Shared.PbDTO", b => { b.Property("Completions") .HasColumnType("integer"); @@ -101,6 +101,8 @@ namespace Server.Migrations .HasColumnType("uuid"); b.ToTable("PbDTO"); + + b.ToSqlQuery("WITH ranked AS (\n SELECT *, rank() OVER (ORDER BY \"Time\" ASC) AS \"Position\", COUNT(\"Id\") OVER () AS \"Completions\"\n FROM \"Laps\"\n)\n\nSELECT * FROM ranked"); }); #pragma warning restore 612, 618 } diff --git a/Server/Models/LapDTO.cs b/Server/Models/LapDTO.cs deleted file mode 100644 index 5fca765..0000000 --- a/Server/Models/LapDTO.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace Server.Models; - -public class LapDTO { - public DateTimeOffset Created { get; set; } - public Guid Id { get; set; } - public Guid UserId { get; set; } - public string Map { get; set; } - public double Time { get; set; } -} diff --git a/Server/Models/PbDTO.cs b/Server/Models/PbDTO.cs deleted file mode 100644 index bc709a3..0000000 --- a/Server/Models/PbDTO.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace Server.Models; - -public class PbDTO { - public Guid Id { get; set; } - public Guid UserId { get; set; } - public DateTimeOffset Created { get; set; } - public double Time { get; set; } - public string Map { get; set; } - public int Completions { get; set; } - public int Position { get; set; } -} diff --git a/Server/Models/SpaceRaceShared.cs b/Server/Models/SpaceRaceShared.cs new file mode 100644 index 0000000..66f4be6 --- /dev/null +++ b/Server/Models/SpaceRaceShared.cs @@ -0,0 +1,21 @@ +using System; + +namespace SpaceRace.Shared { + public class LapDTO { + public DateTimeOffset Created { get; set; } + public Guid Id { get; set; } + public Guid UserId { get; set; } + public string Map { get; set; } + public double Time { get; set; } + } + + public class PbDTO { + public Guid Id { get; set; } + public Guid UserId { get; set; } + public DateTimeOffset Created { get; set; } + public double Time { get; set; } + public string Map { get; set; } + public int Completions { get; set; } + public int Position { get; set; } + } +} diff --git a/Server/Repositories/SpaceRaceDbContext.cs b/Server/Repositories/SpaceRaceDbContext.cs index 94690df..450fdef 100644 --- a/Server/Repositories/SpaceRaceDbContext.cs +++ b/Server/Repositories/SpaceRaceDbContext.cs @@ -1,7 +1,7 @@ using Microsoft.EntityFrameworkCore; using System.ComponentModel.DataAnnotations; -using Server.Models; +using SpaceRace.Shared; namespace Server; @@ -52,21 +52,21 @@ class SpaceRaceInitialiser { new Lap { Created = DateTimeOffset.UtcNow, Id = Guid.NewGuid(), - UserId = Guid.NewGuid(), + UserId = new Guid("11111111-1111-1111-1111-111111111111"), Time = 128f, Map = "sr_TestTrack", }, new Lap { Created = DateTimeOffset.UtcNow, Id = Guid.NewGuid(), - UserId = Guid.NewGuid(), + UserId = new Guid("22222222-2222-2222-2222-222222222222"), Time = 132f, Map = "sr_TestTrack", }, new Lap { Created = DateTimeOffset.UtcNow, Id = Guid.NewGuid(), - UserId = Guid.NewGuid(), + UserId = new Guid("33333333-3333-3333-3333-333333333333"), Time = 148f, Map = "sr_TestTrack", }