Skip to content

Commit

Permalink
Merge pull request #537 from MusclePr/feature/use_REST_API_for_player…
Browse files Browse the repository at this point in the history
…_logging

The ShowPlayers and Broadcast commands prefer to use the REST API when it is enabled for player logging.
  • Loading branch information
thijsvanloef committed Apr 22, 2024
2 parents eadee03 + 63eb8d6 commit 2743c3f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 15 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ ENV HOME=/home/steam \
RCON_ENABLED=true \
RCON_PORT=25575 \
QUERY_PORT=27015 \
REST_API_PORT=8212 \
TZ=UTC \
SERVER_DESCRIPTION= \
BACKUP_ENABLED=true \
Expand Down
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"
}
}

23 changes: 16 additions & 7 deletions scripts/player_logging.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,30 @@ get_steamid(){

get_playername(){
local player_info="${1}"
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'
echo "${player_info}" | sed -E 's/,([0-9A-Z]+),[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
server_pid=$(pidof PalServer-Linux-Shipping)
if [ -n "${server_pid}" ]; then
# Player IDs are usally 9 or 10 digits however when a player joins for the first time for a given boot their ID is temporary 00000000 (8x zeros) while loading
# Player ID is also 00000000 (8x zeros) when in character creation
mapfile -t current_player_list < <( get_players_list | tail -n +2 | sed '/,00000000,[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]/d' | sort )
# Player IDs are usally 9 or 10 digits however when a player joins for the first time for a given boot their ID is temporary 00000000 (8x zeros or 32x zeros) while loading
# Player ID is also 00000000 (8x zeros or 32x zeros) when in character creation
mapfile -t current_player_list < <( get_players_list | tail -n +2 | sed -E '/,(0{8}|0{32}),[0-9]+/d' | sort )

# If there are current players then some may have joined
if [ "${#current_player_list[@]}" -gt 0 ]; then
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 2743c3f

Please sign in to comment.