From d2277f4e0f3f32a9dd97e3305f53293b10438ba0 Mon Sep 17 00:00:00 2001 From: ntr Date: Tue, 29 Aug 2023 11:06:53 +1000 Subject: [PATCH] server post wip --- Assets/Images/controls.svg.meta | 54 ++++++++++++ Assets/Src/LapStorage.cs | 84 +++++++++++++++++++ Assets/Src/Laps.cs | 2 +- README.html | 32 +++++++ Server/Controllers/CompletionController.cs | 33 +++++++- ...> 20230828112623_Beginninging.Designer.cs} | 7 +- ...ging.cs => 20230828112623_Beginninging.cs} | 11 +-- .../SpaceRaceDbContextModelSnapshot.cs | 5 +- Server/Models/CompletionDTO.cs | 9 -- Server/Models/LapDTO.cs | 9 ++ Server/Models/PbDTO.cs | 10 +++ Server/Repositories/SpaceRaceDbContext.cs | 12 +-- docker-compose.yaml | 1 + 13 files changed, 239 insertions(+), 30 deletions(-) create mode 100644 Assets/Images/controls.svg.meta create mode 100644 Assets/Src/LapStorage.cs create mode 100644 README.html rename Server/Migrations/{20230823032740_Beginninging.Designer.cs => 20230828112623_Beginninging.Designer.cs} (89%) rename Server/Migrations/{20230823032740_Beginninging.cs => 20230828112623_Beginninging.cs} (82%) delete mode 100644 Server/Models/CompletionDTO.cs create mode 100644 Server/Models/LapDTO.cs create mode 100644 Server/Models/PbDTO.cs diff --git a/Assets/Images/controls.svg.meta b/Assets/Images/controls.svg.meta new file mode 100644 index 0000000..62e3dae --- /dev/null +++ b/Assets/Images/controls.svg.meta @@ -0,0 +1,54 @@ +fileFormatVersion: 2 +guid: e627c538dce2b838d8702905b032ac82 +ScriptedImporter: + internalIDToNameTable: [] + externalObjects: {} + serializedVersion: 2 + userData: + assetBundleName: + assetBundleVariant: + script: {fileID: 11500000, guid: a57477913897c46af95d590f580878bd, type: 3} + svgType: 0 + texturedSpriteMeshType: 0 + svgPixelsPerUnit: 100 + gradientResolution: 64 + alignment: 0 + customPivot: {x: 0, y: 0} + generatePhysicsShape: 0 + viewportOptions: 0 + preserveViewport: 0 + advancedMode: 0 + predefinedResolutionIndex: 1 + targetResolution: 1080 + resolutionMultiplier: 1 + stepDistance: 10 + samplingStepDistance: 100 + maxCordDeviationEnabled: 0 + maxCordDeviation: 1 + maxTangentAngleEnabled: 0 + maxTangentAngle: 5 + keepTextureAspectRatio: 1 + textureSize: 256 + textureWidth: 256 + textureHeight: 256 + wrapMode: 0 + filterMode: 1 + sampleCount: 4 + preserveSVGImageAspect: 0 + useSVGPixelsPerUnit: 0 + spriteData: + TessellationDetail: 0 + SpriteRect: + name: + originalName: + pivot: {x: 0, y: 0} + alignment: 0 + border: {x: 0, y: 0, z: 0, w: 0} + rect: + serializedVersion: 2 + x: 0 + y: 0 + width: 0 + height: 0 + spriteID: 974c0935effe6880abbfb6994005227d + PhysicsOutlines: [] diff --git a/Assets/Src/LapStorage.cs b/Assets/Src/LapStorage.cs new file mode 100644 index 0000000..ff6f5ba --- /dev/null +++ b/Assets/Src/LapStorage.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.IO; + +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 class LapStorage { + public string Map = "unset"; + public List Laps = new(); + public int Completions = 0; + public int Position = 0; +} + +static class LapStorageService { + static string playerDataPath = Path.Combine(Application.persistentDataPath, "playerData.json"); + PlayerData player; + + LapStorage lapStorage; + + static LapStorageService() { + try { + string json = File.ReadAllText(playerDataPath); + JsonUtility.FromJsonOverwrite(json, player); + } catch { + Debug.Log("No existing playerData.json"); + CreatePlayerData(); + } + } + + public static LapJSON GetPlayerLaps(string mapName) { + // get + return new Lap; + } + + // laps.count + public static int NumberOfCompletions() { return 0; } + + // finds your PB in all completions + public static int Position() { return 1; } + + public static void PersistLap(Lap lap) { + Debug.Log(JsonUtility.ToJson(lap)); + } + + PlayerData CreatePlayerData() { + player = new PlayerData { Id = Guid.NewGuid() } + File.WriteAllText(playerDataPath, JsonUtility.ToJson(data)); + return player; + } + + public static Lap PB(string mapName) { + return lapsByMap[mapName].Laps + .MinBy(l => l.TotalTime); + } +} diff --git a/Assets/Src/Laps.cs b/Assets/Src/Laps.cs index 37f6f97..f1ae99a 100644 --- a/Assets/Src/Laps.cs +++ b/Assets/Src/Laps.cs @@ -131,7 +131,7 @@ static class LapStorageService { public static int NumberOfCompletions() { return 0; } // finds your PB in all completions - public static int Rank() { return 1; } + public static int Position() { return 1; } public static void PersistLap(Lap lap) { LapJSON lapJson = GetPlayerLaps(lap.Map); diff --git a/README.html b/README.html new file mode 100644 index 0000000..9f8f645 --- /dev/null +++ b/README.html @@ -0,0 +1,32 @@ + + + + + + SpaceRace Information + + + + + + +

Go!

+
Run SpaceRace.exe to start the game.
+ + + + \ No newline at end of file diff --git a/Server/Controllers/CompletionController.cs b/Server/Controllers/CompletionController.cs index 1242092..eb25a85 100644 --- a/Server/Controllers/CompletionController.cs +++ b/Server/Controllers/CompletionController.cs @@ -1,5 +1,4 @@ using Microsoft.AspNetCore.Mvc; - using Server.Models; namespace Server.Controllers; @@ -22,18 +21,44 @@ public class LapController : ControllerBase { UserId = c.UserId, Map = c.Map, Time = c.Time, - Date = c.Date, + Created = c.Created, }); } + // [HttpGet()] + // public LapStorageDTO UserLaps(Guid userId, string mapName) { + // List laps = _db.Laps + // .Where(l => l.Id == userId) + // .Where(l => l.Map == mapName); + + // int completions = _db.Laps + // .Where(l => l.Map == mapName) + // .Count(); + + // int position = _db.Database.sql(""" + // SELECT + // """); + + // return new LapStorageDTO { + // Map = mapName, + // Laps = laps, + // public int Completions { get; set; } + // public int Position { get; set; } + + // } + // } + [HttpPost()] public async Task Post(LapDTO dto) { + Lap existing = _db.Laps + .SingleOrDefault(l => l.UserId == dto.UserId && l.Map == dto.Map); + Lap v = new Lap { Id = Guid.NewGuid(), - UserId = Guid.NewGuid(), + UserId = dto.UserId, Map = dto.Map, Time = dto.Time, - Date = DateTimeOffset.UtcNow, + Created = DateTimeOffset.UtcNow, }; _db.Laps.Add(v); diff --git a/Server/Migrations/20230823032740_Beginninging.Designer.cs b/Server/Migrations/20230828112623_Beginninging.Designer.cs similarity index 89% rename from Server/Migrations/20230823032740_Beginninging.Designer.cs rename to Server/Migrations/20230828112623_Beginninging.Designer.cs index fcc9cc5..f7a9660 100644 --- a/Server/Migrations/20230823032740_Beginninging.Designer.cs +++ b/Server/Migrations/20230828112623_Beginninging.Designer.cs @@ -12,7 +12,7 @@ using Server; namespace Server.Migrations { [DbContext(typeof(SpaceRaceDbContext))] - [Migration("20230823032740_Beginninging")] + [Migration("20230828112623_Beginninging")] partial class Beginninging { /// @@ -31,7 +31,7 @@ namespace Server.Migrations .ValueGeneratedOnAdd() .HasColumnType("uuid"); - b.Property("Date") + b.Property("Created") .HasColumnType("timestamp with time zone"); b.Property("Map") @@ -48,7 +48,8 @@ namespace Server.Migrations b.HasIndex("Map"); - b.HasIndex("UserId"); + b.HasIndex("UserId", "Map") + .IsUnique(); b.ToTable("Laps"); }); diff --git a/Server/Migrations/20230823032740_Beginninging.cs b/Server/Migrations/20230828112623_Beginninging.cs similarity index 82% rename from Server/Migrations/20230823032740_Beginninging.cs rename to Server/Migrations/20230828112623_Beginninging.cs index 8376df7..6a94f1a 100644 --- a/Server/Migrations/20230823032740_Beginninging.cs +++ b/Server/Migrations/20230828112623_Beginninging.cs @@ -16,10 +16,10 @@ namespace Server.Migrations columns: table => new { Id = table.Column(type: "uuid", nullable: false), - Date = table.Column(type: "timestamp with time zone", nullable: false), + Created = table.Column(type: "timestamp with time zone", nullable: false), UserId = table.Column(type: "uuid", nullable: false), - Map = table.Column(type: "text", nullable: false), - Time = table.Column(type: "double precision", nullable: false) + Time = table.Column(type: "double precision", nullable: false), + Map = table.Column(type: "text", nullable: false) }, constraints: table => { @@ -32,9 +32,10 @@ namespace Server.Migrations column: "Map"); migrationBuilder.CreateIndex( - name: "IX_Laps_UserId", + name: "IX_Laps_UserId_Map", table: "Laps", - column: "UserId"); + columns: new[] { "UserId", "Map" }, + unique: true); } /// diff --git a/Server/Migrations/SpaceRaceDbContextModelSnapshot.cs b/Server/Migrations/SpaceRaceDbContextModelSnapshot.cs index 1721093..7337612 100644 --- a/Server/Migrations/SpaceRaceDbContextModelSnapshot.cs +++ b/Server/Migrations/SpaceRaceDbContextModelSnapshot.cs @@ -28,7 +28,7 @@ namespace Server.Migrations .ValueGeneratedOnAdd() .HasColumnType("uuid"); - b.Property("Date") + b.Property("Created") .HasColumnType("timestamp with time zone"); b.Property("Map") @@ -45,7 +45,8 @@ namespace Server.Migrations b.HasIndex("Map"); - b.HasIndex("UserId"); + b.HasIndex("UserId", "Map") + .IsUnique(); b.ToTable("Laps"); }); diff --git a/Server/Models/CompletionDTO.cs b/Server/Models/CompletionDTO.cs deleted file mode 100644 index a61c435..0000000 --- a/Server/Models/CompletionDTO.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace Server.Models; - -public class LapDTO { - public DateTimeOffset Date { 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/LapDTO.cs b/Server/Models/LapDTO.cs new file mode 100644 index 0000000..5fca765 --- /dev/null +++ b/Server/Models/LapDTO.cs @@ -0,0 +1,9 @@ +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 new file mode 100644 index 0000000..226433a --- /dev/null +++ b/Server/Models/PbDTO.cs @@ -0,0 +1,10 @@ +namespace Server.Models; + +public class PbDTO { + public Guid Id { get; set; } + public Guid UserId { 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 f20a2f5..7d8eedc 100644 --- a/Server/Repositories/SpaceRaceDbContext.cs +++ b/Server/Repositories/SpaceRaceDbContext.cs @@ -8,12 +8,12 @@ public class SpaceRaceDbContext : DbContext { public SpaceRaceDbContext(DbContextOptions options) : base(options) {} } -[Index(nameof(UserId))] +[Index(nameof(UserId), nameof(Map), IsUnique = true)] [Index(nameof(Map))] public class Lap { - public DateTimeOffset Date { get; set; } - public Guid Id { get; set; } - public Guid UserId { get; set; } - [Required] public string Map { get; set; } - public double Time { get; set; } + public DateTimeOffset Created { get; set; } + public Guid Id { get; set; } + public Guid UserId { get; set; } + public double Time { get; set; } + [Required] public string Map { get; set; } } diff --git a/docker-compose.yaml b/docker-compose.yaml index 1db63cb..c7ff806 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -12,6 +12,7 @@ services: - '40080:80' environment: CONNECTIONSTRINGS__SPACERACE: "Server=pg;Port=5432;Database=${POSTGRES_DB};User Id=${POSTGRES_USER};Password=${POSTGRES_PASSWORD}" + DOTNET_WATCH_RESTART_ON_RUDE_EDIT: 1 command: dotnet watch working_dir: "/Server" volumes: