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
- SAVEDIR="/home/factorio/factorio/saves" #Directory the saves for factorio are located in
- BINARYDIR="/home/factorio/factorio/bin/x64/factorio" #Directory the binary for factorio is located in
- AUTOSAVE_TIME=10 #How much time in minutes pass for each autosave
- AUTOSAVE_SLOTS=20 #How many autosaves you want to be on the server at one time
- LATENCY_MS=200 #The delay you want factorio to run at in MS
- CURRENT_TIME=$(date "+%m/%d/%y|%H:%M:%S") #Gets the current time
- PID=$(pgrep factorio) #Runs a pgrep to get the PID of the binary
- 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) #Gets all players that have connected to this server in this session
- #------------------------------Log parsing functions-----------------------------------------
- log_players() {
- #Change PlayersID to null to avoid false positives
- mysql -D "$MYSQL_DATABASE" -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" -s -N -e "UPDATE logging SET PlayerID = \"null\", Online = \"0\""
- 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.
- #echo "$PLAYER_NAME_ID"
- 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\", PlayerID = \"$PLAYER_NAME_ID\", Online = \"1\" WHERE PlayerName = \"$players\""
- else
- mysql -D "$MYSQL_DATABASE" -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" -s -N -e "INSERT INTO logging (PlayerName,IP,FirstJoined,LastJoined,PlayerID,Online) VALUES (\"$players\", \"$PLAYER_IP\", \"$CURRENT_TIME\", \"$CURRENT_TIME\", \"$PLAYER_NAME_ID\", \"1\")"
- echo "Row for player \"$players\" didn't exist... creating it"
- fi
- sleep 1
- done
- }
- online_player() {
- PLAYER_DISCONNECT=($(grep "Disconnect notification for peer" /home/factorio/factorio/factorio-current.log | cut -d "(" -f2 | cut -d ")" -f1))
- PLAYER_LEFT=($(grep "removing peer" /home/factorio/factorio/factorio-current.log | cut -d "(" -f3 | cut -d ")" -f1 | grep -o -w '\w\{1,3\}' | sort -u))
- # PLAYER_REJOIN=($(grep "adding peer" /home/factorio/factorio/factorio-current.log | cut -d "(" -f3 | cut -d ")" -f1 | sort -u))
- for players in ${PLAYER_DISCONNECT[*]}
- do
- mysql -D "$MYSQL_DATABASE" -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" -s -N -e "UPDATE logging SET Online = \"0\" WHERE PlayerID = \"$players\""
- echo "players disconnect" "$players"
- sleep 1
- done
- for players in ${PLAYER_LEFT[*]}
- do
- mysql -D "$MYSQL_DATABASE" -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" -s -N -e "UPDATE logging SET Online = \"0\" WHERE PlayerID = \"$players\""
- echo "players left" "$players"
- sleep 1
- done
- # for players in ${PLAYER_REJOIN[*]}
- # do
- # mysql -D "$MYSQL_DATABASE" -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" -s -N -e "UPDATE logging SET Online = \"1\" WHERE PlayerID = \"$players\""
- # echo "players rejoin" "$players"
- # sleep 1
- # done
- }
- #------------------------------Not sure yet-----------------------------------------
- case "$1" in
- logplayers)
- log_players
- ;;
- onlineplayers)
- online_player
- ;;
- *)
- echo "No such command!"
- echo
- usage
- exit 1
- ;;
- esac
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement