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"
|
2016-11-19 14:54:32 +00:00
|
|
|
"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 {
|
2017-01-14 14:03:25 +00:00
|
|
|
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"`
|
2017-01-14 14:03:25 +00:00
|
|
|
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"`
|
|
|
|
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"`
|
2017-01-16 12:09:31 +00:00
|
|
|
Gain float64 `json:"gain"`
|
2017-12-16 01:12:44 +00:00
|
|
|
Files []myTeamFile `json:"files,omitempty"`
|
|
|
|
Keys []string `json:"keys,omitempty"`
|
|
|
|
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"`
|
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"`
|
2016-01-24 10:42:26 +00:00
|
|
|
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)
|
2016-01-24 10:42:26 +00:00
|
|
|
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 t == nil {
|
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 = 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
|
|
|
|
solved, exercice.SolvedTime, exercice.SolvedRank = t.HasSolved(e)
|
|
|
|
|
|
|
|
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 = gain
|
|
|
|
} 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 {
|
|
|
|
exercice.Files = append(exercice.Files, myTeamFile{path.Join(FilesDir, f.Path), f.Name, hex.EncodeToString(f.Checksum), f.Size})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-16 01:12:44 +00:00
|
|
|
// Expose exercice MCQs
|
|
|
|
|
|
|
|
exercice.MCQs = []myTeamMCQ{}
|
|
|
|
|
|
|
|
if mcqs, err := e.GetMCQ(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
for _, mcq := range mcqs {
|
|
|
|
choices := map[int64]string{}
|
|
|
|
for _, e := range mcq.Entries {
|
|
|
|
choices[e.Id] = e.Label
|
|
|
|
}
|
2017-12-17 01:48:02 +00:00
|
|
|
if t == nil {
|
2017-12-17 15:28:31 +00:00
|
|
|
exercice.MCQs = append(exercice.MCQs, myTeamMCQ{mcq.Title, choices, nil})
|
2017-12-17 01:48:02 +00:00
|
|
|
} else {
|
2017-12-17 15:28:31 +00:00
|
|
|
exercice.MCQs = append(exercice.MCQs, myTeamMCQ{mcq.Title, choices, t.HasPartiallyRespond(mcq)})
|
2017-12-17 01:48:02 +00:00
|
|
|
}
|
2017-12-16 01:12:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-04 18:15:39 +00:00
|
|
|
// Expose exercice keys
|
2016-01-23 11:20:30 +00:00
|
|
|
|
2016-12-04 18:15:39 +00:00
|
|
|
exercice.Keys = []string{}
|
|
|
|
|
|
|
|
if keys, err := e.GetKeys(); err != nil {
|
2016-01-23 11:20:30 +00:00
|
|
|
return nil, err
|
|
|
|
} else {
|
2016-12-04 18:15:39 +00:00
|
|
|
for _, k := range keys {
|
|
|
|
if t == nil {
|
2017-12-12 04:06:34 +00:00
|
|
|
exercice.Keys = append(exercice.Keys, fmt.Sprintf("%x", k.Checksum)+k.Label)
|
2016-12-04 18:15:39 +00:00
|
|
|
} else {
|
2017-12-12 04:06:34 +00:00
|
|
|
exercice.Keys = append(exercice.Keys, k.Label)
|
2016-12-04 18:08:46 +00:00
|
|
|
if PartialValidation {
|
|
|
|
exercice.SolvedMat = append(exercice.SolvedMat, t.HasPartiallySolved(k) != nil)
|
|
|
|
}
|
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
|
|
|
|
}
|