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