Skip to content

Commit

Permalink
Use channels to handle turdserve errors
Browse files Browse the repository at this point in the history
  • Loading branch information
z-riley committed Oct 13, 2024
1 parent 53a2769 commit 020e25d
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 40 deletions.
10 changes: 5 additions & 5 deletions common/arena.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ type Arena struct {
bgTiles [numTiles][numTiles]*turdgl.CurvedRect // every grid space
background *turdgl.CurvedRect // the background of the arena
latestState backend.Game // used to detect changes in game state (for animations etc...)
animationCh chan (animationState) // for sending animations to animator goroutine
animationCh chan animationState // for sending animations to animator goroutine
}

// NewArena constructs a new arena widget. pos is the top-left pixel of the
Expand Down Expand Up @@ -283,7 +283,7 @@ func (a *Arena) handleAnimations() {
}

// animateMove animates a tile moving.
func (a *Arena) animateMove(animation moveAnimation, errCh chan (error), wg *sync.WaitGroup) {
func (a *Arena) animateMove(animation moveAnimation, errCh chan error, wg *sync.WaitGroup) {
defer wg.Done()

origin, dest := animation.origin, animation.dest
Expand All @@ -308,7 +308,7 @@ func (a *Arena) animateMove(animation moveAnimation, errCh chan (error), wg *syn

// animateMoveToCombine animates a tile moving into another like tile, resulting
// in a combine.
func (a *Arena) animateMoveToCombine(animation moveToCombineAnimation, errCh chan (error), wg *sync.WaitGroup) {
func (a *Arena) animateMoveToCombine(animation moveToCombineAnimation, errCh chan error, wg *sync.WaitGroup) {
defer wg.Done()

origin, dest := animation.origin, animation.dest
Expand Down Expand Up @@ -338,7 +338,7 @@ func (a *Arena) animateMoveToCombine(animation moveToCombineAnimation, errCh cha
}

// animateSpawn animates a tile spawn animation.
func (a *Arena) animateSpawn(animation spawnAnimation, errCh chan (error), wg *sync.WaitGroup) {
func (a *Arena) animateSpawn(animation spawnAnimation, errCh chan error, wg *sync.WaitGroup) {
defer wg.Done()

dest := animation.dest
Expand Down Expand Up @@ -379,7 +379,7 @@ func (a *Arena) animateSpawn(animation spawnAnimation, errCh chan (error), wg *s
}
}

func (a *Arena) animateNewFromCombine(animation newFromCombineAnimation, errCh chan (error), wg *sync.WaitGroup) {
func (a *Arena) animateNewFromCombine(animation newFromCombineAnimation, errCh chan error, wg *sync.WaitGroup) {
defer wg.Done()

dest := animation.dest
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/brunoga/deep v1.2.4
github.com/google/uuid v1.6.0
github.com/z-riley/turdgl v0.0.0-20241010200905-70b7776935f4
github.com/z-riley/turdserve v0.0.0-20240929075615-11b6fcd6fe3a
github.com/z-riley/turdserve v0.0.0-20241012214724-ac0a7bd77e0a
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa
)

Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ github.com/z-riley/turdgl v0.0.0-20241010200905-70b7776935f4 h1:0zb+jC6JuP3uBneC
github.com/z-riley/turdgl v0.0.0-20241010200905-70b7776935f4/go.mod h1:PUy0rnENvtQrZRghi9dYinsUUE7gvC9wOz8C/gcwJ8A=
github.com/z-riley/turdserve v0.0.0-20240929075615-11b6fcd6fe3a h1:voAv/zH9eMiTP+u0so6XxpCEu0TI6Jh3xTxfWx2N0+w=
github.com/z-riley/turdserve v0.0.0-20240929075615-11b6fcd6fe3a/go.mod h1:i9Dlhid4XYNAFdTC4L94dEpXujy8FsfEGlkV7VH4Wmk=
github.com/z-riley/turdserve v0.0.0-20241012184205-2aba824acb8f h1:1GJun5yT7WlZ2cf2y44lJlzIluG7PY5SIgd6IYEj4ns=
github.com/z-riley/turdserve v0.0.0-20241012184205-2aba824acb8f/go.mod h1:i9Dlhid4XYNAFdTC4L94dEpXujy8FsfEGlkV7VH4Wmk=
github.com/z-riley/turdserve v0.0.0-20241012185106-15a04b456a19 h1:JnCnVdOrXV+E6HykC3RQS9MxFMFLOegeoxyzBOmVjUk=
github.com/z-riley/turdserve v0.0.0-20241012185106-15a04b456a19/go.mod h1:i9Dlhid4XYNAFdTC4L94dEpXujy8FsfEGlkV7VH4Wmk=
github.com/z-riley/turdserve v0.0.0-20241012185942-e41420074ff0 h1:aQmEfikptwkozCLu0qaT8ts7W6LdTJFbwbgIslnnNQc=
github.com/z-riley/turdserve v0.0.0-20241012185942-e41420074ff0/go.mod h1:i9Dlhid4XYNAFdTC4L94dEpXujy8FsfEGlkV7VH4Wmk=
github.com/z-riley/turdserve v0.0.0-20241012204716-eb30c0537cfd h1:6l6XJyfspiOGpBmN7bTYQ7qBVbRo1OXIz7ZYd/eCEJY=
github.com/z-riley/turdserve v0.0.0-20241012204716-eb30c0537cfd/go.mod h1:i9Dlhid4XYNAFdTC4L94dEpXujy8FsfEGlkV7VH4Wmk=
github.com/z-riley/turdserve v0.0.0-20241012214724-ac0a7bd77e0a h1:osSGToKmun666CuIm/2clKw/3F5KpThk9diQJ9h6IgE=
github.com/z-riley/turdserve v0.0.0-20241012214724-ac0a7bd77e0a/go.mod h1:i9Dlhid4XYNAFdTC4L94dEpXujy8FsfEGlkV7VH4Wmk=
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa h1:ELnwvuAXPNtPk1TJRuGkI9fDTwym6AYBu0qzT8AcHdI=
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa/go.mod h1:akd2r19cwCdwSwWeIdzYQGa/EZZyqcOdwWiwj5L5eKQ=
golang.org/x/image v0.19.0 h1:D9FX4QWkLfkeqaC62SonffIIuYdOk/UE2XKUBgRIBIQ=
Expand Down
23 changes: 12 additions & 11 deletions launch_two_instances.sh
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
#!/bin/bash
# Script that launches two games at once (for multiplayer testing)

cleanup() {
echo "Stopping processes..."
kill $pid1 $pid2 2>/dev/null
wait $pid1 2>/dev/null
wait $pid2 2>/dev/null
exit
}

# Trap for Ctrl+C to clean up processes
trap cleanup INT

# Start the processes in the background
process1_command="go run cmd/main.go --screen multiplayerHost"
$process1_command &
process1_command="go run cmd/main.go --screen multiplayerJoin"
$process1_command 2>&1 &
pid1=$!

process2_command="go run cmd/main.go --screen multiplayerJoin"
$process2_command &
process2_command="go run cmd/main.go --screen multiplayerHost"
$process2_command 2>&1 &
pid2=$!

# Wait for either process to finish
wait -n $pid1 $pid2

# If one process ends, clean up both
echo "Stopping processes..."
kill $pid1 $pid2 2>/dev/null
wait $pid1 2>/dev/null
wait $pid2 2>/dev/null
exit
2 changes: 1 addition & 1 deletion screen/multiplayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type MultiplayerScreen struct {
guide *turdgl.Text
backend *backend.Game
arena *common.Arena
arenaInputCh chan (func())
arenaInputCh chan func()

// Opponent's grid
opponentScore *common.GameUIBox
Expand Down
12 changes: 10 additions & 2 deletions screen/multiplayer_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,19 @@ func (s *MultiplayerHostScreen) Init(_ InitData) {
})

// Start server to allow other players to connect
errCh := make(chan error)
go func() {
if err := s.server.Run("0.0.0.0", serverPort); err != nil {
panic("server crashed: " + err.Error())
fmt.Println("a")
for err := range errCh {
fmt.Println("b")
if err != nil {
panic("server crashed: " + err.Error())
}
}
}()
if err := s.server.Start("0.0.0.0", serverPort, errCh); err != nil {
panic(err)
}
}

// Deinit deinitialises the screen.
Expand Down
43 changes: 24 additions & 19 deletions screen/multiplayer_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package screen
import (
"encoding/json"
"fmt"
"time"

"github.com/z-riley/go-2048-battle/common"
"github.com/z-riley/go-2048-battle/comms"
Expand All @@ -19,8 +18,8 @@ type MultiplayerJoinScreen struct {
buttons []*common.MenuButton
entries []*common.EntryBox

hostReady bool
client *turdserve.Client
hostIsReady chan bool
client *turdserve.Client
}

// NewTitle Screen constructs a new multiplayer menu screen for the given window.
Expand Down Expand Up @@ -49,16 +48,17 @@ func NewMultiplayerJoinScreen(win *turdgl.Window) *MultiplayerJoinScreen {
SetLabelOffset(turdgl.Vec{X: 0, Y: 32}).SetLabelText("Back")

s := MultiplayerJoinScreen{
win: win,
title: title,
buttons: []*common.MenuButton{ipHeading, nameHeading, join, back},
entries: []*common.EntryBox{nameEntry, ipEntry},
hostReady: false,
client: turdserve.NewClient(),
win: win,
title: title,
buttons: []*common.MenuButton{ipHeading, nameHeading, join, back},
entries: []*common.EntryBox{nameEntry, ipEntry},
hostIsReady: make(chan bool),
client: turdserve.NewClient(),
}

back.SetCallback(func(_ turdgl.MouseState) {
join.SetLabelText("Join")
s.client.Destroy()
SetScreen(MultiplayerMenu, nil)
})

Expand All @@ -74,13 +74,9 @@ func NewMultiplayerJoinScreen(win *turdgl.Window) *MultiplayerJoinScreen {
join.SetCallback(func(_ turdgl.MouseState) {})

go func() {
for {
// Pass client to next screen
if s.hostReady {
SetScreen(Multiplayer, InitData{clientKey: s.client})
return
}
time.Sleep(250 & time.Millisecond)
if <-s.hostIsReady {
SetScreen(Multiplayer, InitData{clientKey: s.client})
return
}
}()
})
Expand Down Expand Up @@ -127,8 +123,17 @@ func (s *MultiplayerJoinScreen) joinGame() error {
// Connect using the user-specified IP address
ipEntry := s.entries[1]
ip := ipEntry.Text.Text()
if err := s.client.Connect(ip, serverPort); err != nil {
return fmt.Errorf("failed to connect to server: %w", err)
errCh := make(chan error)
go func() {
for err := range errCh {
if err != nil {
fmt.Println(fmt.Errorf("client error: %w", err).Error())
s.client.Destroy()
}
}
}()
if err := s.client.Connect(ip, serverPort, errCh); err != nil {
panic(err)
}

s.client.SetCallback(func(b []byte) {
Expand Down Expand Up @@ -188,6 +193,6 @@ func (s *MultiplayerJoinScreen) handleServerData(b []byte) error {
// handleEventData handles incoming player data.
func (s *MultiplayerJoinScreen) handleEventData(data comms.EventData) {
if data.Event == comms.EventHostStartGame {
s.hostReady = true
s.hostIsReady <- true
}
}
2 changes: 1 addition & 1 deletion screen/singleplayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type SingleplayerScreen struct {

backend *backend.Game
arena *common.Arena
arenaInputCh chan (func())
arenaInputCh chan func()

debugGridText *turdgl.Text
debugTimeText *turdgl.Text
Expand Down

0 comments on commit 020e25d

Please sign in to comment.