diff --git a/handlers/add_monster_handler.go b/handlers/add_monster_handler.go index bbec87b..bd84af3 100644 --- a/handlers/add_monster_handler.go +++ b/handlers/add_monster_handler.go @@ -73,7 +73,11 @@ func AddMonster(Monsters *[]model.Monster) http.HandlerFunc { }, }, } + mu.Lock() + defer mu.Unlock() *Monsters = append(*Monsters, monster) + log.Printf("Monster hinzugefügt. Anzahl der Monster jetzt: %d\n", len(*Monsters)) + http.Redirect(w, r, "/monsterTable", http.StatusFound) } } diff --git a/handlers/form_handler.go b/handlers/form_handler.go index 09df89b..878ba66 100644 --- a/handlers/form_handler.go +++ b/handlers/form_handler.go @@ -1,27 +1,32 @@ package handlers import ( + "ddServer/model" "embed" "html/template" "log" "net/http" ) -func FormHandler(content embed.FS, filename string) http.HandlerFunc { +func FormHandler(content embed.FS, monsters *[]model.Monster, filename string) http.HandlerFunc { log.Print("FormHandler called") + mu.Lock() + defer mu.Unlock() return func(w http.ResponseWriter, r *http.Request) { - tmpl, err := template.ParseFS(content, "templates/base.html", "templates/header.html", "templates/main.html", "templates/footer.html", "templates/monsterForm.html") + tmpl, err := template.ParseFS(content, "templates/base.html", "templates/header.html", "templates/main.html", "templates/footer.html", "templates/monsterForm.html", "templates/monster.html", "templates/monsterTable.html") if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } err = tmpl.ExecuteTemplate(w, "base", map[string]interface{}{ - "Title": "Dungeons & Dragons Monster Generator", + "Title": "Dungeons & Dragons Monster Generator", + "Monsters": *monsters, }) if err != nil { log.Printf("Template execution error: %v\n", err) http.Error(w, err.Error(), http.StatusInternalServerError) } + log.Printf("Template mit %d Monstern gerendert\n", len(*monsters)) } } diff --git a/handlers/monster_table_handler.go b/handlers/monster_table_handler.go new file mode 100644 index 0000000..ebba144 --- /dev/null +++ b/handlers/monster_table_handler.go @@ -0,0 +1,29 @@ +package handlers + +import ( + "ddServer/model" + "embed" + "html/template" + "log" + "net/http" +) + +func MonsterTableHandler(content embed.FS, monsters *[]model.Monster) http.HandlerFunc { + log.Print("AboutHandler called") + return func(w http.ResponseWriter, r *http.Request) { + tmpl, err := template.ParseFS(content, "templates/base.html", "templates/header.html", "templates/main.html", "templates/footer.html", "templates/monsterTable.html", "templates/monster.html") + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + err = tmpl.ExecuteTemplate(w, "monsterTable", map[string]interface{}{ + "Title": "Dungeons & Dragons Monster Generator", + "Monsters": *monsters, + }) + if err != nil { + log.Printf("Template execution error: %v\n", err) + http.Error(w, err.Error(), http.StatusInternalServerError) + } + } +} diff --git a/handlers/submit_handler.go b/handlers/submit_handler.go index 02a6fde..f6987ba 100644 --- a/handlers/submit_handler.go +++ b/handlers/submit_handler.go @@ -46,6 +46,7 @@ func SubmitHandler(content embed.FS, chars *[]model.Character, Monsters *[]model return } + log.Printf("Monster hinzugefügt. Anzahl der Monster jetzt: %d\n", len(*Monsters)) // JSON-Daten in die Datei schreiben err = model.WriteToFile(filename, charJSON) if err != nil { diff --git a/main.go b/main.go index d6db9e5..df3a371 100644 --- a/main.go +++ b/main.go @@ -21,12 +21,13 @@ var ( func main() { filename := "" - http.HandleFunc("/", handlers.FormHandler(content, filename)) + http.HandleFunc("/", handlers.FormHandler(content, &Monsters, filename)) http.HandleFunc("/submit", handlers.SubmitHandler(content, &chars, &Monsters, filename)) http.Handle("/images/", http.StripPrefix("/images/", http.FileServer(http.FS(content)))) http.HandleFunc("/addMonster", handlers.AddMonster(&Monsters)) http.HandleFunc("/about", handlers.AboutHandler(content)) http.HandleFunc("/contact", handlers.ContactHandler(content)) + http.HandleFunc("/monsterTable", handlers.MonsterTableHandler(content, &Monsters)) log.Print("Server gestartet, erreichbar unter http://localhost:8080") http.ListenAndServe(":8080", nil) diff --git a/model/model.go b/model/model.go index 03f2949..1ad1bb0 100644 --- a/model/model.go +++ b/model/model.go @@ -69,8 +69,8 @@ type Action struct { // Character struct für die Daten des Charakters type Character struct { - Monster []Monster `json:"monster"` Meta Meta `json:"_meta"` + Monster []Monster `json:"monster"` } // Meta struct für Meta-Informationen diff --git a/templates/base.html b/templates/base.html index cbfd77f..73660f2 100644 --- a/templates/base.html +++ b/templates/base.html @@ -36,18 +36,6 @@ height: 100%; } - .form-item { - display: grid; - grid-template-columns: auto auto auto; - gap: 10px; - } - - .form-item2 { - display: flex; - flex-direction: row; - justify-content: space-evenly; - } - .banner { position: relative; text-align: center; @@ -70,7 +58,6 @@ {{ template "footer" . }} - diff --git a/templates/main.html b/templates/main.html index 7ab043c..f1cfccf 100644 --- a/templates/main.html +++ b/templates/main.html @@ -1,5 +1,5 @@ {{ define "main" }} -
+

