server/libfic/theme_export.go

131 lines
3.8 KiB
Go
Raw Permalink Normal View History

2018-01-09 14:16:16 +00:00
package fic
import (
"fmt"
2018-11-25 02:20:03 +00:00
"path"
"strings"
2018-01-09 14:16:16 +00:00
)
2021-09-06 09:58:03 +00:00
var GlobalScoreCoefficient float64 = 1
2018-03-09 18:07:08 +00:00
// exportedExercice is a structure representing a challenge, as exposed to players.
type exportedExercice struct {
Id int64 `json:"id"`
Title string `json:"title"`
Authors string `json:"authors,omitempty"`
Headline string `json:"headline,omitempty"`
Image string `json:"image,omitempty"`
BackgroundColor string `json:"background_color,omitempty"`
URLId string `json:"urlid"`
Tags []string `json:"tags"`
Gain int64 `json:"gain"`
Coeff float64 `json:"curcoeff"`
Solved int64 `json:"solved"`
Tried int64 `json:"tried"`
2018-01-09 14:16:16 +00:00
}
2018-03-09 18:07:08 +00:00
// exportedTheme is a structure representing a Theme, as exposed to players.
2018-01-09 14:16:16 +00:00
type exportedTheme struct {
Name string `json:"name,omitempty"`
URLId string `json:"urlid"`
Locked bool `json:"locked,omitempty"`
Authors string `json:"authors,omitempty"`
Headline string `json:"headline,omitempty"`
Intro string `json:"intro,omitempty"`
Image string `json:"image,omitempty"`
BackgroundColor string `json:"background_color,omitempty"`
PartnerImage string `json:"partner_img,omitempty"`
PartnerLink string `json:"partner_href,omitempty"`
PartnerText string `json:"partner_txt,omitempty"`
Exercices []exportedExercice `json:"exercices"`
2018-01-09 14:16:16 +00:00
}
2018-03-09 18:07:08 +00:00
// Exportedthemes exports themes from the database, to be displayed to players.
2018-01-09 14:16:16 +00:00
func ExportThemes() (interface{}, error) {
if themes, err := GetThemes(); err != nil {
return nil, err
} else {
2024-03-17 16:07:25 +00:00
// Append standalone exercices fake-themes
themes = append(themes, &Theme{
Name: "Défis indépendants",
URLId: "_",
Path: "exercices",
})
2018-01-09 14:16:16 +00:00
ret := map[string]exportedTheme{}
for _, theme := range themes {
2023-03-20 14:20:20 +00:00
exos := []exportedExercice{}
2018-01-09 14:16:16 +00:00
if exercices, err := theme.GetExercices(); err != nil {
return nil, err
} else if theme.URLId == "_" && len(exercices) == 0 {
// If no standalone exercices, don't append them
continue
2018-01-09 14:16:16 +00:00
} else {
for _, exercice := range exercices {
if exercice.Disabled && theme.Locked {
2023-03-20 14:20:20 +00:00
continue
}
2023-06-14 18:51:48 +00:00
exoimgpath := ""
if len(exercice.Image) > 0 {
exoimgpath = path.Join(FilesDir, exercice.Image)
}
exobackgroundcolor := ""
if exercice.BackgroundColor > 0 || len(exercice.Image) > 0 {
exobackgroundcolor = fmt.Sprintf("#%06X", exercice.BackgroundColor)
}
2023-06-14 18:51:48 +00:00
tags, _ := exercice.GetTags()
exos = append(exos, exportedExercice{
exercice.Id,
2018-01-09 14:16:16 +00:00
exercice.Title,
2023-06-14 18:53:09 +00:00
exercice.Authors,
exercice.Headline,
2023-06-14 18:51:48 +00:00
exoimgpath,
exobackgroundcolor,
2018-01-18 10:07:50 +00:00
exercice.URLId,
tags,
2021-09-06 09:58:03 +00:00
int64(float64(exercice.Gain) * GlobalScoreCoefficient),
2018-01-09 14:16:16 +00:00
exercice.Coefficient,
exercice.SolvedCount(),
exercice.TriedTeamCount(),
})
2018-01-09 14:16:16 +00:00
}
2023-03-20 14:20:20 +00:00
}
2018-11-25 02:20:03 +00:00
2023-03-20 14:20:20 +00:00
imgpath := ""
if len(theme.Image) > 0 {
imgpath = path.Join(FilesDir, theme.Image)
}
2018-11-25 02:20:03 +00:00
thmbackgroundcolor := ""
if theme.BackgroundColor > 0 || len(theme.Image) > 0 {
thmbackgroundcolor = fmt.Sprintf("#%06X", theme.BackgroundColor)
}
2023-03-20 14:20:20 +00:00
partnerImgpath := ""
if len(theme.PartnerImage) > 0 {
partnerImgpath = path.Join(FilesDir, theme.PartnerImage)
}
2021-09-01 08:15:26 +00:00
2023-03-20 14:20:20 +00:00
ret[fmt.Sprintf("%d", theme.Id)] = exportedTheme{
theme.Name,
theme.URLId,
theme.Locked,
theme.Authors,
theme.Headline,
strings.Replace(theme.Intro, "$FILES$", FilesDir, -1),
imgpath,
thmbackgroundcolor,
2023-03-20 14:20:20 +00:00
partnerImgpath,
theme.PartnerLink,
theme.PartnerText,
exos,
2018-01-09 14:16:16 +00:00
}
}
return ret, nil
}
}