Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
149 lines (128 loc) · 3.36 KB
/
Copy pathmain.go
File metadata and controls
149 lines (128 loc) · 3.36 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
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
// Define the teamNameToNumber map
var teamNameToNumber = map[string]string{
"Richmond": "0",
"Chattanooga": "1",
"Bangor": "2",
"Hartford": "3",
"Peninsula": "4",
"Midland": "5",
"Lynchburg": "6",
"Amarillo": "7",
"Peoria": "8",
"Sugar Land": "9",
"Omaha": "10",
"San Jose": "11",
"Lake Elsinore": "12",
"Oklahoma City": "13",
"Las Vegas": "14",
"Maine": "15",
"Mahoning Valley": "16",
"Scranton": "17",
"Lousiville": "18",
"Bowie": "19",
"El Paso": "20",
"Rocket City": "21",
"Edmonton": "22",
"Altoona": "23",
"Wisconsin": "24",
"Idaho Falls": "25",
"Corpus Christi": "26",
"Portland": "27",
"Casper": "28",
"Billings": "29",
}
// Function to add headers to each box score file
func addHeader(filePath string, outputFolder string) {
// Read the content of the file
content, err := ioutil.ReadFile(filePath)
if err != nil {
panic(err)
}
lines := strings.Split(string(content), "\n")
if len(lines) < 4 {
fmt.Println("Invalid file format: Not enough lines.")
return
}
var teamsLineIndex int
for i, line := range lines {
if strings.Contains(line, "FINAL") {
teamsLineIndex = i + 2
break
}
}
if teamsLineIndex >= len(lines) {
fmt.Println("Could not find teams line in file.")
return
}
filename := filepath.Base(filePath)
day := strings.Split(filename, "_")[0]
teamXLine := strings.Fields(lines[teamsLineIndex])
teamYLine := strings.Fields(lines[teamsLineIndex+1])
if len(teamXLine) < 1 || len(teamYLine) < 1 {
fmt.Println("Invalid file format: Not enough fields.")
return
}
// Find the index of the first digit in the line
findFirstDigit := func(str string) int {
for i, c := range str {
if c >= '0' && c <= '9' {
return i
}
}
return -1
}
xIndex := findFirstDigit(lines[teamsLineIndex])
yIndex := findFirstDigit(lines[teamsLineIndex+1])
if xIndex == -1 || yIndex == -1 {
fmt.Println("Could not find team names in file.")
return
}
teamXName := strings.TrimSpace(lines[teamsLineIndex][:xIndex])
teamYName := strings.TrimSpace(lines[teamsLineIndex+1][:yIndex])
// Use the teamNameToNumber map to fetch the corresponding numbers
teamXNum, exists := teamNameToNumber[teamXName]
if !exists {
teamXNum = "UNKNOWN"
}
teamYNum, exists := teamNameToNumber[teamYName]
if !exists {
teamYNum = "UNKNOWN"
}
header := fmt.Sprintf("Team%s vs Team%s (Day %s)", teamXNum, teamYNum, day)
newContent := header + "\n" + string(content)
if _, err := os.Stat(outputFolder); os.IsNotExist(err) {
err = os.MkdirAll(outputFolder, 0755)
if err != nil {
panic(err)
}
}
newFilePath := filepath.Join(outputFolder, filename)
err = ioutil.WriteFile(newFilePath, []byte(newContent), 0644)
if err != nil {
panic(err)
}
}
func main() {
// Loop through the "boxes" directory
err := filepath.Walk("boxes", func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Println("Error:", err)
return err
}
if !info.IsDir() && strings.HasSuffix(info.Name(), ".txt") && !strings.Contains(info.Name(), "team_numbers") {
addHeader(path, "new_boxes")
}
return nil
})
if err != nil {
fmt.Println("Error walking the path:", err)
}
}