admin: tymy, pridani a mazani
This commit is contained in:
60
admin.go
60
admin.go
@@ -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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
8
klice.go
8
klice.go
@@ -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 {
|
||||||
|
|||||||
10
templates.go
10
templates.go
@@ -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
|
||||||
|
|||||||
@@ -31,6 +31,8 @@
|
|||||||
Jméno: <input type="text" name="name" required>
|
Jméno: <input type="text" name="name" required>
|
||||||
<button type="submit">Přidat úroveň</button>
|
<button type="submit">Přidat úroveň</button>
|
||||||
</form>
|
</form>
|
||||||
|
<hr>
|
||||||
|
<a href="/admin">Zpět na admin panel</a>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
||||||
@@ -34,6 +34,8 @@
|
|||||||
</table>
|
</table>
|
||||||
<hr>
|
<hr>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
<hr>
|
||||||
|
<a href="/admin">Zpět na admin panel</a>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
||||||
@@ -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>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user