diff --git a/main.go b/main.go index bb4ce58..0f8ff5e 100644 --- a/main.go +++ b/main.go @@ -43,6 +43,11 @@ func main() { defer ln.Close() fmt.Printf("Server is listening on port %s\n", port) + // Create ticker for fixed game state updates + ticker := time.NewTicker(tickRate) + defer ticker.Stop() + + // Handle incoming connections in a separate goroutine go func() { for { conn, err := ln.Accept() @@ -54,12 +59,9 @@ func main() { } }() - lastTick := time.Now() - for { - if time.Since(lastTick) >= tickRate { - lastTick = time.Now() - processActions() - } + // Main game loop + for range ticker.C { + processActions() } } @@ -178,7 +180,7 @@ func processActions() { p.Unlock() } - // Add new chat messages to the state + // Add chat messages to the state chatMutex.RLock() state.ChatMessages = chatHistory[max(0, len(chatHistory)-5):] // Only send last 5 messages chatMutex.RUnlock()