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

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

v25: Fixes #22, #23. Also adds new serverconfig options

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