Improve broadcast warning messages

This commit is contained in:
Ben Peddell 2016-05-16 17:38:37 +10:00
parent e9b2c9b21f
commit 053a938f56

View File

@ -896,49 +896,137 @@ doInstall() {
getCurrentVersion getCurrentVersion
} }
#
# Formats a warning message based on replacement strings
#
printWarnMessage(){
local msg
if [ -n "$msgWarnReason" ]; then
local reason
local msgtime
if [ "$3" == "minutes" ]; then
if [ -n "$msgTimeMinutes" ]; then
msgtime="${msgTimeMinutes//\{minutes\}/$4}"
else
msgtime="$4 minutes"
fi
else
if [ -n "$msgTimeSeconds" ]; then
msgtime="${msgTimeSeconds//\{seconds\}/$4}"
else
msgtime="$4 seconds"
fi
fi
msg="${msgWarnReason//\{time\}/$msgtime}"
if [ "$1" == "update" ]; then
if [ -n "$appupdate" ]; then
if [ -n "$modupdate" ]; then
if [ -n "$msgReasonUpdateAppMod" ]; then
reason="$msgReasonUpdateMod"
else
reason="an update to the game and an update to mod(s) {modnamesupdated}"
fi
else
if [ -n "$msgReasonUpdateApp" ]; then
reason="$msgReasonUpdateApp"
else
reason="an update to the game"
fi
fi
elif [ -n "$modupdate" ]; then
if [ -n "$msgReasonUpdateMod" ]; then
reason="$msgReasonUpdateMod"
else
reason="an update to mod(s) {modnamesupdated}"
fi
fi
elif [ -n "$shutdownreason" ]; then
reason="$shutdownreason"
elif [ "$1" == "restart" ]; then
if [ -n "$msgReasonRestart" ]; then
reason="$msgReasonRestart"
else
reason="a restart"
fi
else
if [ -n "$msgReasonShutdown" ]; then
reason="$msgReasonShutdown"
else
reason="maintenance"
fi
fi
reason="${reason//\{modnamesupdated\}/${modnamesupdated}}"
msg="${msg//\{reason\}/${reason}}"
printf "%s\n" "$msg"
else
if [ "$1" == "update" ]; then
if [ "$3" == "minutes" ]; then
if [ -n "$msgWarnUpdateMinutes" ]; then
msg="${msgWarnUpdateMinutes//%d/$4}"
else
msg="This ARK server will shutdown for an update in $4 minutes"
fi
else
if [ -n "$msgWarnUpdateSeconds" ]; then
msg="${msgWarnUpdateSeconds//%d/$4}"
else
msg="This ARK server will shutdown for an update in $4 seconds"
fi
fi
elif [ "$1" == "restart" ]; then
if [ "$3" == "minutes" ]; then
if [ -n "$msgWarnRestartMinutes" ]; then
msg="${msgWarnRestartMinutes//%d/$4}"
else
msg="This ARK server will shutdown for a restart in $4 minutes"
fi
else
if [ -n "$msgWarnRestartSeconds" ]; then
msg="${msgWarnRestartSeconds//%d/$4}"
else
msg="This ARK server will shutdown for a restart in $4 seconds"
fi
fi
else
if [ "$3" == "minutes" ]; then
if [ -n "$msgWarnShutdownMinutes" ]; then
msg="${msgWarnShutdownMinutes//%d/$4}"
else
msg="This ARK server will shutdown in $4 minutes"
fi
else
if [ -n "$msgWarnShutdownSeconds" ]; then
msg="${msgWarnShutdownSeconds//%d/$4}"
else
msg="This ARK server will shutdown in $4 seconds"
fi
fi
fi
fi
doBroadcastWithEcho "$msg"
}
# #
# Waits for a configurable number of minutes before updating the server # Waits for a configurable number of minutes before updating the server
# #
doWarn(){ doWarn(){
cd "$arkserverroot" cd "$arkserverroot"
local warnmsgmin (
local warnmsgsec update_cancelled(){
if [ -n "$msgUpdateCancelled" ]; then
msg="${msgUpdateCancelled//%s/$1}"
else
msg="Update cancelled by operator ($1)"
fi
doBroadcastWithEcho "${msg}"
}
if [ "$1" == "update" ]; then trap "update_cancelled 'Ctrl+C'" SIGINT
if [ -n "$msgWarnUpdateMinutes" ]; then trap "update_cancelled 'Terminated'" SIGTERM
warnmsgmin="$msgWarnUpdateMinutes" trap "update_cancelled 'Connection Closed'" SIGHUP
else trap "update_cancelled 'Quit'" SIGQUIT
warnmsgmin="This ARK server will shutdown for an update in %d minutes"
fi
if [ -n "$msgWarnUpdateSeconds" ]; then
warnmsgsec="$msgWarnUpdateSeconds"
else
warnmsgsec="This ARK server will shutdown for an update in %d seconds"
fi
elif [ "$1" == "restart" ]; then
if [ -n "$msgWarnRestartMinutes" ]; then
warnmsgmin="$msgWarnRestartMinutes"
else
warnmsgmin="This ARK server will shutdown for a restart in %d minutes"
fi
if [ -n "$msgWarnRestartSeconds" ]; then
warnmsgsec="$msgWarnRestartSeconds"
else
warnmsgsec="This ARK server will shutdown for a restart in %d seconds"
fi
else
if [ -n "$msgWarnShutdownMinutes" ]; then
warnmsgmin="$msgWarnShutdownMinutes"
else
warnmsgmin="This ARK server will shutdown in %d minutes"
fi
if [ -n "$msgWarnShutdownSeconds" ]; then
warnmsgsec="$msgWarnShutdownSeconds"
else
warnmsgsec="This ARK server will shutdown in %d seconds"
fi
fi
local pid=`getServerPID` local pid=`getServerPID`
local sleeppid local sleeppid
@ -956,16 +1044,15 @@ doWarn(){
echo "Server has stopped. Aborting $1" echo "Server has stopped. Aborting $1"
return 1 return 1
fi fi
if (( warnminutes > warninterval )); then if (( warnminutes >= warninterval )); then
sleep 1m & sleep 1m &
sleeppid=$! sleeppid=$!
warnmsg="$(printf "$warnmsgmin" "$warnminutes")" printWarnMessage "$1" "$2" "minutes" "$warnminutes"
doBroadcastWithEcho "$warnmsg"
for (( min = warnminutes - 1; min >= warninterval; min-- )); do for (( min = warnminutes - 1; min >= warninterval; min-- )); do
numplayers=$(numPlayersConnected) numplayers=$(numPlayersConnected)
echo "There are ${numplayers} players connected" echo "There are ${numplayers} players connected"
if (( (numplayers + 0) == 0 )); then if (( (numplayers + 0) == 0 )); then
echo "Nobody is connected. Shutting down immediately" doBroadcastWithEcho "Nobody is connected. Shutting down immediately"
return 0 return 0
fi fi
wait $sleeppid wait $sleeppid
@ -987,13 +1074,12 @@ doWarn(){
echo "Server has stopped. Aborting update" echo "Server has stopped. Aborting update"
return 1 return 1
fi fi
warnmsg="$(printf "$warnmsgsec" "$warnseconds")" printWarnMessage "$1" "$2" "seconds" "$warnseconds"
doBroadcastWithEcho "$warnmsg"
if (( warnseconds >= 20 )); then if (( warnseconds >= 20 )); then
numplayers=$(numPlayersConnected) numplayers=$(numPlayersConnected)
echo "There are ${numplayers} players connected" echo "There are ${numplayers} players connected"
if (( (numplayers + 0) == 0 )); then if (( (numplayers + 0) == 0 )); then
echo "Nobody is connected. Shutting down immediately" doBroadcastWithEcho "Nobody is connected. Shutting down immediately"
return 0 return 0
fi fi
fi fi
@ -1008,6 +1094,9 @@ doWarn(){
fi fi
return 0 return 0
)
return $?
} }
# #
@ -1313,17 +1402,49 @@ isModUpdateNeeded(){
return 1 return 1
} }
#
# Get the name of the specified mod
#
getModName(){
local modid=$1
local modsrcdir="$steamcmdroot/steamapps/workshop/content/$mod_appid/$modid"
if [ -n "${modsrcdirs[$modid]}" ]; then
modsrcdir="${modsrcdirs[$modid]}"
fi
modname="$(curl -s "http://steamcommunity.com/sharedfiles/filedetails/?id=${modid}" | sed -n 's|^.*<div class="workshopItemTitle">\([^<]*\)</div>.*|\1|p')"
if [ -n "$modname" ]; then
echo "$modname"
else
perl -e '
my $data;
{ local $/; $data = <STDIN>; }
my $mapnamelen = unpack("@0 L<", $data);
my $mapname = substr($data, 4, $mapnamelen - 1);
print $mapname
' <"${modsrcdir}/mod.info"
fi
}
# #
# Checks if any installed or requested mods need to be updated # Checks if any installed or requested mods need to be updated
# #
isAnyModUpdateNeeded(){ isAnyModUpdateNeeded(){
modnamesupdated=""
local ismodupdateneeded=1
for modid in $(getModIds); do for modid in $(getModIds); do
if isModUpdateNeeded $modid; then if isModUpdateNeeded $modid; then
return 0 ismodupdateneeded=0
if [ -n "$modnamesupdated" ]; then
modnamesupdated="${modnamesupdated}, "
fi
modnamesupdated="${modnamesupdated}$(getModName "$modid")"
fi fi
done done
return 1 return $ismodupdateneeded
} }
# #