Initial commit
This commit is contained in:
commit
d035a9cff4
13
.proto
Normal file
13
.proto
Normal file
@ -0,0 +1,13 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package actions;
|
||||
|
||||
message Action {
|
||||
enum ActionType {
|
||||
MOVE = 0;
|
||||
}
|
||||
|
||||
ActionType type = 1;
|
||||
int32 x = 2;
|
||||
int32 y = 3;
|
||||
}
|
5
go.mod
Normal file
5
go.mod
Normal file
@ -0,0 +1,5 @@
|
||||
module gitea.boner.be/bdnugget/goonserver
|
||||
|
||||
go 1.23.0
|
||||
|
||||
require google.golang.org/protobuf v1.35.1
|
6
go.sum
Normal file
6
go.sum
Normal file
@ -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=
|
93
main.go
Normal file
93
main.go
Normal file
@ -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
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user