spaceraceshared

This commit is contained in:
ntr committed 2023-08-29 17:38:16 +10:00
1 parent 1fb2d9eb1d
commit c851a0942a
14 files changed
+118 -93

No files matched your search

+34 -40
View File
@@ -8,45 +8,26 @@ using UnityEngine;
using UnityEngine.Networking; using UnityEngine.Networking;
using UnityEngine.UIElements; 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<SectorTime> Sectors;
// 0:1:39
public override string ToString() {
return $"{TimeSpan.FromSeconds(TotalTime):m\\:ss\\.fff}";
}
public double TotalTime => Sectors.Sum(s => s.Seconds);
}
[Serializable] [Serializable]
public class PlayerData { public class PlayerData {
public Guid Id; public string Id;
} }
public class LapStorage { public class LapDTO {
public string Map = "unset"; public DateTimeOffset Created { get; set; }
public List<Lap> Laps = new(); public Guid Id { get; set; }
public int Completions = 0; public Guid UserId { get; set; }
public int Position = 0; 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"); static string playerDataPath = Path.Combine(Application.persistentDataPath, "playerData.json");
string _mapName;
PlayerData player; PlayerData player;
LapStorage lapStorage; public LapFetchService(string mapName) {
_mapName = mapName;
static LapStorageService() {
try { try {
string json = File.ReadAllText(playerDataPath); string json = File.ReadAllText(playerDataPath);
JsonUtility.FromJsonOverwrite(json, player); JsonUtility.FromJsonOverwrite(json, player);
@@ -56,9 +37,25 @@ static class LapStorageService {
} }
} }
public static LapJSON GetPlayerLaps(string mapName) { public IEnumerator GetPb() {
// get string uri = $"localhost:40080/Pb?userId={player.Id}&mapName={_mapName}";
return new Lap; 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 // laps.count
@@ -72,13 +69,10 @@ static class LapStorageService {
} }
PlayerData CreatePlayerData() { PlayerData CreatePlayerData() {
player = new PlayerData { Id = Guid.NewGuid() } player = new PlayerData { Id = Guid.NewGuid().ToString() };
File.WriteAllText(playerDataPath, JsonUtility.ToJson(data)); Debug.Log(player);
Debug.Log(JsonUtility.ToJson(player));
File.WriteAllText(playerDataPath, JsonUtility.ToJson(player));
return player; return player;
} }
public static Lap PB(string mapName) {
return lapsByMap[mapName].Laps
.MinBy(l => l.TotalTime);
}
} }
+11
View File
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 00d682b1176e566f9aae5613d818f385
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+1
View File
@@ -0,0 +1 @@
../../Server/Models/SpaceRaceShared.cs
+11
View File
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 920bca976301d21d2bfd4560944f9803
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+6 -3
View File
@@ -25,6 +25,8 @@ public class Timing: MonoBehaviour {
IEnumerator noticeLifecycle; IEnumerator noticeLifecycle;
LapFetchService lapFetchService;
void Awake() { void Awake() {
ui = GetComponent<TimingUI>(); ui = GetComponent<TimingUI>();
sectorTimes = new List<SectorTime>(); sectorTimes = new List<SectorTime>();
@@ -38,6 +40,9 @@ public class Timing: MonoBehaviour {
Debug.Log("Track Sector Count - " + sectors.Length); Debug.Log("Track Sector Count - " + sectors.Length);
LapStorageService.GetPlayerLaps(mapName); LapStorageService.GetPlayerLaps(mapName);
lapFetchService = new LapFetchService(mapName);
StartCoroutine(lapFetchService.GetPb());
} }
void Update() { void Update() {
@@ -129,9 +134,7 @@ public class Timing: MonoBehaviour {
ui.HideRecentLap(); ui.HideRecentLap();
} }
public Lap PB() { public Lap PB() => LapStorageService.PB(mapName);
return LapStorageService.PB(mapName);
}
public TimingNotice PBSectorDiff(SectorTime s) => PB() switch { public TimingNotice PBSectorDiff(SectorTime s) => PB() switch {
Lap pb => pb.Sectors.SingleOrDefault(fls => fls.Sector == s.Sector) switch { Lap pb => pb.Sectors.SingleOrDefault(fls => fls.Sector == s.Sector) switch {
+1 -1
View File
@@ -1,7 +1,7 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System.Data.SqlClient; using System.Data.SqlClient;
using Server.Models; using SpaceRace.Shared;
namespace Server.Controllers; namespace Server.Controllers;
+1 -1
View File
@@ -1,7 +1,7 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System.Data.SqlClient; using System.Data.SqlClient;
using Server.Models; using SpaceRace.Shared;
namespace Server.Controllers; namespace Server.Controllers;
@@ -12,7 +12,7 @@ using Server;
namespace Server.Migrations namespace Server.Migrations
{ {
[DbContext(typeof(SpaceRaceDbContext))] [DbContext(typeof(SpaceRaceDbContext))]
[Migration("20230829030053_Beginninging")] [Migration("20230829062456_Beginninging")]
partial class Beginninging partial class Beginninging
{ {
/// <inheritdoc /> /// <inheritdoc />
@@ -56,31 +56,31 @@ namespace Server.Migrations
b.HasData( b.HasData(
new new
{ {
Id = new Guid("dd85f455-658c-4e39-b77e-a8f227c3d0cf"), Id = new Guid("42ee5177-d287-4ea1-91ed-ddd1d596e81f"),
Created = new DateTimeOffset(new DateTime(2023, 8, 29, 3, 0, 53, 704, DateTimeKind.Unspecified).AddTicks(7826), new TimeSpan(0, 0, 0, 0, 0)), 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", Map = "sr_TestTrack",
Time = 128.0, Time = 128.0,
UserId = new Guid("a6e068ac-5aaa-48f2-adf5-ca30963d2f9d") UserId = new Guid("11111111-1111-1111-1111-111111111111")
}, },
new new
{ {
Id = new Guid("be83b30b-15f0-4013-95c1-d4c71ac602f9"), Id = new Guid("81eb5dcb-1dd4-483b-993e-e73e1b95c5ff"),
Created = new DateTimeOffset(new DateTime(2023, 8, 29, 3, 0, 53, 704, DateTimeKind.Unspecified).AddTicks(7873), new TimeSpan(0, 0, 0, 0, 0)), 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", Map = "sr_TestTrack",
Time = 132.0, Time = 132.0,
UserId = new Guid("3620fbda-a45a-4eec-824b-9a6c77e9dbfd") UserId = new Guid("22222222-2222-2222-2222-222222222222")
}, },
new new
{ {
Id = new Guid("cb42e7cb-02c4-4725-8d41-a62fa3868d67"), Id = new Guid("69002b76-54e2-455f-99aa-2fdb7f11c130"),
Created = new DateTimeOffset(new DateTime(2023, 8, 29, 3, 0, 53, 704, DateTimeKind.Unspecified).AddTicks(7881), new TimeSpan(0, 0, 0, 0, 0)), 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", Map = "sr_TestTrack",
Time = 148.0, 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<int>("Completions") b.Property<int>("Completions")
.HasColumnType("integer"); .HasColumnType("integer");
@@ -104,6 +104,8 @@ namespace Server.Migrations
.HasColumnType("uuid"); .HasColumnType("uuid");
b.ToTable("PbDTO"); 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 #pragma warning restore 612, 618
} }
@@ -49,9 +49,9 @@ namespace Server.Migrations
columns: new[] { "Id", "Created", "Map", "Time", "UserId" }, columns: new[] { "Id", "Created", "Map", "Time", "UserId" },
values: new object[,] 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("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("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("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("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("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( migrationBuilder.CreateIndex(
@@ -53,31 +53,31 @@ namespace Server.Migrations
b.HasData( b.HasData(
new new
{ {
Id = new Guid("dd85f455-658c-4e39-b77e-a8f227c3d0cf"), Id = new Guid("42ee5177-d287-4ea1-91ed-ddd1d596e81f"),
Created = new DateTimeOffset(new DateTime(2023, 8, 29, 3, 0, 53, 704, DateTimeKind.Unspecified).AddTicks(7826), new TimeSpan(0, 0, 0, 0, 0)), 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", Map = "sr_TestTrack",
Time = 128.0, Time = 128.0,
UserId = new Guid("a6e068ac-5aaa-48f2-adf5-ca30963d2f9d") UserId = new Guid("11111111-1111-1111-1111-111111111111")
}, },
new new
{ {
Id = new Guid("be83b30b-15f0-4013-95c1-d4c71ac602f9"), Id = new Guid("81eb5dcb-1dd4-483b-993e-e73e1b95c5ff"),
Created = new DateTimeOffset(new DateTime(2023, 8, 29, 3, 0, 53, 704, DateTimeKind.Unspecified).AddTicks(7873), new TimeSpan(0, 0, 0, 0, 0)), 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", Map = "sr_TestTrack",
Time = 132.0, Time = 132.0,
UserId = new Guid("3620fbda-a45a-4eec-824b-9a6c77e9dbfd") UserId = new Guid("22222222-2222-2222-2222-222222222222")
}, },
new new
{ {
Id = new Guid("cb42e7cb-02c4-4725-8d41-a62fa3868d67"), Id = new Guid("69002b76-54e2-455f-99aa-2fdb7f11c130"),
Created = new DateTimeOffset(new DateTime(2023, 8, 29, 3, 0, 53, 704, DateTimeKind.Unspecified).AddTicks(7881), new TimeSpan(0, 0, 0, 0, 0)), 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", Map = "sr_TestTrack",
Time = 148.0, 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<int>("Completions") b.Property<int>("Completions")
.HasColumnType("integer"); .HasColumnType("integer");
@@ -101,6 +101,8 @@ namespace Server.Migrations
.HasColumnType("uuid"); .HasColumnType("uuid");
b.ToTable("PbDTO"); 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 #pragma warning restore 612, 618
} }
-9
View File
@@ -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; }
}
-11
View File
@@ -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; }
}
+21
View File
@@ -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; }
}
}
+4 -4
View File
@@ -1,7 +1,7 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using Server.Models; using SpaceRace.Shared;
namespace Server; namespace Server;
@@ -52,21 +52,21 @@ class SpaceRaceInitialiser {
new Lap { new Lap {
Created = DateTimeOffset.UtcNow, Created = DateTimeOffset.UtcNow,
Id = Guid.NewGuid(), Id = Guid.NewGuid(),
UserId = Guid.NewGuid(), UserId = new Guid("11111111-1111-1111-1111-111111111111"),
Time = 128f, Time = 128f,
Map = "sr_TestTrack", Map = "sr_TestTrack",
}, },
new Lap { new Lap {
Created = DateTimeOffset.UtcNow, Created = DateTimeOffset.UtcNow,
Id = Guid.NewGuid(), Id = Guid.NewGuid(),
UserId = Guid.NewGuid(), UserId = new Guid("22222222-2222-2222-2222-222222222222"),
Time = 132f, Time = 132f,
Map = "sr_TestTrack", Map = "sr_TestTrack",
}, },
new Lap { new Lap {
Created = DateTimeOffset.UtcNow, Created = DateTimeOffset.UtcNow,
Id = Guid.NewGuid(), Id = Guid.NewGuid(),
UserId = Guid.NewGuid(), UserId = new Guid("33333333-3333-3333-3333-333333333333"),
Time = 148f, Time = 148f,
Map = "sr_TestTrack", Map = "sr_TestTrack",
} }