#!/usr/bin/env bash # Word tracking stats module - shows user's current stats # shellcheck disable=SC1091 [ -f functions.sh ] && source functions.sh # shellcheck disable=SC1091 [ -f triggers/wordtrack/categories.sh ] && source triggers/wordtrack/categories.sh name="$1" channelName="$2" shift 2 # Optional: check another user's stats targetUser="${1:-$name}" # User data file dataDir="triggers/wordtrack/data/${channelName}" userDataFile="${dataDir}/${targetUser}.dat" # Check if user has any data if [[ ! -f "$userDataFile" ]]; then msg "$channelName" "$name: ${targetUser} has not been tracked yet." exit 0 fi # Load user data declare -A userCounts while IFS='=' read -r category count; do userCounts["$category"]="$count" done < "$userDataFile" # Build stats message statsMessage="${targetUser}'s word tracking stats: " statsParts=() # shellcheck disable=SC2154 for category in "${categories[@]}"; do count="${userCounts[$category]:-0}" if ((count > 0)); then # Get current level for this category levelsArrayName="${category}Levels" declare -n levelsRef="$levelsArrayName" currentLevel="Unranked" nextThreshold="" # Find current level and next threshold for threshold in $(printf '%s\n' "${!levelsRef[@]}" | sort -n); do if ((count >= threshold)); then currentLevel="${levelsRef[$threshold]}" elif [[ -z "$nextThreshold" ]]; then nextThreshold="$threshold" fi done unset -n levelsRef # Build stat string if [[ -n "$nextThreshold" ]]; then remaining=$((nextThreshold - count)) statsParts+=("${category}: ${currentLevel} (${count}/${nextThreshold}, ${remaining} to next)") else statsParts+=("${category}: ${currentLevel} (MAX LEVEL - ${count} words)") fi fi done if [[ ${#statsParts[@]} -eq 0 ]]; then msg "$channelName" "$name: ${targetUser} has not earned any levels yet." else statsMessage+=$(IFS=' | '; echo "${statsParts[*]}") msg "$channelName" "$statsMessage" fi