Use ticker like in client network.go to simplify logic

This commit is contained in:
bdnugget 2025-01-18 14:23:04 +01:00
parent 23474c19dc
commit b73d8de851

16
main.go
View File

@ -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()