From 4dd9c1883231ced04f6d413cb9ca789547c692c7 Mon Sep 17 00:00:00 2001 From: Patryk Hegenberg Date: Wed, 29 Nov 2023 22:36:00 +0100 Subject: [PATCH] added a basic function to add multiple Monsters --- main.go | 79 ++++++++++++- templates/base.html | 3 + templates/main.html | 13 ++- templates/monsterForm.html | 234 ++++++++++++++++++------------------- 4 files changed, 207 insertions(+), 122 deletions(-) diff --git a/main.go b/main.go index 8bb32c8..d2f8ba3 100644 --- a/main.go +++ b/main.go @@ -100,7 +100,8 @@ var ( chars []Character //go:embed templates/*.html //go:embed images/* - content embed.FS + content embed.FS + Monsters []Monster ) func main() { @@ -109,6 +110,7 @@ func main() { http.HandleFunc("/", formHandler(filename)) http.HandleFunc("/submit", submitHandler(filename)) http.Handle("/images/", http.StripPrefix("/images/", http.FileServer(http.FS(content)))) + http.HandleFunc("/addMonster", addMonster()) fmt.Println("Server gestartet, erreichbar unter http://localhost:8080") http.ListenAndServe(":8080", nil) @@ -149,7 +151,7 @@ func submitHandler(filename string) http.HandlerFunc { // Monster-Objekt erstellen filename := r.FormValue("filename") - monster := Monster{ + /*monster := Monster{ Name: r.FormValue("name"), Source: r.FormValue("source"), Size: []string{r.FormValue("size")}, @@ -199,14 +201,14 @@ func submitHandler(filename string) http.HandlerFunc { Entries: []string{r.FormValue("actionEntry")}, }, }, - } + }*/ // Charakter-Objekt erstellen oder aktualisieren mu.Lock() defer mu.Unlock() char := getOrCreateCharacter(filename) - char.Monster = append(char.Monster, monster) + char.Monster = append(char.Monster, Monsters...) // Charakterdaten in JSON umwandeln charJSON, err := json.Marshal(char) @@ -293,3 +295,72 @@ func parseInt(s string) int { } return i } + +func addMonster() http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + // TODO + if r.Method != http.MethodPost { + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) + return + } + + err := r.ParseForm() + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + monster := Monster{ + Name: r.FormValue("name"), + Source: r.FormValue("source"), + Size: []string{r.FormValue("size")}, + Type: r.FormValue("type"), + Alignment: []string{r.FormValue("alignment")}, + AC: []AC{ + { + AC: parseInt(r.FormValue("ac")), + From: []string{r.FormValue("acFrom")}, + }, + }, + HP: HP{ + Average: parseInt(r.FormValue("hpAverage")), + Formula: r.FormValue("hpFormula"), + }, + Speed: Speed{ + Walk: parseInt(r.FormValue("speed")), + }, + Str: parseInt(r.FormValue("str")), + Dex: parseInt(r.FormValue("dex")), + Con: parseInt(r.FormValue("con")), + Int: parseInt(r.FormValue("int")), + Wis: parseInt(r.FormValue("wis")), + Cha: parseInt(r.FormValue("cha")), + Save: Save{ + Dex: r.FormValue("saveDex"), + Con: r.FormValue("saveCon"), + Wis: r.FormValue("saveWis"), + }, + Skill: Skill{ + Perception: r.FormValue("perception"), + Stealth: r.FormValue("stealth"), + }, + DamageRes: []string{r.FormValue("damageRes")}, + Senses: []string{r.FormValue("senses")}, + Languages: []string{r.FormValue("languages")}, + CR: r.FormValue("cr"), + Traits: []Trait{ + { + Name: r.FormValue("traitName"), + Entries: []string{r.FormValue("traitEntry")}, + }, + }, + Actions: []Action{ + { + Name: r.FormValue("actionName"), + Entries: []string{r.FormValue("actionEntry")}, + }, + }, + } + Monsters = append(Monsters, monster) + } +} diff --git a/templates/base.html b/templates/base.html index ececfbb..0fba542 100644 --- a/templates/base.html +++ b/templates/base.html @@ -9,6 +9,9 @@ +