Compare commits

...

2 Commits

Author SHA1 Message Date
045c91c4a0 admin: tymy, pridani a mazani 2025-10-09 16:19:44 +02:00
f6075c5eb6 admin: urovne obtiznosti 2025-10-09 15:48:59 +02:00
9 changed files with 204 additions and 13 deletions

120
admin.go
View File

@@ -89,6 +89,40 @@ func adminTeamsHandler(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/admin/login", http.StatusSeeOther) http.Redirect(w, r, "/admin/login", http.StatusSeeOther)
return 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") 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 { if err != nil {
http.Error(w, "Database error", http.StatusInternalServerError) 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) http.Error(w, "Database error", http.StatusInternalServerError)
return 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) http.Error(w, "Template error", http.StatusInternalServerError)
return return
} }
@@ -183,3 +241,63 @@ func AdminRouteHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
} }
func AdminLevelHandler(w http.ResponseWriter, r *http.Request) {
if !isAdmin(r) {
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 difficulty level
if r.PostForm.Has("delete") {
levelName := r.FormValue("delete")
_, err := db.Exec("DELETE FROM difficulty_levels WHERE level_name = ?", levelName)
if err != nil {
http.Error(w, "Database error", http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/admin/levels", http.StatusSeeOther)
return
}
// Adding a new difficulty level
levelName := r.FormValue("name")
if levelName == "" {
http.Error(w, "Level name cannot be empty", http.StatusBadRequest)
return
}
_, err := db.Exec("INSERT INTO difficulty_levels (level_name) VALUES (?)", levelName)
if err != nil {
http.Error(w, "Database error", http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/admin/levels", http.StatusSeeOther)
return
}
rows, err := db.Query("SELECT level_name FROM difficulty_levels ORDER BY id")
if err != nil {
http.Error(w, "Database error", http.StatusInternalServerError)
return
}
defer rows.Close()
var difficultyLevels []string
for rows.Next() {
var level string
if err := rows.Scan(&level); 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
}
if err := AdminLevelTemplate.Execute(w, difficultyLevels); err != nil {
http.Error(w, "Template error", http.StatusInternalServerError)
return
}
}

View File

@@ -10,10 +10,9 @@ DROP TABLE IF EXISTS PENALTIES;
CREATE TABLE TEAMS ( CREATE TABLE TEAMS (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL, name VARCHAR(100) NOT NULL,
city VARCHAR(100) NOT NULL,
difficulty_level INTEGER NOT NULL, difficulty_level INTEGER NOT NULL,
password VARCHAR(255) 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, penalty INTEGER DEFAULT 0,
FOREIGN KEY (difficulty_level) REFERENCES DIFFICULTY_LEVELS(id) FOREIGN KEY (difficulty_level) REFERENCES DIFFICULTY_LEVELS(id)
); );

View File

@@ -5,10 +5,10 @@ INSERT INTO DIFFICULTY_LEVELS (id, level_name) VALUES
(3, 'Těžká'); (3, 'Těžká');
-- Vložení týmů: heslo1, heslo2, heslo3 -- Vložení týmů: heslo1, heslo2, heslo3
INSERT INTO TEAMS (id, name, city, difficulty_level, password, last_cipher, penalty) VALUES INSERT INTO TEAMS (id, name, difficulty_level, password, last_cipher, penalty) VALUES
(1, 'Rychlé šípy', 'Praha', 1, '4bc2ef0648cdf275032c83bb1e87dd554d47f4be293670042212c8a01cc2ccbe', 0, 0), (1, 'Rychlé šípy', 1, '4bc2ef0648cdf275032c83bb1e87dd554d47f4be293670042212c8a01cc2ccbe', 1, 0),
(2, 'Vlčí smečka', 'Brno', 2, '274efeaa827a33d7e35be9a82cd6150b7caf98f379a4252aa1afce45664dcbe1', 0, 10), (2, 'Vlčí smečka', 2, '274efeaa827a33d7e35be9a82cd6150b7caf98f379a4252aa1afce45664dcbe1', 1, 10),
(3, 'Orli', 'Ostrava', 3, '05af533c6614544a704c4cf51a45be5c10ff19bd10b7aa1dfe47efc0fd059ede', 1, 5); (3, 'Orli', 3, '05af533c6614544a704c4cf51a45be5c10ff19bd10b7aa1dfe47efc0fd059ede', 1, 5);
-- Vložení pozic -- Vložení pozic
INSERT INTO POSITIONS (id, gps, clue) VALUES INSERT INTO POSITIONS (id, gps, clue) VALUES

View File

@@ -181,13 +181,13 @@ func qrHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
// check if the task is available for the team // check if the task is available for the team
// if order > last_cipher + 1, task is not yet available // if order > last_cipher, task is not yet available
// if order == last_cipher + 1, task is now available, update last_cipher // if order == last_cipher, task is now available
// if order <= last_cipher, task has been already visited, allow viewing // 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) http.Error(w, "This task is not yet available", http.StatusForbidden)
return return
} else if order == last_cipher+1 { } else if order == last_cipher {
last_cipher = order last_cipher = order
_, err = db.Exec("UPDATE teams SET last_cipher = ? WHERE id = ?", order, teamID) _, err = db.Exec("UPDATE teams SET last_cipher = ? WHERE id = ?", order, teamID)
if err != nil { if err != nil {
@@ -339,6 +339,7 @@ func main() {
http.HandleFunc("/admin/teams", adminTeamsHandler) http.HandleFunc("/admin/teams", adminTeamsHandler)
http.HandleFunc("/admin/start", AdminStartHandler) http.HandleFunc("/admin/start", AdminStartHandler)
http.HandleFunc("/admin/routes", AdminRouteHandler) http.HandleFunc("/admin/routes", AdminRouteHandler)
http.HandleFunc("/admin/levels", AdminLevelHandler)
fmt.Println("Server started at :8080") fmt.Println("Server started at :8080")
http.ListenAndServe(":8080", nil) http.ListenAndServe(":8080", nil)

View File

@@ -23,6 +23,16 @@ type TeamTemplateS struct {
Penalties int Penalties int
} }
type DifficultyLevelS struct {
ID int
Name string
}
type TeamsTemplateS struct {
Teams []TeamTemplateS
Difficulties []DifficultyLevelS
}
type AdminRoutesTemplateS struct { type AdminRoutesTemplateS struct {
Name string Name string
Ciphers []CipherTemplateS Ciphers []CipherTemplateS
@@ -32,3 +42,4 @@ var CipherTemplate = template.Must(template.ParseFiles("templates/assignment.htm
var TeamTemplate = template.Must(template.ParseFiles("templates/team.html")) var TeamTemplate = template.Must(template.ParseFiles("templates/team.html"))
var AdminTeamsTemplate = template.Must(template.ParseFiles("templates/adminTeams.html")) var AdminTeamsTemplate = template.Must(template.ParseFiles("templates/adminTeams.html"))
var AdminRoutesTemplate = template.Must(template.ParseFiles("templates/adminRoutes.html")) var AdminRoutesTemplate = template.Must(template.ParseFiles("templates/adminRoutes.html"))
var AdminLevelTemplate = template.Must(template.ParseFiles("templates/adminLevels.html"))

View File

@@ -0,0 +1,38 @@
<!DOCTYPE html>
<html lang="cs">
<head>
<meta charset="UTF-8">
<title>Úrovně obtížnosti</title>
</head>
<body>
<h1>Úrovně obtížnosti</h1>
<table border="1">
<tr>
<th>Jméno</th>
<th>Smazat</th>
</tr>
{{range .}}
<tr>
<td>{{.}}</td>
<td>
<form action="/admin/levels" method="post">
<input type="hidden" name="delete" value="{{.}}">
<button type="submit">Smazat</button>
</form>
</td>
</tr>
{{end}}
</table>
<hr>
<h2>Přidat novou úroveň</h2>
<form action="/admin/levels" method="post">
Jméno: <input type="text" name="name" required>
<button type="submit">Přidat úroveň</button>
</form>
<hr>
<a href="/admin">Zpět na admin panel</a>
</body>
</html>

View File

@@ -10,6 +10,7 @@
<h1>Admin Panel</h1> <h1>Admin Panel</h1>
<a href="/admin/teams">Týmy</a> <br> <a href="/admin/teams">Týmy</a> <br>
<a href="/admin/routes">Trasy</a> <br> <a href="/admin/routes">Trasy</a> <br>
<a href="/admin/levels">Úrovně</a> <br>
<hr> <hr>
<form method="post" action="/admin/logout"> <form method="post" action="/admin/logout">
<input type="submit" value="Logout"> <input type="submit" value="Logout">

View File

@@ -34,6 +34,8 @@
</table> </table>
<hr> <hr>
{{end}} {{end}}
<hr>
<a href="/admin">Zpět na admin panel</a>
</body> </body>
</html> </html>

View File

@@ -3,7 +3,7 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>Admin Panel</title> <title>Týmy</title>
</head> </head>
<body> <body>
@@ -14,13 +14,21 @@
<th>Obtížnost</th> <th>Obtížnost</th>
<th>Poslední šifra</th> <th>Poslední šifra</th>
<th>Penalizace (minuty)</th> <th>Penalizace (minuty)</th>
<th>Smazat tým</th>
</th>
</tr> </tr>
{{range .}} {{range .Teams}}
<tr> <tr>
<td>{{.TeamName}}</td> <td>{{.TeamName}}</td>
<td>{{.Difficulty}}</td> <td>{{.Difficulty}}</td>
<td>{{.LastCipher}}</td> <td>{{.LastCipher}}</td>
<td>{{.Penalties}}</td> <td>{{.Penalties}}</td>
<td>
<form action="/admin/teams" method="post">
<input type="hidden" name="delete" value="{{.TeamName}}">
<button type="submit">Smazat</button>
</form>
</td>
</tr> </tr>
{{end}} {{end}}
</table> </table>
@@ -28,6 +36,19 @@
<p>Vynulování penalizací a posledních šifer.</p> <p>Vynulování penalizací a posledních šifer.</p>
<a href="/admin/start">Start Závodu</a> <a href="/admin/start">Start Závodu</a>
<hr> <hr>
<h2>Přidat tým</h2>
<form method="post" action="/admin/teams">
Název týmu: <input type="text" name="name" required>
Obtížnost:
<select name="difficulty">
{{range .Difficulties}}
<option value="{{.ID}}">{{.Name}}</option>
{{end}}
</select>
Heslo: <input type="text" name="password" required>
<input type="submit" value="Přidat tým">
</form>
<hr>
<a href="/admin">Zpět na admin panel</a> <a href="/admin">Zpět na admin panel</a>
</body> </body>