Able to sync and export standalone exercices

This commit is contained in:
nemunaire 2024-03-15 17:46:50 +01:00
commit adb0e36dd4
15 changed files with 159 additions and 31 deletions

View file

@ -91,17 +91,44 @@ func GetExercice(id int64) (*Exercice, error) {
// GetExercice retrieves the challenge with the given id.
func (t *Theme) GetExercice(id int) (*Exercice, error) {
return getExercice("exercices", "WHERE id_theme = ? AND id_exercice = ?", t.Id, id)
query := "WHERE id_exercice = ? AND id_theme = ?"
args := []interface{}{id}
if t.GetId() == nil {
query = "WHERE id_exercice = ? AND id_theme IS NULL"
} else {
args = append(args, t.GetId())
}
return getExercice("exercices", query, args...)
}
// GetExerciceByTitle retrieves the challenge with the given title.
func (t *Theme) GetExerciceByTitle(title string) (*Exercice, error) {
return getExercice("exercices", "WHERE id_theme = ? AND title = ?", t.Id, title)
query := "WHERE title = ? AND id_theme = ?"
args := []interface{}{title}
if t.GetId() == nil {
query = "WHERE title = ? AND id_theme IS NULL"
} else {
args = append(args, t.GetId())
}
return getExercice("exercices", query, args...)
}
// GetExerciceByPath retrieves the challenge with the given path.
func (t *Theme) GetExerciceByPath(epath string) (*Exercice, error) {
return getExercice("exercices", "WHERE id_theme = ? AND path = ?", t.Id, epath)
query := "WHERE path = ? AND id_theme = ?"
args := []interface{}{epath}
if t.GetId() == nil {
query = "WHERE path = ? AND id_theme IS NULL"
} else {
args = append(args, t.GetId())
}
return getExercice("exercices", query, args...)
}
// GetDiscountedExercice retrieves the challenge with the given id.
@ -153,7 +180,15 @@ func GetDiscountedExercices() ([]*Exercice, error) {
// GetExercices returns the list of all challenges in the Theme.
func (t *Theme) GetExercices() ([]*Exercice, error) {
if rows, err := DBQuery("SELECT id_exercice, id_theme, title, authors, image, disabled, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, resolution, seealso, finished FROM exercices WHERE id_theme = ? ORDER BY path ASC", t.Id); err != nil {
query := "SELECT id_exercice, id_theme, title, authors, image, disabled, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, resolution, seealso, finished FROM exercices WHERE id_theme IS NULL ORDER BY path ASC"
args := []interface{}{}
if t.GetId() != nil {
query = "SELECT id_exercice, id_theme, title, authors, image, disabled, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, resolution, seealso, finished FROM exercices WHERE id_theme = ? ORDER BY path ASC"
args = append(args, t.GetId())
}
if rows, err := DBQuery(query, args...); err != nil {
return nil, err
} else {
defer rows.Close()
@ -224,7 +259,7 @@ func (t *Theme) addExercice(e *Exercice) (err error) {
if e.WIP {
wip = "%"
}
if res, err := DBExec("INSERT INTO exercices (id_theme, title, authors, image, disabled, url_id, path, statement, overview, finished, headline, issue, depend, gain, video_uri, resolution, seealso, issue_kind, coefficient_cur) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, "+ik+", "+cc+")", t.Id, wip+e.Title, e.Authors, e.Image, e.Disabled, e.URLId, e.Path, e.Statement, e.Overview, e.Finished, e.Headline, e.Issue, e.Depend, e.Gain, e.VideoURI, e.Resolution, e.SeeAlso); err != nil {
if res, err := DBExec("INSERT INTO exercices (id_theme, title, authors, image, disabled, url_id, path, statement, overview, finished, headline, issue, depend, gain, video_uri, resolution, seealso, issue_kind, coefficient_cur) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, "+ik+", "+cc+")", t.GetId(), wip+e.Title, e.Authors, e.Image, e.Disabled, e.URLId, e.Path, e.Statement, e.Overview, e.Finished, e.Headline, e.Issue, e.Depend, e.Gain, e.VideoURI, e.Resolution, e.SeeAlso); err != nil {
return err
} else if eid, err := res.LastInsertId(); err != nil {
return err

View file

@ -140,7 +140,9 @@ func MyJSONTeam(t *Team, started bool) (interface{}, error) {
exercice := myTeamExercice{}
exercice.Disabled = e.Disabled
exercice.WIP = e.WIP
exercice.ThemeId = *e.IdTheme
if e.IdTheme != nil {
exercice.ThemeId = *e.IdTheme
}
exercice.Statement = strings.Replace(e.Statement, "$FILES$", FilesDir, -1)

View file

@ -19,6 +19,14 @@ type Theme struct {
PartnerText string `json:"partner_txt,omitempty"`
}
func (t *Theme) GetId() *int64 {
if t.Id == 0 {
return nil
}
return &t.Id
}
// CmpTheme returns true if given Themes are identicals.
func CmpTheme(t1 *Theme, t2 *Theme) bool {
return t1 != nil && t2 != nil && !(t1.Name != t2.Name || t1.URLId != t2.URLId || t1.Path != t2.Path || t1.Authors != t2.Authors || t1.Intro != t2.Intro || t1.Headline != t2.Headline || t1.Image != t2.Image || t1.PartnerImage != t2.PartnerImage || t1.PartnerLink != t2.PartnerLink || t1.PartnerText != t2.PartnerText)

View file

@ -25,12 +25,12 @@ type exportedExercice struct {
// exportedTheme is a structure representing a Theme, as exposed to players.
type exportedTheme struct {
Name string `json:"name"`
Name string `json:"name,omitempty"`
URLId string `json:"urlid"`
Locked bool `json:"locked,omitempty"`
Authors string `json:"authors"`
Authors string `json:"authors,omitempty"`
Headline string `json:"headline,omitempty"`
Intro string `json:"intro"`
Intro string `json:"intro,omitempty"`
Image string `json:"image,omitempty"`
PartnerImage string `json:"partner_img,omitempty"`
PartnerLink string `json:"partner_href,omitempty"`
@ -43,12 +43,17 @@ func ExportThemes() (interface{}, error) {
if themes, err := GetThemes(); err != nil {
return nil, err
} else {
themes = append(themes, &Theme{URLId: "_", Path: "exercices"})
ret := map[string]exportedTheme{}
for _, theme := range themes {
exos := []exportedExercice{}
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
} else {
for _, exercice := range exercices {
if exercice.Disabled && theme.Locked {