63 lines
1.3 KiB
Go
63 lines
1.3 KiB
Go
package routes
|
|
|
|
import (
|
|
"dk/internal/actions"
|
|
"dk/internal/models/users"
|
|
"dk/internal/router"
|
|
"dk/internal/template/components"
|
|
"strconv"
|
|
)
|
|
|
|
func Index(ctx router.Ctx, _ []string) {
|
|
user, ok := ctx.UserValue("user").(*users.User)
|
|
if !ok || user == nil {
|
|
components.RenderPage(ctx, "", "intro.html", nil)
|
|
return
|
|
}
|
|
|
|
switch user.Currently {
|
|
case "In Town":
|
|
ctx.Redirect("/town", 303)
|
|
case "Exploring":
|
|
ctx.Redirect("/explore", 303)
|
|
default:
|
|
ctx.Redirect("/explore", 303)
|
|
}
|
|
}
|
|
|
|
func Move(ctx router.Ctx, _ []string) {
|
|
user := ctx.UserValue("user").(*users.User)
|
|
dir, err := strconv.Atoi(string(ctx.PostArgs().Peek("direction")))
|
|
if err != nil {
|
|
ctx.SetContentType("text/plain")
|
|
ctx.SetBodyString("move form parsing error")
|
|
return
|
|
}
|
|
|
|
currently, newX, newY, err := actions.Move(user, actions.Direction(dir))
|
|
if err != nil {
|
|
ctx.SetContentType("text/plain")
|
|
ctx.SetBodyString("move error: " + err.Error())
|
|
return
|
|
}
|
|
|
|
user.Currently = currently
|
|
user.X, user.Y = newX, newY
|
|
|
|
if currently == "In Town" {
|
|
ctx.Redirect("/town", 303)
|
|
return
|
|
}
|
|
|
|
ctx.Redirect("/explore", 303)
|
|
}
|
|
|
|
func Explore(ctx router.Ctx, _ []string) {
|
|
user := ctx.UserValue("user").(*users.User)
|
|
if user.Currently != "Exploring" {
|
|
ctx.Redirect("/", 303)
|
|
return
|
|
}
|
|
components.RenderPage(ctx, "", "explore.html", nil)
|
|
}
|