Repository navigation
Expand file tree
/
Copy pathfetch.go
More file actions
1070 lines (938 loc) · 32 KB
/
Copy pathfetch.go
File metadata and controls
1070 lines (938 loc) · 32 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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ABOUTME: External API fetching functions for SleeperPy
// ABOUTME: Handles fetching data from Sleeper API, Boris Chen tiers, and DynastyProcess values
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"math"
"sort"
"strconv"
"strings"
"sync"
"time"
)
func fetchPlayers() (map[string]interface{}, error) {
// Check cache first
sleeperPlayersCache.RLock()
if time.Since(sleeperPlayersCache.timestamp) < sleeperPlayersCache.ttl && sleeperPlayersCache.data != nil {
debugLog("[DEBUG] Using cached Sleeper players data")
sleeperPlayersCache.RUnlock()
return sleeperPlayersCache.data, nil
}
sleeperPlayersCache.RUnlock()
debugLog("[DEBUG] Fetching fresh Sleeper players data")
players, err := fetchJSON("https://api.sleeper.app/v1/players/nfl")
if err != nil {
return nil, err
}
// Update cache
sleeperPlayersCache.Lock()
sleeperPlayersCache.data = players
sleeperPlayersCache.timestamp = time.Now()
sleeperPlayersCache.Unlock()
debugLog("[DEBUG] Cached %d Sleeper players", len(players))
return players, nil
}
// fetchTrendingPlayers returns Sleeper's trending add/drop counts (player_id
// -> count of adds/drops in the last 24h). trendType is "add" or "drop". This
// is a behavioral signal (what managers are actually doing), independent of
// any expert-consensus ranking source -- complementary to Boris Chen tiers,
// not a replacement for them.
var fetchTrendingPlayers = func(trendType string) map[string]int {
sleeperTrendingCache.RLock()
if cached, exists := sleeperTrendingCache.data[trendType]; exists {
if time.Since(sleeperTrendingCache.timestamp[trendType]) < sleeperTrendingCache.ttl {
sleeperTrendingCache.RUnlock()
return cached
}
}
sleeperTrendingCache.RUnlock()
url := fmt.Sprintf("https://api.sleeper.app/v1/players/nfl/trending/%s?lookback_hours=24&limit=100", trendType)
resp, err := httpClient.Get(url)
if err != nil {
debugLog("[DEBUG] Failed to fetch trending %s players: %v", trendType, err)
return nil
}
defer resp.Body.Close()
var entries []struct {
PlayerID string `json:"player_id"`
Count int `json:"count"`
}
if err := json.NewDecoder(resp.Body).Decode(&entries); err != nil {
debugLog("[DEBUG] Failed to decode trending %s players: %v", trendType, err)
return nil
}
out := make(map[string]int, len(entries))
for _, e := range entries {
out[e.PlayerID] = e.Count
}
sleeperTrendingCache.Lock()
sleeperTrendingCache.data[trendType] = out
sleeperTrendingCache.timestamp[trendType] = time.Now()
sleeperTrendingCache.Unlock()
debugLog("[DEBUG] Loaded %d trending-%s players", len(out), trendType)
return out
}
// fetchSeasonByeWeeks reads the season year out of an NFL state response
// (as returned by FetchNFLState) and fetches that season's bye weeks.
func fetchSeasonByeWeeks(nflState map[string]interface{}) map[string]int {
seasonStr, _ := nflState["league_season"].(string)
year, err := strconv.Atoi(seasonStr)
if err != nil {
debugLog("[DEBUG] Could not parse league_season %q for bye weeks", seasonStr)
return nil
}
return fetchByeWeeks(year)
}
// fetchByeWeeks returns each NFL team's bye week number for the given
// season, derived from Sleeper's schedule endpoint (undocumented, but real
// and live -- there's no dedicated bye-week field on the player object or a
// documented bye-week endpoint). A team's bye week is whichever week it
// doesn't appear as home or away in the full schedule.
var fetchByeWeeks = func(year int) map[string]int {
byeWeeksCache.RLock()
if cached, exists := byeWeeksCache.data[year]; exists {
if time.Since(byeWeeksCache.timestamp[year]) < byeWeeksCache.ttl {
byeWeeksCache.RUnlock()
return cached
}
}
byeWeeksCache.RUnlock()
url := fmt.Sprintf("https://api.sleeper.app/schedule/nfl/regular/%d", year)
resp, err := httpClient.Get(url)
if err != nil {
debugLog("[DEBUG] Failed to fetch NFL schedule for %d: %v", year, err)
return nil
}
defer resp.Body.Close()
var games []scheduleGame
if err := json.NewDecoder(resp.Body).Decode(&games); err != nil {
debugLog("[DEBUG] Failed to decode NFL schedule for %d: %v", year, err)
return nil
}
byeWeeks := deriveByeWeeks(games)
byeWeeksCache.Lock()
byeWeeksCache.data[year] = byeWeeks
byeWeeksCache.timestamp[year] = time.Now()
byeWeeksCache.Unlock()
debugLog("[DEBUG] Loaded bye weeks for %d teams (season %d)", len(byeWeeks), year)
return byeWeeks
}
type scheduleGame struct {
Week int `json:"week"`
Home string `json:"home"`
Away string `json:"away"`
}
// deriveByeWeeks finds each team's bye week: the one week (1..maxWeek) where
// it doesn't appear as home or away in any game.
func deriveByeWeeks(games []scheduleGame) map[string]int {
allTeams := map[string]bool{}
teamsPlayingByWeek := map[int]map[string]bool{}
maxWeek := 0
for _, g := range games {
allTeams[g.Home] = true
allTeams[g.Away] = true
if teamsPlayingByWeek[g.Week] == nil {
teamsPlayingByWeek[g.Week] = map[string]bool{}
}
teamsPlayingByWeek[g.Week][g.Home] = true
teamsPlayingByWeek[g.Week][g.Away] = true
if g.Week > maxWeek {
maxWeek = g.Week
}
}
byeWeeks := make(map[string]int, len(allTeams))
for team := range allTeams {
for week := 1; week <= maxWeek; week++ {
if !teamsPlayingByWeek[week][team] {
byeWeeks[team] = week
break
}
}
}
return byeWeeks
}
func fetchJSON(url string) (map[string]interface{}, error) {
resp, err := httpClient.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var out map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&out)
return out, err
}
func fetchJSONArray(url string) ([]map[string]interface{}, error) {
resp, err := httpClient.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var out []map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&out)
return out, err
}
// fetchDynastyValues returns blended dynasty values from DynastyProcess and
// FantasyCalc. players (Sleeper player_id -> player object, as returned by
// fetchPlayers) is used to cross-reference FantasyCalc's sleeperId field
// against DynastyProcess's name-keyed data; pass nil to skip the FantasyCalc
// blend (e.g. from callers that don't already have the players map handy).
func fetchDynastyValues(players map[string]interface{}) (map[string]DynastyValue, string) {
// Check cache first
dynastyValuesCache.RLock()
if time.Since(dynastyValuesCache.timestamp) < dynastyValuesCache.ttl && len(dynastyValuesCache.data) > 0 {
debugLog("[DEBUG] Using cached dynasty values")
scrapeDate := ""
for _, v := range dynastyValuesCache.data {
scrapeDate = v.ScrapeDate
break
}
dynastyValuesCache.RUnlock()
return dynastyValuesCache.data, scrapeDate
}
dynastyValuesCache.RUnlock()
debugLog("[DEBUG] Fetching fresh dynasty values from DynastyProcess")
resp, err := httpClient.Get("https://raw.githubusercontent.com/dynastyprocess/data/master/files/values-players.csv")
if err != nil {
log.Printf("[ERROR] Failed to fetch dynasty values: %v", err)
return nil, ""
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("[ERROR] Failed to read dynasty values: %v", err)
return nil, ""
}
// Parse CSV
lines := strings.Split(string(body), "\n")
values := make(map[string]DynastyValue)
scrapeDate := ""
for i, line := range lines {
if i == 0 || line == "" {
continue // Skip header and empty lines
}
// Parse CSV line - handle quoted fields
fields := parseCSVLine(line)
if len(fields) < 12 {
continue
}
playerName := strings.Trim(fields[0], "\"")
position := strings.Trim(fields[1], "\"")
value1QB, _ := strconv.Atoi(strings.Trim(fields[8], "\""))
value2QB, _ := strconv.Atoi(strings.Trim(fields[9], "\""))
date := strings.Trim(fields[10], "\"")
if scrapeDate == "" {
scrapeDate = date
}
// Store with normalized name as key
normalizedName := normalizeName(playerName)
values[normalizedName] = DynastyValue{
Name: playerName,
Position: position,
Value1QB: value1QB,
Value2QB: value2QB,
ScrapeDate: date,
DynastyProcessValue1QB: value1QB,
DynastyProcessValue2QB: value2QB,
}
}
if players != nil {
blendFantasyCalcValues(values, players)
}
blendDynastyADPValues(values)
// Update cache
dynastyValuesCache.Lock()
dynastyValuesCache.data = values
dynastyValuesCache.timestamp = time.Now()
dynastyValuesCache.Unlock()
debugLog("[DEBUG] Loaded %d dynasty values (last updated: %s)", len(values), scrapeDate)
return values, scrapeDate
}
// fetchFantasyCalcValuesImpl fetches FantasyCalc's current dynasty values for
// the given QB format (1 for single-QB, 2 for superflex), keyed by Sleeper
// player_id (FantasyCalc includes it directly, so no name matching needed).
var fetchFantasyCalcValues = func(numQBs int) map[string]int {
url := fmt.Sprintf("https://api.fantasycalc.com/values/current?isDynasty=true&numQbs=%d&ppr=1", numQBs)
resp, err := httpClient.Get(url)
if err != nil {
debugLog("[DEBUG] Failed to fetch FantasyCalc values (numQbs=%d): %v", numQBs, err)
return nil
}
defer resp.Body.Close()
var entries []struct {
Player struct {
SleeperID string `json:"sleeperId"`
} `json:"player"`
Value int `json:"value"`
}
if err := json.NewDecoder(resp.Body).Decode(&entries); err != nil {
debugLog("[DEBUG] Failed to decode FantasyCalc values (numQbs=%d): %v", numQBs, err)
return nil
}
out := make(map[string]int, len(entries))
for _, e := range entries {
if e.Player.SleeperID == "" {
continue
}
out[e.Player.SleeperID] = e.Value
}
return out
}
// blendFantasyCalcValues merges FantasyCalc values into an existing
// DynastyProcess-keyed values map (keyed by normalized player name), matching
// players via Sleeper player_id, then recomputes Value1QB/Value2QB as the
// average of whichever sources are available for each player.
func blendFantasyCalcValues(values map[string]DynastyValue, players map[string]interface{}) {
fc1QB := fetchFantasyCalcValues(1)
fc2QB := fetchFantasyCalcValues(2)
if fc1QB == nil && fc2QB == nil {
debugLog("[DEBUG] FantasyCalc values unavailable, using DynastyProcess only")
return
}
for playerID, p := range players {
player, ok := p.(map[string]interface{})
if !ok {
continue
}
v1, has1 := fc1QB[playerID]
v2, has2 := fc2QB[playerID]
if !has1 && !has2 {
continue
}
fullName, _ := player["full_name"].(string)
if fullName == "" {
continue
}
normName := normalizeName(fullName)
entry, exists := values[normName]
if !exists {
position, _ := player["position"].(string)
entry = DynastyValue{Name: fullName, Position: position}
}
if has1 {
entry.FantasyCalcValue1QB = v1
}
if has2 {
entry.FantasyCalcValue2QB = v2
}
entry.Value1QB = blendValue(entry.DynastyProcessValue1QB, entry.FantasyCalcValue1QB)
entry.Value2QB = blendValue(entry.DynastyProcessValue2QB, entry.FantasyCalcValue2QB)
values[normName] = entry
}
}
// blendValue averages two source values, falling back to whichever one is
// available (a zero value means that source had no data for this player).
func blendValue(a, b int) int {
switch {
case a > 0 && b > 0:
return (a + b) / 2
case a > 0:
return a
default:
return b
}
}
// blendValue3 averages whichever of three source values are actually
// available (a zero value means that source had no data for this player),
// rather than requiring all three.
func blendValue3(a, b, c int) int {
sum, n := 0, 0
for _, v := range []int{a, b, c} {
if v > 0 {
sum += v
n++
}
}
if n == 0 {
return 0
}
return sum / n
}
// dynastyADPHalfLifePicks controls how fast the ADP-derived value below
// decays with draft position -- picked so the value roughly halves every 40
// picks, putting it in the same ballpark as DynastyProcess/FantasyCalc's
// 0-9999 scales without trying to exactly match either one.
const dynastyADPHalfLifePicks = 40.0
// fetchDynastyADPValues fetches FantasyFootballCalculator's live dynasty
// startup-draft ADP and converts it into a 0-9999 value via exponential
// decay, keyed by normalized player name. This is a third, independent
// dynasty value signal -- what real dynasty managers are drafting right now,
// as opposed to a single site's editorial rankings -- blended alongside
// DynastyProcess and FantasyCalc in fetchDynastyValues.
var fetchDynastyADPValues = func() map[string]int {
url := fmt.Sprintf("https://fantasyfootballcalculator.com/api/v1/adp/dynasty?teams=12&year=%d", time.Now().Year())
resp, err := httpClient.Get(url)
if err != nil {
debugLog("[DEBUG] Failed to fetch FFC dynasty ADP: %v", err)
return nil
}
defer resp.Body.Close()
var parsed adpResponse
if err := json.NewDecoder(resp.Body).Decode(&parsed); err != nil {
debugLog("[DEBUG] Failed to decode FFC dynasty ADP: %v", err)
return nil
}
if len(parsed.Players) == 0 {
debugLog("[DEBUG] FFC dynasty ADP returned no players (likely too early in the season for a sample)")
return nil
}
out := make(map[string]int, len(parsed.Players))
for _, p := range parsed.Players {
decay := math.Exp(-math.Ln2 / dynastyADPHalfLifePicks * (p.ADP - 1))
out[normalizeName(p.Name)] = int(math.Round(9999 * decay))
}
return out
}
// blendDynastyADPValues folds the FFC dynasty-ADP-derived value into an
// existing values map (keyed by normalized player name) as a third source,
// recomputing Value1QB/Value2QB as the average of whichever sources are
// available. Only updates players DynastyProcess or FantasyCalc already
// know about -- ADP alone doesn't carry reliable position data to seed a
// brand new entry.
func blendDynastyADPValues(values map[string]DynastyValue) {
adpValues := fetchDynastyADPValues()
if len(adpValues) == 0 {
debugLog("[DEBUG] Dynasty ADP values unavailable, blending DynastyProcess/FantasyCalc only")
return
}
for normName, adpValue := range adpValues {
entry, exists := values[normName]
if !exists {
continue
}
entry.DynastyADPValue = adpValue
entry.Value1QB = blendValue3(entry.DynastyProcessValue1QB, entry.FantasyCalcValue1QB, adpValue)
entry.Value2QB = blendValue3(entry.DynastyProcessValue2QB, entry.FantasyCalcValue2QB, adpValue)
values[normName] = entry
}
}
var borisURLs = map[string]map[string]string{
"PPR": {
"QB": "https://s3-us-west-1.amazonaws.com/fftiers/out/text_QB.txt",
"RB": "https://s3-us-west-1.amazonaws.com/fftiers/out/text_RB-PPR.txt",
"WR": "https://s3-us-west-1.amazonaws.com/fftiers/out/text_WR-PPR.txt",
"TE": "https://s3-us-west-1.amazonaws.com/fftiers/out/text_TE-PPR.txt",
"FLX": "https://s3-us-west-1.amazonaws.com/fftiers/out/text_FLX-PPR.txt",
"K": "https://s3-us-west-1.amazonaws.com/fftiers/out/text_K.txt",
"DST": "https://s3-us-west-1.amazonaws.com/fftiers/out/text_DST.txt",
},
"Half PPR": {
"QB": "https://s3-us-west-1.amazonaws.com/fftiers/out/text_QB.txt",
"RB": "https://s3-us-west-1.amazonaws.com/fftiers/out/text_RB-HALF.txt",
"WR": "https://s3-us-west-1.amazonaws.com/fftiers/out/text_WR-HALF.txt",
"TE": "https://s3-us-west-1.amazonaws.com/fftiers/out/text_TE-HALF.txt",
"FLX": "https://s3-us-west-1.amazonaws.com/fftiers/out/text_FLX-HALF.txt",
"K": "https://s3-us-west-1.amazonaws.com/fftiers/out/text_K.txt",
"DST": "https://s3-us-west-1.amazonaws.com/fftiers/out/text_DST.txt",
},
"Standard": {
"QB": "https://s3-us-west-1.amazonaws.com/fftiers/out/text_QB.txt",
"RB": "https://s3-us-west-1.amazonaws.com/fftiers/out/text_RB.txt",
"WR": "https://s3-us-west-1.amazonaws.com/fftiers/out/text_WR.txt",
"TE": "https://s3-us-west-1.amazonaws.com/fftiers/out/text_TE.txt",
"FLX": "https://s3-us-west-1.amazonaws.com/fftiers/out/text_FLX.txt",
"K": "https://s3-us-west-1.amazonaws.com/fftiers/out/text_K.txt",
"DST": "https://s3-us-west-1.amazonaws.com/fftiers/out/text_DST.txt",
},
}
// fetchBorisTiers is a variable to allow mocking in tests
var fetchBorisTiers = func(scoring string) map[string][][]string {
return fetchBorisTiersImpl(scoring)
}
func fetchBorisTiersImpl(scoring string) map[string][][]string {
// Check cache first
borisTiersCache.RLock()
if cached, exists := borisTiersCache.data[scoring]; exists {
if time.Since(borisTiersCache.timestamp[scoring]) < borisTiersCache.ttl {
debugLog("[DEBUG] Using cached Boris tiers for %s", scoring)
borisTiersCache.RUnlock()
return cached
}
}
borisTiersCache.RUnlock()
debugLog("[DEBUG] Fetching fresh Boris tiers for %s", scoring)
urls := borisURLs[scoring]
out := make(map[string][][]string)
var mu sync.Mutex
var wg sync.WaitGroup
// Fetch all position tiers concurrently
for pos, url := range urls {
wg.Add(1)
go func(position, tierURL string) {
defer wg.Done()
resp, err := httpClient.Get(tierURL)
if err != nil {
debugLog("[DEBUG] Error fetching %s tiers: %v", position, err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
debugLog("[DEBUG] Error reading %s tier data: %v", position, err)
return
}
posTiers := parseBorisTierText(body)
mu.Lock()
out[position] = posTiers
mu.Unlock()
}(pos, url)
}
wg.Wait()
// Cache the result
borisTiersCache.Lock()
borisTiersCache.data[scoring] = out
borisTiersCache.timestamp[scoring] = time.Now()
borisTiersCache.Unlock()
return out
}
// parseBorisTierText parses Boris Chen's "Tier N: Name, Name, ..." text
// format, shared by both the per-position weekly tiers and the combined
// cross-position draft kit tiers (same format, different files).
func parseBorisTierText(body []byte) [][]string {
var tiers [][]string
lines := strings.Split(string(body), "\n")
for _, line := range lines {
if !strings.HasPrefix(line, "Tier ") {
continue
}
tierContent := strings.TrimPrefix(line, "Tier ")
parts := strings.SplitN(tierContent, ":", 2)
if len(parts) < 2 {
continue
}
tier := strings.TrimSpace(parts[1])
names := strings.Split(tier, ",")
for j := range names {
names[j] = strings.TrimSpace(names[j])
}
tiers = append(tiers, names)
}
return tiers
}
// draftKitURLs holds Boris Chen's combined cross-position "Draft Kit" tier
// files -- unlike the weekly per-position tiers, these rank every skill
// position on one board for pre-draft cheat-sheet use. Each scoring format
// has 3 variants ("adjust" 0/1/2); Boris Chen's site doesn't document the
// exact difference between them, so the app just labels them neutrally.
// These URLs are fetched live -- whenever Boris Chen republishes them for a
// new season, this app picks it up automatically with no code change.
var draftKitURLs = map[string]map[int]string{
"Standard": {
0: "https://s3-us-west-1.amazonaws.com/fftiers/out/text_ALL-adjust0.txt",
1: "https://s3-us-west-1.amazonaws.com/fftiers/out/text_ALL-adjust1.txt",
2: "https://s3-us-west-1.amazonaws.com/fftiers/out/text_ALL-adjust2.txt",
},
"PPR": {
0: "https://s3-us-west-1.amazonaws.com/fftiers/out/text_ALL-PPR-adjust0.txt",
1: "https://s3-us-west-1.amazonaws.com/fftiers/out/text_ALL-PPR-adjust1.txt",
2: "https://s3-us-west-1.amazonaws.com/fftiers/out/text_ALL-PPR-adjust2.txt",
},
"Half PPR": {
0: "https://s3-us-west-1.amazonaws.com/fftiers/out/text_ALL-HALF-PPR-adjust0.txt",
1: "https://s3-us-west-1.amazonaws.com/fftiers/out/text_ALL-HALF-PPR-adjust1.txt",
2: "https://s3-us-west-1.amazonaws.com/fftiers/out/text_ALL-HALF-PPR-adjust2.txt",
},
}
// fetchDraftKitTiers is a variable to allow mocking in tests
var fetchDraftKitTiers = func(scoring string, adjust int) [][]string {
return fetchDraftKitTiersImpl(scoring, adjust)
}
func fetchDraftKitTiersImpl(scoring string, adjust int) [][]string {
key := fmt.Sprintf("%s:%d", scoring, adjust)
draftKitCache.RLock()
if cached, exists := draftKitCache.data[key]; exists {
if time.Since(draftKitCache.timestamp[key]) < draftKitCache.ttl {
draftKitCache.RUnlock()
return cached
}
}
draftKitCache.RUnlock()
urls, ok := draftKitURLs[scoring]
if !ok {
debugLog("[DEBUG] Unknown draft kit scoring format %q", scoring)
return nil
}
url, ok := urls[adjust]
if !ok {
debugLog("[DEBUG] Unknown draft kit adjust level %d", adjust)
return nil
}
debugLog("[DEBUG] Fetching fresh draft kit tiers for %s", key)
resp, err := httpClient.Get(url)
if err != nil {
debugLog("[DEBUG] Failed to fetch draft kit tiers (%s): %v", key, err)
return nil
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
debugLog("[DEBUG] Failed to read draft kit tiers (%s): %v", key, err)
return nil
}
tiers := parseBorisTierText(body)
draftKitCache.Lock()
draftKitCache.data[key] = tiers
draftKitCache.timestamp[key] = time.Now()
draftKitCache.Unlock()
return tiers
}
// ffcScoringParam maps the app's scoring format names to FantasyFootballCalculator's
// URL path segments.
var ffcScoringParam = map[string]string{
"PPR": "ppr",
"Half PPR": "half-ppr",
"Standard": "standard",
}
type adpPlayer struct {
Name string `json:"name"`
Position string `json:"position"`
ADP float64 `json:"adp"`
StDev float64 `json:"stdev"`
}
type adpResponse struct {
Players []adpPlayer `json:"players"`
}
// fetchADPTiers is a variable to allow mocking in tests
var fetchADPTiers = func(scoring string) map[string][][]string {
return fetchADPTiersImpl(scoring)
}
// fetchADPTiersImpl builds a second, independent tier system from
// FantasyFootballCalculator's live 12-team ADP data -- real draft-market
// behavior clustered by statistical gap, as opposed to Boris Chen's
// expert-panel-clustered tiers. Same [][]string shape as Boris Chen's
// output, keyed by position + "FLX", so it's a drop-in alternative anywhere
// a Boris Chen tier map is used.
func fetchADPTiersImpl(scoring string) map[string][][]string {
adpTiersCache.RLock()
if cached, exists := adpTiersCache.data[scoring]; exists {
if time.Since(adpTiersCache.timestamp[scoring]) < adpTiersCache.ttl {
adpTiersCache.RUnlock()
return cached
}
}
adpTiersCache.RUnlock()
debugLog("[DEBUG] Fetching fresh ADP tiers for %s", scoring)
param, ok := ffcScoringParam[scoring]
if !ok {
param = "ppr"
}
url := fmt.Sprintf("https://fantasyfootballcalculator.com/api/v1/adp/%s?teams=12&year=%d", param, time.Now().Year())
resp, err := httpClient.Get(url)
if err != nil {
debugLog("[DEBUG] Failed to fetch FFC ADP (%s): %v", scoring, err)
return nil
}
defer resp.Body.Close()
var parsed adpResponse
if err := json.NewDecoder(resp.Body).Decode(&parsed); err != nil {
debugLog("[DEBUG] Failed to decode FFC ADP (%s): %v", scoring, err)
return nil
}
out := buildADPTiers(parsed.Players)
adpTiersCache.Lock()
adpTiersCache.data[scoring] = out
adpTiersCache.timestamp[scoring] = time.Now()
adpTiersCache.Unlock()
debugLog("[DEBUG] Built ADP tiers for %s from %d players", scoring, len(parsed.Players))
return out
}
// normalizeADPPosition maps FFC's position codes onto the ones the rest of
// the app uses ("PK" -> "K", "DEF" -> "DST"); returns "" for anything else
// (there shouldn't be anything else in an NFL ADP report, but skip rather
// than mis-bucket if the API ever adds a new code).
func normalizeADPPosition(pos string) string {
switch pos {
case "QB", "RB", "WR", "TE":
return pos
case "PK":
return "K"
case "DEF":
return "DST"
default:
return ""
}
}
func buildADPTiers(players []adpPlayer) map[string][][]string {
byPos := make(map[string][]adpPlayer)
var flexPool []adpPlayer
for _, p := range players {
pos := normalizeADPPosition(p.Position)
if pos == "" {
continue
}
byPos[pos] = append(byPos[pos], p)
if pos == "RB" || pos == "WR" || pos == "TE" {
flexPool = append(flexPool, p)
}
}
byPos["FLX"] = flexPool
out := make(map[string][][]string, len(byPos))
for pos, list := range byPos {
sort.Slice(list, func(i, j int) bool { return list[i].ADP < list[j].ADP })
out[pos] = clusterByADPGap(list)
}
return out
}
// clusterByADPGap groups ADP-sorted players into tiers by splitting
// wherever the gap to the next player exceeds their combined draft-position
// uncertainty (stdev, as reported by FFC across thousands of real drafts).
// A 1.0-pick floor keeps ties (identical ADP, zero stdev) from each becoming
// their own tier.
func clusterByADPGap(players []adpPlayer) [][]string {
if len(players) == 0 {
return nil
}
var tiers [][]string
current := []string{players[0].Name}
for i := 1; i < len(players); i++ {
gap := players[i].ADP - players[i-1].ADP
threshold := players[i-1].StDev + players[i].StDev
if threshold < 1.0 {
threshold = 1.0
}
if gap > threshold {
tiers = append(tiers, current)
current = nil
}
current = append(current, players[i].Name)
}
if len(current) > 0 {
tiers = append(tiers, current)
}
return tiers
}
func fetchRecentTransactions(leagueID string, currentWeek int, players map[string]interface{}, rosters []map[string]interface{}, userNames map[string]string, dynastyValues map[string]DynastyValue, isSuperFlex bool) []Transaction {
transactions := []Transaction{}
// Fetch transactions from multiple weeks (last 3 weeks)
startWeek := currentWeek - 2
if startWeek < 1 {
startWeek = 1
}
debugLog("[DEBUG] Fetching transactions from week %d to %d", startWeek, currentWeek)
// Fetch all weeks concurrently
type weekTxns struct {
week int
data []map[string]interface{}
err error
}
results := make(chan weekTxns, 3)
for week := startWeek; week <= currentWeek; week++ {
go func(w int) {
url := fmt.Sprintf("https://api.sleeper.app/v1/league/%s/transactions/%d", leagueID, w)
txnData, err := fetchJSONArray(url)
results <- weekTxns{week: w, data: txnData, err: err}
}(week)
}
// Collect results
allTxns := []map[string]interface{}{}
for i := 0; i < (currentWeek - startWeek + 1); i++ {
result := <-results
if result.err != nil {
debugLog("[DEBUG] Error fetching transactions for week %d: %v", result.week, result.err)
continue
}
allTxns = append(allTxns, result.data...)
}
close(results)
debugLog("[DEBUG] Fetched %d total transactions", len(allTxns))
// Create map of roster_id -> team name for easier lookup
rosterTeams := make(map[float64]string)
for _, r := range rosters {
rosterID, _ := r["roster_id"].(float64)
ownerID, _ := r["owner_id"].(string)
teamName := "Unknown"
if userNames != nil {
if name, exists := userNames[ownerID]; exists {
teamName = name
}
}
// Try to get custom team name from metadata
if metadata, ok := r["metadata"].(map[string]interface{}); ok {
if tn, ok := metadata["team_name"].(string); ok && tn != "" {
teamName = tn
}
}
rosterTeams[rosterID] = teamName
}
// Process each transaction
for _, txn := range allTxns {
txnType, _ := txn["type"].(string)
if txnType == "" {
continue
}
// Parse timestamp
created, _ := txn["created"].(float64)
timestamp := time.Unix(int64(created/1000), 0)
// Handle trades
if txnType == "trade" {
rosterIDs, _ := txn["roster_ids"].([]interface{})
if len(rosterIDs) < 2 {
continue
}
roster1, _ := rosterIDs[0].(float64)
roster2, _ := rosterIDs[1].(float64)
team1 := rosterTeams[roster1]
team2 := rosterTeams[roster2]
// Get draft picks involved (map of roster_id -> pick details)
draftPicks, _ := txn["draft_picks"].([]interface{})
// Get players involved
adds, _ := txn["adds"].(map[string]interface{})
_, _ = txn["drops"].(map[string]interface{})
// Determine what each team gave
team1Gave := []string{}
team2Gave := []string{}
// Players
for playerID, rosterID := range adds {
recipientRosterID, _ := rosterID.(float64)
if p, ok := players[playerID].(map[string]interface{}); ok {
playerName := getPlayerName(p)
if recipientRosterID == roster1 {
team2Gave = append(team2Gave, playerName)
} else {
team1Gave = append(team1Gave, playerName)
}
}
}
// Draft picks
for _, pickData := range draftPicks {
pick, _ := pickData.(map[string]interface{})
ownerID, _ := pick["owner_id"].(float64) // Who originally owned this pick
rosterID, _ := pick["roster_id"].(float64) // Who now owns this pick
season, _ := pick["season"].(string)
round, _ := pick["round"].(float64)
pickDesc := fmt.Sprintf("%s Round %d", season, int(round))
// Determine who gave this pick
if rosterID == roster1 {
// roster1 now has the pick, so roster2 (or original owner) gave it
if ownerID == roster2 {
team2Gave = append(team2Gave, pickDesc)
} else {
// Pick was traded to roster2 before, now going to roster1
origTeam := rosterTeams[ownerID]
team2Gave = append(team2Gave, fmt.Sprintf("%s (from %s)", pickDesc, origTeam))
}
} else {
// roster2 now has the pick, so roster1 (or original owner) gave it
if ownerID == roster1 {
team1Gave = append(team1Gave, pickDesc)
} else {
origTeam := rosterTeams[ownerID]
team1Gave = append(team1Gave, fmt.Sprintf("%s (from %s)", pickDesc, origTeam))
}
}
}
// Calculate dynasty values if available
team1GaveValue := 0
team2GaveValue := 0
if dynastyValues != nil {
for _, playerName := range team1Gave {
// Skip draft picks (they contain "Round")
if !strings.Contains(playerName, "Round") {
normalizedName := normalizeName(playerName)
if value, exists := dynastyValues[normalizedName]; exists {
if isSuperFlex {
team1GaveValue += value.Value2QB
} else {
team1GaveValue += value.Value1QB
}
}
}
}
for _, playerName := range team2Gave {
// Skip draft picks
if !strings.Contains(playerName, "Round") {
normalizedName := normalizeName(playerName)
if value, exists := dynastyValues[normalizedName]; exists {
if isSuperFlex {
team2GaveValue += value.Value2QB
} else {
team2GaveValue += value.Value1QB
}
}
}
}
}
// Net value: positive means team1 gained value (got more than they gave)
netValue := team2GaveValue - team1GaveValue
// Build description
desc := fmt.Sprintf("%s traded with %s", team1, team2)
txn := Transaction{
Type: "trade",
LeagueID: leagueID,
Timestamp: timestamp,
Description: desc,
TeamNames: []string{team1, team2},
Team1: team1,
Team2: team2,
Team1Gave: team1Gave,
Team2Gave: team2Gave,
Team1GaveValue: team1GaveValue,
Team2GaveValue: team2GaveValue,
NetValue: netValue,
}
// Calculate fairness (Feature #3) - only for dynasty leagues
if dynastyValues != nil && (team1GaveValue > 0 || team2GaveValue > 0) {
txn.Fairness = calculateTradeFairness(txn, nil)
}