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

Last change on this file since 468 was 294, checked in by alloc, 8 years ago

Scripts 103

File size: 3.7 KB
Line 
1#!/bin/bash
2
3# Copyright 2016 Christian 'Alloc' Illy
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17
18PLAYERSXML=$(getInstancePath $1)/players.xml
19PLAYERSLOG=$(getInstancePath $1)/logs/$(date '+%Y-%m-%d_%H-%M-%S')_players.log
20
21timestamp() {
22 date '+%Y.%m.%d %H:%M:%S'
23}
24
25# Create empty player list if not existing
26createPlayerList() {
27 if [ ! -f "$PLAYERSXML" ]; then
28 echo "<Players/>" > $PLAYERSXML
29 fi
30 if [ -z "$(cat $PLAYERSXML)" ]; then
31 echo "<Players/>" > $PLAYERSXML
32 fi
33}
34
35# Set all players for an instance to offline (on startup/shutdown)
36setAllPlayersOffline() {
37 createPlayerList
38 $XMLSTARLET ed -L \
39 -u "/Players/Player/@online" -v "false" \
40 "$PLAYERSXML"
41 rm $(getInstancePath $INSTANCE)/logs/current_players.log
42 ln -s $PLAYERSLOG $(getInstancePath $INSTANCE)/logs/current_players.log
43}
44
45# Handle a player connect for logging/tracking
46# Params:
47# 1: Instance name
48# 2: Entity ID
49# 3: Steam ID
50# 4: Nick name
51# 5: IP
52# 6: Steam Owner ID
53logPlayerConnect() {
54 local ENTITYID="$2"
55 local NICKNAME="$3"
56 local STEAMID="$4"
57 local IP="$5"
58 local OWNERID="$6"
59
60 echo "$(timestamp) +++ $ENTITYID $NICKNAME $STEAMID $IP $OWNERID" >> "$PLAYERSLOG"
61
62 createPlayerList
63
64 XPATHBASE="/Players/Player[@steamid='$STEAMID']"
65
66 if [ -z $($XMLSTARLET sel -t -v "$XPATHBASE/@steamid" "$PLAYERSXML") ]; then
67 $XMLSTARLET ed -L \
68 -s "/Players" -t elem -n "Player" -v "" \
69 -i "/Players/Player[not(@steamid)]" -t attr -n "steamid" -v "$STEAMID" \
70 -i "$XPATHBASE" -t attr -n "nick" -v "$NICKNAME" \
71 -i "$XPATHBASE" -t attr -n "playtime" -v "0" \
72 -i "$XPATHBASE" -t attr -n "logins" -v "1" \
73 -i "$XPATHBASE" -t attr -n "lastlogin" -v "$(date '+%s')" \
74 -i "$XPATHBASE" -t attr -n "online" -v "true" \
75 -i "$XPATHBASE" -t attr -n "entityid" -v "$ENTITYID" \
76 -i "$XPATHBASE" -t attr -n "lastIp" -v "$IP" \
77 -i "$XPATHBASE" -t attr -n "steamOwner" -v "$OWNERID" \
78 "$PLAYERSXML"
79 else
80 LOGINS=$($XMLSTARLET sel -t -v "$XPATHBASE/@logins" "$PLAYERSXML")
81 (( LOGINS++ ))
82 $XMLSTARLET ed -L \
83 -u "$XPATHBASE/@lastlogin" -v "$(date '+%s')" \
84 -u "$XPATHBASE/@online" -v "true" \
85 -u "$XPATHBASE/@nick" -v "$NICKNAME" \
86 -u "$XPATHBASE/@entityid" -v "$ENTITYID" \
87 -u "$XPATHBASE/@logins" -v "$LOGINS" \
88 -u "$XPATHBASE/@lastIp" -v "$IP" \
89 -u "$XPATHBASE/@steamOwner" -v "$OWNERID" \
90 "$PLAYERSXML"
91 fi
92}
93
94# Handle a player disconnect for logging/tracking
95# Params:
96# 1: Instance name
97# 2: Entity ID
98logPlayerDisconnect() {
99 ENTITYID="$2"
100
101 createPlayerList
102
103 XPATHBASE="/Players/Player[@entityid='$ENTITYID'][@online='true']"
104
105 if [ -f $PLAYERSXML ]; then
106 if [ ! -z $($XMLSTARLET sel -t -v "$XPATHBASE/@steamid" "$PLAYERSXML") ]; then
107 NICKNAME=$($XMLSTARLET sel -t -v "$XPATHBASE/@nick" "$PLAYERSXML")
108 STEAMID=$($XMLSTARLET sel -t -v "$XPATHBASE/@steamid" "$PLAYERSXML")
109 LOGINTIME=$($XMLSTARLET sel -t -v "$XPATHBASE/@lastlogin" "$PLAYERSXML")
110 PLAYTIME=$($XMLSTARLET sel -t -v "$XPATHBASE/@playtime" "$PLAYERSXML")
111 NOW=$(date '+%s')
112 PLAYTIME=$(( PLAYTIME + NOW - LOGINTIME ))
113 $XMLSTARLET ed -L \
114 -u "$XPATHBASE/@playtime" -v "$PLAYTIME" \
115 -u "$XPATHBASE/@online" -v "false" \
116 "$PLAYERSXML"
117 fi
118 fi
119
120 echo "$(timestamp) --- $ENTITYID $NICKNAME $STEAMID" >> "$PLAYERSLOG"
121}
122
Note: See TracBrowser for help on using the repository browser.