From 206b5bb5063f18ce6a79503b2bb4ac150c9ef243 Mon Sep 17 00:00:00 2001 From: Patryk Hegenberg Date: Tue, 12 Dec 2023 20:56:55 +0100 Subject: [PATCH] add everything necessary to be able to calculate skills interactive. added new route to main to handle skill field update added new handler to work with htmx requests for skill calculation made skill fields readonly adapted templates for reloading of skills only --- handlers/add_monster_handler.go | 3 +- handlers/form_handler.go | 1 + handlers/skill_calculation_handler.go | 142 +++++ main.go | 1 + templates/monsterForm.html | 785 ++++++++++---------------- templates/skills.html | 185 ++++++ 6 files changed, 644 insertions(+), 473 deletions(-) create mode 100644 handlers/skill_calculation_handler.go create mode 100644 templates/skills.html diff --git a/handlers/add_monster_handler.go b/handlers/add_monster_handler.go index a2306aa..38cb1a1 100644 --- a/handlers/add_monster_handler.go +++ b/handlers/add_monster_handler.go @@ -5,6 +5,7 @@ import ( "log" "net/http" "strconv" + "strings" ) // AddMonster is a http.HandlerFunc that adds a new monster to the Monsters slice. @@ -67,7 +68,7 @@ func parseMonster(r *http.Request) model.Monster { Name: r.FormValue("name"), Source: r.FormValue("source"), Size: []string{r.FormValue("size")}, - Type: r.FormValue("type"), + Type: strings.ToLower(r.FormValue("type")), Alignment: []string{r.FormValue("alignment")}, AC: []model.AC{ { diff --git a/handlers/form_handler.go b/handlers/form_handler.go index f809eeb..c31cee4 100644 --- a/handlers/form_handler.go +++ b/handlers/form_handler.go @@ -31,6 +31,7 @@ func FormHandler(content embed.FS, monsters *[]model.Monster) http.HandlerFunc { "templates/footer.html", "templates/monsterForm.html", "templates/monster.html", + "templates/skills.html", "templates/monsterTable.html", } tmpl, err := template.ParseFS(content, templateFiles...) diff --git a/handlers/skill_calculation_handler.go b/handlers/skill_calculation_handler.go new file mode 100644 index 0000000..48935cf --- /dev/null +++ b/handlers/skill_calculation_handler.go @@ -0,0 +1,142 @@ +package handlers + +import ( + "embed" + "html/template" + "log" + "net/http" + "strconv" +) + +// SkillCalculationHandler ist ein http.HandlerFunc, der von htmx getriggert wird, +// wenn der Benutzer Einträge in bestimmten Feldern macht, und dann die Skill-Felder befüllt. +func SkillCalculationHandler(content embed.FS) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + log.Print("SkillCalculationHandler called") + + // Überprüfen Sie, ob die Anfrage eine POST-Anfrage ist. + if r.Method != http.MethodPost { + log.Print("Method not allowed") + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) + return + } + + // Parse Formulardaten. + err := r.ParseForm() + if err != nil { + log.Printf("Error parsing form data: %v", err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + tmplFiles := []string{"templates/base.html", "templates/header.html", "templates/skills.html", "templates/main.html", "templates/footer.html", "templates/about.html"} + tmpl, err := template.ParseFS(content, tmplFiles...) + if err != nil { + log.Printf("Template parsing error: %v\n", err) + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + + str := parseFieldValue(r.FormValue("str")) + dex := parseFieldValue(r.FormValue("dex")) + int := parseFieldValue(r.FormValue("int")) + cha := parseFieldValue(r.FormValue("cha")) + wis := parseFieldValue(r.FormValue("wis")) + cr := parseFieldValue(r.FormValue("cr")) + crBonus := calcBonus(cr) + + skillValues := map[string]string{ + "acrobatics": strconv.Itoa(calcAbilityScore(dex) + crBonus), + "animalHandling": strconv.Itoa(calcAbilityScore(wis) + crBonus), + "arcana": strconv.Itoa(calcAbilityScore(int) + crBonus), + "athletics": strconv.Itoa(calcAbilityScore(str) + crBonus), + "deception": strconv.Itoa(calcAbilityScore(cha) + crBonus), + "history": strconv.Itoa(calcAbilityScore(int) + crBonus), + "insight": strconv.Itoa(calcAbilityScore(wis) + crBonus), + "intimidation": strconv.Itoa(calcAbilityScore(cha) + crBonus), + "investigation": strconv.Itoa(calcAbilityScore(int) + crBonus), + "medicine": strconv.Itoa(calcAbilityScore(wis) + crBonus), + "nature": strconv.Itoa(calcAbilityScore(int) + crBonus), + "perception": strconv.Itoa(calcAbilityScore(wis) + crBonus), + "performance": strconv.Itoa(calcAbilityScore(cha) + crBonus), + "persuasion": strconv.Itoa(calcAbilityScore(cha) + crBonus), + "religion": strconv.Itoa(calcAbilityScore(int) + crBonus), + "sleightOfHand": strconv.Itoa(calcAbilityScore(dex) + crBonus), + "stealth": strconv.Itoa(calcAbilityScore(dex) + crBonus), + "survival": strconv.Itoa(calcAbilityScore(wis) + crBonus), + } + + err = tmpl.ExecuteTemplate(w, "skills", skillValues) + if err != nil { + log.Printf("Template execution error: %v\n", err) + http.Error(w, err.Error(), http.StatusBadRequest) + } + } +} + +func calcBonus(cr int) int { + if cr >= 0 && cr < 5 { + return 2 + } else if cr >= 5 && cr < 9 { + return 3 + } else if cr >= 9 && cr < 14 { + return 4 + } else if cr >= 14 && cr < 18 { + return 5 + } else if cr >= 18 && cr < 21 { + return 6 + } else if cr >= 21 && cr < 25 { + return 7 + } else if cr >= 25 && cr < 28 { + return 8 + } else if cr >= 28 && cr < 31 { + return 9 + } else { + return 0 + } +} + +func calcAbilityScore(val int) int { + if val < 2 { + return -5 + } else if val >= 2 && val < 4 { + return -4 + } else if val >= 4 && val < 6 { + return -3 + } else if val >= 6 && val < 8 { + return -2 + } else if val >= 8 && val < 10 { + return -1 + } else if val >= 10 && val < 12 { + return 0 + } else if val >= 12 && val < 14 { + return 1 + } else if val >= 14 && val < 16 { + return 2 + } else if val >= 16 && val < 18 { + return 3 + } else if val >= 18 && val < 20 { + return 4 + } else if val >= 20 && val < 22 { + return 5 + } else if val >= 22 && val < 24 { + return 6 + } else if val >= 24 && val < 26 { + return 7 + } else if val >= 26 && val < 28 { + return 8 + } else if val >= 28 && val < 30 { + return 9 + } else { + return 10 + } +} + +func parseFieldValue(value string) int { + val, err := strconv.Atoi(value) + if err != nil { + log.Printf("Error converting field value to integer: %v", err) + return 0 + } + return val +} diff --git a/main.go b/main.go index 546583c..d163e79 100644 --- a/main.go +++ b/main.go @@ -36,6 +36,7 @@ func main() { routes.HandleFunc("/about", handlers.AboutHandler(content)) routes.HandleFunc("/contact", handlers.ContactHandler(content)) routes.HandleFunc("/monsterTable", handlers.MonsterTableHandler(content, &Monsters)) + routes.HandleFunc("/calculate-skills", handlers.SkillCalculationHandler(content)) // Print the message indicating that 'static' has been included. log.Printf("Eingebunden is %v\n", static) diff --git a/templates/monsterForm.html b/templates/monsterForm.html index 04e2dbc..ca69873 100644 --- a/templates/monsterForm.html +++ b/templates/monsterForm.html @@ -1,537 +1,378 @@ {{ define "monsterform" }}
-
-
- -
- -
-
+
+
+ +
+ +
-
-
- -
- -
-
+
+
+
+ +
+ +
-
-
- -
-
- -
-
+
+
+
+ +
+
+
+
-
-
- -
- -
-
+
+
+
+ +
+ +
+
-
-
- -
- -
-
+
+
+ +
+ +
+
-
-
- -
- -
-
+
+
+ +
+ +
-
-
- -
- -
-
+
+
+
+ +
+ +
+
-
-
- -
- -
-
+
+
+ +
+ +
-
-
- -
- -
-
+
+
+
+ +
+ +
+
-
-

