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

Last change on this file since 495 was 482, checked in by alloc, 12 months ago

Scripts

File size: 4.0 KB
RevLine 
[17]1#!/bin/bash
2
[258]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
[67]18PLAYERSXML=$(getInstancePath $1)/players.xml
19PLAYERSLOG=$(getInstancePath $1)/logs/$(date '+%Y-%m-%d_%H-%M-%S')_players.log
20
[17]21timestamp() {
22 date '+%Y.%m.%d %H:%M:%S'
23}
24
25# Create empty player list if not existing
26createPlayerList() {
[71]27 if [ ! -f "$PLAYERSXML" ]; then
[67]28 echo "<Players/>" > $PLAYERSXML
[17]29 fi
[71]30 if [ -z "$(cat $PLAYERSXML)" ]; then
31 echo "<Players/>" > $PLAYERSXML
32 fi
[17]33}
34
35# Set all players for an instance to offline (on startup/shutdown)
36setAllPlayersOffline() {
[67]37 createPlayerList
[17]38 $XMLSTARLET ed -L \
39 -u "/Players/Player/@online" -v "false" \
[71]40 "$PLAYERSXML"
[87]41 rm $(getInstancePath $INSTANCE)/logs/current_players.log
42 ln -s $PLAYERSLOG $(getInstancePath $INSTANCE)/logs/current_players.log
[17]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
[294]51# 5: IP
52# 6: Steam Owner ID
[17]53logPlayerConnect() {
[87]54 local ENTITYID="$2"
55 local NICKNAME="$3"
[482]56 local PLATFORMID="$4"
57 local CROSSID="$5"
58 local IP="$6"
59 local OWNERID="$7"
[17]60
[482]61 echo "$(timestamp) +++ $ENTITYID $NICKNAME $PLATFORMID $CROSSID $IP $OWNERID" >> "$PLAYERSLOG"
[17]62
[67]63 createPlayerList
[17]64
[482]65 XPATHBASE="/Players/Player[@platformid='$PLATFORMID']"
[17]66
[482]67 if [ -z $($XMLSTARLET sel -t -v "$XPATHBASE/@platformid" "$PLAYERSXML") ]; then
[17]68 $XMLSTARLET ed -L \
69 -s "/Players" -t elem -n "Player" -v "" \
[482]70 -i "/Players/Player[not(@platformid)]" -t attr -n "platformid" -v "$PLATFORMID" \
71 -i "$XPATHBASE" -t attr -n "crossid" -v "$CROSSID" \
[17]72 -i "$XPATHBASE" -t attr -n "nick" -v "$NICKNAME" \
73 -i "$XPATHBASE" -t attr -n "playtime" -v "0" \
74 -i "$XPATHBASE" -t attr -n "logins" -v "1" \
75 -i "$XPATHBASE" -t attr -n "lastlogin" -v "$(date '+%s')" \
76 -i "$XPATHBASE" -t attr -n "online" -v "true" \
77 -i "$XPATHBASE" -t attr -n "entityid" -v "$ENTITYID" \
[87]78 -i "$XPATHBASE" -t attr -n "lastIp" -v "$IP" \
[294]79 -i "$XPATHBASE" -t attr -n "steamOwner" -v "$OWNERID" \
[71]80 "$PLAYERSXML"
[17]81 else
[71]82 LOGINS=$($XMLSTARLET sel -t -v "$XPATHBASE/@logins" "$PLAYERSXML")
[17]83 (( LOGINS++ ))
84 $XMLSTARLET ed -L \
85 -u "$XPATHBASE/@lastlogin" -v "$(date '+%s')" \
86 -u "$XPATHBASE/@online" -v "true" \
[217]87 -u "$XPATHBASE/@nick" -v "$NICKNAME" \
[17]88 -u "$XPATHBASE/@entityid" -v "$ENTITYID" \
89 -u "$XPATHBASE/@logins" -v "$LOGINS" \
[87]90 -u "$XPATHBASE/@lastIp" -v "$IP" \
[294]91 -u "$XPATHBASE/@steamOwner" -v "$OWNERID" \
[71]92 "$PLAYERSXML"
[17]93 fi
94}
95
96# Handle a player disconnect for logging/tracking
97# Params:
98# 1: Instance name
99# 2: Entity ID
100logPlayerDisconnect() {
[63]101 ENTITYID="$2"
[17]102
[67]103 createPlayerList
[17]104
105 XPATHBASE="/Players/Player[@entityid='$ENTITYID'][@online='true']"
106
[67]107 if [ -f $PLAYERSXML ]; then
[482]108 if [ ! -z $($XMLSTARLET sel -t -v "$XPATHBASE/@platformid" "$PLAYERSXML") ]; then
[71]109 NICKNAME=$($XMLSTARLET sel -t -v "$XPATHBASE/@nick" "$PLAYERSXML")
[482]110 PLATFORMID=$($XMLSTARLET sel -t -v "$XPATHBASE/@platformid" "$PLAYERSXML")
111 CROSSID=$($XMLSTARLET sel -t -v "$XPATHBASE/@crossid" "$PLAYERSXML")
112 IP=$($XMLSTARLET sel -t -v "$XPATHBASE/@lastIp" "$PLAYERSXML")
113 OWNERID=$($XMLSTARLET sel -t -v "$XPATHBASE/@steamOwner" "$PLAYERSXML")
[71]114 LOGINTIME=$($XMLSTARLET sel -t -v "$XPATHBASE/@lastlogin" "$PLAYERSXML")
115 PLAYTIME=$($XMLSTARLET sel -t -v "$XPATHBASE/@playtime" "$PLAYERSXML")
[17]116 NOW=$(date '+%s')
117 PLAYTIME=$(( PLAYTIME + NOW - LOGINTIME ))
118 $XMLSTARLET ed -L \
119 -u "$XPATHBASE/@playtime" -v "$PLAYTIME" \
120 -u "$XPATHBASE/@online" -v "false" \
[71]121 "$PLAYERSXML"
[17]122 fi
123 fi
124
[482]125 echo "$(timestamp) --- $ENTITYID $NICKNAME $PLATFORMID $CROSSID" >> "$PLAYERSLOG"
[17]126}
127
Note: See TracBrowser for help on using the repository browser.