Skip to content

Commit

Permalink
fix: Handling Blackjack dealer running state
Browse files Browse the repository at this point in the history
  • Loading branch information
lcox74 committed Apr 22, 2024
1 parent 2af9b67 commit e29098e
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions pkg/blackjack/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@ func Running() bool {
dealer.mu.Lock()
defer dealer.mu.Unlock()

return dealer.Stage != IdleStage
return dealer.Stage != IdleStage && dealer.Stage != FinishedStage
}

// Host initialises and starts a new game of Blackjack. It requires a callback
// function that is invoked on game state changes, which can be used to update
// clients. It returns an error if a game is already in progress.
func Host(stateCb StateChangeCallback, achievementCb AchievementCallback, messageId, channelId string) error {
dealer.mu.Lock()
defer dealer.mu.Unlock()

if dealer.Stage != IdleStage && dealer.Stage != FinishedStage {
if Running() {
return ErrDealerBusy
}

// Ensure the args are valid
if stateCb == nil || messageId == "" || channelId == "" {
return ErrInvalidAction
}

// Lock the dealer to set up the new game
dealer.mu.Lock()
defer dealer.mu.Unlock()

// Initialise a new game state
dealer.State = newState()
dealer.action = make(chan int)
Expand Down

0 comments on commit e29098e

Please sign in to comment.