Monster Form

@@ -10,7 +10,9 @@
- +
{{ template "monsterform" . }} @@ -19,4 +21,47 @@
+
+

Existing Monsters

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + {{ template "monsterTable" }} +
NameSourceSizeTypeAlignmentACAC FormHP AverageHP FormulaSpeedStrDexConIntWisChaSave DexSave ConSavve WisPerceptionStealthDamage ResistanceSensesLanguagesCRTrait NameTrait EntryAction NameAction Entry
+
{{ end }} diff --git a/templates/monster.html b/templates/monster.html new file mode 100644 index 0000000..2cd8b84 --- /dev/null +++ b/templates/monster.html @@ -0,0 +1,33 @@ +{{ define "monster" }} + + {{.Name}} + {{.Source}} + {{range .Size}}{{.}}{{end}} + {{.Type}} + {{range .Alignment}}{{.}}{{end}} + {{range .AC}}{{.AC}}{{end}} + {{range .AC}}{{.From}}{{end}} + {{.HP.Average}} + {{.HP.Formula}} + {{.Speed.Walk}} + {{.Str}} + {{.Dex}} + {{.Con}} + {{.Int}} + {{.Wis}} + {{.Cha}} + {{.Save.Dex}} + {{.Save.Con}} + {{.Save.Wis}} + {{.Skill.Perception}} + {{.Skill.Stealth}} + {{range .DamageRes}}{{.}}{{end}} + {{range .Senses}}{{.}}{{end}} + {{range .Languages}}{{.}}{{end}} + {{.CR}} + {{range .Traits}}{{.Name}}{{end}} + {{range .Traits}}{{range .Entries}}{{.}}{{end}}{{end}} + {{range .Actions}}{{.Name}}{{end}} + {{range .Actions}}{{range .Entries}}{{.}}{{end}}{{end}} + +{{ end }} diff --git a/templates/monsterTable.html b/templates/monsterTable.html new file mode 100644 index 0000000..d5aef50 --- /dev/null +++ b/templates/monsterTable.html @@ -0,0 +1,7 @@ +{{ define "monsterTable" }} + + {{ range .Monsters }} + {{ template "monster" . }} + +{{ end }} +{{ end }}