Able to check submission

This commit is contained in:
nemunaire 2016-01-15 12:57:35 +01:00
parent a9f35d224b
commit e3f7cce80a
4 changed files with 51 additions and 3 deletions

View File

@ -37,7 +37,13 @@ func treatSubmission(pathname string, team_id string, exercice_id string) {
cnt += scanner.Text()
}
log.Println(id, team, exercice, cnt)
if solved, err := exercice.CheckResponse(cnt, team); err != nil {
log.Println(id, "[ERR]", err)
} else if solved {
exercice.Solved(team)
log.Printf("%s Team %d SOLVED exercice %d\n", id, team.Id, exercice.Id)
} else {
log.Printf("%s Team %d submit an invalid solution for exercice %d\n", id, team.Id, exercice.Id)
}
}
}

View File

@ -1,6 +1,7 @@
package fic
import (
"errors"
"time"
)
@ -109,3 +110,27 @@ func (e Exercice) Solved(t Team) error {
return nil
}
}
func (e Exercice) CheckResponse(response string, t Team) (bool, error) {
if t.HasSolved(e) {
return true, nil
}
if err := e.NewTry(t); err != nil {
return false, err
} else if keys, err := e.GetKeys(); err != nil {
return false, err
} else {
if len(keys) < 1 {
return true, errors.New("Exercice with no key registered")
}
for _, k := range keys {
if !k.Check(response) {
return false, nil
}
}
return true, nil
}
}

View File

@ -34,8 +34,12 @@ func (e Exercice) GetKeys() ([]Key, error) {
}
}
func getHashedKey(raw_value string) [64]byte {
return sha512.Sum512([]byte(raw_value))
}
func (e Exercice) AddRawKey(name string, raw_value string) (Key, error) {
return e.AddKey(name, sha512.Sum512([]byte(raw_value)))
return e.AddKey(name, getHashedKey(raw_value))
}
func (e Exercice) AddKey(name string, value [64]byte) (Key, error) {
@ -67,3 +71,7 @@ func (k Key) Delete() (int64, error) {
return nb, err
}
}
func (k Key) Check(val string) bool {
return k.Value == getHashedKey(val)
}

View File

@ -67,3 +67,12 @@ func (t Team) Delete() (int64, error) {
return nb, err
}
}
func (t Team) HasSolved(e Exercice) bool {
var nb int
if err := DBQueryRow("SELECT COUNT(id_exercice) FROM exercice_solved WHERE id_team = ? AND id_exercice = ?", t.Id, e.Id).Scan(&nb); err != nil {
return false
} else {
return nb > 0
}
}