Skip to content

Commit

Permalink
update loading text, auto-refresh and store difficulty
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Steinke committed Feb 13, 2024
1 parent c402c5e commit 14d44f1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 25 deletions.
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"log"
"net/http"
"time"

"github.com/jan-xyz/simon-says/ui"
"github.com/maxence-charriere/go-app/v9/pkg/app"
Expand Down Expand Up @@ -47,6 +48,8 @@ func main() {
Icon: app.Icon{
Default: "/web/icon.png",
},
LoadingLabel: "Loading...",
AutoUpdateInterval: 15 * time.Minute,
}
if !*serve {
h.Resources = app.GitHubPages("simon-says")
Expand Down
64 changes: 39 additions & 25 deletions ui/menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,59 @@ const (
Endless Difficulty = "endless"
)

const (
localStorageDifficulty = "difficulty"
)

type menu struct {
app.Compo
selectedDifficulty Difficulty
}

func NewMenu() *menu {
return &menu{selectedDifficulty: Easy}
return &menu{}
}

func (g *menu) OnMount(ctx app.Context) {
var d Difficulty
ctx.LocalStorage().Get(localStorageDifficulty, &d)
if d == "" {
d = Easy
}
g.selectedDifficulty = d
}

func (g *menu) Render() app.UI {
return app.Div().Body(
modes := []app.UI{}
for _, mode := range []Difficulty{Easy, Medium, Hard, Endless} {
input := app.Input().Type("radio").Name("difficulty-setting").ID("difficulty%d", mode).Value(mode).OnClick(g.storeValue)
label := app.Label().For("difficulty%d", mode).Body(app.Span().Text(mode))
if g.selectedDifficulty == mode {
input.Checked(true)
}
modes = append(modes, input, label)
}
t := app.Div().Body(

app.Button().
Class("simon-button", "new-game").
Body(app.Span().Text("New Game")).
OnClick(func(ctx app.Context, _ app.Event) {
ctx.NewActionWithValue(EventNewGame, g.selectedDifficulty)
}),
OnClick(g.startNewGame),
app.Div().Class("difficulty").Body(
app.Input().Type("radio").Name("difficulty-setting").ID("difficulty%d", Easy).Value(Easy).Checked(true).OnClick(func(ctx app.Context, _ app.Event) {
val := ctx.JSSrc().Get("value").String()
g.selectedDifficulty = Difficulty(val)
}),
app.Label().For("difficulty%d", Easy).Body(app.Span().Text("easy")),
app.Input().Type("radio").Name("difficulty-setting").ID("difficulty%d", Medium).Value(Medium).OnClick(func(ctx app.Context, _ app.Event) {
val := ctx.JSSrc().Get("value").String()
g.selectedDifficulty = Difficulty(val)
}),
app.Label().For("difficulty%d", Medium).Body(app.Span().Text("medium")),
app.Input().Type("radio").Name("difficulty-setting").ID("difficulty%d", Hard).Value(Hard).OnClick(func(ctx app.Context, _ app.Event) {
val := ctx.JSSrc().Get("value").String()
g.selectedDifficulty = Difficulty(val)
}),
app.Label().For("difficulty%d", Hard).Body(app.Span().Text("hard")),
app.Input().Type("radio").Name("difficulty-setting").ID("difficulty%d", Endless).Value(Endless).OnClick(func(ctx app.Context, _ app.Event) {
val := ctx.JSSrc().Get("value").String()
g.selectedDifficulty = Difficulty(val)
}),
app.Label().For("difficulty%d", Endless).Body(app.Span().Text("endless")),
modes...,
),
)
return t
}

func (g *menu) storeValue(ctx app.Context, _ app.Event) {
val := ctx.JSSrc().Get("value").String()
ctx.LocalStorage().Set(localStorageDifficulty, val)
g.selectedDifficulty = Difficulty(val)
}

func (g *menu) startNewGame(ctx app.Context, _ app.Event) {
d := Easy
ctx.LocalStorage().Get(localStorageDifficulty, d)
ctx.NewActionWithValue(EventNewGame, d)
}

0 comments on commit 14d44f1

Please sign in to comment.