Advertisement
Guest User

cancer

a guest
Jun 13th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. #!/bin/bash
  2. #------------------------------Global Variables-----------------------------------------
  3. MYSQL_USER=root #Your username for MySql
  4. MYSQL_PASSWORD=TFdPYkkdzaYtA34t #Your password for that user
  5. MYSQL_DATABASE=website #The MySql database you'd like to use for logging
  6. MYSQL_TABLE=logging #The Mysql table you'd like to use for logging
  7. MYSQL_SERVERID=1 #The ID you'd like to use for the server.
  8. LOGDIR="/home/factorio/factorio/factorio-current.log" #Directory to the log file for factorio
  9. TEMPDIR="/home/factorio/factorio/log.log" #Should eventually either be removed or recoded into an array or mysql table.
  10. IFS=$'\n' #Don't change this
  11. Verbose=true #If you want more verbose messages about what the script is doing leave this to true otherwise set to false
  12. #------------------------------Log parsing functions-----------------------------------------
  13. online_player() {
  14. PLAYER_DISCONNECT=($(grep "Disconnect notification for peer" "$LOGDIR" | cut -d "(" -f2 | cut -d ")" -f1 | sort -n)) #Gets all the players that logged off properly
  15. 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
  16. for players in ${PLAYER_DISCONNECT[*]}
  17. do
  18. 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\""
  19. if [ "$Verbose" = true ]
  20. then
  21. echo "players disconnect" "$players"
  22. fi
  23. sleep 0.001
  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 $MYSQL_TABLE SET Online = \"0\" WHERE PlayerID = \"$players\" AND ServerID = \"$MYSQL_SERVERID\""
  28. if [ "$Verbose" = true ]
  29. then
  30. echo "players left" "$players"
  31. fi
  32. sleep 0.001
  33. done
  34. 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\""))
  35. CURRENT_TIME=$(date "+%m/%d/%y|%H:%M:%S") #Gets the current time
  36. 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\""
  37. echo "Updating last joined time for online players ${MYSQL_ARRAY[*]}"
  38. #Need a way to check for desyncs.
  39. }
  40.  
  41. log_players() {
  42. 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
  43. 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"))
  44. #Change PlayersID to null to avoid false positives
  45. 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\""
  46. for players in ${PLAYER_NAME[*]}
  47. do
  48. # These variables are just fancy grep work to get the data we need
  49. 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))
  50. PLAYER_IP=($(grep "\<$PLAYER_NAME_ID\> " "$TEMPDIR" | cut -d " " -f2 | cut -d ":" -f1))
  51. 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\")")
  52. # Check if player already has a row in the table. If true then update the table instead of create it.
  53. #echo "$PLAYER_NAME_ID"
  54. if [ $MYSQL_EXISTS -eq 1 ]
  55. then
  56. if [ "$Verbose" = true ]
  57. then
  58. echo "row for \"$players\" existed... updating row"
  59. fi
  60. 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\""
  61. else
  62. 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\")"
  63. if [ "$Verbose" = true ]
  64. then
  65. echo "Row for player \"$players\" didn't exist... creating it"
  66. fi
  67. fi
  68. sleep 0.001
  69. done
  70. online_player
  71. }
  72.  
  73. is_running() {
  74. pid=$(pgrep factorio)
  75. if [[ "${pid}" -gt 0 ]]; then
  76. return 0
  77. else
  78. return 1
  79. fi
  80. }
  81.  
  82. log_loop() {
  83. while true
  84. do
  85. log_players
  86. sleep 60
  87. done
  88. }
  89.  
  90.  
  91. #------------------------------Not sure yet-----------------------------------------
  92. case "$1" in
  93. start)
  94. log_loop
  95. ;;
  96. *)
  97. echo "No such command!"
  98. exit 1
  99. ;;
  100. esac
  101.  
  102. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement