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

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

Version 4

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