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

Last change on this file since 100 was 87, checked in by alloc, 10 years ago

v.32

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" \
66 -u "$XPATHBASE/@entityid" -v "$ENTITYID" \
67 -u "$XPATHBASE/@logins" -v "$LOGINS" \
[87]68 -u "$XPATHBASE/@lastIp" -v "$IP" \
[71]69 "$PLAYERSXML"
[17]70 fi
71}
72
73# Handle a player disconnect for logging/tracking
74# Params:
75# 1: Instance name
76# 2: Entity ID
77logPlayerDisconnect() {
[63]78 ENTITYID="$2"
[17]79
[67]80 createPlayerList
[17]81
82 XPATHBASE="/Players/Player[@entityid='$ENTITYID'][@online='true']"
83
[67]84 if [ -f $PLAYERSXML ]; then
[71]85 if [ ! -z $($XMLSTARLET sel -t -v "$XPATHBASE/@steamid" "$PLAYERSXML") ]; then
86 NICKNAME=$($XMLSTARLET sel -t -v "$XPATHBASE/@nick" "$PLAYERSXML")
87 STEAMID=$($XMLSTARLET sel -t -v "$XPATHBASE/@steamid" "$PLAYERSXML")
88 LOGINTIME=$($XMLSTARLET sel -t -v "$XPATHBASE/@lastlogin" "$PLAYERSXML")
89 PLAYTIME=$($XMLSTARLET sel -t -v "$XPATHBASE/@playtime" "$PLAYERSXML")
[17]90 NOW=$(date '+%s')
91 PLAYTIME=$(( PLAYTIME + NOW - LOGINTIME ))
92 $XMLSTARLET ed -L \
93 -u "$XPATHBASE/@playtime" -v "$PLAYTIME" \
94 -u "$XPATHBASE/@online" -v "false" \
[71]95 "$PLAYERSXML"
[17]96 fi
97 fi
98
[71]99 echo "$(timestamp) --- $ENTITYID $NICKNAME $STEAMID" >> "$PLAYERSLOG"
[17]100}
101
Note: See TracBrowser for help on using the repository browser.