From 31a2b73cf99a1b77ff09c256b911e88fbd83914d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=20Jurmanovi=C4=87?= Date: Wed, 7 May 2025 00:25:02 +0200 Subject: [PATCH] add more loggers and fix models --- local/controller/config.go | 5 +- local/model/config.go | 121 +++++++++++++++++++++++-------------- local/model/server.go | 3 - local/service/config.go | 12 ---- 4 files changed, 80 insertions(+), 61 deletions(-) diff --git a/local/controller/config.go b/local/controller/config.go index fb2bf34..618a0dd 100644 --- a/local/controller/config.go +++ b/local/controller/config.go @@ -60,7 +60,10 @@ func (ac *ConfigController) updateConfig(c *fiber.Ctx) error { return c.Status(400).SendString(err.Error()) } if restart { - ac.apiService.ApiRestartServer(c) + _, err := ac.apiService.ApiRestartServer(c) + if err != nil { + log.Print(err.Error()) + } } return c.JSON(ConfigModel) diff --git a/local/model/config.go b/local/model/config.go index f0b9be8..46e3abc 100644 --- a/local/model/config.go +++ b/local/model/config.go @@ -1,6 +1,13 @@ package model -import "time" +import ( + "encoding/json" + "fmt" + "strconv" + "time" +) + +type IntString int // Config tracks configuration modifications type Config struct { @@ -24,77 +31,101 @@ type ServerSettings struct { ServerName string `json:"serverName"` AdminPassword string `json:"adminPassword"` CarGroup string `json:"carGroup"` - TrackMedalsRequirement int `json:"trackMedalsRequirement"` - SafetyRatingRequirement int `json:"safetyRatingRequirement"` - RacecraftRatingRequirement int `json:"racecraftRatingRequirement"` + TrackMedalsRequirement IntString `json:"trackMedalsRequirement"` + SafetyRatingRequirement IntString `json:"safetyRatingRequirement"` + RacecraftRatingRequirement IntString `json:"racecraftRatingRequirement"` Password string `json:"password"` SpectatorPassword string `json:"spectatorPassword"` - MaxCarSlots int `json:"maxCarSlots"` - DumpLeaderboards int `json:"dumpLeaderboards"` - IsRaceLocked int `json:"isRaceLocked"` - RandomizeTrackWhenEmpty int `json:"randomizeTrackWhenEmpty"` + MaxCarSlots IntString `json:"maxCarSlots"` + DumpLeaderboards IntString `json:"dumpLeaderboards"` + IsRaceLocked IntString `json:"isRaceLocked"` + RandomizeTrackWhenEmpty IntString `json:"randomizeTrackWhenEmpty"` CentralEntryListPath string `json:"centralEntryListPath"` - AllowAutoDQ int `json:"allowAutoDQ"` - ShortFormationLap int `json:"shortFormationLap"` - FormationLapType int `json:"formationLapType"` - IgnorePrematureDisconnects int `json:"ignorePrematureDisconnects"` + AllowAutoDQ IntString `json:"allowAutoDQ"` + ShortFormationLap IntString `json:"shortFormationLap"` + FormationLapType IntString `json:"formationLapType"` + IgnorePrematureDisconnects IntString `json:"ignorePrematureDisconnects"` } type EventConfig struct { Track string `json:"track"` - PreRaceWaitingTimeSeconds int `json:"preRaceWaitingTimeSeconds"` - SessionOverTimeSeconds int `json:"sessionOverTimeSeconds"` - AmbientTemp int `json:"ambientTemp"` + PreRaceWaitingTimeSeconds IntString `json:"preRaceWaitingTimeSeconds"` + SessionOverTimeSeconds IntString `json:"sessionOverTimeSeconds"` + AmbientTemp IntString `json:"ambientTemp"` CloudLevel float64 `json:"cloudLevel"` Rain float64 `json:"rain"` - WeatherRandomness int `json:"weatherRandomness"` - PostQualySeconds int `json:"postQualySeconds"` - PostRaceSeconds int `json:"postRaceSeconds"` - SimracerWeatherConditions int `json:"simracerWeatherConditions"` - IsFixedConditionQualification int `json:"isFixedConditionQualification"` + WeatherRandomness IntString `json:"weatherRandomness"` + PostQualySeconds IntString `json:"postQualySeconds"` + PostRaceSeconds IntString `json:"postRaceSeconds"` + SimracerWeatherConditions IntString `json:"simracerWeatherConditions"` + IsFixedConditionQualification IntString `json:"isFixedConditionQualification"` Sessions []Session `json:"sessions"` } type Session struct { - HourOfDay int `json:"hourOfDay"` - DayOfWeekend int `json:"dayOfWeekend"` - TimeMultiplier int `json:"timeMultiplier"` + HourOfDay IntString `json:"hourOfDay"` + DayOfWeekend IntString `json:"dayOfWeekend"` + TimeMultiplier IntString `json:"timeMultiplier"` SessionType string `json:"sessionType"` - SessionDurationMinutes int `json:"sessionDurationMinutes"` + SessionDurationMinutes IntString `json:"sessionDurationMinutes"` } type AssistRules struct { - StabilityControlLevelMax int `json:"stabilityControlLevelMax"` - DisableAutosteer int `json:"disableAutosteer"` - DisableAutoLights int `json:"disableAutoLights"` - DisableAutoWiper int `json:"disableAutoWiper"` - DisableAutoEngineStart int `json:"disableAutoEngineStart"` - DisableAutoPitLimiter int `json:"disableAutoPitLimiter"` - DisableAutoGear int `json:"disableAutoGear"` - DisableAutoClutch int `json:"disableAutoClutch"` - DisableIdealLine int `json:"disableIdealLine"` + StabilityControlLevelMax IntString `json:"stabilityControlLevelMax"` + DisableAutosteer IntString `json:"disableAutosteer"` + DisableAutoLights IntString `json:"disableAutoLights"` + DisableAutoWiper IntString `json:"disableAutoWiper"` + DisableAutoEngineStart IntString `json:"disableAutoEngineStart"` + DisableAutoPitLimiter IntString `json:"disableAutoPitLimiter"` + DisableAutoGear IntString `json:"disableAutoGear"` + DisableAutoClutch IntString `json:"disableAutoClutch"` + DisableIdealLine IntString `json:"disableIdealLine"` } type EventRules struct { - QualifyStandingType int `json:"qualifyStandingType"` - PitWindowLengthSec int `json:"pitWindowLengthSec"` - DriverStintTimeSec int `json:"driverStintTimeSec"` - MandatoryPitstopCount int `json:"mandatoryPitstopCount"` - MaxTotalDrivingTime int `json:"maxTotalDrivingTime"` + QualifyStandingType IntString `json:"qualifyStandingType"` + PitWindowLengthSec IntString `json:"pitWindowLengthSec"` + DriverStIntStringTimeSec IntString `json:"driverStIntStringTimeSec"` + MandatoryPitstopCount IntString `json:"mandatoryPitstopCount"` + MaxTotalDrivingTime IntString `json:"maxTotalDrivingTime"` IsRefuellingAllowedInRace bool `json:"isRefuellingAllowedInRace"` IsRefuellingTimeFixed bool `json:"isRefuellingTimeFixed"` IsMandatoryPitstopRefuellingRequired bool `json:"isMandatoryPitstopRefuellingRequired"` IsMandatoryPitstopTyreChangeRequired bool `json:"isMandatoryPitstopTyreChangeRequired"` IsMandatoryPitstopSwapDriverRequired bool `json:"isMandatoryPitstopSwapDriverRequired"` - TyreSetCount int `json:"tyreSetCount"` + TyreSetCount IntString `json:"tyreSetCount"` } type Configuration struct { - UdpPort int `json:"udpPort"` - TcpPort int `json:"tcpPort"` - MaxConnections int `json:"maxConnections"` - LanDiscovery int `json:"lanDiscovery"` - RegisterToLobby int `json:"registerToLobby"` - ConfigVersion int `json:"configVersion"` + UdpPort IntString `json:"udpPort"` + TcpPort IntString `json:"tcpPort"` + MaxConnections IntString `json:"maxConnections"` + LanDiscovery IntString `json:"lanDiscovery"` + RegisterToLobby IntString `json:"registerToLobby"` + ConfigVersion IntString `json:"configVersion"` } + +func (i *IntString) UnmarshalJSON(b []byte) error { + var str string + if err := json.Unmarshal(b, &str); err == nil { + if (str == "") { + *i = IntString(0) + } else { + n, err := strconv.Atoi(str) + if err != nil { + return err + } + *i = IntString(n) + } + return nil + } + + var num int + if err := json.Unmarshal(b, &num); err == nil { + *i = IntString(num) + return nil + } + + return fmt.Errorf("invalid postQualySeconds value") +} \ No newline at end of file diff --git a/local/model/server.go b/local/model/server.go index 3225864..96c51ee 100644 --- a/local/model/server.go +++ b/local/model/server.go @@ -14,9 +14,6 @@ type Server struct { Port int `gorm:"not null" json:"-"` ConfigPath string `gorm:"not null" json:"-"` // e.g. "/acc/servers/server1/" ServiceName string `gorm:"not null" json:"-"` // Windows service name - BroadcastIP string `json:"-"` - BroadcastPort int `json:"-"` - BroadcastPassword string `json:"-"` } type PlayerState struct { diff --git a/local/service/config.go b/local/service/config.go index b6f73c0..f9e92d7 100644 --- a/local/service/config.go +++ b/local/service/config.go @@ -127,18 +127,6 @@ func (as ConfigService) UpdateConfig(ctx *fiber.Ctx, body *map[string]interface{ context := ctx.UserContext() - if (configFile == ConfigurationJson) { - config, err := DecodeToMap[model.Configuration](newDataUTF16) - if err != nil { - return nil, err - } - if (server.BroadcastPort != config.UdpPort || server.Port != config.TcpPort) { - server.BroadcastPort = config.UdpPort - server.Port = config.TcpPort - as.serverRepository.UpdateServer(context, server) - } - } - if err := os.WriteFile(configPath, newDataUTF16, 0644); err != nil { return nil, err }