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

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

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

File size: 2.5 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() {
[67]12 if [ ! -f $PLAYERSXML ]; then
13 echo "<Players/>" > $PLAYERSXML
[17]14 fi
15}
16
17# Set all players for an instance to offline (on startup/shutdown)
18setAllPlayersOffline() {
[67]19 createPlayerList
[17]20 $XMLSTARLET ed -L \
21 -u "/Players/Player/@online" -v "false" \
[67]22 $PLAYERSXML
[17]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() {
[63]32 ENTITYID="$2"
33 STEAMID="$3"
34 NICKNAME="$4"
[17]35
[67]36 echo "$(timestamp) +++ $ENTITYID $NICKNAME $STEAMID" >> $PLAYERSLOG
[17]37
[67]38 createPlayerList
[17]39
40 XPATHBASE="/Players/Player[@steamid='$STEAMID']"
41
[67]42 if [ -z $($XMLSTARLET sel -t -v "$XPATHBASE/@steamid" $PLAYERSXML) ]; then
[17]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" \
[67]52 $PLAYERSXML
[17]53 else
[67]54 LOGINS=$($XMLSTARLET sel -t -v "$XPATHBASE/@logins" $PLAYERSXML)
[17]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" \
[67]61 $PLAYERSXML
[17]62 fi
63}
64
65# Handle a player disconnect for logging/tracking
66# Params:
67# 1: Instance name
68# 2: Entity ID
69logPlayerDisconnect() {
[63]70 ENTITYID="$2"
[17]71
[67]72 createPlayerList
[17]73
74 XPATHBASE="/Players/Player[@entityid='$ENTITYID'][@online='true']"
75
[67]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)
[17]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" \
[67]87 $PLAYERSXML
[17]88 fi
89 fi
90
[67]91 echo "$(timestamp) --- $ENTITYID $NICKNAME $STEAMID" >> $PLAYERSLOG
[17]92}
93
Note: See TracBrowser for help on using the repository browser.