Partial resolution of exercices

This commit is contained in:
nemunaire 2016-12-04 19:08:46 +01:00
commit 3cd6cd959d
8 changed files with 46 additions and 24 deletions

View file

@ -107,6 +107,17 @@ CREATE TABLE IF NOT EXISTS exercice_keys(
value BINARY(64) NOT NULL,
FOREIGN KEY(id_exercice) REFERENCES exercices(id_exercice)
);
`); err != nil {
return err
}
if _, err := db.Exec(`
CREATE TABLE IF NOT EXISTS key_found(
id_key INTEGER NOT NULL,
id_team INTEGER NOT NULL,
time TIMESTAMP NOT NULL,
FOREIGN KEY(id_key) REFERENCES exercice_keys(id_key),
FOREIGN KEY(id_team) REFERENCES teams(id_team)
);
`); err != nil {
return err
}

View file

@ -5,6 +5,8 @@ import (
"time"
)
var PartialValidation bool
type Exercice struct {
Id int64 `json:"id"`
Title string `json:"title"`
@ -201,13 +203,14 @@ func (e Exercice) CheckResponse(resps map[string]string, t Team) (bool, error) {
valid := true
for _, key := range keys {
if _, ok := resps[key.Type]; !ok {
if res, ok := resps[key.Type]; !ok {
valid = false
break
}
if !key.Check(resps[key.Type]) {
valid = false
break
} else if !key.Check(res) {
if !PartialValidation || t.HasPartiallySolved(key) == nil {
valid = false
}
} else {
key.FoundBy(t)
}
}

View file

@ -2,6 +2,7 @@ package fic
import (
"crypto/sha512"
"time"
)
type Key struct {
@ -88,3 +89,7 @@ func (k Key) Check(val string) bool {
return true
}
func (k Key) FoundBy(t Team) {
DBExec("INSERT INTO key_found (id_key, id_team, time) VALUES (?, ?, ?)", k.Id, t.Id, time.Now())
}

View file

@ -244,6 +244,12 @@ func IsSolved(e Exercice) (int, time.Time) {
}
}
func (t Team) HasPartiallySolved(k Key) (*time.Time) {
var tm *time.Time
DBQueryRow("SELECT MIN(time) FROM key_found WHERE id_team = ? AND id_key = ?", t.Id, k.Id).Scan(&tm)
return tm
}
type statLine struct {
Tip string `json:"tip"`
Total int `json:"total"`

View file

@ -28,6 +28,7 @@ type myTeamExercice struct {
Files []myTeamFile `json:"files"`
Keys []string `json:"keys"`
Solved bool `json:"solved"`
SolvedMat []bool `json:"solved_matrix"`
SolvedTime time.Time `json:"solved_time"`
SolvedNumber int64 `json:"solved_number"`
VideoURI string `json:"video_uri"`
@ -116,6 +117,9 @@ func MyJSONTeam(t *Team, started bool) (interface{}, error) {
exercice.Keys = append(exercice.Keys, fmt.Sprintf("%x", k.Value)+k.Type)
} else {
exercice.Keys = append(exercice.Keys, k.Type)
if PartialValidation {
exercice.SolvedMat = append(exercice.SolvedMat, t.HasPartiallySolved(k) != nil)
}
}
}
}