server/libfic/team_stats.go

107 lines
2.0 KiB
Go
Raw Normal View History

2016-12-27 20:12:17 +00:00
package fic
import (
"fmt"
)
2018-03-09 18:07:08 +00:00
// statLine is a line of statistics for the file stats.json exposed to players.
2016-12-27 20:12:17 +00:00
type statLine struct {
Tip string `json:"tip"`
Total int `json:"total"`
Solved int `json:"solved"`
Tried int `json:"tried"`
Tries int `json:"tries"`
}
2018-03-09 18:07:08 +00:00
// teamStats represents the structure of stats.json.
2016-12-27 20:12:17 +00:00
type teamStats struct {
Levels []statLine `json:"levels"`
Themes map[int64]statLine `json:"themes"`
2016-12-27 20:12:17 +00:00
}
2018-03-09 18:07:08 +00:00
// GetLevel
2016-12-27 20:12:17 +00:00
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]
}
2018-03-09 18:07:08 +00:00
// GetStats generates statistics for the Team.
2016-12-27 20:12:17 +00:00
func (t Team) GetStats() (interface{}, error) {
return GetTeamsStats(&t)
}
2018-03-09 18:07:08 +00:00
// GetTeamsStats returns statistics limited to the given Team.
2016-12-27 20:12:17 +00:00
func GetTeamsStats(t *Team) (interface{}, error) {
stat := teamStats{
[]statLine{},
map[int64]statLine{},
}
2016-12-27 20:12:17 +00:00
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 {
2021-11-22 14:35:07 +00:00
if b := t.HasSolved(exercice); b != nil {
2016-12-27 20:12:17 +00:00
solved += 1
sLvl.Solved += 1
}
} else {
2018-03-09 18:07:08 +00:00
if n, _ := exercice.IsSolved(); n > 0 {
solved += n
2016-12-27 20:12:17 +00:00
sLvl.Solved += 1
}
}
try := NbTry(t, exercice)
if try > 0 {
tried += 1
tries += try
sLvl.Tried += 1
sLvl.Tries += try
}
}
}
stat.Themes[theme.Id] = statLine{
2016-12-27 20:12:17 +00:00
theme.Name,
total,
solved,
tried,
tries,
}
2016-12-27 20:12:17 +00:00
}
return stat, nil
}
}