Write docs!

This commit is contained in:
nemunaire 2018-03-09 19:07:08 +01:00
commit bcc598ebd5
37 changed files with 478 additions and 188 deletions

View file

@ -5,15 +5,23 @@ import (
"strings"
)
// EHint represents a challenge hint.
type EHint struct {
Id int64 `json:"id"`
// IdExercice is the identifier of the underlying challenge
IdExercice int64 `json:"idExercice"`
// Title is the hint name displayed to players
Title string `json:"title"`
// Content is the actual content of small text hints (mutually exclusive with File field)
// When File is filled, Content contains the hexadecimal file's hash.
Content string `json:"content"`
// File is path, relative to FilesDir where the file hint is stored (mutually exclusive with Content field)
File string `json:"file"`
// Cost is the amount of points the player will loose if it unlocks the hint
Cost int64 `json:"cost"`
}
// treatHintContent reads Content to detect if this is a hint file in order to convert to such hint.
func treatHintContent(h *EHint) {
if strings.HasPrefix(h.Content, "$FILES") {
fpath := strings.TrimPrefix(h.Content, "$FILES")
@ -23,6 +31,7 @@ func treatHintContent(h *EHint) {
}
}
// GetHint retrieves the hint with the given id.
func GetHint(id int64) (EHint, error) {
var h EHint
if err := DBQueryRow("SELECT id_hint, id_exercice, title, content, cost FROM exercice_hints WHERE id_hint = ?", id).Scan(&h.Id, &h.IdExercice, &h.Title, &h.Content, &h.Cost); err != nil {
@ -33,6 +42,7 @@ func GetHint(id int64) (EHint, error) {
return h, nil
}
// GetHints returns a list of hints comming with the challenge.
func (e Exercice) GetHints() ([]EHint, error) {
if rows, err := DBQuery("SELECT id_hint, title, content, cost FROM exercice_hints WHERE id_exercice = ?", e.Id); err != nil {
return nil, err
@ -57,6 +67,7 @@ func (e Exercice) GetHints() ([]EHint, error) {
}
}
// AddHint creates and fills a new struct EHint and registers it into the database.
func (e Exercice) AddHint(title string, content string, cost int64) (EHint, error) {
if res, err := DBExec("INSERT INTO exercice_hints (id_exercice, title, content, cost) VALUES (?, ?, ?, ?)", e.Id, title, content, cost); err != nil {
return EHint{}, err
@ -67,6 +78,7 @@ func (e Exercice) AddHint(title string, content string, cost int64) (EHint, erro
}
}
// Update applies modifications back to the database.
func (h EHint) Update() (int64, error) {
if res, err := DBExec("UPDATE exercice_hints SET id_exercice = ?, title = ?, content = ?, cost = ? WHERE id_hint = ?", h.IdExercice, h.Title, h.Content, h.Cost, h.Id); err != nil {
return 0, err
@ -77,6 +89,7 @@ func (h EHint) Update() (int64, error) {
}
}
// Delete the hint from the database.
func (h EHint) Delete() (int64, error) {
if res, err := DBExec("DELETE FROM exercice_hints WHERE id_hint = ?", h.Id); err != nil {
return 0, err
@ -87,6 +100,7 @@ func (h EHint) Delete() (int64, error) {
}
}
// WipeHints deletes (only in the database, not on disk) hints coming with the challenge.
func (e Exercice) WipeHints() (int64, error) {
if res, err := DBExec("DELETE FROM exercice_hints WHERE id_exercice = ?", e.Id); err != nil {
return 0, err
@ -97,6 +111,7 @@ func (e Exercice) WipeHints() (int64, error) {
}
}
// GetExercice returns the parent Exercice where this hint can be found.
func (h EHint) GetExercice() (Exercice, error) {
var eid int64
if err := DBQueryRow("SELECT id_exercice FROM exercice_hints WHERE id_hint = ?", h.Id).Scan(&eid); err != nil {