Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
xstar97 committed Mar 7, 2024
1 parent 3d1cc59 commit b270726
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 15 deletions.
120 changes: 109 additions & 11 deletions internal/config/server.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package config

import (
"fmt"
"strings"
"log"
"unicode"
"unicode/utf8"
)

type ServerInfo struct {
Expand All @@ -26,29 +27,112 @@ type Players struct {
func GetServerData(serverName string) (*ServerInfo, error) {
serverInfo := &ServerInfo{}

infoOutput, err := runRCONCommand(serverName, "info")
infoOutput, err := sendCommand(serverName, PALWORLD_RCON_COMMANDS.INFO)
if err != nil {
return nil, fmt.Errorf("error running 'rcon-cli info': %v", err)
log.Printf("Error running INFO: %v", err)
}
parseServerInfo(infoOutput, serverInfo)
log.Printf("infoOutput: %v", infoOutput)

// Parse server version and name
serverInfo.Version = ParseServerVersion(infoOutput)
serverInfo.Name = ParseServerName(infoOutput)

playersOutput, err := runRCONCommand(serverName, "showplayers")
playersOutput, err := sendCommand(serverName, PALWORLD_RCON_COMMANDS.SHOW_PLAYERS)
if err != nil {
return nil, fmt.Errorf("error running 'rcon-cli showplayers': %v", err)
log.Printf("Error running INFO: %v", err)
}
parsePlayerList(playersOutput, serverInfo)
// Parse player list
count, players := ParsePlayerList(playersOutput)
serverInfo.Players.Count = count
serverInfo.Players.List = players

return serverInfo, nil
}

func runRCONCommand(serverName string, command string) (string, error) {
func ParseServerVersion(input string) string {
parts := strings.Split(input, "[")
if len(parts) < 2 {
log.Println("Invalid input format in ParseServerVersion")
return "" // Invalid input format
}
version := strings.TrimSpace(strings.TrimSuffix(strings.Split(parts[1], "]")[0], " "))
return version
}
func ParseServerName(input string) string {
// Find the index of "]" and "\n"
start := strings.Index(input, "]")
end := strings.Index(input, "\n")

// Check if both markers are found
if start == -1 || end == -1 {
return "" // Invalid input format
}

// Extract the name between "]" and "\n"
name := strings.TrimSpace(input[start+1:end])

// Remove any null terminators from the name
name = strings.TrimRightFunc(name, func(r rune) bool {
return r == '\u0000'
})

// Remove any other non-printable characters from the name
name = strings.Map(func(r rune) rune {
if r < utf8.RuneSelf && !unicode.IsPrint(r) {
return -1
}
return r
}, name)

return name
}
func ParsePlayerList(input string) (int, []Player) {
var players []Player

lines := strings.Split(input, "\n")
count := 0
for i := 3; i < len(lines); i++ { // Adjusted the start index to skip the separator line
line := strings.TrimSpace(lines[i])
if line == "" {
continue // Skip empty lines
}
playerData := strings.Split(line, ",")
if len(playerData) != 3 {
log.Printf("Malformed player data in line %d: %s", i, line)
continue // Skip malformed player data
}
player := Player{
Name: strings.TrimSpace(playerData[0]),
PID: strings.TrimSpace(playerData[1]),
SID: strings.TrimSpace(playerData[2]),
}
players = append(players, player)
count++
}

return count, players
}

func sendCommand(serverName string, command string) (string, error) {
output, err := ExecuteShellCommand(CONFIG.CLI_ROOT, COMMANDS.CONFIG, CONFIG.CLI_CONFIG, COMMANDS.ENV, serverName, command)
if err != nil {
return "", fmt.Errorf("failed to run rcon-cli: %v", err)
return "", err // Return the error directly
}
return string(output), nil // Convert output to string before returning
}












/*
func parseServerInfo(output string, serverInfo *ServerInfo) {
lines := strings.Split(output, "\n")
for _, line := range lines {
Expand All @@ -61,7 +145,6 @@ func parseServerInfo(output string, serverInfo *ServerInfo) {
log.Printf("Extracted Version: %s, Server Name: %s\n", version, serverName)
serverInfo.Version = version
serverInfo.Name = serverName
log.Printf("ServerInfo after parsing: %+v\n", serverInfo)
break
}
}
Expand Down Expand Up @@ -94,4 +177,19 @@ func parsePlayerList(output string, serverInfo *ServerInfo) {
players.Count = len(players.List)
serverInfo.Players = players
log.Printf("Player list parsed. Total players: %d\n", players.Count)
}
}*/



/*
infoOutput, err := sendCommand(serverName)
if err != nil {
return nil, fmt.Errorf("error running: %v", err)
}
parseServerInfo(infoOutput, serverInfo)
playersOutput, err := sendCommand(serverName)
if err != nil {
return nil, fmt.Errorf("error running: %v", err)
}
parsePlayerList(playersOutput, serverInfo)*/
9 changes: 5 additions & 4 deletions internal/config/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,9 @@ func ExecuteShellCommand(command string, args ...string) ([]byte, error) {
// Capture the output of the command
output, err := cmd.CombinedOutput()
if err != nil {
return nil, err
log.Println("Error executing command:", err)
}

return output, nil
}

log.Println("output: ", string(output))
return output, nil // Always return output and nil error
}

0 comments on commit b270726

Please sign in to comment.