- Speed -

-
-
-
-
-
-
- -
- -
-
-
-
-
- -
- -
-
-
-
-
- -
- -
-
-
-
-
-
-
- -
- -
-
-
-
-
- -
- -
-
-
+
+

+ Speed +

+
+
+
+
+
+
+ +
+
+
+
+
+ +
+ +
+
+
+
+
+ +
+ +
+
+
+
+
+
+
+ +
+ +
+
+
+
+
+ +
+ +
+
+
+
+
-
-
- -
- -
-
+
+
+ +
+ +
-
-
- -
- -
-
+
+
+
+ +
+ +
-
-
- -
- -
-
+
+
+
+ +
+ +
-
-
- -
- -
-
+
+
+
+ +
+ +
+
-
-
- -
- -
-
+
+
+ +
+ +
-
-
- -
- -
-
+
+
+
+ +
+ +
-
-
- -
- -
-
+
+
+
+ +
+ +
-
-
- -
- -
-
+
+
+
+ +
+ +
+
-
-

- Save -

-
-
-
-
-
-
- -
- -
-
-
-
-
- -
- -
-
-
-
-
- -
- -
-
-
+
+

+ Save +

+
+
+
+
+
+
+ +
+
+
-
-
-
- -
- -
-
-
-
-
- -
- -
-
-
-
-
- -
- -
-
+
+
+ +
+
+
+
+
+ +
+ +
+
+
+
+
+
+
+ +
+ +
+
+
+
+
+ +
+ +
+
+
+
+
+ +
+ +
+
+
+
+
-
-
-

