-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsrTracker.sh
More file actions
163 lines (142 loc) · 4.66 KB
/
srTracker.sh
File metadata and controls
163 lines (142 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/bin/bash
#
# Purpose: A script to help me learn stats about my Overwatch SR ranking. Can be used for other numerical ranking systems, as well
#
# Usage: ./srTracker.sh [text_file]
#
# v1.1, 31 May 2018, 20:45 PST
source /usr/share/commonFunctions.sh
### Vars
longName="srTracker"
shortName="srt"
trackFile="sr.txt" # Location of the SR file, usually in present dir
timeBetweenUpdates=3 # Number of games between notifying user of SR stats
### Functions
function displayStats() {
if [[ ! -z $1 ]]; then
printf "%s\n" "$1" # Prints given message before continuing
fi
# Math
local sessionDiff=0
local overallDiff=0
local sessionGames=0
[[ -z $lastUpdate ]] && lastUpdate="$currentSR"
local updateDiff=0
((sessionDiff=-1*(beginSR-currentSR)))
((overallDiff=-1*(start-currentSR)))
((updateDiff=-1*(lastUpdate-currentSR)))
((sessionGames=numGames-beginGames))
# Words
if [[ "$sessionDiff" -lt 0 ]]; then
sessionWord="down"
((sessionDiff*=-1))
elif [[ "$sessionDiff" -eq 0 ]]; then
sessionWord="at"
else
sessionWord="up"
fi
if [[ "$overallDiff" -lt 0 ]]; then
overallWord="down"
((overallDiff*=-1))
elif [[ "$overallDiff" -eq 0 ]]; then
overallWord="at"
else
overallWord="up"
fi
if [[ "$updateDiff" -lt 0 ]]; then
updateWord="down"
((updateDiff*=-1))
elif [[ "$updateDiff" -eq 0 ]]; then
updateWord="at"
else
updateWord="up"
fi
# Print it all in one big printf statement
printf "Stats: SR %s, you are %s %s SR this session (%s %s SR since last update), %s %s SR overall; %s games tonight.\n" "$currentSR" "$sessionWord" "$sessionDiff" "$updateWord" "$updateDiff" "$overallWord" "$overallDiff" "$sessionGames"
}
### Main
# Use $1 as trackFile, if present
if [[ -f "$1" ]]; then
debug "l2" "INFO: Using supplied file $1 as SR file!"
trackFile="$1"
elif [[ "$1" != " " || "$1" != "" ]]; then
debug "l2" "WARN: File not found at input $1!"
getUserAnswer "Would you like to use $1 as the SR file?"
if [[ $? -eq 0 ]]; then
debug "WARN: Using $1 as SR file per user request"
trackFile="$1"
else
debug "l2" "FATAL: SR file not found! Exiting..."
exit 1
fi
fi
# Make sure file exists
if [[ ! -e "$trackFile" ]]; then
debug "l2" "ERROR: trackFile not found, initializing!"
if ! touch "$trackFile"; then
debug "l2" "FATAL: User does not have permission to write to srTracker directory! Please fix and re-run!"
exit 1
fi
read -p "Please enter current SR: " start
echo "$start" > "$trackFile"
else
start="$(head -n1 $trackFile)"
fi
# Initialize all numbers
numGames=0 # Divisor for average
totalDelta=0 # Add all the deltas together, divide it by numGames later
currentSR="$start"
#announce "Welcome back! Your current SR is: $currentSR. Good luck!"
while read line
do
# Prevents error by re-importing initial value
if [[ -z $flag ]]; then
flag=1
continue
fi
# Sanity check
if [[ "$line" -ne "$line" ]]; then # Checks to see if line is a number
debug "l2" "ERROR: line $line is not a number! Continuing anyways, press CTRL+C to stop..."
continue
fi
((numGames++))
((totalDelta+=-1*(currentSR-line))) # Needs to be negative, because math
currentSR="$line"
done<"$trackFile"
beginSR="$currentSR" # Start of SR for the play session
beginGames="$numGames" # Tells you total number of games in play session
sessionDelta="$totalDelta" # Also need the beginning delta for session delta
updateCount=1 # Number of entries since last update
#debug "l2" "INFO: Starting the night with SR $beginSR"
displayStats "Beginning the session with the following stats!"
# And now, endless loop
while [[ -z $exitFlag ]];
do
read -p "Enter the SR from your last match, or enter quit to exit: " newSR
case $newSR in
q*|e*)
exitFlag=1
;;
*)
if [[ "$newSR" -ne "$newSR" ]]; then
debug "l2" "ERROR: Invalid input detected - $newSR!"
else
((totalDelta+=-1*(currentSR-newSR)))
((numGames++))
currentSR="$newSR"
echo "$newSR" >> "$trackFile" # Accidentally put the word 'exit' in the trackFile, so I moved the location
fi
#echo "$newSR" >> "$trackFile"
;;
esac
if [[ "$updateCount" -ge "$timeBetweenUpdates" ]]; then
displayStats
updateCount=1
else
((updateCount++))
fi
done
debug "INFO: Done playing for this session, printing final stats and exiting!"
displayStats "Final stats for this session!"
exit 0
#EOF