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

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

scripts 70

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/@nick" -v "$NICKNAME" \
67 -u "$XPATHBASE/@entityid" -v "$ENTITYID" \
68 -u "$XPATHBASE/@logins" -v "$LOGINS" \
69 -u "$XPATHBASE/@lastIp" -v "$IP" \
70 "$PLAYERSXML"
71 fi
72}
73
74# Handle a player disconnect for logging/tracking
75# Params:
76# 1: Instance name
77# 2: Entity ID
78logPlayerDisconnect() {
79 ENTITYID="$2"
80
81 createPlayerList
82
83 XPATHBASE="/Players/Player[@entityid='$ENTITYID'][@online='true']"
84
85 if [ -f $PLAYERSXML ]; then
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")
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" \
96 "$PLAYERSXML"
97 fi
98 fi
99
100 echo "$(timestamp) --- $ENTITYID $NICKNAME $STEAMID" >> "$PLAYERSLOG"
101}
102
Note: See TracBrowser for help on using the repository browser.