Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| cf7473b373 | |||
| 35c53cc54c |
@@ -22,6 +22,9 @@ func NewLeaderboardController(ls *service.LeaderboardService, routeGroups *commo
|
|||||||
errorHandler: error_handler.NewControllerErrorHandler(),
|
errorHandler: error_handler.NewControllerErrorHandler(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
apiServerRoutes := routeGroups.Api.Group("/server/:id")
|
||||||
|
apiServerRoutes.Get("/leaderboard", lc.Get)
|
||||||
|
|
||||||
routeGroups.Leaderboard.Get("/", lc.Get)
|
routeGroups.Leaderboard.Get("/", lc.Get)
|
||||||
routeGroups.Leaderboard.Put("/", auth.Authenticate, lc.Update)
|
routeGroups.Leaderboard.Put("/", auth.Authenticate, lc.Update)
|
||||||
|
|
||||||
@@ -52,7 +55,7 @@ func (lc *LeaderboardController) Update(c *fiber.Ctx) error {
|
|||||||
return lc.errorHandler.HandleUUIDError(c, "server ID")
|
return lc.errorHandler.HandleUUIDError(c, "server ID")
|
||||||
}
|
}
|
||||||
|
|
||||||
var input model.LeaderboardInput
|
var input model.Leaderboard
|
||||||
if err := c.BodyParser(&input); err != nil {
|
if err := c.BodyParser(&input); err != nil {
|
||||||
return lc.errorHandler.HandleParsingError(c, err)
|
return lc.errorHandler.HandleParsingError(c, err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,36 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Score is a custom type that accepts both numbers and strings in JSON.
|
||||||
|
type Score string
|
||||||
|
|
||||||
|
func (s *Score) UnmarshalJSON(data []byte) error {
|
||||||
|
var n json.Number
|
||||||
|
if err := json.Unmarshal(data, &n); err == nil {
|
||||||
|
*s = Score(n.String())
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var str string
|
||||||
|
if err := json.Unmarshal(data, &str); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
*s = Score(str)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s Score) MarshalJSON() ([]byte, error) {
|
||||||
|
if n, err := strconv.Atoi(string(s)); err == nil {
|
||||||
|
return json.Marshal(n)
|
||||||
|
}
|
||||||
|
return json.Marshal(string(s))
|
||||||
|
}
|
||||||
|
|
||||||
type Leaderboard struct {
|
type Leaderboard struct {
|
||||||
ID uuid.UUID `gorm:"type:uuid;primary_key;" json:"id"`
|
ID uuid.UUID `gorm:"type:uuid;primary_key;" json:"id"`
|
||||||
ServerID uuid.UUID `gorm:"uniqueIndex;not null;type:uuid" json:"serverId"`
|
ServerID uuid.UUID `gorm:"uniqueIndex;not null;type:uuid" json:"serverId"`
|
||||||
@@ -12,38 +38,38 @@ type Leaderboard struct {
|
|||||||
FLColor string `gorm:"default:'#8b5cf6'" json:"flColor"`
|
FLColor string `gorm:"default:'#8b5cf6'" json:"flColor"`
|
||||||
FLTextColor string `gorm:"default:'#000000'" json:"flTextColor"`
|
FLTextColor string `gorm:"default:'#000000'" json:"flTextColor"`
|
||||||
Drivers []LeaderboardDriver `gorm:"foreignKey:LeaderboardID;constraint:OnDelete:CASCADE" json:"drivers"`
|
Drivers []LeaderboardDriver `gorm:"foreignKey:LeaderboardID;constraint:OnDelete:CASCADE" json:"drivers"`
|
||||||
Races []LeaderboardRace `gorm:"foreignKey:LeaderboardID;constraint:OnDelete:CASCADE" json:"races"`
|
Races []LeaderboardRace `gorm:"foreignKey:LeaderboardID;constraint:OnDelete:CASCADE" json:"tracks"`
|
||||||
PointRows []LeaderboardPointRow `gorm:"foreignKey:LeaderboardID;constraint:OnDelete:CASCADE" json:"pointRows"`
|
PointRows []LeaderboardPointRow `gorm:"foreignKey:LeaderboardID;constraint:OnDelete:CASCADE" json:"pointsTable"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type LeaderboardDriver struct {
|
type LeaderboardDriver struct {
|
||||||
ID uuid.UUID `gorm:"type:uuid;primary_key;" json:"id"`
|
ID uuid.UUID `gorm:"type:uuid;primary_key;" json:"id"`
|
||||||
LeaderboardID uuid.UUID `gorm:"not null;type:uuid" json:"leaderboardId"`
|
LeaderboardID uuid.UUID `gorm:"not null;type:uuid" json:"-"`
|
||||||
Name string `gorm:"not null" json:"name"`
|
Name string `gorm:"not null" json:"name"`
|
||||||
Initials string `json:"initials"`
|
Initials string `json:"initials"`
|
||||||
Color string `json:"color"`
|
Color string `json:"color"`
|
||||||
Position int `json:"position"`
|
Position int `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type LeaderboardRace struct {
|
type LeaderboardRace struct {
|
||||||
ID uuid.UUID `gorm:"type:uuid;primary_key;" json:"id"`
|
ID uuid.UUID `gorm:"type:uuid;primary_key;" json:"id"`
|
||||||
LeaderboardID uuid.UUID `gorm:"not null;type:uuid" json:"leaderboardId"`
|
LeaderboardID uuid.UUID `gorm:"not null;type:uuid" json:"-"`
|
||||||
Name string `gorm:"not null" json:"name"`
|
Name string `gorm:"not null" json:"name"`
|
||||||
Position int `json:"position"`
|
Position int `json:"-"`
|
||||||
FastestLapInitials string `json:"fastestLapInitials"`
|
FastestLapDriverID *uuid.UUID `gorm:"type:uuid" json:"fastestLapDriverId"`
|
||||||
Results []LeaderboardResult `gorm:"foreignKey:RaceID;constraint:OnDelete:CASCADE" json:"results"`
|
Results []LeaderboardResult `gorm:"foreignKey:RaceID;constraint:OnDelete:CASCADE" json:"results"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type LeaderboardResult struct {
|
type LeaderboardResult struct {
|
||||||
ID uuid.UUID `gorm:"type:uuid;primary_key;" json:"id"`
|
ID uuid.UUID `gorm:"type:uuid;primary_key;" json:"-"`
|
||||||
RaceID uuid.UUID `gorm:"not null;type:uuid" json:"raceId"`
|
RaceID uuid.UUID `gorm:"not null;type:uuid" json:"-"`
|
||||||
DriverID uuid.UUID `gorm:"not null;type:uuid" json:"driverId"`
|
DriverID uuid.UUID `gorm:"not null;type:uuid" json:"driverId"`
|
||||||
Score string `json:"score"`
|
Score Score `json:"score"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type LeaderboardPointRow struct {
|
type LeaderboardPointRow struct {
|
||||||
ID uuid.UUID `gorm:"type:uuid;primary_key;" json:"id"`
|
ID uuid.UUID `gorm:"type:uuid;primary_key;" json:"id"`
|
||||||
LeaderboardID uuid.UUID `gorm:"not null;type:uuid" json:"leaderboardId"`
|
LeaderboardID uuid.UUID `gorm:"not null;type:uuid" json:"-"`
|
||||||
Label string `json:"label"`
|
Label string `json:"label"`
|
||||||
Points int `json:"points"`
|
Points int `json:"points"`
|
||||||
Color string `json:"color"`
|
Color string `json:"color"`
|
||||||
@@ -51,42 +77,6 @@ type LeaderboardPointRow struct {
|
|||||||
Priority int `json:"priority"`
|
Priority int `json:"priority"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// LeaderboardInput is the wire format matching the HTML's appData shape.
|
|
||||||
type LeaderboardInput struct {
|
|
||||||
Drivers []LeaderboardDriverInput `json:"drivers"`
|
|
||||||
PointsTable []LeaderboardPointRowInput `json:"pointsTable"`
|
|
||||||
FLPoints LeaderboardFLInput `json:"flPoints"`
|
|
||||||
Tracks []LeaderboardTrackInput `json:"tracks"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type LeaderboardDriverInput struct {
|
|
||||||
Name string `json:"name"`
|
|
||||||
Color string `json:"color"`
|
|
||||||
Initials string `json:"initials"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type LeaderboardPointRowInput struct {
|
|
||||||
Points int `json:"points"`
|
|
||||||
Label string `json:"label"`
|
|
||||||
Color string `json:"color"`
|
|
||||||
TextColor string `json:"textColor"`
|
|
||||||
Priority int `json:"priority"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type LeaderboardFLInput struct {
|
|
||||||
Points int `json:"points"`
|
|
||||||
Label string `json:"label"`
|
|
||||||
Color string `json:"color"`
|
|
||||||
TextColor string `json:"textColor"`
|
|
||||||
Priority int `json:"priority"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type LeaderboardTrackInput struct {
|
|
||||||
Name string `json:"name"`
|
|
||||||
Results []interface{} `json:"results"`
|
|
||||||
FastestLapInitials string `json:"fastestLapInitials"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *Leaderboard) BeforeCreate(tx *gorm.DB) error {
|
func (l *Leaderboard) BeforeCreate(tx *gorm.DB) error {
|
||||||
if l.ID == uuid.Nil {
|
if l.ID == uuid.Nil {
|
||||||
l.ID = uuid.New()
|
l.ID = uuid.New()
|
||||||
|
|||||||
@@ -92,7 +92,6 @@ func (r *LeaderboardRepository) FullReplace(ctx context.Context, serverID uuid.U
|
|||||||
// Re-insert children with correct leaderboard ID
|
// Re-insert children with correct leaderboard ID
|
||||||
for i := range lb.Drivers {
|
for i := range lb.Drivers {
|
||||||
lb.Drivers[i].LeaderboardID = existing.ID
|
lb.Drivers[i].LeaderboardID = existing.ID
|
||||||
lb.Drivers[i].ID = uuid.Nil // force new ID
|
|
||||||
}
|
}
|
||||||
if len(lb.Drivers) > 0 {
|
if len(lb.Drivers) > 0 {
|
||||||
if err := tx.Create(&lb.Drivers).Error; err != nil {
|
if err := tx.Create(&lb.Drivers).Error; err != nil {
|
||||||
@@ -112,7 +111,6 @@ func (r *LeaderboardRepository) FullReplace(ctx context.Context, serverID uuid.U
|
|||||||
|
|
||||||
for i := range lb.Races {
|
for i := range lb.Races {
|
||||||
lb.Races[i].LeaderboardID = existing.ID
|
lb.Races[i].LeaderboardID = existing.ID
|
||||||
lb.Races[i].ID = uuid.Nil
|
|
||||||
}
|
}
|
||||||
if len(lb.Races) > 0 {
|
if len(lb.Races) > 0 {
|
||||||
// Create races first (without results) to get IDs
|
// Create races first (without results) to get IDs
|
||||||
|
|||||||
@@ -17,189 +17,21 @@ func NewLeaderboardService(repo *repository.LeaderboardRepository) *LeaderboardS
|
|||||||
return &LeaderboardService{repo: repo}
|
return &LeaderboardService{repo: repo}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get returns the leaderboard for a server in the wire format (LeaderboardInput).
|
// Get returns the leaderboard for a server.
|
||||||
func (s *LeaderboardService) Get(ctx context.Context, serverID uuid.UUID) (*model.LeaderboardInput, error) {
|
func (s *LeaderboardService) Get(ctx context.Context, serverID uuid.UUID) (*model.Leaderboard, error) {
|
||||||
lb, err := s.repo.GetOrCreateByServerID(ctx, serverID)
|
return s.repo.GetOrCreateByServerID(ctx, serverID)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return toInput(lb), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update replaces the leaderboard for a server from wire format input.
|
// Update replaces the leaderboard for a server.
|
||||||
func (s *LeaderboardService) Update(ctx context.Context, serverID uuid.UUID, input *model.LeaderboardInput) (*model.LeaderboardInput, error) {
|
func (s *LeaderboardService) Update(ctx context.Context, serverID uuid.UUID, lb *model.Leaderboard) (*model.Leaderboard, error) {
|
||||||
lb, err := fromInput(serverID, input)
|
if lb == nil {
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
result, err := s.repo.FullReplace(ctx, serverID, lb)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return toInput(result), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// toInput converts the normalized DB model to the flat wire format.
|
|
||||||
func toInput(lb *model.Leaderboard) *model.LeaderboardInput {
|
|
||||||
// Build driver map (id -> index in slice) for result ordering
|
|
||||||
driverIndexByID := make(map[uuid.UUID]int, len(lb.Drivers))
|
|
||||||
|
|
||||||
// Sort drivers by position
|
|
||||||
drivers := make([]model.LeaderboardDriverInput, len(lb.Drivers))
|
|
||||||
for i, d := range lb.Drivers {
|
|
||||||
drivers[i] = model.LeaderboardDriverInput{
|
|
||||||
Name: d.Name,
|
|
||||||
Color: d.Color,
|
|
||||||
Initials: d.Initials,
|
|
||||||
}
|
|
||||||
driverIndexByID[d.ID] = d.Position
|
|
||||||
}
|
|
||||||
// Re-index by position order
|
|
||||||
driverByPos := make(map[int]model.LeaderboardDriver, len(lb.Drivers))
|
|
||||||
for _, d := range lb.Drivers {
|
|
||||||
driverByPos[d.Position] = d
|
|
||||||
}
|
|
||||||
driverCount := len(lb.Drivers)
|
|
||||||
orderedDrivers := make([]model.LeaderboardDriver, driverCount)
|
|
||||||
for i := 0; i < driverCount; i++ {
|
|
||||||
if d, ok := driverByPos[i]; ok {
|
|
||||||
orderedDrivers[i] = d
|
|
||||||
drivers[i] = model.LeaderboardDriverInput{
|
|
||||||
Name: d.Name,
|
|
||||||
Color: d.Color,
|
|
||||||
Initials: d.Initials,
|
|
||||||
}
|
|
||||||
driverIndexByID[d.ID] = i
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build tracks
|
|
||||||
raceByPos := make(map[int]model.LeaderboardRace, len(lb.Races))
|
|
||||||
for _, r := range lb.Races {
|
|
||||||
raceByPos[r.Position] = r
|
|
||||||
}
|
|
||||||
tracks := make([]model.LeaderboardTrackInput, len(lb.Races))
|
|
||||||
for i := 0; i < len(lb.Races); i++ {
|
|
||||||
race, ok := raceByPos[i]
|
|
||||||
if !ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
results := make([]interface{}, driverCount)
|
|
||||||
for j := range results {
|
|
||||||
results[j] = "0"
|
|
||||||
}
|
|
||||||
for _, res := range race.Results {
|
|
||||||
idx, found := driverIndexByID[res.DriverID]
|
|
||||||
if found && idx < driverCount {
|
|
||||||
results[idx] = res.Score
|
|
||||||
}
|
|
||||||
}
|
|
||||||
tracks[i] = model.LeaderboardTrackInput{
|
|
||||||
Name: race.Name,
|
|
||||||
Results: results,
|
|
||||||
FastestLapInitials: race.FastestLapInitials,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build points table
|
|
||||||
pointsTable := make([]model.LeaderboardPointRowInput, len(lb.PointRows))
|
|
||||||
for i, p := range lb.PointRows {
|
|
||||||
pointsTable[i] = model.LeaderboardPointRowInput{
|
|
||||||
Points: p.Points,
|
|
||||||
Label: p.Label,
|
|
||||||
Color: p.Color,
|
|
||||||
TextColor: p.TextColor,
|
|
||||||
Priority: p.Priority,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
flColor := lb.FLColor
|
|
||||||
if flColor == "" {
|
|
||||||
flColor = "#8b5cf6"
|
|
||||||
}
|
|
||||||
flTextColor := lb.FLTextColor
|
|
||||||
if flTextColor == "" {
|
|
||||||
flTextColor = "#000000"
|
|
||||||
}
|
|
||||||
|
|
||||||
return &model.LeaderboardInput{
|
|
||||||
Drivers: drivers,
|
|
||||||
PointsTable: pointsTable,
|
|
||||||
FLPoints: model.LeaderboardFLInput{
|
|
||||||
Points: lb.FLPoints,
|
|
||||||
Label: "FL +1",
|
|
||||||
Color: flColor,
|
|
||||||
TextColor: flTextColor,
|
|
||||||
Priority: len(pointsTable) + 1,
|
|
||||||
},
|
|
||||||
Tracks: tracks,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// fromInput converts the wire format to the normalized DB model.
|
|
||||||
func fromInput(serverID uuid.UUID, input *model.LeaderboardInput) (*model.Leaderboard, error) {
|
|
||||||
if input == nil {
|
|
||||||
return nil, fmt.Errorf("input is required")
|
return nil, fmt.Errorf("input is required")
|
||||||
}
|
}
|
||||||
|
for i := range lb.Drivers {
|
||||||
lb := &model.Leaderboard{
|
lb.Drivers[i].Position = i
|
||||||
ServerID: serverID,
|
|
||||||
FLPoints: input.FLPoints.Points,
|
|
||||||
FLColor: input.FLPoints.Color,
|
|
||||||
FLTextColor: input.FLPoints.TextColor,
|
|
||||||
}
|
}
|
||||||
|
for i := range lb.Races {
|
||||||
// Build drivers
|
lb.Races[i].Position = i
|
||||||
drivers := make([]model.LeaderboardDriver, len(input.Drivers))
|
|
||||||
driverIDs := make([]uuid.UUID, len(input.Drivers))
|
|
||||||
for i, d := range input.Drivers {
|
|
||||||
id := uuid.New()
|
|
||||||
driverIDs[i] = id
|
|
||||||
drivers[i] = model.LeaderboardDriver{
|
|
||||||
ID: id,
|
|
||||||
Name: d.Name,
|
|
||||||
Initials: d.Initials,
|
|
||||||
Color: d.Color,
|
|
||||||
Position: i,
|
|
||||||
}
|
}
|
||||||
}
|
return s.repo.FullReplace(ctx, serverID, lb)
|
||||||
lb.Drivers = drivers
|
|
||||||
|
|
||||||
// Build point rows
|
|
||||||
pointRows := make([]model.LeaderboardPointRow, len(input.PointsTable))
|
|
||||||
for i, p := range input.PointsTable {
|
|
||||||
pointRows[i] = model.LeaderboardPointRow{
|
|
||||||
Label: p.Label,
|
|
||||||
Points: p.Points,
|
|
||||||
Color: p.Color,
|
|
||||||
TextColor: p.TextColor,
|
|
||||||
Priority: p.Priority,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
lb.PointRows = pointRows
|
|
||||||
|
|
||||||
// Build races
|
|
||||||
races := make([]model.LeaderboardRace, len(input.Tracks))
|
|
||||||
for i, t := range input.Tracks {
|
|
||||||
results := make([]model.LeaderboardResult, 0, len(t.Results))
|
|
||||||
for j, score := range t.Results {
|
|
||||||
if j >= len(driverIDs) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
scoreStr := fmt.Sprintf("%v", score)
|
|
||||||
results = append(results, model.LeaderboardResult{
|
|
||||||
DriverID: driverIDs[j],
|
|
||||||
Score: scoreStr,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
races[i] = model.LeaderboardRace{
|
|
||||||
Name: t.Name,
|
|
||||||
Position: i,
|
|
||||||
FastestLapInitials: t.FastestLapInitials,
|
|
||||||
Results: results,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
lb.Races = races
|
|
||||||
|
|
||||||
return lb, nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user