feat: show random cat picture on results page based on score

This commit is contained in:
Jānis Kacēns
2026-05-11 17:00:53 +03:00
parent 6b486a558a
commit 5f68a80d14
4 changed files with 47 additions and 0 deletions
+38
View File
@@ -7,7 +7,9 @@ import (
"log/slog" "log/slog"
"math/rand" "math/rand"
"net/http" "net/http"
"os"
"strconv" "strconv"
"strings"
"time" "time"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
@@ -18,6 +20,9 @@ import (
"qbank/internal/sampling" "qbank/internal/sampling"
) )
// happyCatThreshold is the minimum score percentage to show a happy cat.
const happyCatThreshold = 60
type TestHandler struct { type TestHandler struct {
auth *auth.Manager auth *auth.Manager
repo *db.Repo repo *db.Repo
@@ -302,11 +307,18 @@ func (h *TestHandler) ResultsGet(w http.ResponseWriter, r *http.Request) {
} }
} }
mood := "sad"
if test.NQuestions > 0 && nCorrect*100/test.NQuestions >= happyCatThreshold {
mood = "happy"
}
catURL := randomCatURL(mood)
data := BaseData(h.auth, r) data := BaseData(h.auth, r)
data["Test"] = test data["Test"] = test
data["Items"] = items data["Items"] = items
data["NCorrect"] = nCorrect data["NCorrect"] = nCorrect
data["TimeTaken"] = timeTaken data["TimeTaken"] = timeTaken
data["CatURL"] = catURL
h.render.Render(w, http.StatusOK, "test_results", data) h.render.Render(w, http.StatusOK, "test_results", data)
} }
@@ -342,6 +354,32 @@ func (h *TestHandler) loadTestAndN(w http.ResponseWriter, r *http.Request) (int,
return n, test, true return n, test, true
} }
// randomCatURL picks a random image from web/static/cats/<mood>/ and returns
// its URL path, or an empty string if the folder is missing or empty.
func randomCatURL(mood string) string {
dir := "web/static/cats/" + mood
entries, err := os.ReadDir(dir)
if err != nil {
return ""
}
var images []string
for _, e := range entries {
if e.IsDir() {
continue
}
n := strings.ToLower(e.Name())
if strings.HasSuffix(n, ".jpg") || strings.HasSuffix(n, ".jpeg") ||
strings.HasSuffix(n, ".png") || strings.HasSuffix(n, ".gif") ||
strings.HasSuffix(n, ".webp") {
images = append(images, e.Name())
}
}
if len(images) == 0 {
return ""
}
return "/static/cats/" + mood + "/" + images[rand.Intn(len(images))]
}
// deterministicShuffle returns a copy of answers shuffled by a seed derived // deterministicShuffle returns a copy of answers shuffled by a seed derived
// from the test ID and question ID, so the order is stable across page reloads. // from the test ID and question ID, so the order is stable across page reloads.
func deterministicShuffle(answers []*models.Answer, testID int64, questionID string) []*models.Answer { func deterministicShuffle(answers []*models.Answer, testID int64, questionID string) []*models.Answer {
View File
View File
+9
View File
@@ -11,6 +11,15 @@
{{end}} {{end}}
</div> </div>
<!-- Cat reaction -->
{{if .CatURL}}
<div class="flex justify-center mb-6">
<img src="{{.CatURL}}"
alt=""
class="rounded-2xl shadow-md max-h-64 w-auto object-cover">
</div>
{{end}}
<!-- Per-question review --> <!-- Per-question review -->
<div class="space-y-5 mb-8"> <div class="space-y-5 mb-8">
{{range $i, $item := .Items}} {{range $i, $item := .Items}}