server/libfic/theme_export.go

90 lines
2.5 KiB
Go

package fic
import (
"fmt"
"path"
"strings"
)
var GlobalScoreCoefficient float64 = 1
// exportedExercice is a structure representing a challenge, as exposed to players.
type exportedExercice struct {
Title string `json:"title"`
Headline string `json:"headline,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"`
}
// 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"`
Headline string `json:"headline,omitempty"`
Intro string `json:"intro"`
Image string `json:"image,omitempty"`
PartnerImage string `json:"partner_img,omitempty"`
PartnerLink string `json:"partner_href,omitempty"`
PartnerText string `json:"partner_txt,omitempty"`
Exercices map[string]exportedExercice `json:"exercices"`
}
// Exportedthemes exports themes from the database, to be displayed to players.
func ExportThemes() (interface{}, error) {
if themes, err := GetThemes(); err != nil {
return nil, err
} else {
ret := map[string]exportedTheme{}
for _, theme := range themes {
if exercices, err := theme.GetExercices(); err != nil {
return nil, err
} else {
exos := map[string]exportedExercice{}
for _, exercice := range exercices {
tags, _ := exercice.GetTags()
exos[fmt.Sprintf("%d", exercice.Id)] = exportedExercice{
exercice.Title,
exercice.Headline,
exercice.URLId,
tags,
int64(float64(exercice.Gain) * GlobalScoreCoefficient),
exercice.Coefficient,
exercice.SolvedCount(),
exercice.TriedTeamCount(),
}
}
imgpath := ""
if len(theme.Image) > 0 {
imgpath = path.Join(FilesDir, theme.Image)
}
partnerImgpath := ""
if len(theme.PartnerImage) > 0 {
partnerImgpath = path.Join(FilesDir, theme.PartnerImage)
}
ret[fmt.Sprintf("%d", theme.Id)] = exportedTheme{
theme.Name,
theme.URLId,
theme.Authors,
theme.Headline,
strings.Replace(theme.Intro, "$FILES$", FilesDir, -1),
imgpath,
partnerImgpath,
theme.PartnerLink,
theme.PartnerText,
exos,
}
}
}
return ret, nil
}
}