Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #------------------------------Global Variables-----------------------------------------
- MYSQL_USER=root #Your username for MySql
- MYSQL_PASSWORD= #Your password for that user
- MYSQL_DATABASE=factorio #The MySql database you'd like to use for logging
- MYSQL_TABLE=logging #The table you'd like MySQL to use for logging
- PLAYER_NAME=$(grep "Received peer info for peer" /home/factorio/factorio/factorio-current.log | cut -d "(" -f3- | cut -d ")" -f1 | sed '/<server>/d' | sort -u)
- CURRENT_TIME=$(date "+%m/%d/%y|%H:%M:%S")
- PID=$(pgrep factorio)
- SAVEDIR="/home/factorio/factorio/saves"
- BINARYDIR="/home/factorio/factorio/bin/x64/factorio"
- #------------------------------Log parsing functions-----------------------------------------
- log_players() {
- for players in ${PLAYER_NAME[*]}
- do
- # These variables are just fancy grep work to get the data we need
- player_id_ip=($(grep "adding peer" /home/factorio/factorio/factorio-current.log | sed 's/^[^a]*a/a/' | grep "(true)$" | cut -d "(" -f2- | sed 's/)//g' | sed 's/address(//' | cut -d "s" -f1 | sort -u | tee /home/factorio/factorio/bans.log))
- player_name_id=($(grep "Received peer info for peer" /home/factorio/factorio/factorio-current.log | cut -d "r" -f6- | sort -u | grep "$players" | cut -d ")" -f1 | sed 's/(//g'))
- player_ip=($(grep -w /home/factorio/factorio/bans.log -e "$player_name_id" | cut -d " " -f2 | cut -d ":" -f1))
- MYSQL_EXISTS=$(mysql -D "$MYSQL_DATABASE" -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" -s -N -e "SELECT EXISTS(SELECT 1 FROM logging WHERE PlayerName = \"$players\")")
- # Check if player already has a row in the table. If true then update the table instead of create it.
- if [ $MYSQL_EXISTS -eq 1 ]
- then
- echo "IT EXISTS"
- mysql -D "$MYSQL_DATABASE" -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" -s -N -e "UPDATE logging SET LastJoined = \"$CURRENT_TIME\" WHERE PlayerName = \"$players\""
- else
- mysql -D "$MYSQL_DATABASE" -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" -s -N -e "INSERT INTO logging (PlayerName,IP,FirstJoined,LastJoined) VALUES (\"$players\", \"$player_ip\", \"$CURRENT_TIME\", \"$CURRENT_TIME\")"
- echo "Row for player \"$players\" didn't exist... creating it"
- fi
- sleep 1
- done
- }
- #------------------------------Factorio functions-----------------------------------------
- start_service() {
- refresh_save
- sudo -u factorio screen -dmS factorio "$BINARYDIR" --start-server ${SAVE_NAME} --latency-ms 200 --autosave-interval 10 --autosave-slots 20 --disallow-commands
- if [ $? -eq 0 ]; then
- #
- # Waiting for the server to start
- #
- seconds=0
- until is_running; do
- sleep 1
- seconds=$seconds+1
- if [[ $seconds -eq 10 ]]; then
- echo "Still not running, waiting a while longer..."
- fi
- if [[ $seconds -ge 30 ]]; then
- echo "Failed to start, please check logs for more information."
- exit 1
- fi
- done
- echo "Factorio is running."
- else
- echo "Failed to start, ensure SCREEN is installed"
- fi
- }
- stop_service() {
- echo "Shutting down server"
- kill -INT $PID
- sleep 0.5
- seconds=0
- while is_running; do
- sleep 1
- seconds=$seconds+1
- if [[ $seconds -eq 10 ]]; then
- echo "Still not shut down, waiting a while longer..."
- fi
- if [[ $seconds -ge 30 ]];
- then
- echo "Failed to shut down, killing the factorio screen!"
- exit 1
- fi
- done
- screen -X -S factorio quit
- echo "Factorio is now shut down."
- }
- #------------------------------Misc functions-----------------------------------------
- usage(){
- echo "Usage: $0 COMMAND"
- echo
- echo "Available commands:"
- echo -e " start \t\t Starts the server"
- echo -e " stop \t\t Stops the server"
- echo -e " restart \t\t Restarts the server"
- echo -e " banplayer \t\t Bans the player"
- }
- refresh_save(){
- SAVE_NAME=latestsave
- # Find the last modified save file
- lastsave=$(ls -t ${SAVEDIR}/*.zip | head -1)
- # If the last modified save is our own, keep using it
- if [ "${lastsave}" == "${SAVEDIR}/${SAVE_NAME}.zip" ]; then
- echo "using existing ${SAVE_NAME}.zip"
- return 0
- fi
- # Else we copy the latest save to our own save file
- echo "using refreshed save"
- cp ${lastsave} ${SAVEDIR}/${SAVE_NAME}.zip
- }
- is_running() {
- if [[ "${PID}" -gt 0 ]]; then
- return 0
- else
- return 1
- fi
- }
- case "$1" in
- start)
- # Starts the server
- if is_running; then
- echo "Server already running."
- else
- start_service
- fi
- ;;
- stop)
- # Stops the server
- if is_running; then
- stop_service
- else
- echo "No running server."
- fi
- ;;
- restart)
- # Restarts the server
- if is_running; then
- stop_service
- else
- echo "No running server, starting it..."
- start_service
- fi
- ;;
- help|--help|-h)
- usage
- ;;
- *)
- echo "No such command!"
- echo
- usage
- exit 1
- ;;
- esac
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement