Menu with broken exit and settings

This commit is contained in:
2025-01-18 23:23:03 +01:00
parent d86cbe15a3
commit b96c7ada7a
6 changed files with 59 additions and 19 deletions

View File

@ -61,11 +61,26 @@ func HandleServerCommunication(conn net.Conn, playerID int32, player *types.Play
actionTicker := time.NewTicker(types.ClientTickRate)
defer actionTicker.Stop()
defer conn.Close()
defer close(player.QuitDone)
// Create a channel to signal when goroutines are done
done := make(chan struct{})
go func() {
for {
select {
case <-quitChan:
// Send disconnect message to server
disconnectMsg := &pb.ActionBatch{
PlayerId: playerID,
Actions: []*pb.Action{{
Type: pb.Action_DISCONNECT,
PlayerId: playerID,
}},
}
writeMessage(conn, disconnectMsg)
done <- struct{}{}
return
case <-actionTicker.C:
player.Lock()
@ -96,6 +111,9 @@ func HandleServerCommunication(conn net.Conn, playerID int32, player *types.Play
for {
select {
case <-quitChan:
<-done // Wait for action goroutine to finish
close(done)
time.Sleep(100 * time.Millisecond) // Give time for disconnect message to be sent
return
default:
// Read message length (4 bytes)