goonserver/main.go

404 lines
9.5 KiB
Go
Raw Permalink 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"
2025-01-19 21:17:07 +01:00
"gitea.boner.be/bdnugget/goonserver/db"
2024-10-10 10:15:52 +02:00
"google.golang.org/protobuf/proto"
)
const (
2025-01-19 21:52:37 +01:00
port = ":6969" // Port to listen on
tickRate = 600 * time.Millisecond
protoVersion = 1
2024-10-10 10:15:52 +02:00
)
type Player struct {
sync.Mutex
2025-01-19 21:52:37 +01:00
ID int
X, Y int
Username string
2024-10-10 10:15:52 +02:00
}
2024-12-13 20:32:27 +01:00
var (
2025-01-19 22:06:41 +01:00
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
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() {
2025-01-19 21:17:07 +01:00
if err := db.InitDB("goonserver.db"); err != nil {
log.Fatalf("Failed to initialize database: %v", err)
}
2024-10-10 10:15:52 +02:00
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)
// Create ticker for fixed game state updates
ticker := time.NewTicker(tickRate)
defer ticker.Stop()
2025-01-19 22:06:41 +01:00
// Start registration attempt cleanup goroutine
go func() {
ticker := time.NewTicker(time.Hour)
defer ticker.Stop()
for range ticker.C {
db.CleanupOldAttempts()
}
}()
// Handle incoming connections in a separate goroutine
2024-10-10 10:15:52 +02:00
go func() {
for {
conn, err := ln.Accept()
if err != nil {
log.Printf("Failed to accept connection: %v", err)
continue
}
go handleConnection(conn)
}
}()
// Main game loop
for range ticker.C {
processActions()
2024-10-10 10:15:52 +02:00
}
}
func handleConnection(conn net.Conn) {
defer conn.Close()
2025-01-19 22:06:41 +01:00
// Get client IP
remoteAddr := conn.RemoteAddr().String()
ip, _, err := net.SplitHostPort(remoteAddr)
if err != nil {
log.Printf("Failed to parse remote address: %v", err)
return
}
2025-01-19 21:17:07 +01:00
// Read initial message for player ID
reader := bufio.NewReader(conn)
// Wait for authentication
lengthBuf := make([]byte, 4)
if _, err := io.ReadFull(reader, lengthBuf); err != nil {
log.Printf("Failed to read auth message length: %v", err)
return
}
messageLength := binary.BigEndian.Uint32(lengthBuf)
messageBuf := make([]byte, messageLength)
if _, err := io.ReadFull(reader, messageBuf); err != nil {
log.Printf("Failed to read auth message: %v", err)
return
}
batch := &pb.ActionBatch{}
if err := proto.Unmarshal(messageBuf, batch); err != nil {
log.Printf("Failed to unmarshal auth message: %v", err)
return
}
if len(batch.Actions) == 0 {
log.Printf("No auth action received")
return
}
action := batch.Actions[0]
var playerID int
var authErr error
2025-01-19 21:52:37 +01:00
if batch.ProtocolVersion == 0 {
response := &pb.ServerMessage{
AuthSuccess: false,
ErrorMessage: "Client using outdated protocol (pre-versioning)",
ProtocolVersion: protoVersion,
}
writeMessage(conn, response)
return
}
if batch.ProtocolVersion < protoVersion {
response := &pb.ServerMessage{
AuthSuccess: false,
ErrorMessage: fmt.Sprintf("Client protocol version too old (client: %d, required: %d)", batch.ProtocolVersion, protoVersion),
ProtocolVersion: protoVersion,
}
writeMessage(conn, response)
return
}
2025-01-19 21:17:07 +01:00
switch action.Type {
case pb.Action_REGISTER:
2025-01-19 22:06:41 +01:00
if err := db.CheckRegistrationLimit(ip); err != nil {
response := &pb.ServerMessage{
AuthSuccess: false,
ErrorMessage: err.Error(),
}
writeMessage(conn, response)
return
}
2025-01-19 21:17:07 +01:00
playerID, authErr = db.RegisterPlayer(action.Username, action.Password)
case pb.Action_LOGIN:
playerID, authErr = db.AuthenticatePlayer(action.Username, action.Password)
default:
log.Printf("Invalid initial action type: %v", action.Type)
return
}
// Send auth response
response := &pb.ServerMessage{
PlayerId: int32(playerID),
AuthSuccess: authErr == nil,
}
if authErr != nil {
response.ErrorMessage = authErr.Error()
if err := writeMessage(conn, response); err != nil {
log.Printf("Failed to send auth response: %v", err)
}
return
}
// Load last known position
x, y, err := db.LoadPlayerState(playerID)
if err != nil {
log.Printf("Error loading state for player %d: %v", playerID, err)
x, y = 5, 5 // Default position
}
2025-01-19 21:52:37 +01:00
username, err := db.GetUsername(playerID)
if err != nil {
log.Printf("Error getting username for player %d: %v", playerID, err)
return
}
2025-01-19 21:17:07 +01:00
player := &Player{
2025-01-19 21:52:37 +01:00
ID: playerID,
X: x,
Y: y,
Username: username,
2025-01-19 21:17:07 +01:00
}
2025-01-19 21:52:37 +01:00
// Prevent multiple logins
mu.Lock()
2025-01-19 21:52:37 +01:00
for _, p := range players {
if p.Username == username {
mu.Unlock()
response := &pb.ServerMessage{
AuthSuccess: false,
ErrorMessage: "Account already logged in",
}
writeMessage(conn, response)
return
}
}
2025-01-19 21:17:07 +01:00
players[playerID] = player
playerConns[playerID] = conn
mu.Unlock()
2024-10-10 10:15:52 +02:00
2025-01-19 21:52:37 +01:00
// Send initial state with correct position
response = &pb.ServerMessage{
PlayerId: int32(playerID),
AuthSuccess: true,
Players: []*pb.PlayerState{{
PlayerId: int32(playerID),
X: int32(x),
Y: int32(y),
Username: username,
}},
ProtocolVersion: protoVersion,
}
2025-01-20 00:03:15 +01:00
addSystemMessage(fmt.Sprintf("%s connected", username))
2025-01-19 21:17:07 +01:00
// Ensure player state is saved on any kind of disconnect
defer func() {
if err := db.SavePlayerState(playerID, player.X, player.Y); err != nil {
log.Printf("Error saving state for player %d: %v", playerID, err)
}
2025-01-20 00:03:15 +01:00
addSystemMessage(fmt.Sprintf("%s disconnected", player.Username))
2025-01-19 21:17:07 +01:00
mu.Lock()
delete(players, playerID)
delete(playerConns, playerID)
delete(actionQueue, playerID)
mu.Unlock()
log.Printf("Player %d disconnected", playerID)
}()
// Send player ID to client
if err := writeMessage(conn, response); err != nil {
log.Printf("Failed to send player ID: %v", err)
return
}
2025-01-19 21:17:07 +01:00
fmt.Printf("Player %d connected\n", playerID)
// Listen for incoming actions from this player
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 {
2025-01-19 21:17:07 +01:00
if err == io.EOF {
log.Printf("Player %d disconnected gracefully", playerID)
} else {
log.Printf("Error reading message length from player %d: %v", playerID, err)
}
2025-01-15 10:51:43 +01:00
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
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) {
2025-01-18 22:39:43 +01:00
for _, action := range batch.Actions {
if action.Type == pb.Action_DISCONNECT {
2025-01-19 21:17:07 +01:00
log.Printf("Player %d requested disconnect", playerID)
2025-01-18 22:39:43 +01:00
return
}
}
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) {
2025-01-19 21:52:37 +01:00
player, exists := players[int(playerID)]
if !exists {
return
}
2025-01-13 13:23:05 +01:00
chatMutex.Lock()
defer chatMutex.Unlock()
msg := &pb.ChatMessage{
PlayerId: playerID,
2025-01-19 21:52:37 +01:00
Username: player.Username,
2025-01-13 13:23:05 +01:00
Content: content,
Timestamp: time.Now().UnixNano(),
2025-01-20 00:03:15 +01:00
}
if len(chatHistory) >= 100 {
chatHistory = chatHistory[1:]
}
chatHistory = append(chatHistory, msg)
}
func addSystemMessage(content string) {
chatMutex.Lock()
defer chatMutex.Unlock()
msg := &pb.ChatMessage{
PlayerId: 0, // System messages use ID 0
Username: "System",
Content: content,
Timestamp: time.Now().UnixNano(),
2025-01-13 13:23:05 +01:00
}
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),
2025-01-19 21:52:37 +01:00
Username: p.Username,
})
p.Unlock()
}
2024-12-13 20:32:27 +01:00
// Add 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
}