Advertisement
Guest User

Untitled

a guest
Mar 27th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.94 KB | None | 0 0
  1. #!/bin/bash
  2. #------------------------------Global Variables-----------------------------------------
  3. MYSQL_USER=root #Your username for MySql
  4. MYSQL_PASSWORD= #Your password for that user
  5. MYSQL_DATABASE=factorio #The MySql database you'd like to use for logging
  6. SAVEDIR="/home/factorio/factorio/saves" #Directory the saves for factorio are located in
  7. BINARYDIR="/home/factorio/factorio/bin/x64/factorio" #Directory the binary for factorio is located in
  8. AUTOSAVE_TIME=10 #How much time in minutes pass for each autosave
  9. AUTOSAVE_SLOTS=20 #How many autosaves you want to be on the server at one time
  10. LATENCY_MS=200 #The delay you want factorio to run at in MS
  11. CURRENT_TIME=$(date "+%m/%d/%y|%H:%M:%S") #Gets the current time
  12. PID=$(pgrep factorio) #Runs a pgrep to get the PID of the binary
  13. 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
  14. IFS=$'\n'
  15. #------------------------------Log parsing functions-----------------------------------------
  16. online_player() {
  17. PLAYER_DISCONNECT=($(grep "Disconnect notification for peer" /home/factorio/factorio/factorio-current.log | cut -d "(" -f2 | cut -d ")" -f1 | sort -n))
  18. 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 | sort -n))
  19.     for players in ${PLAYER_DISCONNECT[*]}
  20.     do
  21.         mysql -D "$MYSQL_DATABASE" -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" -s -N -e "UPDATE logging SET Online = \"0\" WHERE PlayerID = \"$players\""
  22.         echo "players disconnect" "$players"
  23.         sleep 0.1
  24.     done
  25.     for players in ${PLAYER_LEFT[*]}
  26.     do
  27.         mysql -D "$MYSQL_DATABASE" -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" -s -N -e "UPDATE logging SET Online = \"0\" WHERE PlayerID = \"$players\""
  28.         echo "players left" "$players"
  29.         sleep 0.1
  30.     done
  31. }
  32.  
  33. log_players() {
  34.     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))
  35.     #Change PlayersID to null to avoid false positives
  36.     mysql -D "$MYSQL_DATABASE" -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" -s -N -e "UPDATE logging SET PlayerID = \"null\", Online = \"0\""
  37.     for players in ${PLAYER_NAME[*]}
  38.     do
  39.         # These variables are just fancy grep work to get the data we need
  40.         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' | sort -n | tail -1))
  41.         PLAYER_IP=($(grep -w /home/factorio/factorio/bans.log -e "$PLAYER_NAME_ID" | cut -d " " -f2 | cut -d ":" -f1))
  42.         MYSQL_EXISTS=$(mysql -D "$MYSQL_DATABASE" -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" -s -N -e "SELECT EXISTS(SELECT 1 FROM logging WHERE PlayerName = \"$players\")")
  43.         # Check if player already has a row in the table. If true then update the table instead of create it.
  44.         #echo "$PLAYER_NAME_ID"
  45.         if [ $MYSQL_EXISTS -eq 1 ]
  46.         then
  47.             echo "IT EXISTS"
  48.             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\""
  49.         else
  50.             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\")"
  51.             echo "Row for player \"$players\" didn't exist... creating it"
  52.         fi
  53.     sleep 0.1
  54.     done
  55.     online_player
  56. }
  57.  
  58.  
  59. #------------------------------Not sure yet-----------------------------------------
  60. case "$1" in
  61.     logplayers)
  62.         log_players
  63.     ;;
  64.     onlineplayers)
  65.         online_player
  66.     ;;
  67.     *)
  68.         echo "No such command!"
  69.         echo
  70.         usage
  71.         exit 1
  72.     ;;
  73. esac
  74.  
  75. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement