fix tick rates and action ques and stuff

This commit is contained in:
bdnugget 2025-01-13 09:59:37 +01:00
parent 8290131998
commit a459e8b4a5

42
main.go
View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"log" "log"
"net" "net"
"sync"
"time" "time"
pb "gitea.boner.be/bdnugget/goonserver/actions" pb "gitea.boner.be/bdnugget/goonserver/actions"
@ -17,6 +18,7 @@ const (
) )
type Player struct { type Player struct {
sync.Mutex
ID int ID int
X, Y int // Position on the game grid X, Y int // Position on the game grid
} }
@ -60,13 +62,15 @@ func handleConnection(conn net.Conn) {
// Assign a new player ID and add the player to the game state // Assign a new player ID and add the player to the game state
playerID := len(players) + 1 playerID := len(players) + 1
players[playerID] = &Player{ID: playerID, X: 5, Y: 5} // Start at default position newPlayer := &Player{ID: playerID, X: 5, Y: 5}
playerConns[playerID] = conn // Store the connection players[playerID] = newPlayer
playerConns[playerID] = conn
fmt.Printf("Player %d connected\n", playerID) fmt.Printf("Player %d connected\n", playerID)
// Send player ID to the client // Send player ID to the client
serverMsg := &pb.ServerMessage{ serverMsg := &pb.ServerMessage{
PlayerId: int32(playerID), PlayerId: int32(playerID),
CurrentTick: 0,
} }
data, err := proto.Marshal(serverMsg) data, err := proto.Marshal(serverMsg)
if err != nil { if err != nil {
@ -85,18 +89,20 @@ func handleConnection(conn net.Conn) {
if err != nil { if err != nil {
log.Printf("Error reading from player %d: %v", playerID, err) log.Printf("Error reading from player %d: %v", playerID, err)
delete(players, playerID) delete(players, playerID)
delete(playerConns, playerID) // Remove connection on error delete(playerConns, playerID)
return return
} }
action := &pb.Action{} batch := &pb.ActionBatch{}
if err := proto.Unmarshal(buf[:n], action); err != nil { if err := proto.Unmarshal(buf[:n], batch); err != nil {
log.Printf("Failed to unmarshal action for player %d: %v", playerID, err) log.Printf("Failed to unmarshal action batch for player %d: %v", playerID, err)
continue continue
} }
// Queue the action for processing in the game loop // Queue the actions for processing
actionQueue[playerID] = append(actionQueue[playerID], action) if batch.PlayerId == int32(playerID) {
actionQueue[playerID] = append(actionQueue[playerID], batch.Actions...)
}
} }
} }
@ -104,6 +110,7 @@ func processActions() {
// Update players based on queued actions // Update players based on queued actions
for playerID, actions := range actionQueue { for playerID, actions := range actionQueue {
player := players[playerID] player := players[playerID]
player.Lock()
for _, action := range actions { for _, action := range actions {
if action.Type == pb.Action_MOVE { if action.Type == pb.Action_MOVE {
player.X = int(action.X) player.X = int(action.X)
@ -111,31 +118,36 @@ func processActions() {
fmt.Printf("Player %d moved to (%d, %d)\n", playerID, player.X, player.Y) fmt.Printf("Player %d moved to (%d, %d)\n", playerID, player.X, player.Y)
} }
} }
player.Unlock()
actionQueue[playerID] = nil // Clear the action queue after processing actionQueue[playerID] = nil // Clear the action queue after processing
} }
// Prepare and broadcast the positions of all players // Prepare and broadcast the current game state
for playerID := range players { currentTick := time.Now().UnixNano() / int64(tickRate)
state := &pb.ServerMessage{} state := &pb.ServerMessage{
CurrentTick: currentTick,
}
for id, p := range players { for id, p := range players {
p.Lock()
state.Players = append(state.Players, &pb.PlayerState{ state.Players = append(state.Players, &pb.PlayerState{
PlayerId: int32(id), PlayerId: int32(id),
X: int32(p.X), X: int32(p.X),
Y: int32(p.Y), Y: int32(p.Y),
}) })
p.Unlock()
} }
data, err := proto.Marshal(state) data, err := proto.Marshal(state)
if err != nil { if err != nil {
log.Printf("Failed to marshal player state: %v", err) log.Printf("Failed to marshal game state: %v", err)
continue return
} }
// Send to each connected player // Send to each connected player
for _, conn := range playerConns { for _, conn := range playerConns {
if _, err := conn.Write(data); err != nil { if _, err := conn.Write(data); err != nil {
log.Printf("Failed to send update to player %d: %v", playerID, err) log.Printf("Failed to send update: %v", err)
}
} }
} }
} }