2024-10-10 10:15:52 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
2024-10-10 10:52:35 +02:00
|
|
|
pb "gitea.boner.be/bdnugget/goonserver/actions"
|
2024-10-10 10:15:52 +02:00
|
|
|
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2024-10-10 10:52:35 +02:00
|
|
|
port = ":6969" // Port to listen on
|
2024-10-10 10:15:52 +02:00
|
|
|
tickRate = 600 * time.Millisecond
|
|
|
|
)
|
|
|
|
|
|
|
|
type Player struct {
|
|
|
|
ID int
|
|
|
|
X, Y int // Position on the game grid
|
|
|
|
}
|
|
|
|
|
2024-12-13 20:32:27 +01:00
|
|
|
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
|
|
|
|
)
|
2024-10-10 10:15:52 +02:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
2024-10-11 14:24:34 +02:00
|
|
|
// Assign a new player ID and add the player to the game state
|
2024-10-10 10:15:52 +02:00
|
|
|
playerID := len(players) + 1
|
|
|
|
players[playerID] = &Player{ID: playerID, X: 5, Y: 5} // Start at default position
|
2024-12-13 20:32:27 +01:00
|
|
|
playerConns[playerID] = conn // Store the connection
|
2024-10-10 10:15:52 +02:00
|
|
|
fmt.Printf("Player %d connected\n", playerID)
|
|
|
|
|
2024-10-11 14:24:34 +02:00
|
|
|
// 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
|
2024-10-10 10:15:52 +02:00
|
|
|
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)
|
2024-12-13 20:32:27 +01:00
|
|
|
delete(playerConns, playerID) // Remove connection on error
|
2024-10-10 10:15:52 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-10-11 14:24:34 +02:00
|
|
|
// Queue the action for processing in the game loop
|
2024-10-10 10:15:52 +02:00
|
|
|
actionQueue[playerID] = append(actionQueue[playerID], action)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func processActions() {
|
2024-12-13 20:32:27 +01:00
|
|
|
// Update players based on queued actions
|
2024-10-10 10:15:52 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2024-12-13 20:32:27 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2024-10-10 10:15:52 +02:00
|
|
|
}
|
|
|
|
}
|