From 05cbcc924d9386b04181073b8087afa3ffb88be3 Mon Sep 17 00:00:00 2001 From: nemunaire Date: Tue, 12 Dec 2017 05:06:34 +0100 Subject: [PATCH] libfic: Type key is now Label --- admin/api/exercice.go | 51 ++++++++++++++++---------------- admin/static/views/exercice.html | 4 +-- libfic/exercice.go | 2 +- libfic/key.go | 6 ++-- libfic/team_my.go | 4 +-- 5 files changed, 34 insertions(+), 33 deletions(-) diff --git a/admin/api/exercice.go b/admin/api/exercice.go index 69324d71..16ea7fc6 100644 --- a/admin/api/exercice.go +++ b/admin/api/exercice.go @@ -113,23 +113,6 @@ func createExercice(theme fic.Theme, body []byte) (interface{}, error) { return theme.AddExercice(ue.Title, ue.Path, ue.Statement, depend, ue.Gain, ue.VideoURI) } -type uploadedKey struct { - Type string - Key string -} - -func createExerciceKey(exercice fic.Exercice, body []byte) (interface{}, error) { - var uk uploadedKey - if err := json.Unmarshal(body, &uk); err != nil { - return nil, err - } - - if len(uk.Key) == 0 { - return nil, errors.New("Key not filled") - } - - return exercice.AddRawKey(uk.Type, uk.Key) -} type uploadedHint struct { Title string @@ -185,28 +168,46 @@ func deleteExerciceHint(hint fic.EHint, _ []byte) (interface{}, error) { } +type uploadedKey struct { + Label string + Key string + Hash []byte +} + +func createExerciceKey(exercice fic.Exercice, body []byte) (interface{}, error) { + var uk uploadedKey + if err := json.Unmarshal(body, &uk); err != nil { + return nil, err + } + + if len(uk.Key) == 0 { + return nil, errors.New("Key not filled") + } + + return exercice.AddRawKey(uk.Label, uk.Key) +} + func showExerciceKey(key fic.Key, _ fic.Exercice, body []byte) (interface{}, error) { return key, nil } func updateExerciceKey(key fic.Key, exercice fic.Exercice, body []byte) (interface{}, error) { - var uk fic.Key + var uk uploadedKey if err := json.Unmarshal(body, &uk); err != nil { return nil, err } - uk.Id = key.Id - uk.IdExercice = exercice.Id - - if len(uk.Type) == 0 { - uk.Type = "Flag" + if len(uk.Label) == 0 { + key.Label = "Flag" + } else { + key.Label = uk.Label } - if _, err := uk.Update(); err != nil { + if _, err := key.Update(); err != nil { return nil, err } - return uk, nil + return key, nil } func deleteExerciceKey(key fic.Key, _ fic.Exercice, _ []byte) (interface{}, error) { diff --git a/admin/static/views/exercice.html b/admin/static/views/exercice.html index 9197bbd2..e2c86e98 100644 --- a/admin/static/views/exercice.html +++ b/admin/static/views/exercice.html @@ -84,9 +84,9 @@
- +
- +
diff --git a/libfic/exercice.go b/libfic/exercice.go index 9a73b3a1..aa2fbc12 100644 --- a/libfic/exercice.go +++ b/libfic/exercice.go @@ -214,7 +214,7 @@ func (e Exercice) CheckResponse(resps map[string]string, t Team) (bool, error) { valid := true for _, key := range keys { - if res, ok := resps[key.Type]; !ok { + if res, ok := resps[key.Label]; !ok { valid = false } else if !key.Check(res) { if !PartialValidation || t.HasPartiallySolved(key) == nil { diff --git a/libfic/key.go b/libfic/key.go index 8fccfdbf..50a190c3 100644 --- a/libfic/key.go +++ b/libfic/key.go @@ -9,7 +9,7 @@ import ( type Key struct { Id int64 `json:"id"` IdExercice int64 `json:"idExercice"` - Type string `json:"type"` + Label string `json:"type"` Checksum []byte `json:"value"` } @@ -24,7 +24,7 @@ func (e Exercice) GetKeys() ([]Key, error) { var k Key k.IdExercice = e.Id - if err := rows.Scan(&k.Id, &k.IdExercice, &k.Type, &k.Checksum); err != nil { + if err := rows.Scan(&k.Id, &k.IdExercice, &k.Label, &k.Checksum); err != nil { return nil, err } keys = append(keys, k) @@ -58,7 +58,7 @@ func (e Exercice) AddKey(name string, checksum []byte) (Key, error) { } func (k Key) Update() (int64, error) { - if res, err := DBExec("UPDATE exercice_keys SET id_exercice = ?, type = ?, cksum = ? WHERE id_key = ?", k.IdExercice, k.Type, k.Checksum, k.Id); err != nil { + if res, err := DBExec("UPDATE exercice_keys SET id_exercice = ?, type = ?, cksum = ? WHERE id_key = ?", k.IdExercice, k.Label, k.Checksum, k.Id); err != nil { return 0, err } else if nb, err := res.RowsAffected(); err != nil { return 0, err diff --git a/libfic/team_my.go b/libfic/team_my.go index 8a21b753..406c8416 100644 --- a/libfic/team_my.go +++ b/libfic/team_my.go @@ -130,9 +130,9 @@ func MyJSONTeam(t *Team, started bool) (interface{}, error) { } else { for _, k := range keys { if t == nil { - exercice.Keys = append(exercice.Keys, fmt.Sprintf("%x", k.Checksum)+k.Type) + exercice.Keys = append(exercice.Keys, fmt.Sprintf("%x", k.Checksum)+k.Label) } else { - exercice.Keys = append(exercice.Keys, k.Type) + exercice.Keys = append(exercice.Keys, k.Label) if PartialValidation { exercice.SolvedMat = append(exercice.SolvedMat, t.HasPartiallySolved(k) != nil) }