Send player ID and add more logging because this fucking sucks donkey assboner

This commit is contained in:
bdnugget 2024-10-11 14:24:34 +02:00
parent eba63c4902
commit 4bd5303cfd

18
main.go
View File

@ -47,6 +47,7 @@ func main() {
for { for {
if time.Since(lastTick) >= tickRate { if time.Since(lastTick) >= tickRate {
lastTick = time.Now() lastTick = time.Now()
// log.Printf("Last tick: %s", lastTick)
processActions() processActions()
} }
} }
@ -55,10 +56,26 @@ func main() {
func handleConnection(conn net.Conn) { func handleConnection(conn net.Conn) {
defer conn.Close() defer conn.Close()
// 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 players[playerID] = &Player{ID: playerID, X: 5, Y: 5} // Start at default position
fmt.Printf("Player %d connected\n", playerID) fmt.Printf("Player %d connected\n", playerID)
// Send player ID to the client
serverMsg := &pb.ServerMessage{
PlayerId: int32(playerID),
}
data, err := proto.Marshal(serverMsg)
if err != nil {
log.Printf("Failed to marshal ServerMessage for player %d: %v", playerID, err)
return
}
if _, err := conn.Write(data); err != nil {
log.Printf("Failed to send player ID to player %d: %v", playerID, err)
return
}
// Listen for incoming actions from this player
buf := make([]byte, 4096) buf := make([]byte, 4096)
for { for {
n, err := conn.Read(buf) n, err := conn.Read(buf)
@ -74,6 +91,7 @@ func handleConnection(conn net.Conn) {
continue continue
} }
// Queue the action for processing in the game loop
actionQueue[playerID] = append(actionQueue[playerID], action) actionQueue[playerID] = append(actionQueue[playerID], action)
} }
} }