source: scripts/usr/local/lib/7dtd/playerlog.sh@ 248

Last change on this file since 248 was 217, checked in by alloc, 10 years ago

scripts 70

File size: 2.9 KB
RevLine 
[17]1#!/bin/bash
2
[67]3PLAYERSXML=$(getInstancePath $1)/players.xml
4PLAYERSLOG=$(getInstancePath $1)/logs/$(date '+%Y-%m-%d_%H-%M-%S')_players.log
5
[17]6timestamp() {
7 date '+%Y.%m.%d %H:%M:%S'
8}
9
10# Create empty player list if not existing
11createPlayerList() {
[71]12 if [ ! -f "$PLAYERSXML" ]; then
[67]13 echo "<Players/>" > $PLAYERSXML
[17]14 fi
[71]15 if [ -z "$(cat $PLAYERSXML)" ]; then
16 echo "<Players/>" > $PLAYERSXML
17 fi
[17]18}
19
20# Set all players for an instance to offline (on startup/shutdown)
21setAllPlayersOffline() {
[67]22 createPlayerList
[17]23 $XMLSTARLET ed -L \
24 -u "/Players/Player/@online" -v "false" \
[71]25 "$PLAYERSXML"
[87]26 rm $(getInstancePath $INSTANCE)/logs/current_players.log
27 ln -s $PLAYERSLOG $(getInstancePath $INSTANCE)/logs/current_players.log
[17]28}
29
30# Handle a player connect for logging/tracking
31# Params:
32# 1: Instance name
33# 2: Entity ID
34# 3: Steam ID
35# 4: Nick name
36logPlayerConnect() {
[87]37 local ENTITYID="$2"
38 local NICKNAME="$3"
39 local STEAMID="$4"
40 local IP="$5"
[17]41
[87]42 echo "$(timestamp) +++ $ENTITYID $NICKNAME $STEAMID $IP" >> "$PLAYERSLOG"
[17]43
[67]44 createPlayerList
[17]45
46 XPATHBASE="/Players/Player[@steamid='$STEAMID']"
47
[71]48 if [ -z $($XMLSTARLET sel -t -v "$XPATHBASE/@steamid" "$PLAYERSXML") ]; then
[17]49 $XMLSTARLET ed -L \
50 -s "/Players" -t elem -n "Player" -v "" \
51 -i "/Players/Player[not(@steamid)]" -t attr -n "steamid" -v "$STEAMID" \
52 -i "$XPATHBASE" -t attr -n "nick" -v "$NICKNAME" \
53 -i "$XPATHBASE" -t attr -n "playtime" -v "0" \
54 -i "$XPATHBASE" -t attr -n "logins" -v "1" \
55 -i "$XPATHBASE" -t attr -n "lastlogin" -v "$(date '+%s')" \
56 -i "$XPATHBASE" -t attr -n "online" -v "true" \
57 -i "$XPATHBASE" -t attr -n "entityid" -v "$ENTITYID" \
[87]58 -i "$XPATHBASE" -t attr -n "lastIp" -v "$IP" \
[71]59 "$PLAYERSXML"
[17]60 else
[71]61 LOGINS=$($XMLSTARLET sel -t -v "$XPATHBASE/@logins" "$PLAYERSXML")
[17]62 (( LOGINS++ ))
63 $XMLSTARLET ed -L \
64 -u "$XPATHBASE/@lastlogin" -v "$(date '+%s')" \
65 -u "$XPATHBASE/@online" -v "true" \
[217]66 -u "$XPATHBASE/@nick" -v "$NICKNAME" \
[17]67 -u "$XPATHBASE/@entityid" -v "$ENTITYID" \
68 -u "$XPATHBASE/@logins" -v "$LOGINS" \
[87]69 -u "$XPATHBASE/@lastIp" -v "$IP" \
[71]70 "$PLAYERSXML"
[17]71 fi
72}
73
74# Handle a player disconnect for logging/tracking
75# Params:
76# 1: Instance name
77# 2: Entity ID
78logPlayerDisconnect() {
[63]79 ENTITYID="$2"
[17]80
[67]81 createPlayerList
[17]82
83 XPATHBASE="/Players/Player[@entityid='$ENTITYID'][@online='true']"
84
[67]85 if [ -f $PLAYERSXML ]; then
[71]86 if [ ! -z $($XMLSTARLET sel -t -v "$XPATHBASE/@steamid" "$PLAYERSXML") ]; then
87 NICKNAME=$($XMLSTARLET sel -t -v "$XPATHBASE/@nick" "$PLAYERSXML")
88 STEAMID=$($XMLSTARLET sel -t -v "$XPATHBASE/@steamid" "$PLAYERSXML")
89 LOGINTIME=$($XMLSTARLET sel -t -v "$XPATHBASE/@lastlogin" "$PLAYERSXML")
90 PLAYTIME=$($XMLSTARLET sel -t -v "$XPATHBASE/@playtime" "$PLAYERSXML")
[17]91 NOW=$(date '+%s')
92 PLAYTIME=$(( PLAYTIME + NOW - LOGINTIME ))
93 $XMLSTARLET ed -L \
94 -u "$XPATHBASE/@playtime" -v "$PLAYTIME" \
95 -u "$XPATHBASE/@online" -v "false" \
[71]96 "$PLAYERSXML"
[17]97 fi
98 fi
99
[71]100 echo "$(timestamp) --- $ENTITYID $NICKNAME $STEAMID" >> "$PLAYERSLOG"
[17]101}
102
Note: See TracBrowser for help on using the repository browser.