Advertisement
Guest User

Untitled

a guest
Mar 25th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 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. MYSQL_TABLE=logging #The table you'd like MySQL to use for logging
  7. 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)
  8. CURRENT_TIME=$(date "+%m/%d/%y|%H:%M:%S")
  9. PID=$(pgrep factorio)
  10. SAVEDIR="/home/factorio/factorio/saves"
  11. BINARYDIR="/home/factorio/factorio/bin/x64/factorio"
  12. #------------------------------Log parsing functions-----------------------------------------
  13. log_players() {
  14. for players in ${PLAYER_NAME[*]}
  15. do
  16. # These variables are just fancy grep work to get the data we need
  17. 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))
  18. 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'))
  19. player_ip=($(grep -w /home/factorio/factorio/bans.log -e "$player_name_id" | cut -d " " -f2 | cut -d ":" -f1))
  20. MYSQL_EXISTS=$(mysql -D "$MYSQL_DATABASE" -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" -s -N -e "SELECT EXISTS(SELECT 1 FROM logging WHERE PlayerName = \"$players\")")
  21. # Check if player already has a row in the table. If true then update the table instead of create it.
  22. if [ $MYSQL_EXISTS -eq 1 ]
  23. then
  24. echo "IT EXISTS"
  25. mysql -D "$MYSQL_DATABASE" -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" -s -N -e "UPDATE logging SET LastJoined = \"$CURRENT_TIME\" WHERE PlayerName = \"$players\""
  26. else
  27. 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\")"
  28. echo "Row for player \"$players\" didn't exist... creating it"
  29. fi
  30. sleep 1
  31. done
  32. }
  33. #------------------------------Factorio functions-----------------------------------------
  34. start_service() {
  35. refresh_save
  36. sudo -u factorio screen -dmS factorio "$BINARYDIR" --start-server ${SAVE_NAME} --latency-ms 200 --autosave-interval 10 --autosave-slots 20 --disallow-commands
  37. if [ $? -eq 0 ]; then
  38. #
  39. # Waiting for the server to start
  40. #
  41. seconds=0
  42. until is_running; do
  43. sleep 1
  44. seconds=$seconds+1
  45. if [[ $seconds -eq 10 ]]; then
  46. echo "Still not running, waiting a while longer..."
  47. fi
  48. if [[ $seconds -ge 30 ]]; then
  49. echo "Failed to start, please check logs for more information."
  50. exit 1
  51. fi
  52. done
  53. echo "Factorio is running."
  54. else
  55. echo "Failed to start, ensure SCREEN is installed"
  56. fi
  57. }
  58.  
  59. stop_service() {
  60. echo "Shutting down server"
  61. kill -INT $PID
  62. sleep 0.5
  63. seconds=0
  64. while is_running; do
  65. sleep 1
  66. seconds=$seconds+1
  67. if [[ $seconds -eq 10 ]]; then
  68. echo "Still not shut down, waiting a while longer..."
  69. fi
  70. if [[ $seconds -ge 30 ]];
  71. then
  72. echo "Failed to shut down, killing the factorio screen!"
  73. exit 1
  74. fi
  75. done
  76. screen -X -S factorio quit
  77. echo "Factorio is now shut down."
  78. }
  79.  
  80. #------------------------------Misc functions-----------------------------------------
  81. usage(){
  82. echo "Usage: $0 COMMAND"
  83. echo
  84. echo "Available commands:"
  85. echo -e " start \t\t Starts the server"
  86. echo -e " stop \t\t Stops the server"
  87. echo -e " restart \t\t Restarts the server"
  88. echo -e " banplayer \t\t Bans the player"
  89. }
  90.  
  91. refresh_save(){
  92. SAVE_NAME=latestsave
  93. # Find the last modified save file
  94. lastsave=$(ls -t ${SAVEDIR}/*.zip | head -1)
  95. # If the last modified save is our own, keep using it
  96. if [ "${lastsave}" == "${SAVEDIR}/${SAVE_NAME}.zip" ]; then
  97. echo "using existing ${SAVE_NAME}.zip"
  98. return 0
  99. fi
  100. # Else we copy the latest save to our own save file
  101. echo "using refreshed save"
  102. cp ${lastsave} ${SAVEDIR}/${SAVE_NAME}.zip
  103. }
  104.  
  105. is_running() {
  106. if [[ "${PID}" -gt 0 ]]; then
  107. return 0
  108. else
  109. return 1
  110. fi
  111. }
  112.  
  113. case "$1" in
  114. start)
  115. # Starts the server
  116. if is_running; then
  117. echo "Server already running."
  118. else
  119. start_service
  120. fi
  121. ;;
  122. stop)
  123. # Stops the server
  124. if is_running; then
  125. stop_service
  126. else
  127. echo "No running server."
  128. fi
  129. ;;
  130. restart)
  131. # Restarts the server
  132. if is_running; then
  133. stop_service
  134. else
  135. echo "No running server, starting it..."
  136. start_service
  137. fi
  138. ;;
  139. help|--help|-h)
  140. usage
  141. ;;
  142. *)
  143. echo "No such command!"
  144. echo
  145. usage
  146. exit 1
  147. ;;
  148. esac
  149.  
  150. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement