server/libfic/theme_export.go

66 lines
1.5 KiB
Go

package fic
import (
"fmt"
"strings"
)
// ExportedExercice is a structure representing a challenge, as exposed to players.
type ExportedExercice struct {
Id int64 `json:"id"`
Title string `json:"title"`
URLId string `json:"urlid"`
Gain int64 `json:"gain"`
Coeff float64 `json:"curcoeff"`
Solved int64 `json:"solved"`
Tried int64 `json:"tried"`
}
// exportedTheme is a structure representing a Theme, as exposed to players.
type exportedTheme struct {
Name string `json:"name"`
URLId string `json:"urlid"`
Authors string `json:"authors"`
Intro string `json:"intro"`
Exercices []ExportedExercice `json:"exercices"`
}
// Exportedthemes exports themes from the database, to be displayed to players.
func ExportThemes() (ret map[string]exportedTheme, err error) {
var themes []Theme
if themes, err = GetThemes(); err != nil {
return
}
ret = map[string]exportedTheme{}
for _, theme := range themes {
var exercices []Exercice
if exercices, err = theme.GetExercices(); err != nil {
return
}
var exos []ExportedExercice
for _, exercice := range exercices {
exos = append(exos, ExportedExercice{
exercice.Id,
exercice.Title,
exercice.URLId,
exercice.Gain,
exercice.Coefficient,
exercice.SolvedCount(),
exercice.TriedTeamCount(),
})
}
ret[fmt.Sprintf("%d", theme.Id)] = exportedTheme{
theme.Name[strings.Index(theme.Name, "-")+1:],
theme.URLId,
theme.Authors,
theme.Intro,
exos,
}
}
return
}