server/libfic/team_my.go

204 lines
5.5 KiB
Go
Raw Normal View History

2016-01-23 11:20:30 +00:00
package fic
import (
"encoding/hex"
"fmt"
2017-01-16 12:09:31 +00:00
"log"
2016-01-23 11:20:30 +00:00
"time"
"path"
2016-01-23 11:20:30 +00:00
)
type myTeamFile struct {
Path string `json:"path"`
Name string `json:"name"`
Checksum string `json:"checksum"`
Size int64 `json:"size"`
}
2016-12-04 18:15:39 +00:00
type myTeamHint struct {
HintId int64 `json:"id"`
Title string `json:"title"`
2018-01-06 15:06:51 +00:00
Content string `json:"content,omitempty"`
File string `json:"file,omitempty"`
Cost int64 `json:"cost"`
2016-12-04 18:15:39 +00:00
}
2017-12-16 01:12:44 +00:00
type myTeamMCQ struct {
Title string `json:"title"`
Justify bool `json:"justify,omitempty"`
2017-12-16 01:12:44 +00:00
Choices map[int64]string `json:"choices,omitempty"`
Solved *time.Time `json:"solved,omitempty"`
}
2016-01-23 11:20:30 +00:00
type myTeamExercice struct {
2016-01-25 02:04:11 +00:00
ThemeId int `json:"theme_id"`
2016-01-23 11:20:30 +00:00
Statement string `json:"statement"`
2017-12-16 01:12:44 +00:00
Hints []myTeamHint `json:"hints,omitempty"`
Gain int `json:"gain"`
2017-12-16 01:12:44 +00:00
Files []myTeamFile `json:"files,omitempty"`
2018-09-24 08:00:17 +00:00
Flags []string `json:"keys,omitempty"`
2017-12-16 01:12:44 +00:00
SolvedMat []bool `json:"solved_matrix,omitempty"`
MCQs []myTeamMCQ `json:"mcqs,omitempty"`
2017-12-17 01:48:02 +00:00
SolveDist int64 `json:"solve_dist,omitempty"`
2017-12-16 01:12:44 +00:00
SolvedTime time.Time `json:"solved_time,omitempty"`
SolvedRank int64 `json:"solved_rank,omitempty"`
Tries int64 `json:"tries,omitempty"`
VideoURI string `json:"video_uri,omitempty"`
Issue string `json:"issue,omitempty"`
IssueKind string `json:"issuekind,omitempty"`
2016-01-23 11:20:30 +00:00
}
type myTeam struct {
Id int64 `json:"team_id"`
2016-01-24 13:23:04 +00:00
Name string `json:"name"`
2016-01-23 18:53:17 +00:00
Points int64 `json:"score"`
Members []Member `json:"members"`
2016-01-23 11:20:30 +00:00
Exercices map[string]myTeamExercice `json:"exercices"`
}
func MyJSONTeam(t *Team, started bool) (interface{}, error) {
ret := myTeam{}
2016-12-04 18:15:39 +00:00
// Fill information about the team
2016-01-23 11:20:30 +00:00
if t == nil {
ret.Id = 0
} else {
2016-01-24 13:23:04 +00:00
ret.Name = t.Name
2016-01-23 11:20:30 +00:00
ret.Id = t.Id
2017-01-12 10:52:51 +00:00
points, _ := t.GetPoints()
ret.Points = int64(points)
if members, err := t.GetMembers(); err == nil {
ret.Members = members
}
2016-01-23 11:20:30 +00:00
}
2016-12-04 18:15:39 +00:00
// Fill exercices, only if the challenge is started
ret.Exercices = map[string]myTeamExercice{}
2016-01-23 11:20:30 +00:00
if exos, err := GetExercices(); err != nil {
return ret, err
} else if started {
for _, e := range exos {
if t == nil || t.HasAccess(e) {
exercice := myTeamExercice{}
2016-01-25 02:04:11 +00:00
if tid, err := e.GetThemeId(); err == nil {
exercice.ThemeId = tid
}
2016-01-23 11:20:30 +00:00
exercice.Statement = e.Statement
if len(e.Issue) > 0 {
exercice.Issue = e.Issue
exercice.IssueKind = e.IssueKind
}
2016-01-23 11:20:30 +00:00
if t == nil {
if e.Overview != "" {
exercice.Statement = e.Overview
}
2016-02-26 00:30:10 +00:00
exercice.VideoURI = e.VideoURI
2017-01-16 12:09:31 +00:00
exercice.SolvedRank = 1
exercice.Tries = e.TriedCount()
exercice.Gain = int(float64(e.Gain) * e.Coefficient)
2016-01-23 11:20:30 +00:00
} else {
2017-01-16 12:09:31 +00:00
var solved bool
2018-03-09 18:07:08 +00:00
solved, exercice.SolvedTime = t.HasSolved(e)
exercice.SolvedRank, _ = t.GetSolvedRank(e)
2017-01-16 12:09:31 +00:00
if solved {
exercice.Tries, _ = t.CountTries(e)
} else {
exercice.Tries, exercice.SolvedTime = t.CountTries(e)
2017-12-17 01:48:02 +00:00
if exercice.Tries > 0 {
exercice.SolveDist = t.LastTryDist(e)
}
2017-01-16 12:09:31 +00:00
}
if gain, err := e.EstimateGain(*t, solved); err == nil {
exercice.Gain = int(gain)
2017-01-16 12:09:31 +00:00
} else {
log.Println("ERROR during gain estimation:", err)
}
2016-01-23 11:20:30 +00:00
}
2016-12-04 18:15:39 +00:00
// Expose exercice files
2016-01-23 11:20:30 +00:00
2016-12-04 18:15:39 +00:00
exercice.Files = []myTeamFile{}
if files, err := e.GetFiles(); err != nil {
2016-01-23 11:20:30 +00:00
return nil, err
} else {
2016-12-04 18:15:39 +00:00
for _, f := range files {
if t == nil || t.CanDownload(f) {
exercice.Files = append(exercice.Files, myTeamFile{path.Join(FilesDir, f.Path), f.Name, hex.EncodeToString(f.Checksum), f.Size})
}
2016-12-04 18:15:39 +00:00
}
}
// Expose exercice hints
exercice.Hints = []myTeamHint{}
if hints, err := e.GetHints(); err != nil {
return nil, err
} else {
for _, h := range hints {
if t == nil || t.HasHint(h) {
2018-01-06 15:06:51 +00:00
exercice.Hints = append(exercice.Hints, myTeamHint{h.Id, h.Title, h.Content, h.File, h.Cost})
2016-01-23 11:20:30 +00:00
} else {
2018-01-06 15:06:51 +00:00
exercice.Hints = append(exercice.Hints, myTeamHint{h.Id, h.Title, "", "", h.Cost})
2016-01-23 11:20:30 +00:00
}
}
}
// Expose exercice flags
2017-12-16 01:12:44 +00:00
var justifiedMCQ map[string]bool
exercice.Flags = []string{}
2017-12-16 01:12:44 +00:00
if flags, err := e.GetFlags(); err != nil {
2017-12-16 01:12:44 +00:00
return nil, err
} else {
for _, k := range flags {
if k.Label[0] == '%' {
justifiedMCQ[k.Label[1:]] = true
} else if t == nil {
exercice.Flags = append(exercice.Flags, fmt.Sprintf("%x", k.Checksum)+k.Label)
2017-12-17 01:48:02 +00:00
} else {
exercice.Flags = append(exercice.Flags, k.Label)
if PartialValidation {
exercice.SolvedMat = append(exercice.SolvedMat, t.HasPartiallySolved(k) != nil)
}
2017-12-17 01:48:02 +00:00
}
2017-12-16 01:12:44 +00:00
}
}
// Expose exercice MCQs
2016-01-23 11:20:30 +00:00
exercice.MCQs = []myTeamMCQ{}
2016-12-04 18:15:39 +00:00
if mcqs, err := e.GetMCQ(); err != nil {
2016-01-23 11:20:30 +00:00
return nil, err
} else {
for _, mcq := range mcqs {
choices := map[int64]string{}
justified := false
for _, e := range mcq.Entries {
choices[e.Id] = e.Label
if _, ok := justifiedMCQ[m.Title + "%" + e.Label]; ok {
justified = true
}
}
2016-12-04 18:15:39 +00:00
if t == nil {
exercice.MCQs = append(exercice.MCQs, myTeamMCQ{mcq.Title, justified, choices, nil})
2016-12-04 18:15:39 +00:00
} else {
exercice.MCQs = append(exercice.MCQs, myTeamMCQ{mcq.Title, justified, choices, t.HasPartiallyRespond(mcq)})
2016-12-04 18:15:39 +00:00
}
2016-01-23 11:20:30 +00:00
}
}
2016-12-04 18:15:39 +00:00
// Hash table ordered by exercice Id
2016-01-23 11:20:30 +00:00
ret.Exercices[fmt.Sprintf("%d", e.Id)] = exercice
}
}
}
return ret, nil
}