Use ticker like in client network.go to simplify logic
This commit is contained in:
parent
23474c19dc
commit
b73d8de851
16
main.go
16
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()
|
||||
|
Loading…
x
Reference in New Issue
Block a user