commit d035a9cff456d8b0df65c837b0e9af2f3f15f561 Author: bdnugget Date: Thu Oct 10 10:15:52 2024 +0200 Initial commit diff --git a/.proto b/.proto new file mode 100644 index 0000000..c34001e --- /dev/null +++ b/.proto @@ -0,0 +1,13 @@ +syntax = "proto3"; + +package actions; + +message Action { + enum ActionType { + MOVE = 0; + } + + ActionType type = 1; + int32 x = 2; + int32 y = 3; +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..9e0fe78 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module gitea.boner.be/bdnugget/goonserver + +go 1.23.0 + +require google.golang.org/protobuf v1.35.1 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..00c144d --- /dev/null +++ b/go.sum @@ -0,0 +1,6 @@ +github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA= +google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= diff --git a/main.go b/main.go new file mode 100644 index 0000000..820ff3b --- /dev/null +++ b/main.go @@ -0,0 +1,93 @@ +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 + } +}