Skip to content

Commit

Permalink
store games
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Steinke committed Feb 13, 2024
1 parent 0adcc87 commit 091cdb1
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 27 deletions.
109 changes: 84 additions & 25 deletions logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/rand"
"fmt"
"math/big"
"sync"
"time"

"github.com/jan-xyz/simon-says/ui"
Expand All @@ -21,6 +22,18 @@ const (
hard
)

var LocalStorageScores = "scores"

type scores struct {
Basic map[ui.Difficulty]score
Endless map[int]int
}

type score struct {
Win int
Loss int
}

var difficulties = map[ui.Difficulty]int{
ui.Easy: 4,
ui.Medium: 8,
Expand All @@ -42,20 +55,15 @@ func NewLogic() *logic {
}

type logic struct {
difficulty ui.Difficulty
sequence []int64
clicks int
stage int
state gameState
difficulty ui.Difficulty
sequence []int64
clicks int
stage int
state gameState
storageMutex sync.Mutex
}

func (g *logic) simonSays(ctx app.Context, a app.Action) {
sequence, ok := a.Value.([]int64)
if !ok {
fmt.Println("wrong type")
return
}

func (g *logic) simonSays(ctx app.Context, sequence []int64) {
go func() {
<-time.After(200 * time.Millisecond)
for _, btnIndex := range sequence {
Expand All @@ -79,7 +87,7 @@ func (g *logic) handleNewGame(ctx app.Context, a app.Action) {
g.stage = 1
g.state = gameStateSimonSays
ctx.NewActionWithValue(ui.EventStateChange, "Simon says...")
ctx.NewActionWithValue(ui.EventSimonSays, g.sequence)
g.simonSays(ctx, g.sequence)
}

func (g *logic) handleClick(ctx app.Context, a app.Action) {
Expand All @@ -93,26 +101,77 @@ func (g *logic) handleClick(ctx app.Context, a app.Action) {
}

if g.sequence[g.clicks] != click {
g.state = gameStateLost
ctx.NewActionWithValue(ui.EventStateChange, fmt.Sprintf("You Lost in %s mode with a streak of %d. Franzi has a highscore of 21.", g.difficulty, len(g.sequence)))
g.lostGame(ctx)
return
}
g.clicks++
if g.difficulty != ui.Endless && difficulties[g.difficulty] == g.clicks {
g.state = gameStateWon
ctx.NewActionWithValue(ui.EventStateChange, fmt.Sprintf("You Won in %s mode. Start a New Game", g.difficulty))
g.wonGame(ctx)
return
}
if g.clicks == g.stage {
g.clicks = 0
g.stage++
g.state = gameStateSimonSays
g.sequence = append(g.sequence, NextNumber())
ctx.NewActionWithValue(ui.EventStateChange, "Simon says...")
ctx.After(1*time.Second, func(ctx app.Context) {
ctx.NewActionWithValue(ui.EventSimonSays, g.sequence)
})
g.nextRound(ctx)
}
}

func (g *logic) lostGame(ctx app.Context) {
g.storageMutex.Lock()
defer g.storageMutex.Unlock()
g.state = gameStateLost
ctx.NewActionWithValue(ui.EventStateChange, fmt.Sprintf("You Lost in %s mode in round %d. Franzi has a highscore of 21.", g.difficulty, len(g.sequence)))

// increment losses
s := &scores{}
ctx.LocalStorage().Get(LocalStorageScores, s)
if g.difficulty != ui.Endless {
if f, ok := s.Basic[g.difficulty]; ok {
f.Loss++
s.Basic[g.difficulty] = f
} else {
if s.Basic == nil {
s.Basic = map[ui.Difficulty]score{}
}
s.Basic[g.difficulty] = score{Loss: 1}
}
} else {
if s.Endless == nil {
s.Endless = map[int]int{}
}
s.Endless[len(g.sequence)]++
}
ctx.LocalStorage().Set(LocalStorageScores, s)
}

func (g *logic) wonGame(ctx app.Context) {
g.storageMutex.Lock()
defer g.storageMutex.Unlock()
g.state = gameStateWon
ctx.NewActionWithValue(ui.EventStateChange, fmt.Sprintf("You Won in %s mode. Start a New Game", g.difficulty))

// increment wins
s := &scores{}
ctx.LocalStorage().Get(LocalStorageScores, s)
if f, ok := s.Basic[g.difficulty]; ok {
f.Win++
s.Basic[g.difficulty] = f
} else {
if s.Basic == nil {
s.Basic = map[ui.Difficulty]score{}
}
s.Basic[g.difficulty] = score{Win: 1}
}
ctx.LocalStorage().Set(LocalStorageScores, s)
}

func (g *logic) nextRound(ctx app.Context) {
g.clicks = 0
g.stage++
g.state = gameStateSimonSays
g.sequence = append(g.sequence, NextNumber())
ctx.NewActionWithValue(ui.EventStateChange, "Simon says...")
ctx.After(1*time.Second, func(ctx app.Context) {
g.simonSays(ctx, g.sequence)
})
}

func NextNumber() int64 {
Expand Down
2 changes: 0 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ func main() {

l := NewLogic()
// TODO: keep scores in local storage
// TODO: keep last selected mode in local storage
// 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.EventSimonSays, l.simonSays)
app.Handle(ui.EventClick, l.handleClick)
app.Handle(ui.EventNewGame, l.handleNewGame)

Expand Down

0 comments on commit 091cdb1

Please sign in to comment.