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=TFdPYkkdzaYtA34t #Your password for that user
- MYSQL_DATABASE=website #The MySql database you'd like to use for logging
- MYSQL_TABLE=logging #The Mysql table you'd like to use for logging
- MYSQL_SERVERID=1 #The ID you'd like to use for the server.
- LOGDIR="/home/factorio/factorio/factorio-current.log" #Directory to the log file for factorio
- TEMPDIR="/home/factorio/factorio/log.log" #Should eventually either be removed or recoded into an array or mysql table.
- IFS=$'\n' #Don't change this
- Verbose=true #If you want more verbose messages about what the script is doing leave this to true otherwise set to false
- #------------------------------Log parsing functions-----------------------------------------
- online_player() {
- PLAYER_DISCONNECT=($(grep "Disconnect notification for peer" "$LOGDIR" | cut -d "(" -f2 | cut -d ")" -f1 | sort -n)) #Gets all the players that logged off properly
- PLAYER_LEFT=($(grep "removing peer" "$LOGDIR" | cut -d "(" -f3 | cut -d ")" -f1 | grep -o -w '\w\{1,3\}' | sort -u | sort -n)) #Gets all the players that either desync'd or killed the client
- for players in ${PLAYER_DISCONNECT[*]}
- do
- mysql -D "$MYSQL_DATABASE" -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" -s -N -e "UPDATE $MYSQL_TABLE SET Online = \"0\" WHERE PlayerID = \"$players\" AND ServerID = \"$MYSQL_SERVERID\""
- if [ "$Verbose" = true ]
- then
- echo "players disconnect" "$players"
- fi
- sleep 0.001
- done
- for players in ${PLAYER_LEFT[*]}
- do
- mysql -D "$MYSQL_DATABASE" -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" -s -N -e "UPDATE $MYSQL_TABLE SET Online = \"0\" WHERE PlayerID = \"$players\" AND ServerID = \"$MYSQL_SERVERID\""
- if [ "$Verbose" = true ]
- then
- echo "players left" "$players"
- fi
- sleep 0.001
- done
- MYSQL_ARRAY=($(mysql -D "$MYSQL_DATABASE" -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" -s -N -e "SELECT * FROM $MYSQL_TABLE WHERE Online = \"1\" AND ServerID = \"$MYSQL_SERVERID\""))
- CURRENT_TIME=$(date "+%m/%d/%y|%H:%M:%S") #Gets the current time
- mysql -D "$MYSQL_DATABASE" -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" -s -N -e "UPDATE $MYSQL_TABLE SET LastJoined = \"$CURRENT_TIME\" WHERE Online = \"1\" AND ServerID = \"$MYSQL_SERVERID\""
- echo "Updating last joined time for online players ${MYSQL_ARRAY[*]}"
- #Need a way to check for desyncs.
- }
- log_players() {
- PLAYER_NAME=$(grep "Received peer info for peer" "$LOGDIR" | cut -d "(" -f3- | cut -d ")" -f1 | sed '/<server>/d' | sort -u) #Gets all players that have connected to this server in this session
- PLAYER_ID_IP=($(grep "adding peer" "$LOGDIR" | sed 's/^[^a]*a/a/' | grep "(true)$" | cut -d "(" -f2- | sed 's/)//g' | sed 's/address(//' | cut -d "s" -f1 | sort -u | tee "$TEMPDIR"))
- #Change PlayersID to null to avoid false positives
- mysql -D "$MYSQL_DATABASE" -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" -s -N -e "UPDATE $MYSQL_TABLE SET PlayerID = \"null\", Online = \"0\" WHERE ServerID = \"$MYSQL_SERVERID\""
- for players in ${PLAYER_NAME[*]}
- do
- # These variables are just fancy grep work to get the data we need
- PLAYER_NAME_ID=($(grep "Received peer info for peer" "$LOGDIR" | cut -d "r" -f6- | sort -u | grep "$players" | cut -d ")" -f1 | sed 's/(//g' | sort -n | tail -1))
- PLAYER_IP=($(grep -w "$TEMPDIR" -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 $MYSQL_TABLE WHERE PlayerName = \"$players\" AND ServerID = \"$MYSQL_SERVERID\")")
- # 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
- if [ "$Verbose" = true ]
- then
- echo "row for \"$players\" existed... updating row"
- fi
- mysql -D "$MYSQL_DATABASE" -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" -s -N -e "UPDATE $MYSQL_TABLE SET PlayerID = \"$PLAYER_NAME_ID\", Online = \"1\", IP = \"$PLAYER_IP\" WHERE PlayerName = \"$players\" AND ServerID = \"$MYSQL_SERVERID\""
- else
- mysql -D "$MYSQL_DATABASE" -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" -s -N -e "INSERT INTO $MYSQL_TABLE (ServerID,PlayerName,IP,FirstJoined,LastJoined,PlayerID,Online) VALUES (\"$MYSQL_SERVERID\", \"$players\", \"$PLAYER_IP\", \"$CURRENT_TIME\", \"$CURRENT_TIME\", \"$PLAYER_NAME_ID\", \"1\")"
- if [ "$Verbose" = true ]
- then
- echo "Row for player \"$players\" didn't exist... creating it"
- fi
- fi
- sleep 0.001
- done
- online_player
- }
- is_running() {
- pid=$(pgrep factorio)
- if [[ "${pid}" -gt 0 ]]; then
- return 0
- else
- return 1
- fi
- }
- log_loop() {
- while true
- do
- log_players
- sleep 60
- done
- }
- #------------------------------Not sure yet-----------------------------------------
- case "$1" in
- start)
- log_loop
- ;;
- *)
- echo "No such command!"
- exit 1
- ;;
- esac
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement