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

Last change on this file since 74 was 71, checked in by alloc, 10 years ago

v.29: Another fix for player logging

File size: 2.6 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}
27
28# Handle a player connect for logging/tracking
29# Params:
30# 1: Instance name
31# 2: Entity ID
32# 3: Steam ID
33# 4: Nick name
34logPlayerConnect() {
35 ENTITYID="$2"
36 STEAMID="$3"
37 NICKNAME="$4"
38
39 echo "$(timestamp) +++ $ENTITYID $NICKNAME $STEAMID" >> "$PLAYERSLOG"
40
41 createPlayerList
42
43 XPATHBASE="/Players/Player[@steamid='$STEAMID']"
44
45 if [ -z $($XMLSTARLET sel -t -v "$XPATHBASE/@steamid" "$PLAYERSXML") ]; then
46 $XMLSTARLET ed -L \
47 -s "/Players" -t elem -n "Player" -v "" \
48 -i "/Players/Player[not(@steamid)]" -t attr -n "steamid" -v "$STEAMID" \
49 -i "$XPATHBASE" -t attr -n "nick" -v "$NICKNAME" \
50 -i "$XPATHBASE" -t attr -n "playtime" -v "0" \
51 -i "$XPATHBASE" -t attr -n "logins" -v "1" \
52 -i "$XPATHBASE" -t attr -n "lastlogin" -v "$(date '+%s')" \
53 -i "$XPATHBASE" -t attr -n "online" -v "true" \
54 -i "$XPATHBASE" -t attr -n "entityid" -v "$ENTITYID" \
55 "$PLAYERSXML"
56 else
57 LOGINS=$($XMLSTARLET sel -t -v "$XPATHBASE/@logins" "$PLAYERSXML")
58 (( LOGINS++ ))
59 $XMLSTARLET ed -L \
60 -u "$XPATHBASE/@lastlogin" -v "$(date '+%s')" \
61 -u "$XPATHBASE/@online" -v "true" \
62 -u "$XPATHBASE/@entityid" -v "$ENTITYID" \
63 -u "$XPATHBASE/@logins" -v "$LOGINS" \
64 "$PLAYERSXML"
65 fi
66}
67
68# Handle a player disconnect for logging/tracking
69# Params:
70# 1: Instance name
71# 2: Entity ID
72logPlayerDisconnect() {
73 ENTITYID="$2"
74
75 createPlayerList
76
77 XPATHBASE="/Players/Player[@entityid='$ENTITYID'][@online='true']"
78
79 if [ -f $PLAYERSXML ]; then
80 if [ ! -z $($XMLSTARLET sel -t -v "$XPATHBASE/@steamid" "$PLAYERSXML") ]; then
81 NICKNAME=$($XMLSTARLET sel -t -v "$XPATHBASE/@nick" "$PLAYERSXML")
82 STEAMID=$($XMLSTARLET sel -t -v "$XPATHBASE/@steamid" "$PLAYERSXML")
83 LOGINTIME=$($XMLSTARLET sel -t -v "$XPATHBASE/@lastlogin" "$PLAYERSXML")
84 PLAYTIME=$($XMLSTARLET sel -t -v "$XPATHBASE/@playtime" "$PLAYERSXML")
85 NOW=$(date '+%s')
86 PLAYTIME=$(( PLAYTIME + NOW - LOGINTIME ))
87 $XMLSTARLET ed -L \
88 -u "$XPATHBASE/@playtime" -v "$PLAYTIME" \
89 -u "$XPATHBASE/@online" -v "false" \
90 "$PLAYERSXML"
91 fi
92 fi
93
94 echo "$(timestamp) --- $ENTITYID $NICKNAME $STEAMID" >> "$PLAYERSLOG"
95}
96
Note: See TracBrowser for help on using the repository browser.