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

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

Version 6: Instance editing more userfriendly

File size: 2.8 KB
Line 
1#!/bin/bash
2
3timestamp() {
4 date '+%Y.%m.%d %H:%M:%S'
5}
6
7# Create empty player list if not existing
8# Params:
9# 1: Instance name
10createPlayerList() {
11 PLAYERLIST=$(getInstancePath $1)/players.xml
12 if [ ! -f $PLAYERLIST ]; then
13 echo "<Players/>" > $PLAYERLIST
14 fi
15}
16
17# Set all players for an instance to offline (on startup/shutdown)
18# Params:
19# 1: Instance name
20setAllPlayersOffline() {
21 PLAYERLIST=$(getInstancePath $1)/players.xml
22 createPlayerList "$1"
23 $XMLSTARLET ed -L \
24 -u "/Players/Player/@online" -v "false" \
25 $PLAYERLIST
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 PLAYERLOG=$(getInstancePath $1)/players.log
36 PLAYERLIST=$(getInstancePath $1)/players.xml
37 ENTITYID=$2
38 STEAMID=$3
39 NICKNAME=$4
40
41 echo "$(timestamp) +++ $ENTITYID $NICKNAME $STEAMID" >> $PLAYERLOG
42
43 createPlayerList "$1"
44
45 XPATHBASE="/Players/Player[@steamid='$STEAMID']"
46
47 if [ -z $($XMLSTARLET sel -t -v "$XPATHBASE/@steamid" $PLAYERLIST) ]; then
48 $XMLSTARLET ed -L \
49 -s "/Players" -t elem -n "Player" -v "" \
50 -i "/Players/Player[not(@steamid)]" -t attr -n "steamid" -v "$STEAMID" \
51 -i "$XPATHBASE" -t attr -n "nick" -v "$NICKNAME" \
52 -i "$XPATHBASE" -t attr -n "playtime" -v "0" \
53 -i "$XPATHBASE" -t attr -n "logins" -v "1" \
54 -i "$XPATHBASE" -t attr -n "lastlogin" -v "$(date '+%s')" \
55 -i "$XPATHBASE" -t attr -n "online" -v "true" \
56 -i "$XPATHBASE" -t attr -n "entityid" -v "$ENTITYID" \
57 $PLAYERLIST
58 else
59 LOGINS=$($XMLSTARLET sel -t -v "$XPATHBASE/@logins" $PLAYERLIST)
60 (( LOGINS++ ))
61 $XMLSTARLET ed -L \
62 -u "$XPATHBASE/@lastlogin" -v "$(date '+%s')" \
63 -u "$XPATHBASE/@online" -v "true" \
64 -u "$XPATHBASE/@entityid" -v "$ENTITYID" \
65 -u "$XPATHBASE/@logins" -v "$LOGINS" \
66 $PLAYERLIST
67 fi
68}
69
70# Handle a player disconnect for logging/tracking
71# Params:
72# 1: Instance name
73# 2: Entity ID
74logPlayerDisconnect() {
75 PLAYERLOG=$(getInstancePath $1)/players.log
76 PLAYERLIST=$(getInstancePath $1)/players.xml
77 ENTITYID=$2
78
79 createPlayerList "$1"
80
81 XPATHBASE="/Players/Player[@entityid='$ENTITYID'][@online='true']"
82
83 if [ -f $PLAYERLIST ]; then
84 if [ ! -z $($XMLSTARLET sel -t -v "$XPATHBASE/@steamid" $PLAYERLIST) ]; then
85 NICKNAME=$($XMLSTARLET sel -t -v "$XPATHBASE/@nick" $PLAYERLIST)
86 STEAMID=$($XMLSTARLET sel -t -v "$XPATHBASE/@steamid" $PLAYERLIST)
87 LOGINTIME=$($XMLSTARLET sel -t -v "$XPATHBASE/@lastlogin" $PLAYERLIST)
88 PLAYTIME=$($XMLSTARLET sel -t -v "$XPATHBASE/@playtime" $PLAYERLIST)
89 NOW=$(date '+%s')
90 PLAYTIME=$(( PLAYTIME + NOW - LOGINTIME ))
91 $XMLSTARLET ed -L \
92 -u "$XPATHBASE/@playtime" -v "$PLAYTIME" \
93 -u "$XPATHBASE/@online" -v "false" \
94 $PLAYERLIST
95 fi
96 fi
97
98 echo "$(timestamp) --- $ENTITYID $NICKNAME $STEAMID" >> $PLAYERLOG
99}
100
Note: See TracBrowser for help on using the repository browser.