From 381aefa5977284f246a162e74ace7496732add52 Mon Sep 17 00:00:00 2001 From: nemunaire Date: Mon, 18 Jan 2016 19:40:11 +0100 Subject: [PATCH] HasSolved now returns a tuple (bool, Time, int) --- libfic/exercice.go | 3 ++- libfic/key.go | 8 ++++---- libfic/team.go | 20 +++++++++++++------- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/libfic/exercice.go b/libfic/exercice.go index 75d52f2f..53b51f85 100644 --- a/libfic/exercice.go +++ b/libfic/exercice.go @@ -134,7 +134,8 @@ func (e Exercice) Solved(t Team) error { } func (e Exercice) CheckResponse(response string, t Team) (bool, error) { - if t.HasSolved(e) { + s, _, _ := t.HasSolved(e) + if s { return true, nil } diff --git a/libfic/key.go b/libfic/key.go index ac78f5cd..a1a32d39 100644 --- a/libfic/key.go +++ b/libfic/key.go @@ -5,10 +5,10 @@ import ( ) type Key struct { - Id int64 `json:"id"` - IdExercice int64 `json:"idExercice"` - Type string `json:"type"` - Value []byte `json:"value"` + Id int64 `json:"id"` + IdExercice int64 `json:"idExercice"` + Type string `json:"type"` + Value []byte `json:"value"` } func (e Exercice) GetKeys() ([]Key, error) { diff --git a/libfic/team.go b/libfic/team.go index b0707cc4..e9f742f1 100644 --- a/libfic/team.go +++ b/libfic/team.go @@ -1,6 +1,8 @@ package fic -import () +import ( + "time" +) type Team struct { Id int64 `json:"id"` @@ -74,15 +76,19 @@ func (t Team) HasAccess(e Exercice) bool { } else { ed := Exercice{} ed.Id = *e.Depend - return t.HasSolved(ed) + s, _, _ := t.HasSolved(ed) + return s } } -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 +func (t Team) HasSolved(e Exercice) (bool, time.Time, int64) { + var nb int64 + var tm time.Time + if err := DBQueryRow("SELECT time FROM exercice_solved WHERE id_team = ? AND id_exercice = ?", t.Id, e.Id).Scan(&tm); err != nil { + return false, time.Now(), 0 + } else if err := DBQueryRow("SELECT COUNT(id_exercice) FROM exercice_solved WHERE id_exercice = ? AND time < ?", e.Id, tm).Scan(&nb); err != nil { + return true, tm, 0 } else { - return nb > 0 + return true, tm, nb } }