package main import ( "fmt" "log" "net" "time" pb "path/to/your/protobuf/package" // Change this to your actual protobuf package path "google.golang.org/protobuf/proto" ) const ( port = ":5000" // Port to listen on tickRate = 600 * time.Millisecond ) type Player struct { ID int X, Y int // Position on the game grid } var players = make(map[int]*Player) var actionQueue = make(map[int][]*pb.Action) // Queue to store actions for each player func main() { ln, err := net.Listen("tcp", port) if err != nil { log.Fatalf("Failed to listen on port %s: %v", port, err) } defer ln.Close() fmt.Printf("Server is listening on port %s\n", port) go func() { for { conn, err := ln.Accept() if err != nil { log.Printf("Failed to accept connection: %v", err) continue } go handleConnection(conn) } }() lastTick := time.Now() for { if time.Since(lastTick) >= tickRate { lastTick = time.Now() processActions() } } } func handleConnection(conn net.Conn) { defer conn.Close() playerID := len(players) + 1 players[playerID] = &Player{ID: playerID, X: 5, Y: 5} // Start at default position fmt.Printf("Player %d connected\n", playerID) buf := make([]byte, 4096) for { n, err := conn.Read(buf) if err != nil { log.Printf("Error reading from player %d: %v", playerID, err) delete(players, playerID) return } action := &pb.Action{} if err := proto.Unmarshal(buf[:n], action); err != nil { log.Printf("Failed to unmarshal action for player %d: %v", playerID, err) continue } actionQueue[playerID] = append(actionQueue[playerID], action) } } func processActions() { for playerID, actions := range actionQueue { player := players[playerID] for _, action := range actions { if action.Type == pb.Action_MOVE { player.X = int(action.X) player.Y = int(action.Y) fmt.Printf("Player %d moved to (%d, %d)\n", playerID, player.X, player.Y) } } actionQueue[playerID] = nil // Clear the queue after processing } }