From 56aa3afd4fdbb287b2749bd11f58727d423e39af Mon Sep 17 00:00:00 2001 From: Sky Johnson Date: Tue, 12 Aug 2025 14:16:18 -0500 Subject: [PATCH] add air config, some style fixes, finish first inn page --- .air.toml | 52 ++++++++++++++++++++++++++ .gitignore | 1 + assets/dk.css | 6 +-- internal/routes/town.go | 8 ++++ internal/template/components/asides.go | 12 +++--- internal/template/template.go | 40 +++++++++++--------- templates/town/inn.html | 20 +++++++++- templates/town/town.html | 20 +++++----- 8 files changed, 120 insertions(+), 39 deletions(-) create mode 100644 .air.toml diff --git a/.air.toml b/.air.toml new file mode 100644 index 0000000..b9dd8d4 --- /dev/null +++ b/.air.toml @@ -0,0 +1,52 @@ +root = "." +testdata_dir = "testdata" +tmp_dir = "tmp" + +[build] + args_bin = [] + bin = "./tmp/main" + cmd = "go build -o ./tmp/main ." + delay = 1000 + exclude_dir = ["assets", "tmp", "vendor", "testdata", "templates"] + exclude_file = [] + exclude_regex = ["_test.go"] + exclude_unchanged = false + follow_symlink = false + full_bin = "" + include_dir = [] + include_ext = ["go", "tpl", "tmpl", "html"] + include_file = [] + kill_delay = "0s" + log = "build-errors.log" + poll = false + poll_interval = 0 + post_cmd = [] + pre_cmd = [] + rerun = false + rerun_delay = 500 + send_interrupt = false + stop_on_error = false + +[color] + app = "" + build = "yellow" + main = "magenta" + runner = "green" + watcher = "cyan" + +[log] + main_only = false + silent = false + time = false + +[misc] + clean_on_exit = false + +[proxy] + app_port = 0 + enabled = false + proxy_port = 0 + +[screen] + clear_on_rebuild = false + keep_scroll = true diff --git a/.gitignore b/.gitignore index 93fdc12..856360a 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ /dk.db /dk.db-* /sessions.json +/tmp \ No newline at end of file diff --git a/assets/dk.css b/assets/dk.css index b1f4d60..266fb53 100644 --- a/assets/dk.css +++ b/assets/dk.css @@ -245,16 +245,16 @@ form.standard { } div.town { - & > div:not(:last-child) { + & > section:not(:last-child) { margin-bottom: 2rem; } - & > div.split { + & > section.split { width: 100%; display: flex; gap: 1rem; - & > div { + & > section { width: 100%; } } diff --git a/internal/routes/town.go b/internal/routes/town.go index 513c9e8..ac77b00 100644 --- a/internal/routes/town.go +++ b/internal/routes/town.go @@ -13,6 +13,7 @@ func RegisterTownRoutes(r *router.Router) { group.Use(middleware.RequireTown()) group.Get("/", showTown) + group.Get("/inn", showInn) } func showTown(ctx router.Ctx, _ []string) { @@ -23,3 +24,10 @@ func showTown(ctx router.Ctx, _ []string) { "whosonline": components.GenerateTownWhosOnline(), }) } + +func showInn(ctx router.Ctx, _ []string) { + town := ctx.UserValue("town").(*towns.Town) + components.RenderPage(ctx, town.Name+" Inn", "town/inn.html", map[string]any{ + "town": town, + }) +} diff --git a/internal/template/components/asides.go b/internal/template/components/asides.go index cf7a422..1148922 100644 --- a/internal/template/components/asides.go +++ b/internal/template/components/asides.go @@ -43,15 +43,15 @@ func RightAside(ctx router.Ctx) map[string]any { } hpPct := helpers.ClampPct(float64(user.HP), float64(user.MaxHP), 0, 100) - data["hpPct"] = hpPct - data["mpPct"] = helpers.ClampPct(float64(user.MP), float64(user.MaxMP), 0, 100) - data["tpPct"] = helpers.ClampPct(float64(user.TP), float64(user.MaxTP), 0, 100) + data["hppct"] = hpPct + data["mppct"] = helpers.ClampPct(float64(user.MP), float64(user.MaxMP), 0, 100) + data["tppct"] = helpers.ClampPct(float64(user.TP), float64(user.MaxTP), 0, 100) - data["hpColor"] = "" + data["hpcolor"] = "" if hpPct < 35 { - data["hpColor"] = "danger" + data["hpcolor"] = "danger" } else if hpPct < 75 { - data["hpColor"] = "warning" + data["hpcolor"] = "warning" } // Build known healing spells list diff --git a/internal/template/template.go b/internal/template/template.go index 22b0e5c..4334596 100644 --- a/internal/template/template.go +++ b/internal/template/template.go @@ -140,8 +140,8 @@ func (t *Template) RenderNamed(data map[string]any) string { result = t.processLoops(result, data) result = t.processConditionals(result, data) - // Process yield before variable substitution - result = t.processYield(result, blocks) + // Process yield with conditionals in blocks + result = t.processYield(result, blocks, data) // Apply data substitutions for key, value := range data { @@ -273,6 +273,20 @@ func (t *Template) getStructField(obj any, fieldName string) any { return field.Interface() } +func (t *Template) getLength(value any) int { + if value == nil { + return 0 + } + + rv := reflect.ValueOf(value) + switch rv.Kind() { + case reflect.Slice, reflect.Array, reflect.Map, reflect.String: + return rv.Len() + default: + return 0 + } +} + func (t *Template) WriteTo(ctx *fasthttp.RequestCtx, data any) { var result string @@ -371,12 +385,16 @@ func (t *Template) processBlocks(content string, blocks map[string]string) strin } // processYield handles {yield} directives for template inheritance -func (t *Template) processYield(content string, blocks map[string]string) string { +func (t *Template) processYield(content string, blocks map[string]string, data map[string]any) string { result := content for blockName, blockContent := range blocks { + // Process conditionals and loops in block content before yielding + processedBlock := t.processLoops(blockContent, data) + processedBlock = t.processConditionals(processedBlock, data) + yieldPlaceholder := fmt.Sprintf("{yield \"%s\"}", blockName) - result = strings.ReplaceAll(result, yieldPlaceholder, blockContent) + result = strings.ReplaceAll(result, yieldPlaceholder, processedBlock) } // Replace any remaining {yield} with empty string @@ -714,20 +732,6 @@ func (t *Template) isTruthy(value any) bool { } } -func (t *Template) getLength(value any) int { - if value == nil { - return 0 - } - - rv := reflect.ValueOf(value) - switch rv.Kind() { - case reflect.Slice, reflect.Array, reflect.Map, reflect.String: - return rv.Len() - default: - return 0 - } -} - // RenderToContext is a simplified helper that renders a template and writes it to the request context // with error handling. Returns true if successful, false if an error occurred (error is written to response). func RenderToContext(ctx *fasthttp.RequestCtx, templateName string, data map[string]any) bool { diff --git a/templates/town/inn.html b/templates/town/inn.html index 8b2226d..d2bf41c 100644 --- a/templates/town/inn.html +++ b/templates/town/inn.html @@ -1,3 +1,19 @@ +{include "layout.html"} + +{block "content"}
- -
\ No newline at end of file +

{town.Name} Inn

+ + {if user.Gold < town.InnCost} +

You do not have enough gold to stay at this Inn tonight.

+

You may return to town, or use the compass on the left to explore.

+ {else} + Resting at the inn will refill your current HP, MP, and TP to their maximum levels.

+ A night's sleep at this Inn will cost you {town.InnCost} gold. Is that ok?

+
+ + +
+ {/if} + +{/block} \ No newline at end of file diff --git a/templates/town/town.html b/templates/town/town.html index ad5173a..d134e32 100644 --- a/templates/town/town.html +++ b/templates/town/town.html @@ -2,7 +2,7 @@ {block "content"}
-
+
Welcome to {town.Name}
Town Options
-
+ -
+
{newscontent} -
+ -
-
+
+
{whosonline} -
+ -
+
Babblebox
@TODO -
-
+ +
{/block}