goonserver/main.go

212 lines
5.0 KiB
Go
Raw Normal View History

2024-10-10 10:15:52 +02:00
package main
import (
2025-01-15 10:51:43 +01:00
"bufio"
"encoding/binary"
2024-10-10 10:15:52 +02:00
"fmt"
2025-01-15 10:51:43 +01:00
"io"
2024-10-10 10:15:52 +02:00
"log"
"net"
"sync"
2024-10-10 10:15:52 +02:00
"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 {
sync.Mutex
2024-10-10 10:15:52 +02:00
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
mu sync.RWMutex // Add mutex for protecting shared maps
2025-01-13 13:23:05 +01:00
chatHistory = make([]*pb.ChatMessage, 0, 100)
chatMutex sync.RWMutex
2024-12-13 20:32:27 +01:00
)
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()
mu.Lock()
2024-10-10 10:15:52 +02:00
playerID := len(players) + 1
newPlayer := &Player{ID: playerID, X: 5, Y: 5}
players[playerID] = newPlayer
playerConns[playerID] = conn
mu.Unlock()
2024-10-10 10:15:52 +02:00
fmt.Printf("Player %d connected\n", playerID)
// Send player ID to the client
serverMsg := &pb.ServerMessage{
PlayerId: int32(playerID),
CurrentTick: 0,
}
2025-01-15 10:51:43 +01:00
if err := writeMessage(conn, serverMsg); err != nil {
log.Printf("Failed to send player ID to player %d: %v", playerID, err)
return
}
// Listen for incoming actions from this player
2025-01-15 10:51:43 +01:00
reader := bufio.NewReader(conn)
2024-10-10 10:15:52 +02:00
for {
2025-01-15 10:51:43 +01:00
// Read message length
lengthBuf := make([]byte, 4)
if _, err := io.ReadFull(reader, lengthBuf); err != nil {
log.Printf("Error reading message length from player %d: %v", playerID, err)
delete(players, playerID)
delete(playerConns, playerID)
delete(actionQueue, playerID)
return
}
messageLength := binary.BigEndian.Uint32(lengthBuf)
// Read message body
messageBuf := make([]byte, messageLength)
if _, err := io.ReadFull(reader, messageBuf); err != nil {
log.Printf("Error reading message from player %d: %v", playerID, err)
2024-10-10 10:15:52 +02:00
delete(players, playerID)
delete(playerConns, playerID)
delete(actionQueue, playerID)
2024-10-10 10:15:52 +02:00
return
}
batch := &pb.ActionBatch{}
2025-01-15 10:51:43 +01:00
if err := proto.Unmarshal(messageBuf, batch); err != nil {
log.Printf("Failed to unmarshal action batch for player %d: %v", playerID, err)
2024-10-10 10:15:52 +02:00
continue
}
// Queue the actions for processing
if batch.PlayerId == int32(playerID) {
actionQueue[playerID] = append(actionQueue[playerID], batch.Actions...)
}
2024-10-10 10:15:52 +02:00
}
}
2025-01-13 13:23:05 +01:00
func addChatMessage(playerID int32, content string) {
chatMutex.Lock()
defer chatMutex.Unlock()
msg := &pb.ChatMessage{
PlayerId: playerID,
Content: content,
Timestamp: time.Now().UnixNano(),
}
if len(chatHistory) >= 100 {
chatHistory = chatHistory[1:]
}
chatHistory = append(chatHistory, msg)
}
2024-10-10 10:15:52 +02:00
func processActions() {
mu.Lock()
defer mu.Unlock()
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]
player.Lock()
2024-10-10 10:15:52 +02:00
for _, action := range actions {
2025-01-13 13:23:05 +01:00
switch action.Type {
case pb.Action_MOVE:
2024-10-10 10:15:52 +02:00
player.X = int(action.X)
player.Y = int(action.Y)
fmt.Printf("Player %d moved to (%d, %d)\n", playerID, player.X, player.Y)
2025-01-13 13:23:05 +01:00
case pb.Action_CHAT:
addChatMessage(int32(playerID), action.ChatMessage)
fmt.Printf("Player %d says: %s\n", playerID, action.ChatMessage)
2024-10-10 10:15:52 +02:00
}
}
player.Unlock()
2024-12-13 20:32:27 +01:00
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,
2025-01-15 10:36:36 +01:00
Players: make([]*pb.PlayerState, 0, len(players)),
}
2024-12-13 20:32:27 +01:00
2025-01-15 10:36:36 +01:00
// Convert players to PlayerState
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()
}
2024-12-13 20:32:27 +01:00
2025-01-15 10:36:36 +01:00
// Add new chat messages to the state
2025-01-13 13:23:05 +01:00
chatMutex.RLock()
2025-01-15 10:36:36 +01:00
state.ChatMessages = chatHistory[max(0, len(chatHistory)-5):] // Only send last 5 messages
2025-01-13 13:23:05 +01:00
chatMutex.RUnlock()
// Send to each connected player
for _, conn := range playerConns {
2025-01-15 10:51:43 +01:00
if err := writeMessage(conn, state); err != nil {
log.Printf("Failed to send update: %v", err)
2024-12-13 20:32:27 +01:00
}
2024-10-10 10:15:52 +02:00
}
}
2025-01-15 10:51:43 +01:00
// Helper function to write length-prefixed messages
func writeMessage(conn net.Conn, msg proto.Message) error {
data, err := proto.Marshal(msg)
if err != nil {
return err
}
// Write length prefix
lengthBuf := make([]byte, 4)
binary.BigEndian.PutUint32(lengthBuf, uint32(len(data)))
if _, err := conn.Write(lengthBuf); err != nil {
return err
}
// Write message body
_, err = conn.Write(data)
return err
}