Skip to content

Commit

Permalink
fix nil-map assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Steinke committed Feb 17, 2024
1 parent bb7edfc commit 717cea6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 42 deletions.
11 changes: 6 additions & 5 deletions logic.go → game/game.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package game

import (
"crypto/rand"
Expand Down Expand Up @@ -53,18 +53,19 @@ type logic struct {
}

func (g *logic) simonSays(ctx app.Context, sequence []int64) {
go func() {
fmt.Println(sequence)
ctx.Async(func() {
<-time.After(200 * time.Millisecond)
for _, btnIndex := range sequence {
ctx.NewAction(fmt.Sprintf(ui.EventPlayButton, btnIndex))
<-time.After(time.Second)
}
g.state = gameStatePlayerSays
ctx.NewActionWithValue(ui.EventStateChange, "Repeat what Simon said...")
}()
})
}

func (g *logic) handleNewGame(ctx app.Context, a app.Action) {
func (g *logic) HandleNewGame(ctx app.Context, a app.Action) {
d, ok := a.Value.(storage.Difficulty)
if !ok {
fmt.Println("wrong type")
Expand All @@ -79,7 +80,7 @@ func (g *logic) handleNewGame(ctx app.Context, a app.Action) {
g.simonSays(ctx, g.sequence)
}

func (g *logic) handleClick(ctx app.Context, a app.Action) {
func (g *logic) HandleClick(ctx app.Context, a app.Action) {
if g.state != gameStatePlayerSays {
return
}
Expand Down
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"time"

"github.com/jan-xyz/simon-says/game"
"github.com/jan-xyz/simon-says/ui"
"github.com/maxence-charriere/go-app/v9/pkg/app"
)
Expand All @@ -14,15 +15,15 @@ func main() {
serve := flag.Bool("serve", false, "set to serve instead of generating resources")
flag.Parse()

l := NewLogic()
l := game.NewLogic()
// TODO: abstract storage behind useful API
// TODO: calculate statistics
// TODO: for endless mode add histogram of how far you got.
// TODO: add tests
// TODO: add dependabot
// TODO: add linter
app.Handle(ui.EventClick, l.handleClick)
app.Handle(ui.EventNewGame, l.handleNewGame)
app.Handle(ui.EventClick, l.HandleClick)
app.Handle(ui.EventNewGame, l.HandleNewGame)

g := ui.NewUI()
app.Route("/", g)
Expand Down
46 changes: 12 additions & 34 deletions storage/scores.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package storage

import (
"reflect"

"github.com/maxence-charriere/go-app/v9/pkg/app"
)

Expand Down Expand Up @@ -30,6 +28,14 @@ type Score struct {
Loss int
}

func newScores() Scores {
return Scores{Basic: map[Difficulty]Score{
Easy: {},
Medium: {},
Hard: {},
}, Endless: map[int]int{}}
}

func IncrementEasyLoss(ctx app.Context) {
IncrementLoss(ctx, Easy)
}
Expand All @@ -43,15 +49,8 @@ func IncrementHardLoss(ctx app.Context) {
}

func IncrementLoss(ctx app.Context, d Difficulty) {
s := Scores{}
s := newScores()
ctx.LocalStorage().Get(localStorageScores, &s)
if reflect.DeepEqual(s, &Scores{}) {
s = Scores{Basic: map[Difficulty]Score{
Easy: {},
Medium: {},
Hard: {},
}, Endless: map[int]int{}}
}
if d != Endless {
f := s.Basic[d]
f.Loss++
Expand All @@ -62,15 +61,8 @@ func IncrementLoss(ctx app.Context, d Difficulty) {
}

func UpdateEndless(ctx app.Context, stage int) {
s := Scores{}
s := newScores()
ctx.LocalStorage().Get(localStorageScores, &s)
if reflect.DeepEqual(s, &Scores{}) {
s = Scores{Basic: map[Difficulty]Score{
Easy: {},
Medium: {},
Hard: {},
}, Endless: map[int]int{}}
}
s.Endless[stage]++
ctx.LocalStorage().Set(localStorageScores, s)
ctx.NewActionWithValue(EventScoreUpdate, s)
Expand All @@ -89,15 +81,8 @@ func IncrementHardWin(ctx app.Context) {
}

func IncrementWin(ctx app.Context, d Difficulty) {
s := Scores{}
s := newScores()
ctx.LocalStorage().Get(localStorageScores, &s)
if reflect.DeepEqual(s, &Scores{}) {
s = Scores{Basic: map[Difficulty]Score{
Easy: {},
Medium: {},
Hard: {},
}, Endless: map[int]int{}}
}
f := s.Basic[d]
f.Win++
s.Basic[d] = f
Expand All @@ -106,14 +91,7 @@ func IncrementWin(ctx app.Context, d Difficulty) {
}

func LoadScores(ctx app.Context) Scores {
s := Scores{}
s := newScores()
ctx.LocalStorage().Get(localStorageScores, &s)
if reflect.DeepEqual(s, &Scores{}) {
s = Scores{Basic: map[Difficulty]Score{
Easy: {},
Medium: {},
Hard: {},
}, Endless: map[int]int{}}
}
return s
}

0 comments on commit 717cea6

Please sign in to comment.