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

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

v.32

File size: 2.9 KB
Line 
1#!/bin/bash
2
3PLAYERSXML=$(getInstancePath $1)/players.xml
4PLAYERSLOG=$(getInstancePath $1)/logs/$(date '+%Y-%m-%d_%H-%M-%S')_players.log
5
6timestamp() {
7 date '+%Y.%m.%d %H:%M:%S'
8}
9
10# Create empty player list if not existing
11createPlayerList() {
12 if [ ! -f "$PLAYERSXML" ]; then
13 echo "<Players/>" > $PLAYERSXML
14 fi
15 if [ -z "$(cat $PLAYERSXML)" ]; then
16 echo "<Players/>" > $PLAYERSXML
17 fi
18}
19
20# Set all players for an instance to offline (on startup/shutdown)
21setAllPlayersOffline() {
22 createPlayerList
23 $XMLSTARLET ed -L \
24 -u "/Players/Player/@online" -v "false" \
25 "$PLAYERSXML"
26 rm $(getInstancePath $INSTANCE)/logs/current_players.log
27 ln -s $PLAYERSLOG $(getInstancePath $INSTANCE)/logs/current_players.log
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() {
37 local ENTITYID="$2"
38 local NICKNAME="$3"
39 local STEAMID="$4"
40 local IP="$5"
41
42 echo "$(timestamp) +++ $ENTITYID $NICKNAME $STEAMID $IP" >> "$PLAYERSLOG"
43
44 createPlayerList
45
46 XPATHBASE="/Players/Player[@steamid='$STEAMID']"
47
48 if [ -z $($XMLSTARLET sel -t -v "$XPATHBASE/@steamid" "$PLAYERSXML") ]; then
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" \
58 -i "$XPATHBASE" -t attr -n "lastIp" -v "$IP" \
59 "$PLAYERSXML"
60 else
61 LOGINS=$($XMLSTARLET sel -t -v "$XPATHBASE/@logins" "$PLAYERSXML")
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" \
68 -u "$XPATHBASE/@lastIp" -v "$IP" \
69 "$PLAYERSXML"
70 fi
71}
72
73# Handle a player disconnect for logging/tracking
74# Params:
75# 1: Instance name
76# 2: Entity ID
77logPlayerDisconnect() {
78 ENTITYID="$2"
79
80 createPlayerList
81
82 XPATHBASE="/Players/Player[@entityid='$ENTITYID'][@online='true']"
83
84 if [ -f $PLAYERSXML ]; then
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")
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" \
95 "$PLAYERSXML"
96 fi
97 fi
98
99 echo "$(timestamp) --- $ENTITYID $NICKNAME $STEAMID" >> "$PLAYERSLOG"
100}
101
Note: See TracBrowser for help on using the repository browser.