From 045c91c4a0ae21f9f0ad71625e607a36a4d17895 Mon Sep 17 00:00:00 2001 From: prokopparuzek Date: Thu, 9 Oct 2025 16:19:44 +0200 Subject: [PATCH] admin: tymy, pridani a mazani --- admin.go | 60 +++++++++++++++++++++++++++++++++++++- create.sql | 3 +- insertTestData.sql | 8 ++--- klice.go | 8 ++--- templates.go | 10 +++++++ templates/adminLevels.html | 2 ++ templates/adminRoutes.html | 2 ++ templates/adminTeams.html | 25 ++++++++++++++-- 8 files changed, 105 insertions(+), 13 deletions(-) diff --git a/admin.go b/admin.go index 0084fc4..771190f 100644 --- a/admin.go +++ b/admin.go @@ -89,6 +89,40 @@ func adminTeamsHandler(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "/admin/login", http.StatusSeeOther) return } + if r.Method == http.MethodPost { + if err := r.ParseForm(); err != nil { + http.Error(w, "Error parsing form", http.StatusBadRequest) + return + } + // Deleting an existing team + if r.PostForm.Has("delete") { + teamName := r.FormValue("delete") + _, err := db.Exec("DELETE FROM teams WHERE name = ?", teamName) + if err != nil { + http.Error(w, "Database error", http.StatusInternalServerError) + return + } + http.Redirect(w, r, "/admin/teams", http.StatusSeeOther) + return + } + // Adding a new team + teamName := r.FormValue("name") + difficulty := r.FormValue("difficulty") + password := r.FormValue("password") + if teamName == "" || difficulty == "" || password == "" { + http.Error(w, "All fields are required", http.StatusBadRequest) + return + } + _, err := db.Exec("INSERT INTO teams (name, difficulty_level, password, last_cipher, penalty) VALUES (?, ?, ?, 1, 0)", teamName, difficulty, hashPassword(password)) + if err != nil { + http.Error(w, "Database error", http.StatusInternalServerError) + return + } + http.Redirect(w, r, "/admin/teams", http.StatusSeeOther) + return + } + // Fetch all teams with their difficulty levels + // Teams rows, err := db.Query("SELECT name, difficulty_levels.level_name, last_cipher, penalty FROM teams JOIN difficulty_levels ON teams.difficulty_level = difficulty_levels.id ORDER BY teams.difficulty_level, teams.name") if err != nil { http.Error(w, "Database error", http.StatusInternalServerError) @@ -108,7 +142,31 @@ func adminTeamsHandler(w http.ResponseWriter, r *http.Request) { http.Error(w, "Database error", http.StatusInternalServerError) return } - if err := AdminTeamsTemplate.Execute(w, teams); err != nil { + // Difficulty levels for the dropdown + rows, err = db.Query("SELECT id, level_name FROM difficulty_levels ORDER BY id") + if err != nil { + http.Error(w, "Database error", http.StatusInternalServerError) + return + } + defer rows.Close() + var difficultyLevels []DifficultyLevelS + for rows.Next() { + var level DifficultyLevelS + if err := rows.Scan(&level.ID, &level.Name); err != nil { + http.Error(w, "Database error", http.StatusInternalServerError) + return + } + difficultyLevels = append(difficultyLevels, level) + } + if err := rows.Err(); err != nil { + http.Error(w, "Database error", http.StatusInternalServerError) + return + } + teamsData := TeamsTemplateS{ + Teams: teams, + Difficulties: difficultyLevels, + } + if err := AdminTeamsTemplate.Execute(w, teamsData); err != nil { http.Error(w, "Template error", http.StatusInternalServerError) return } diff --git a/create.sql b/create.sql index 89220cd..b730177 100644 --- a/create.sql +++ b/create.sql @@ -10,10 +10,9 @@ DROP TABLE IF EXISTS PENALTIES; CREATE TABLE TEAMS ( id INTEGER PRIMARY KEY, name VARCHAR(100) NOT NULL, - city VARCHAR(100) NOT NULL, difficulty_level INTEGER NOT NULL, password VARCHAR(255) NOT NULL, - last_cipher INTEGER DEFAULT 0, + last_cipher INTEGER DEFAULT 0, -- index of cipher which team is solving or searching now penalty INTEGER DEFAULT 0, FOREIGN KEY (difficulty_level) REFERENCES DIFFICULTY_LEVELS(id) ); diff --git a/insertTestData.sql b/insertTestData.sql index f80290f..5980336 100644 --- a/insertTestData.sql +++ b/insertTestData.sql @@ -5,10 +5,10 @@ INSERT INTO DIFFICULTY_LEVELS (id, level_name) VALUES (3, 'Těžká'); -- Vložení týmů: heslo1, heslo2, heslo3 -INSERT INTO TEAMS (id, name, city, difficulty_level, password, last_cipher, penalty) VALUES - (1, 'Rychlé šípy', 'Praha', 1, '4bc2ef0648cdf275032c83bb1e87dd554d47f4be293670042212c8a01cc2ccbe', 0, 0), - (2, 'Vlčí smečka', 'Brno', 2, '274efeaa827a33d7e35be9a82cd6150b7caf98f379a4252aa1afce45664dcbe1', 0, 10), - (3, 'Orli', 'Ostrava', 3, '05af533c6614544a704c4cf51a45be5c10ff19bd10b7aa1dfe47efc0fd059ede', 1, 5); +INSERT INTO TEAMS (id, name, difficulty_level, password, last_cipher, penalty) VALUES + (1, 'Rychlé šípy', 1, '4bc2ef0648cdf275032c83bb1e87dd554d47f4be293670042212c8a01cc2ccbe', 1, 0), + (2, 'Vlčí smečka', 2, '274efeaa827a33d7e35be9a82cd6150b7caf98f379a4252aa1afce45664dcbe1', 1, 10), + (3, 'Orli', 3, '05af533c6614544a704c4cf51a45be5c10ff19bd10b7aa1dfe47efc0fd059ede', 1, 5); -- Vložení pozic INSERT INTO POSITIONS (id, gps, clue) VALUES diff --git a/klice.go b/klice.go index e3fb75b..fa7c72a 100644 --- a/klice.go +++ b/klice.go @@ -181,13 +181,13 @@ func qrHandler(w http.ResponseWriter, r *http.Request) { return } // check if the task is available for the team - // if order > last_cipher + 1, task is not yet available - // if order == last_cipher + 1, task is now available, update last_cipher + // if order > last_cipher, task is not yet available + // if order == last_cipher, task is now available // if order <= last_cipher, task has been already visited, allow viewing - if order > last_cipher+1 { + if order > last_cipher { http.Error(w, "This task is not yet available", http.StatusForbidden) return - } else if order == last_cipher+1 { + } else if order == last_cipher { last_cipher = order _, err = db.Exec("UPDATE teams SET last_cipher = ? WHERE id = ?", order, teamID) if err != nil { diff --git a/templates.go b/templates.go index a375ed9..cb0b393 100644 --- a/templates.go +++ b/templates.go @@ -23,6 +23,16 @@ type TeamTemplateS struct { Penalties int } +type DifficultyLevelS struct { + ID int + Name string +} + +type TeamsTemplateS struct { + Teams []TeamTemplateS + Difficulties []DifficultyLevelS +} + type AdminRoutesTemplateS struct { Name string Ciphers []CipherTemplateS diff --git a/templates/adminLevels.html b/templates/adminLevels.html index 98452f4..556af58 100644 --- a/templates/adminLevels.html +++ b/templates/adminLevels.html @@ -31,6 +31,8 @@ Jméno: +
+ Zpět na admin panel \ No newline at end of file diff --git a/templates/adminRoutes.html b/templates/adminRoutes.html index 7d8912e..a157984 100644 --- a/templates/adminRoutes.html +++ b/templates/adminRoutes.html @@ -34,6 +34,8 @@
{{end}} +
+ Zpět na admin panel \ No newline at end of file diff --git a/templates/adminTeams.html b/templates/adminTeams.html index 1fb2b04..d8963b5 100644 --- a/templates/adminTeams.html +++ b/templates/adminTeams.html @@ -3,7 +3,7 @@ - Admin Panel + Týmy @@ -14,13 +14,21 @@ Obtížnost Poslední šifra Penalizace (minuty) + Smazat tým + - {{range .}} + {{range .Teams}} {{.TeamName}} {{.Difficulty}} {{.LastCipher}} {{.Penalties}} + +
+ + +
+ {{end}} @@ -28,6 +36,19 @@

Vynulování penalizací a posledních šifer.

Start Závodu
+

Přidat tým

+
+ Název týmu: + Obtížnost: + + Heslo: + +
+
Zpět na admin panel