Skip to content

Commit

Permalink
RCON cannot handle multibyte strings, so the ShowPlayers and Broadcas…
Browse files Browse the repository at this point in the history
…t commands prefer to use the REST API when it is enabled.
  • Loading branch information
MusclePr committed Apr 19, 2024
1 parent eadee03 commit 4f253dd
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
50 changes: 43 additions & 7 deletions scripts/helper_functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,31 @@ isExecutable() {
return "$return_val"
}

# Convert player list from JSON format
convert_JSON_to_CSV_players(){
echo 'name,playeruid,steamid'
echo -n "${1}" | \
jq -r '.players[] | [ .name, .playerId, .userId ] | @csv' | \
sed -re 's/"None"/"00000000000000000000000000000000"/' \
-re 's/"steam_/"/' \
-re 's/"//g'
}

# Lists players
# Outputs nothing if RCON is not enabled and returns 1
# Outputs player list if RCON is enabled and returns 0
# Outputs nothing if REST API or RCON is not enabled and returns 1
# Outputs player list if REST API or RCON is enabled and returns 0
get_players_list() {
local return_val=0
if [ "${RCON_ENABLED,,}" != true ]; then
return_val=1
fi
# Prefer REST API
if [ "${REST_API_ENABLED,,}" != true ]; then
if [ "${RCON_ENABLED,,}" != true ]; then
return_val=1
fi

RCON "ShowPlayers"
RCON "ShowPlayers"
return "$return_val"
fi
convert_JSON_to_CSV_players "$(REST_API players)"
return "$return_val"
}

Expand Down Expand Up @@ -155,6 +170,19 @@ DiscordMessage() {
fi
}

# REST API Call
REST_API(){
local DATA="${2}"
local URL="http://localhost:${REST_API_PORT}/v1/api/${1}"
local ACCEPT="Accept: application/json"
local USERPASS="admin:${ADMIN_PASSWORD}"
if [ "${DATA}" = "" ]; then
curl -s -L -X GET "${URL}" -H "${ACCEPT}" -u "${USERPASS}"
else
curl -s -L -X POST "${URL}" -H "${ACCEPT}" -u "${USERPASS}" --json "${DATA}"
fi
}

# RCON Call
RCON() {
local args="$1"
Expand All @@ -167,6 +195,13 @@ RCON() {
# Returns 1 if not able to broadcast
broadcast_command() {
local return_val=0
if [ "${REST_API_ENABLED,,}" = true ]; then
local json="{\"message\":\"${1}\"}"
if ! REST_API announce "${json}"; then
return_val=1
fi
return "$return_val"
fi
# Replaces spaces with underscore
local message="${1// /_}"
if [[ $TEXT = *[![:ascii:]]* ]]; then
Expand Down Expand Up @@ -283,4 +318,5 @@ get_latest_version() {
latest_version=$(curl https://api.github.com/repos/thijsvanloef/palworld-server-docker/releases/latest -s | jq .name -r)

echo "$latest_version"
}
}

15 changes: 12 additions & 3 deletions scripts/player_logging.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,19 @@ get_playername(){
echo "${player_info}" | sed -E 's/,([0-9]+),[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]//g'
}

# Wait until rcon port is open
while ! nc -z 127.0.0.1 "${RCON_PORT}"; do
# Prefer REST API
if [ "${REST_API_ENABLED,,}" = true ]; then
_PORT=${REST_API_PORT}
_LABEL="REST API"
else
_PORT=${RCON_PORT}
_LABEL="RCON"
fi

# Wait until rcon/rest-api port is open
while ! nc -z localhost "${_PORT}"; do
sleep 5
LogInfo "Waiting for RCON port to open to show player logging..."
LogInfo "Waiting for ${_LABEL}(${_PORT}) port to open to show player logging..."
done

while true; do
Expand Down
2 changes: 1 addition & 1 deletion scripts/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ default:
password: "${ADMIN_PASSWORD}"
EOL

if [ "${ENABLE_PLAYER_LOGGING,,}" = true ] && [[ "${PLAYER_LOGGING_POLL_PERIOD}" =~ ^[0-9]+$ ]] && [ "${RCON_ENABLED,,}" = true ]; then
if [ "${ENABLE_PLAYER_LOGGING,,}" = true ] && [[ "${PLAYER_LOGGING_POLL_PERIOD}" =~ ^[0-9]+$ ]] && ( [ "${REST_API_ENABLED,,}" = true ] || [ "${RCON_ENABLED,,}" = true ] ); then
if [[ "$(id -u)" -eq 0 ]]; then
su steam -c /home/steam/server/player_logging.sh &
else
Expand Down

0 comments on commit 4f253dd

Please sign in to comment.