server post wip

This commit is contained in:
ntr committed 2023-08-29 11:06:53 +10:00
1 parent f6cd8d42ce
commit d2277f4e0f
13 files changed
+239 -30

No files matched your search

+54
View File
@@ -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: []
+84
View File
@@ -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<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]
public class PlayerData {
public Guid Id;
}
public class LapStorage {
public string Map = "unset";
public List<Lap> 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);
}
}
+1 -1
View File
@@ -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);
+32
View File
@@ -0,0 +1,32 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="description" content="SpaceRace">
<title>SpaceRace Information</title>
<link href="https://fonts.googleapis.com/css?family=Jura" rel="stylesheet">
<style>
body {
background: black;
color: #f5f5f5;
font-family: "Jura";
font-size: 2rem;
}
img {
width: 100%;
}
h1, div {
margin-left: 2rem;
}
</style>
<img src="./controls.png" title="Controls" />
<h1>Go!</h1>
<div>Run SpaceRace.exe to start the game.</div>
</head>
<body>
</body>
</html>
+29 -4
View File
@@ -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<Lap> 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<Laps>.sql("""
// SELECT
// """);
// return new LapStorageDTO {
// Map = mapName,
// Laps = laps,
// public int Completions { get; set; }
// public int Position { get; set; }
// }
// }
[HttpPost()]
public async Task<Lap> 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);
@@ -12,7 +12,7 @@ using Server;
namespace Server.Migrations
{
[DbContext(typeof(SpaceRaceDbContext))]
[Migration("20230823032740_Beginninging")]
[Migration("20230828112623_Beginninging")]
partial class Beginninging
{
/// <inheritdoc />
@@ -31,7 +31,7 @@ namespace Server.Migrations
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<DateTimeOffset>("Date")
b.Property<DateTimeOffset>("Created")
.HasColumnType("timestamp with time zone");
b.Property<string>("Map")
@@ -48,7 +48,8 @@ namespace Server.Migrations
b.HasIndex("Map");
b.HasIndex("UserId");
b.HasIndex("UserId", "Map")
.IsUnique();
b.ToTable("Laps");
});
@@ -16,10 +16,10 @@ namespace Server.Migrations
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
Date = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
Created = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
UserId = table.Column<Guid>(type: "uuid", nullable: false),
Map = table.Column<string>(type: "text", nullable: false),
Time = table.Column<double>(type: "double precision", nullable: false)
Time = table.Column<double>(type: "double precision", nullable: false),
Map = table.Column<string>(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);
}
/// <inheritdoc />
@@ -28,7 +28,7 @@ namespace Server.Migrations
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<DateTimeOffset>("Date")
b.Property<DateTimeOffset>("Created")
.HasColumnType("timestamp with time zone");
b.Property<string>("Map")
@@ -45,7 +45,8 @@ namespace Server.Migrations
b.HasIndex("Map");
b.HasIndex("UserId");
b.HasIndex("UserId", "Map")
.IsUnique();
b.ToTable("Laps");
});
-9
View File
@@ -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; }
}
+9
View File
@@ -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; }
}
+10
View File
@@ -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; }
}
+6 -6
View File
@@ -8,12 +8,12 @@ public class SpaceRaceDbContext : DbContext {
public SpaceRaceDbContext(DbContextOptions<SpaceRaceDbContext> 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; }
}
+1
View File
@@ -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: