Export themes as array

This commit is contained in:
nemunaire 2018-05-13 14:15:53 +02:00
commit 0ddd6c4899
3 changed files with 83 additions and 70 deletions

View file

@ -2,10 +2,12 @@ package fic
import (
"fmt"
"strings"
)
// exportedExercice is a structure representing a challenge, as exposed to players.
type exportedExercice struct {
// 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"`
@ -16,44 +18,48 @@ type exportedExercice struct {
// 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 map[string]exportedExercice `json:"exercices"`
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() (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 {
exos[fmt.Sprintf("%d", exercice.Id)] = exportedExercice{
exercice.Title,
exercice.URLId,
exercice.Gain,
exercice.Coefficient,
exercice.SolvedCount(),
exercice.TriedTeamCount(),
}
}
ret[fmt.Sprintf("%d", theme.Id)] = exportedTheme{
theme.Name,
theme.URLId,
theme.Authors,
theme.Intro,
exos,
}
}
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
}
return ret, nil
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
}