Split team.go into multiple files
This commit is contained in:
parent
ddc9a515f6
commit
2dae44e9ec
4 changed files with 249 additions and 222 deletions
98
libfic/team_stats.go
Normal file
98
libfic/team_stats.go
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
package fic
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type statLine struct {
|
||||
Tip string `json:"tip"`
|
||||
Total int `json:"total"`
|
||||
Solved int `json:"solved"`
|
||||
Tried int `json:"tried"`
|
||||
Tries int `json:"tries"`
|
||||
}
|
||||
|
||||
type teamStats struct {
|
||||
Levels []statLine `json:"levels"`
|
||||
Themes []statLine `json:"themes"`
|
||||
}
|
||||
|
||||
func (s *teamStats) GetLevel(level int) *statLine {
|
||||
level -= 1
|
||||
|
||||
for len(s.Levels) <= level {
|
||||
s.Levels = append(s.Levels, statLine{
|
||||
fmt.Sprintf("Level %d", (len(s.Levels) + 1)),
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
})
|
||||
}
|
||||
|
||||
return &s.Levels[level]
|
||||
}
|
||||
|
||||
func (t Team) GetStats() (interface{}, error) {
|
||||
return GetTeamsStats(&t)
|
||||
}
|
||||
|
||||
func GetTeamsStats(t *Team) (interface{}, error) {
|
||||
stat := teamStats{}
|
||||
|
||||
if themes, err := GetThemes(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
for _, theme := range themes {
|
||||
total := 0
|
||||
solved := 0
|
||||
tried := 0
|
||||
tries := 0
|
||||
|
||||
if exercices, err := theme.GetExercices(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
for _, exercice := range exercices {
|
||||
var lvl int
|
||||
if lvl, err = exercice.GetLevel(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sLvl := stat.GetLevel(lvl)
|
||||
|
||||
total += 1
|
||||
sLvl.Total += 1
|
||||
|
||||
if t != nil {
|
||||
if b, _, _ := t.HasSolved(exercice); b {
|
||||
solved += 1
|
||||
sLvl.Solved += 1
|
||||
}
|
||||
} else {
|
||||
if n, _ := IsSolved(exercice); n > 0 {
|
||||
solved += 1
|
||||
sLvl.Solved += 1
|
||||
}
|
||||
}
|
||||
|
||||
try := NbTry(t, exercice)
|
||||
if try > 0 {
|
||||
tried += 1
|
||||
tries += try
|
||||
sLvl.Tried += 1
|
||||
sLvl.Tries += try
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stat.Themes = append(stat.Themes, statLine{
|
||||
theme.Name,
|
||||
total,
|
||||
solved,
|
||||
tried,
|
||||
tries,
|
||||
})
|
||||
}
|
||||
|
||||
return stat, nil
|
||||
}
|
||||
}
|
||||
Reference in a new issue