#!/bin/bash

PLAYERSXML=$(getInstancePath $1)/players.xml
PLAYERSLOG=$(getInstancePath $1)/logs/$(date '+%Y-%m-%d_%H-%M-%S')_players.log

timestamp() {
	date '+%Y.%m.%d %H:%M:%S'
}

# Create empty player list if not existing
createPlayerList() {
	if [ ! -f "$PLAYERSXML" ]; then
		echo "<Players/>" > $PLAYERSXML
	fi
	if [ -z "$(cat $PLAYERSXML)" ]; then
		echo "<Players/>" > $PLAYERSXML
	fi
}

# Set all players for an instance to offline (on startup/shutdown)
setAllPlayersOffline() {
	createPlayerList
	$XMLSTARLET ed -L \
		-u "/Players/Player/@online" -v "false" \
		"$PLAYERSXML"
	rm $(getInstancePath $INSTANCE)/logs/current_players.log
	ln -s $PLAYERSLOG $(getInstancePath $INSTANCE)/logs/current_players.log
}

# Handle a player connect for logging/tracking
# Params:
#   1: Instance name
#   2: Entity ID
#   3: Steam ID
#   4: Nick name
logPlayerConnect() {
	local ENTITYID="$2"
	local NICKNAME="$3"
	local STEAMID="$4"
	local IP="$5"

	echo "$(timestamp) +++ $ENTITYID $NICKNAME $STEAMID $IP" >> "$PLAYERSLOG"

	createPlayerList
	
	XPATHBASE="/Players/Player[@steamid='$STEAMID']"

	if [ -z $($XMLSTARLET sel -t -v "$XPATHBASE/@steamid" "$PLAYERSXML") ]; then
		$XMLSTARLET ed -L \
			-s "/Players" -t elem -n "Player" -v "" \
			-i "/Players/Player[not(@steamid)]" -t attr -n "steamid" -v "$STEAMID" \
			-i "$XPATHBASE" -t attr -n "nick" -v "$NICKNAME" \
			-i "$XPATHBASE" -t attr -n "playtime" -v "0" \
			-i "$XPATHBASE" -t attr -n "logins" -v "1" \
			-i "$XPATHBASE" -t attr -n "lastlogin" -v "$(date '+%s')" \
			-i "$XPATHBASE" -t attr -n "online" -v "true" \
			-i "$XPATHBASE" -t attr -n "entityid" -v "$ENTITYID" \
			-i "$XPATHBASE" -t attr -n "lastIp" -v "$IP" \
			"$PLAYERSXML"
	else
		LOGINS=$($XMLSTARLET sel -t -v "$XPATHBASE/@logins" "$PLAYERSXML")
		(( LOGINS++ ))
		$XMLSTARLET ed -L \
			-u "$XPATHBASE/@lastlogin" -v "$(date '+%s')" \
			-u "$XPATHBASE/@online" -v "true" \
			-u "$XPATHBASE/@nick" -v "$NICKNAME" \ 
			-u "$XPATHBASE/@entityid" -v "$ENTITYID" \
			-u "$XPATHBASE/@logins" -v "$LOGINS" \
			-u "$XPATHBASE/@lastIp" -v "$IP" \
			"$PLAYERSXML"
	fi
}

# Handle a player disconnect for logging/tracking
# Params:
#   1: Instance name
#   2: Entity ID
logPlayerDisconnect() {
	ENTITYID="$2"

	createPlayerList

	XPATHBASE="/Players/Player[@entityid='$ENTITYID'][@online='true']"

	if [ -f $PLAYERSXML ]; then
		if [ ! -z $($XMLSTARLET sel -t -v "$XPATHBASE/@steamid" "$PLAYERSXML") ]; then
			NICKNAME=$($XMLSTARLET sel -t -v "$XPATHBASE/@nick" "$PLAYERSXML")
			STEAMID=$($XMLSTARLET sel -t -v "$XPATHBASE/@steamid" "$PLAYERSXML")
			LOGINTIME=$($XMLSTARLET sel -t -v "$XPATHBASE/@lastlogin" "$PLAYERSXML")
			PLAYTIME=$($XMLSTARLET sel -t -v "$XPATHBASE/@playtime" "$PLAYERSXML")
			NOW=$(date '+%s')
			PLAYTIME=$(( PLAYTIME + NOW - LOGINTIME ))
			$XMLSTARLET ed -L \
				-u "$XPATHBASE/@playtime" -v "$PLAYTIME" \
				-u "$XPATHBASE/@online" -v "false" \
				"$PLAYERSXML"
		fi
	fi

	echo "$(timestamp) --- $ENTITYID $NICKNAME $STEAMID" >> "$PLAYERSLOG"
}

