alter primary keys to uuids and adjust the membership system

This commit is contained in:
Fran Jurmanović
2025-06-30 22:50:52 +02:00
parent caba5bae70
commit c17e7742ee
53 changed files with 12641 additions and 805 deletions

View File

@@ -6,115 +6,137 @@ import (
"os"
"strconv"
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
type IntString int
type IntBool int
// Config tracks configuration modifications
type Config struct {
ID uint `json:"id" gorm:"primaryKey"`
ServerID uint `json:"serverId" gorm:"not null"`
type Config struct {
ID uuid.UUID `json:"id" gorm:"type:uuid;primary_key;"`
ServerID uuid.UUID `json:"serverId" gorm:"not null;type:uuid"`
ConfigFile string `json:"configFile" gorm:"not null"` // e.g. "settings.json"
OldConfig string `json:"oldConfig" gorm:"type:text"`
NewConfig string `json:"newConfig" gorm:"type:text"`
ChangedAt time.Time `json:"changedAt" gorm:"default:CURRENT_TIMESTAMP"`
}
// BeforeCreate is a GORM hook that runs before creating new config entries
func (c *Config) BeforeCreate(tx *gorm.DB) error {
if c.ID == uuid.Nil {
c.ID = uuid.New()
}
if c.ChangedAt.IsZero() {
c.ChangedAt = time.Now().UTC()
}
return nil
}
type Configurations struct {
Configuration Configuration `json:"configuration"`
AssistRules AssistRules `json:"assistRules"`
Event EventConfig `json:"event"`
EventRules EventRules `json:"eventRules"`
Configuration Configuration `json:"configuration"`
AssistRules AssistRules `json:"assistRules"`
Event EventConfig `json:"event"`
EventRules EventRules `json:"eventRules"`
Settings ServerSettings `json:"settings"`
}
type ServerSettings struct {
ServerName string `json:"serverName"`
AdminPassword string `json:"adminPassword"`
CarGroup string `json:"carGroup"`
TrackMedalsRequirement IntString `json:"trackMedalsRequirement"`
SafetyRatingRequirement IntString `json:"safetyRatingRequirement"`
RacecraftRatingRequirement IntString `json:"racecraftRatingRequirement"`
Password string `json:"password"`
SpectatorPassword string `json:"spectatorPassword"`
MaxCarSlots IntString `json:"maxCarSlots"`
DumpLeaderboards IntString `json:"dumpLeaderboards"`
IsRaceLocked IntString `json:"isRaceLocked"`
RandomizeTrackWhenEmpty IntString `json:"randomizeTrackWhenEmpty"`
CentralEntryListPath string `json:"centralEntryListPath"`
AllowAutoDQ IntString `json:"allowAutoDQ"`
ShortFormationLap IntString `json:"shortFormationLap"`
FormationLapType IntString `json:"formationLapType"`
IgnorePrematureDisconnects IntString `json:"ignorePrematureDisconnects"`
ServerName string `json:"serverName"`
AdminPassword string `json:"adminPassword"`
CarGroup string `json:"carGroup"`
TrackMedalsRequirement IntString `json:"trackMedalsRequirement"`
SafetyRatingRequirement IntString `json:"safetyRatingRequirement"`
RacecraftRatingRequirement IntString `json:"racecraftRatingRequirement"`
Password string `json:"password"`
SpectatorPassword string `json:"spectatorPassword"`
MaxCarSlots IntString `json:"maxCarSlots"`
DumpLeaderboards IntString `json:"dumpLeaderboards"`
IsRaceLocked IntString `json:"isRaceLocked"`
RandomizeTrackWhenEmpty IntString `json:"randomizeTrackWhenEmpty"`
CentralEntryListPath string `json:"centralEntryListPath"`
AllowAutoDQ IntString `json:"allowAutoDQ"`
ShortFormationLap IntString `json:"shortFormationLap"`
FormationLapType IntString `json:"formationLapType"`
IgnorePrematureDisconnects IntString `json:"ignorePrematureDisconnects"`
}
type EventConfig struct {
Track string `json:"track"`
PreRaceWaitingTimeSeconds IntString `json:"preRaceWaitingTimeSeconds"`
SessionOverTimeSeconds IntString `json:"sessionOverTimeSeconds"`
AmbientTemp IntString `json:"ambientTemp"`
CloudLevel float64 `json:"cloudLevel"`
Rain float64 `json:"rain"`
WeatherRandomness IntString `json:"weatherRandomness"`
PostQualySeconds IntString `json:"postQualySeconds"`
PostRaceSeconds IntString `json:"postRaceSeconds"`
SimracerWeatherConditions IntString `json:"simracerWeatherConditions"`
IsFixedConditionQualification IntString `json:"isFixedConditionQualification"`
Track string `json:"track"`
PreRaceWaitingTimeSeconds IntString `json:"preRaceWaitingTimeSeconds"`
SessionOverTimeSeconds IntString `json:"sessionOverTimeSeconds"`
AmbientTemp IntString `json:"ambientTemp"`
CloudLevel float64 `json:"cloudLevel"`
Rain float64 `json:"rain"`
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 IntString `json:"hourOfDay"`
DayOfWeekend IntString `json:"dayOfWeekend"`
TimeMultiplier IntString `json:"timeMultiplier"`
SessionType string `json:"sessionType"`
SessionDurationMinutes IntString `json:"sessionDurationMinutes"`
HourOfDay IntString `json:"hourOfDay"`
DayOfWeekend IntString `json:"dayOfWeekend"`
TimeMultiplier IntString `json:"timeMultiplier"`
SessionType string `json:"sessionType"`
SessionDurationMinutes IntString `json:"sessionDurationMinutes"`
}
type AssistRules struct {
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"`
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 IntString `json:"qualifyStandingType"`
PitWindowLengthSec IntString `json:"pitWindowLengthSec"`
DriverStIntStringTimeSec IntString `json:"driverStIntStringTimeSec"`
MandatoryPitstopCount IntString `json:"mandatoryPitstopCount"`
MaxTotalDrivingTime IntString `json:"maxTotalDrivingTime"`
IsRefuellingAllowedInRace IntBool `json:"isRefuellingAllowedInRace"`
IsRefuellingTimeFixed IntBool `json:"isRefuellingTimeFixed"`
IsMandatoryPitstopRefuellingRequired IntBool `json:"isMandatoryPitstopRefuellingRequired"`
IsMandatoryPitstopTyreChangeRequired IntBool `json:"isMandatoryPitstopTyreChangeRequired"`
IsMandatoryPitstopSwapDriverRequired IntBool `json:"isMandatoryPitstopSwapDriverRequired"`
TyreSetCount IntString `json:"tyreSetCount"`
QualifyStandingType IntString `json:"qualifyStandingType"`
PitWindowLengthSec IntString `json:"pitWindowLengthSec"`
DriverStIntStringTimeSec IntString `json:"driverStIntStringTimeSec"`
MandatoryPitstopCount IntString `json:"mandatoryPitstopCount"`
MaxTotalDrivingTime IntString `json:"maxTotalDrivingTime"`
IsRefuellingAllowedInRace IntBool `json:"isRefuellingAllowedInRace"`
IsRefuellingTimeFixed IntBool `json:"isRefuellingTimeFixed"`
IsMandatoryPitstopRefuellingRequired IntBool `json:"isMandatoryPitstopRefuellingRequired"`
IsMandatoryPitstopTyreChangeRequired IntBool `json:"isMandatoryPitstopTyreChangeRequired"`
IsMandatoryPitstopSwapDriverRequired IntBool `json:"isMandatoryPitstopSwapDriverRequired"`
TyreSetCount IntString `json:"tyreSetCount"`
}
type Configuration struct {
UdpPort IntString `json:"udpPort"`
TcpPort IntString `json:"tcpPort"`
MaxConnections IntString `json:"maxConnections"`
LanDiscovery IntString `json:"lanDiscovery"`
RegisterToLobby IntString `json:"registerToLobby"`
ConfigVersion IntString `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"`
}
type SystemConfig struct {
ID uint `json:"id"`
Key string `json:"key"`
Value string `json:"value"`
DefaultValue string `json:"defaultValue"`
Description string `json:"description"`
DateModified string `json:"dateModified"`
ID uuid.UUID `json:"id" gorm:"type:uuid;primary_key;"`
Key string `json:"key"`
Value string `json:"value"`
DefaultValue string `json:"defaultValue"`
Description string `json:"description"`
DateModified string `json:"dateModified"`
}
// BeforeCreate is a GORM hook that runs before creating new system config entries
func (sc *SystemConfig) BeforeCreate(tx *gorm.DB) error {
if sc.ID == uuid.Nil {
sc.ID = uuid.New()
}
return nil
}
// Known configuration keys
@@ -125,7 +147,7 @@ const (
// Cache keys
const (
CacheKeySystemConfig = "system_config_%s" // Format with config key
CacheKeySystemConfig = "system_config_%s" // Format with config key
)
func (i *IntBool) UnmarshalJSON(b []byte) error {
@@ -159,7 +181,7 @@ func (i IntBool) ToBool() bool {
func (i *IntString) UnmarshalJSON(b []byte) error {
var str string
if err := json.Unmarshal(b, &str); err == nil {
if (str == "") {
if str == "" {
*i = IntString(0)
} else {
n, err := strconv.Atoi(str)
@@ -184,7 +206,7 @@ func (i IntString) ToString() string {
return strconv.Itoa(int(i))
}
func (i IntString) ToInt() (int) {
func (i IntString) ToInt() int {
return int(i)
}
@@ -203,7 +225,7 @@ func (c *SystemConfig) Validate() error {
// Use default value if value is empty
c.Value = c.DefaultValue
}
// Check if path exists
if _, err := os.Stat(c.Value); os.IsNotExist(err) {
return fmt.Errorf("path does not exist: %s", c.Value)
@@ -218,4 +240,4 @@ func (c *SystemConfig) GetEffectiveValue() string {
return c.Value
}
return c.DefaultValue
}
}