check_players_online.sh 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/bin/bash
  2. # LinuxGSM check_players_online.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: https://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Checks if players are on the server.
  7. moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  8. playersonline=""
  9. playerscounted=""
  10. playercountunavailable=""
  11. if [ "${stoponlyifnoplayers}" == "on" ] || [ "${playercheckforce}" == "1" ]; then
  12. if [ "${querymode}" != "2" ] && [ "${querymode}" != "3" ]; then
  13. playercountunavailable="querymode ${querymode} cannot count players"
  14. elif [ ! -f "${lgsmdir}/node_modules/gamedig/bin/gamedig.js" ] && [ ! "$(command -v gamedig 2> /dev/null)" ]; then
  15. playercountunavailable="gamedig is not installed"
  16. elif [ ! "$(command -v jq 2> /dev/null)" ]; then
  17. playercountunavailable="jq is not installed"
  18. else
  19. for queryip in "${queryips[@]}"; do
  20. query_gamedig.sh
  21. if [ "${querystatus}" == "0" ]; then
  22. playerscounted="1"
  23. if [ -n "${gdplayers}" ] && [ "${gdplayers}" -ne 0 ]; then
  24. playersonline="${gdplayers}"
  25. break
  26. fi
  27. fi
  28. done
  29. if [ "${playerscounted}" != "1" ]; then
  30. playercountunavailable="query failed"
  31. fi
  32. fi
  33. # The player count could not be determined. Fail open (proceed as if empty) rather than
  34. # block the server indefinitely, but warn clearly since the check did not actually run.
  35. if [ -n "${playercountunavailable}" ] && [ "${playercheckforce}" != "1" ]; then
  36. fn_print_warn_nl "Unable to determine player count (${playercountunavailable}): stoponlyifnoplayers check skipped"
  37. fn_script_log_warn "Unable to determine player count (${playercountunavailable}): stoponlyifnoplayers check skipped"
  38. fi
  39. fi