fix racism condition / deadlock
This commit is contained in:
parent
c40e4ae7ac
commit
509bc8b20b
16
game/game.go
16
game/game.go
@ -18,7 +18,6 @@ type Game struct {
|
|||||||
Chat *Chat
|
Chat *Chat
|
||||||
MenuOpen bool
|
MenuOpen bool
|
||||||
QuitChan chan struct{} // Channel to signal shutdown
|
QuitChan chan struct{} // Channel to signal shutdown
|
||||||
QuitDone chan struct{} // New channel to signal when cleanup is complete
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func New() *Game {
|
func New() *Game {
|
||||||
@ -42,7 +41,6 @@ func New() *Game {
|
|||||||
},
|
},
|
||||||
Chat: NewChat(),
|
Chat: NewChat(),
|
||||||
QuitChan: make(chan struct{}),
|
QuitChan: make(chan struct{}),
|
||||||
QuitDone: make(chan struct{}),
|
|
||||||
}
|
}
|
||||||
game.Player.UserData = game
|
game.Player.UserData = game
|
||||||
game.Chat.userData = game
|
game.Chat.userData = game
|
||||||
@ -142,7 +140,7 @@ func (g *Game) DrawPlayer(player *types.Player, model rl.Model) {
|
|||||||
|
|
||||||
rl.DrawModel(model, playerPos, 16, rl.White)
|
rl.DrawModel(model, playerPos, 16, rl.White)
|
||||||
|
|
||||||
if player.FloatingMessage != nil && time.Now().Before(player.FloatingMessage.ExpireTime) {
|
if player.FloatingMessage != nil {
|
||||||
screenPos := rl.GetWorldToScreen(rl.Vector3{
|
screenPos := rl.GetWorldToScreen(rl.Vector3{
|
||||||
X: playerPos.X,
|
X: playerPos.X,
|
||||||
Y: playerPos.Y + 24.0,
|
Y: playerPos.Y + 24.0,
|
||||||
@ -150,8 +148,6 @@ func (g *Game) DrawPlayer(player *types.Player, model rl.Model) {
|
|||||||
}, g.Camera)
|
}, g.Camera)
|
||||||
|
|
||||||
player.FloatingMessage.ScreenPos = screenPos
|
player.FloatingMessage.ScreenPos = screenPos
|
||||||
} else if player.FloatingMessage != nil {
|
|
||||||
player.FloatingMessage = nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(player.TargetPath) > 0 {
|
if len(player.TargetPath) > 0 {
|
||||||
@ -293,9 +289,7 @@ func (g *Game) DrawMenu() {
|
|||||||
case "Settings":
|
case "Settings":
|
||||||
// TODO: Implement settings
|
// TODO: Implement settings
|
||||||
case "Exit Game":
|
case "Exit Game":
|
||||||
close(g.QuitChan)
|
g.Shutdown()
|
||||||
<-g.QuitDone
|
|
||||||
rl.CloseWindow()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -310,3 +304,9 @@ func (g *Game) DrawMenu() {
|
|||||||
buttonY += buttonSpacing
|
buttonY += buttonSpacing
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (g *Game) Shutdown() {
|
||||||
|
close(g.QuitChan)
|
||||||
|
<-g.Player.QuitDone
|
||||||
|
rl.CloseWindow()
|
||||||
|
}
|
||||||
|
@ -111,9 +111,18 @@ func HandleServerCommunication(conn net.Conn, playerID int32, player *types.Play
|
|||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-quitChan:
|
case <-quitChan:
|
||||||
<-done // Wait for action goroutine to finish
|
done := make(chan struct{})
|
||||||
close(done)
|
go func() {
|
||||||
time.Sleep(100 * time.Millisecond) // Give time for disconnect message to be sent
|
<-done
|
||||||
|
close(player.QuitDone)
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-done:
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
case <-time.After(1 * time.Second):
|
||||||
|
log.Println("Shutdown timed out")
|
||||||
|
}
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
// Read message length (4 bytes)
|
// Read message length (4 bytes)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user