-
Notifications
You must be signed in to change notification settings - Fork 0
/
stronk.go
306 lines (258 loc) · 5.63 KB
/
stronk.go
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Package stronk contains the domain types for doing exercise stuff.
package stronk
import (
"errors"
"fmt"
"strconv"
)
var (
ErrUserNotFound = errors.New("user not found")
ErrNoSmallestDenom = errors.New("no smallest denom")
)
type SkippedWeek struct {
Week int
Iteration int
Note string
}
type ComparableLifts struct {
ClosestWeight *Lift
PersonalRecord *Lift
PREquivalentReps float64
}
func MainExercises() []Exercise {
return []Exercise{
OverheadPress,
Squat,
BenchPress,
Deadlift,
}
}
type Exercise string
const (
OverheadPress = Exercise("OVERHEAD_PRESS")
Squat = Exercise("SQUAT")
BenchPress = Exercise("BENCH_PRESS")
Deadlift = Exercise("DEADLIFT")
)
type SetType string
const (
Warmup = SetType("WARMUP")
Main = SetType("MAIN")
Assistance = SetType("ASSISTANCE")
)
type WeightUnit string
const (
// E.g. 1775 decipounds == 177.5 lbs
DeciPounds = WeightUnit("DECI_POUNDS")
)
type Weight struct {
Unit WeightUnit
Value int
}
func (w *Weight) String() string {
if w.Unit != DeciPounds {
return "UNKNOWN_UNIT"
}
if w.Value%10 == 0 {
return strconv.Itoa(w.Value / 10)
}
return fmt.Sprintf("%d.%d", w.Value/10, w.Value%10)
}
type TrainingMax struct {
Max Weight
Exercise Exercise
}
type Routine struct {
Name string
Weeks []*WorkoutWeek
}
func (r *Routine) Clone() *Routine {
if r == nil {
return nil
}
return &Routine{
Name: r.Name,
Weeks: cloneWeeks(r.Weeks),
}
}
func cloneWeeks(weeks []*WorkoutWeek) []*WorkoutWeek {
var out []*WorkoutWeek
for _, wk := range weeks {
out = append(out, wk.Clone())
}
return out
}
type WorkoutWeek struct {
WeekName string
Optional bool
Days []*WorkoutDay
}
func (w *WorkoutWeek) Clone() *WorkoutWeek {
if w == nil {
return nil
}
return &WorkoutWeek{
WeekName: w.WeekName,
Optional: w.Optional,
Days: cloneDays(w.Days),
}
}
func cloneDays(days []*WorkoutDay) []*WorkoutDay {
var out []*WorkoutDay
for _, d := range days {
out = append(out, d.Clone())
}
return out
}
type WorkoutDay struct {
DayName string
Movements []*Movement
}
func (w *WorkoutDay) Clone() *WorkoutDay {
if w == nil {
return nil
}
return &WorkoutDay{
DayName: w.DayName,
Movements: cloneMovements(w.Movements),
}
}
func cloneMovements(mvmts []*Movement) []*Movement {
var out []*Movement
for _, mvmt := range mvmts {
out = append(out, mvmt.Clone())
}
return out
}
type Movement struct {
Exercise Exercise
SetType SetType
Sets []*Set
}
func (m *Movement) Clone() *Movement {
if m == nil {
return nil
}
return &Movement{
Exercise: m.Exercise,
SetType: m.SetType,
Sets: cloneSets(m.Sets),
}
}
func cloneSets(sets []*Set) []*Set {
var out []*Set
for _, set := range sets {
out = append(out, set.Clone())
}
return out
}
type Set struct {
RepTarget int
// ToFailure indicates if this set should go until no more reps can be done.
// If true, usually indicated with a "+" in the UI, like "5+"
ToFailure bool
// TrainingMaxPercentage is a number between 0 and 100 indicating what
// portion of your training max this lift is going for.
TrainingMaxPercentage int
// WeightTarget isn't set when users configure it, only in responses sent to
// clients.
WeightTarget Weight
// Only set if the lift is to failure (i.e. ToFailure == true)
FailureComparables *ComparableLifts
// Only set if we found a match, won't always be the case.
AssociatedLiftID LiftID
}
func (s *Set) Clone() *Set {
if s == nil {
return nil
}
return &Set{
RepTarget: s.RepTarget,
ToFailure: s.ToFailure,
TrainingMaxPercentage: s.TrainingMaxPercentage,
WeightTarget: s.WeightTarget,
}
}
type LiftID int
type Lift struct {
ID LiftID
Exercise Exercise
SetType SetType
Weight Weight
SetNumber int
Reps int
Note string
// Day - 0, 1, 2, ... in a given week
// Week - 0, 1, 2, ... in a given iteration
// Iteration - 0, 1, 2, ... basically how many times you've gone through the
// routine
DayNumber int
WeekNumber int
IterationNumber int
ToFailure bool
}
func (l *Lift) AsOneRepMax() Weight {
return Weight{
// ORM = Weight + (Weight * Num reps * 0.0333333)
Value: int(float64(l.Weight.Value) + 0.033333333*float64(l.Weight.Value)*float64(l.Reps)),
Unit: l.Weight.Unit,
}
}
func (l *Lift) CalcEquivalentReps(weight Weight) float64 {
// To calculate how many reps that would be, we basically run the ORM calc in reverse:
// ORM = Weight + (Weight * Num reps * 0.0333333)
// (ORM - Weight) / (Weight * 0.0333333) = Num reps
orm := l.AsOneRepMax()
return float64((orm.Value-weight.Value)*30) / float64(weight.Value)
}
func FindPR(lifts []*Lift) *Lift {
if len(lifts) == 0 {
return nil
}
var max, maxIndex int
for i, l := range lifts {
orm := l.AsOneRepMax()
if orm.Value > max {
max = orm.Value
maxIndex = i
}
}
return lifts[maxIndex]
}
func CalcComparables(lifts []*Lift, weight Weight) *ComparableLifts {
pr := FindPR(lifts)
var equivReps float64
if pr != nil {
equivReps = pr.CalcEquivalentReps(weight)
}
return &ComparableLifts{
ClosestWeight: FindClosest(lifts, weight),
PersonalRecord: pr,
PREquivalentReps: equivReps,
}
}
func FindClosest(lifts []*Lift, weight Weight) *Lift {
if len(lifts) == 0 {
return nil
}
var (
closest = abs(lifts[0].Weight.Value - weight.Value)
max, idx int
)
for i, l := range lifts {
dist := abs(l.Weight.Value - weight.Value)
orm := l.AsOneRepMax()
if dist < closest || dist == closest && orm.Value > max {
closest = dist
max = orm.Value
idx = i
}
}
return lifts[idx]
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}