checker: deep redesign

This commit is contained in:
nemunaire 2021-03-05 15:14:45 +01:00
commit 853477e54a
4 changed files with 132 additions and 38 deletions

View file

@ -95,6 +95,19 @@ CREATE TABLE IF NOT EXISTS student_challenges(
CONSTRAINT token_found UNIQUE (id_student,challenge),
FOREIGN KEY(id_student) REFERENCES students(id_student)
) DEFAULT CHARACTER SET = utf8 COLLATE = utf8_bin;
`); err != nil {
return err
}
if _, err := db.Exec(`
CREATE TABLE IF NOT EXISTS student_challenge_errors(
id_st INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
id_student INTEGER NOT NULL,
challenge INTEGER NOT NULL,
time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
error VARCHAR(255) NOT NULL,
CONSTRAINT token_found UNIQUE (id_student,challenge),
FOREIGN KEY(id_student) REFERENCES students(id_student)
) DEFAULT CHARACTER SET = utf8 COLLATE = utf8_bin;
`); err != nil {
return err
}

View file

@ -3,6 +3,7 @@ package adlin
import (
"crypto/hmac"
"crypto/sha512"
"fmt"
"time"
)
@ -150,6 +151,18 @@ func (s Student) GetStatesByChallenge() (ucs []UnlockedChallenge, err error) {
}
}
func (s Student) UnlockChallenge(challenge int, value string) (uc UnlockedChallenge, err error) {
if uc, err = s.UnlockNewChallenge(challenge, value); err != nil {
if uc, err = s.UpdateUnlockedChallenge(challenge, value); err != nil {
return
}
}
s.RegisterChallengeError(challenge, fmt.Errorf("OK"))
return
}
func (s Student) UnlockNewChallenge(challenge int, value string) (UnlockedChallenge, error) {
if res, err := DBExec("INSERT INTO student_challenges (id_student, challenge, time, value) VALUES (?, ?, ?, ?)", s.Id, challenge, time.Now(), value); err != nil {
return UnlockedChallenge{}, err
@ -168,6 +181,24 @@ func (s Student) UpdateUnlockedChallenge(challenge int, value string) (UnlockedC
}
}
type ErroredChallenge struct {
Id int64 `json:"id,omitempty"`
IdStudent int64 `json:"id_student"`
Challenge int `json:"challenge,omitempty"`
Time time.Time `json:"time"`
Error string `json:"error,omitempty"`
}
func (s Student) RegisterChallengeError(challenge int, err error) error {
if _, errr := DBExec("INSERT INTO student_challenge_errors (id_student, challenge, time, error) VALUES (?, ?, ?, ?)", s.Id, challenge, time.Now(), err.Error()); errr == nil {
return nil
} else if _, errr := DBExec("UPDATE student_challenge_errors SET time = ?, error = ? WHERE id_student = ? AND challenge = ?", time.Now(), err.Error(), s.Id, challenge); errr != nil {
return errr
} else {
return nil
}
}
func (s Student) RegisterAccess(ip, mac string) error {
if res, err := DBExec("INSERT INTO student_login (id_student, ip, mac, time) VALUES (?, ?, ?, ?)", s.Id, ip, mac, time.Now()); err != nil {
return err