goonserver/main.go
2024-12-13 20:32:27 +01:00

142 lines
3.3 KiB
Go

package main
import (
"fmt"
"log"
"net"
"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 {
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
)
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()
// Assign a new player ID and add the player to the game state
playerID := len(players) + 1
players[playerID] = &Player{ID: playerID, X: 5, Y: 5} // Start at default position
playerConns[playerID] = conn // Store the connection
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)
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) // Remove connection on error
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
}
// Queue the action for processing in the game loop
actionQueue[playerID] = append(actionQueue[playerID], action)
}
}
func processActions() {
// Update players based on queued actions
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 action queue after processing
}
// Prepare and broadcast the positions of all players
for playerID := range players {
state := &pb.ServerMessage{}
for id, p := range players {
state.Players = append(state.Players, &pb.PlayerState{
PlayerId: int32(id),
X: int32(p.X),
Y: int32(p.Y),
})
}
data, err := proto.Marshal(state)
if err != nil {
log.Printf("Failed to marshal player state: %v", err)
continue
}
// Send to each connected player
for _, conn := range playerConns {
if _, err := conn.Write(data); err != nil {
log.Printf("Failed to send update to player %d: %v", playerID, err)
}
}
}
}