Skip to content

Commit

Permalink
fix: Client sometimes get stuck in lobby
Browse files Browse the repository at this point in the history
There was a previous fix attempt for this. The client still got stuck in
the lobby sometimes after the game has started. This was due to a race
between the state update handler and the login form handler. These both
relied on the gameStarted flag which has now been removed. Only the
state update handler can switch pages based on the update contents.
  • Loading branch information
milonoir committed Oct 17, 2023
1 parent 44bc24d commit a13b86d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 16 deletions.
13 changes: 3 additions & 10 deletions internal/client/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"log/slog"
"net"
"sync/atomic"
"time"

"github.com/gobwas/ws"
Expand Down Expand Up @@ -61,10 +60,9 @@ type Application struct {
stocks [4]int
prices [4]int

gameStarted atomic.Bool
server network.Connection
errCh chan string
l *slog.Logger
server network.Connection
errCh chan string
l *slog.Logger
}

func NewApplication(l *slog.Logger) *Application {
Expand Down Expand Up @@ -109,11 +107,6 @@ func (a *Application) initUI() {
}
// Successful login.
a.lobby.Reset()
if a.gameStarted.Load() {
a.pages.SwitchToPage(gamePageName)
} else {
a.pages.SwitchToPage(lobbyPageName)
}
},
func() {
a.Stop()
Expand Down
10 changes: 4 additions & 6 deletions internal/client/event_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ func (a *Application) handleStateUpdate(state *message.GameState) {

// Readiness update.
if !state.Started {
if a.gameStarted.Load() {
a.gameStarted.Store(false)
if page, _ := a.pages.GetFrontPage(); page == titlePageName || page == gamePageName {
a.pages.SwitchToPage(lobbyPageName)
}

Expand All @@ -41,9 +40,8 @@ func (a *Application) handleStateUpdate(state *message.GameState) {
return
}

// Switch to main page if game started.
if !a.gameStarted.Load() {
a.gameStarted.Store(true)
// Switch to game page if game started.
if page, _ := a.pages.GetFrontPage(); page == titlePageName || page == lobbyPageName {
a.pages.SwitchToPage(gamePageName)
}

Expand All @@ -53,7 +51,7 @@ func (a *Application) handleStateUpdate(state *message.GameState) {

// Update local game data.
if len(a.hand) == 0 || len(a.hand) != len(state.Player.Hand) {
// This is a little trick to only update the card list if the hand size did not change.
// This is a little trick to only update the actions display if the hand changed.
// It prevents the list from scrolling to the top on each update made by other players.
a.actions.Update(state.Player.Hand)
}
Expand Down

0 comments on commit a13b86d

Please sign in to comment.