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

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

v.29: Another fix for player logging

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