goonserver/main.go

160 lines
3.5 KiB
Go

package main
import (
"fmt"
"log"
"net"
"sync"
"time"
pb "gitea.boner.be/bdnugget/goonserver/actions"
"google.golang.org/protobuf/proto"
)
const (
port = ":6969" // Port to listen on
tickRate = 600 * time.Millisecond
)
type Player struct {
sync.Mutex
ID int
X, Y int // Position on the game grid
}
var (
players = make(map[int]*Player)
actionQueue = make(map[int][]*pb.Action) // Queue to store actions for each player
playerConns = make(map[int]net.Conn) // Map to store player connections
mu sync.RWMutex // Add mutex for protecting shared maps
)
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()
mu.Lock()
playerID := len(players) + 1
newPlayer := &Player{ID: playerID, X: 5, Y: 5}
players[playerID] = newPlayer
playerConns[playerID] = conn
mu.Unlock()
fmt.Printf("Player %d connected\n", playerID)
// Send player ID to the client
serverMsg := &pb.ServerMessage{
PlayerId: int32(playerID),
CurrentTick: 0,
}
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)
for {
n, err := conn.Read(buf)
if err != nil {
log.Printf("Error reading from player %d: %v", playerID, err)
delete(players, playerID)
delete(playerConns, playerID)
delete(actionQueue, playerID)
return
}
batch := &pb.ActionBatch{}
if err := proto.Unmarshal(buf[:n], batch); err != nil {
log.Printf("Failed to unmarshal action batch for player %d: %v", playerID, err)
continue
}
// Queue the actions for processing
if batch.PlayerId == int32(playerID) {
actionQueue[playerID] = append(actionQueue[playerID], batch.Actions...)
}
}
}
func processActions() {
mu.Lock()
defer mu.Unlock()
// Update players based on queued actions
for playerID, actions := range actionQueue {
player := players[playerID]
player.Lock()
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)
}
}
player.Unlock()
actionQueue[playerID] = nil // Clear the action queue after processing
}
// Prepare and broadcast the current game state
currentTick := time.Now().UnixNano() / int64(tickRate)
state := &pb.ServerMessage{
CurrentTick: currentTick,
}
for id, p := range players {
p.Lock()
state.Players = append(state.Players, &pb.PlayerState{
PlayerId: int32(id),
X: int32(p.X),
Y: int32(p.Y),
})
p.Unlock()
}
data, err := proto.Marshal(state)
if err != nil {
log.Printf("Failed to marshal game state: %v", err)
return
}
// Send to each connected player
for _, conn := range playerConns {
if _, err := conn.Write(data); err != nil {
log.Printf("Failed to send update: %v", err)
}
}
}