Repository navigation
Expand file tree
/
Copy pathutils.go
More file actions
219 lines (202 loc) · 6.28 KB
/
Copy pathutils.go
File metadata and controls
219 lines (202 loc) · 6.28 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
// ABOUTME: Utility functions for SleeperPy application
// ABOUTME: Includes string manipulation, name normalization, and helper functions
package main
import (
"strings"
)
// isOffseasonMode reports whether we're in the window from the end of the
// previous fantasy season through about a week before regular-season
// kickoff. During this window, weekly matchup-dependent content (tiers,
// swap/waiver suggestions) has nothing to show; dynasty tools (trade
// targets, season plan, draft strategy) are the focus instead.
//
// Deliberately uses season_type/week, not season_start_date: verified
// against the live API that season_start_date marks the start of Sleeper's
// fantasy year (roster resets, ~early August), not the regular-season
// opener (~early September) -- using it directly would end offseason mode
// a month early, right as preseason starts. NFL preseason runs 3 weeks;
// treat the last one (kickoff about a week out) as no longer offseason.
func isOffseasonMode(nflState map[string]interface{}) bool {
seasonType, _ := nflState["season_type"].(string)
switch seasonType {
case "off":
return true
case "pre":
week, ok := nflState["week"].(float64)
return !ok || week < 3
default: // "regular", "post", or unrecognized
return false
}
}
// toStringSlice converts interface{} to []string safely
func toStringSlice(val interface{}) []string {
arr := []string{}
if val == nil {
return arr
}
switch v := val.(type) {
case []interface{}:
for _, x := range v {
if s, ok := x.(string); ok {
arr = append(arr, s)
}
}
}
return arr
}
// diff returns elements in a that are not in b
func diff(a, b []string) []string {
m := make(map[string]bool)
for _, x := range b {
m[x] = true
}
out := []string{}
for _, x := range a {
if !m[x] {
out = append(out, x)
}
}
return out
}
// findTier locates the tier number for a player by name
func findTier(tiers [][]string, name string) int {
norm := normalizeName(name)
for i, names := range tiers {
for _, n := range names {
if normalizeName(n) == norm {
return i + 1
}
}
}
return 0
}
// buildUserNameMap resolves a league's /users response into user_id ->
// display name (falling back to username when no display_name is set).
func buildUserNameMap(leagueUsers []map[string]interface{}) map[string]string {
userNames := make(map[string]string)
for _, u := range leagueUsers {
uid, ok := u["user_id"].(string)
if !ok {
continue
}
displayName := ""
if dn, ok := u["display_name"].(string); ok && dn != "" {
displayName = dn
} else if un, ok := u["username"].(string); ok {
displayName = un
}
userNames[uid] = displayName
}
return userNames
}
// normalizeName standardizes player names for comparison
func normalizeName(name string) string {
name = strings.ToLower(name)
name = strings.ReplaceAll(name, ".", "")
name = strings.ReplaceAll(name, ",", "")
for _, suf := range []string{" jr", " sr", " ii", " iii", " iv", " v"} {
name = strings.TrimSuffix(name, suf)
}
// Remove non-alphanumeric except spaces
var result strings.Builder
for _, r := range name {
if (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') || r == ' ' {
result.WriteRune(r)
}
}
name = strings.Join(strings.Fields(result.String()), " ")
return name
}
// stripHTML removes HTML tags from strings
func stripHTML(s string) string {
if idx := strings.Index(s, "<span"); idx >= 0 {
return strings.TrimSpace(s[:idx])
}
return s
}
// isDynastyLeague determines if a league is dynasty format
func isDynastyLeague(league map[string]interface{}) bool {
// Check type field - type 2 indicates dynasty league
if settings, ok := league["settings"].(map[string]interface{}); ok {
if leagueType, ok := settings["type"].(float64); ok && leagueType == 2 {
debugLog("[DEBUG] League detected as dynasty via type: %v", leagueType)
return true
}
}
// Check for taxi squad (dynasty-specific feature)
if settings, ok := league["settings"].(map[string]interface{}); ok {
if taxiSlots, ok := settings["taxi_slots"].(float64); ok && taxiSlots > 0 {
debugLog("[DEBUG] League detected as dynasty via taxi_slots: %v", taxiSlots)
return true
}
}
// Fallback: check league name for "dynasty" keyword
if name, ok := league["name"].(string); ok {
nameLower := strings.ToLower(name)
if strings.Contains(nameLower, "dynasty") {
debugLog("[DEBUG] League detected as dynasty via name: %s", name)
return true
}
}
return false
}
// resolveTaxiSettings reads a league's taxi squad configuration. taxiYears
// defaults to 1 (rookies/1st-year players only) when the league doesn't set
// taxi_years explicitly -- that's the most common dynasty taxi rule, and a
// safe default when the setting is missing rather than assuming vets are
// taxi-eligible.
func resolveTaxiSettings(league map[string]interface{}) (taxiSlots int, taxiYears int) {
taxiYears = 1
settings, ok := league["settings"].(map[string]interface{})
if !ok {
return 0, taxiYears
}
if v, ok := settings["taxi_slots"].(float64); ok {
taxiSlots = int(v)
}
if v, ok := settings["taxi_years"].(float64); ok && v > 0 {
taxiYears = int(v)
}
return taxiSlots, taxiYears
}
// detectScoringFormat auto-derives a league's scoring format ("PPR", "Half
// PPR", or "Standard") from its Sleeper reception-points setting. This is
// the auto-detection fallback; callers should prefer a user's manual
// per-league override (see UserSettings.ScoringOverrides) when one is set.
func detectScoringFormat(league map[string]interface{}) string {
scoring := "PPR"
if scoringSettings, ok := league["scoring_settings"].(map[string]interface{}); ok {
if rec, ok := scoringSettings["rec"].(float64); ok {
if rec == 0.5 {
scoring = "Half PPR"
} else if rec == 0.0 {
scoring = "Standard"
}
}
}
return scoring
}
// parseCSVLine parses a CSV line handling quoted fields
func parseCSVLine(line string) []string {
var fields []string
var current strings.Builder
inQuotes := false
for i := 0; i < len(line); i++ {
char := line[i]
if char == '"' {
if inQuotes && i+1 < len(line) && line[i+1] == '"' {
current.WriteByte('"')
i++
} else {
inQuotes = !inQuotes
}
} else if char == ',' && !inQuotes {
fields = append(fields, current.String())
current.Reset()
} else {
current.WriteByte(char)
}
}
fields = append(fields, current.String())
return fields
}