- Skill -

-
-
-
-
-
-
- -
- -
-
-
-
-
- -
- -
-
-
-
-
- -
- -
-
-
-
-
-
-
- -
- -
-
-
-
-
- -
- -
-
-
-
-
- -
- -
-
-
-
-
-
-
- -
- -
-
-
-
-
- -
- -
-
-
-
-
- -
- -
-
-
-
-
-
-
- -
- -
-
-
-
-
- -
- -
-
-
-
-
- -
- -
-
-
-
-
-
-
- -
- -
-
-
-
-
- -
- -
-
-
-
-
- -
- -
-
-
-
-
-
-
- -
- -
-
-
-
-
- -
- -
-
-
-
-
- -
- -
-
-
-
-
-
+
+ {{ template "skills" }}
-
-
- -
- -
-
+
+
+ +
+ +
-
-
- -
- -
-
+
+
+
+ +
+ +
-
-
- -
- -
-
+
+
+
+ +
+ +
-
-
- -
- -
-
+
+
+
+ +
+ +
+
-
-
- -
- -
-
+
+
+ +
+ +
+
-
-
- -
- -
-
+
+
+ +
+ +
+
-
-
- -
- -
-
+
+
+ +
+ +
-
-
- -
- -
-
+
+
+
+ +
+ +
+
-
-
- -
- -
-
+
+
+ +
+ +
-
-
- -
- -
-
+
+
+
+ +
+ +
+
{{end}} diff --git a/templates/skills.html b/templates/skills.html new file mode 100644 index 0000000..9322260 --- /dev/null +++ b/templates/skills.html @@ -0,0 +1,185 @@ +{{ define "skills" }} +
+

+ Skill +

+
+
+
+
+
+
+ +
+ +
+
+
+
+
+ +
+ +
+
+
+
+
+ +
+ +
+
+
+
+
+
+
+ +
+ +
+
+
+
+
+ +
+ +
+
+
+
+
+ +
+ +
+
+
+
+
+
+
+ +
+ +
+
+
+
+
+ +
+ +
+
+
+
+
+ +
+ +
+
+
+
+
+
+
+ +
+ +
+
+
+
+
+ +
+ +
+
+
+
+
+ +
+ +
+
+
+
+
+
+
+ +
+ +
+
+
+
+
+ +
+ +
+
+
+
+
+ +
+ +
+
+
+
+
+
+
+ +
+ +
+
+
+
+
+ +
+ +
+
+
+
+
+ +
+ +
+
+
+
+
+
+{{ end }}