-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraffle_io.go
132 lines (108 loc) · 2.73 KB
/
raffle_io.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
package main
import (
"bufio"
"encoding/csv"
"fmt"
"io"
"io/fs"
"log"
"os"
"strconv"
"strings"
"github.com/zyedidia/generic/list"
)
func formatArray(arr []int) string {
return fmt.Sprintf("(%s)", strings.Trim(strings.Join(strings.Fields(fmt.Sprint(arr)), ", "), "[]"))
}
func printToFile(rolledEntries map[string]*RolledEntry) {
file, err := os.Create("raffle_results.txt")
if err != nil {
log.Fatal(err)
}
defer file.Close()
writer := bufio.NewWriter(file)
defer writer.Flush()
for patron, rolledEntry := range rolledEntries {
writer.WriteString(fmt.Sprintf("+%d for %s %s \n", rolledEntry.points, patron, formatArray(rolledEntry.rolls)))
}
}
func importDataFile() *list.List[RaffleEntry] {
raffleEntries := list.New[RaffleEntry]()
readFile, err := os.Open("data.txt")
if err != nil {
log.Fatal(err)
}
defer readFile.Close()
fileScanner := bufio.NewScanner(readFile)
fileScanner.Split(bufio.ScanLines)
for fileScanner.Scan() {
line := fileScanner.Text()
lineSplit := strings.Split(line, ": ")
points, err := strconv.Atoi(lineSplit[1])
if err != nil {
log.Fatal(err)
}
raffleEntries.PushBack(RaffleEntry{Patron: lineSplit[0], Points: points})
}
// writeData(raffleEntries, true)
return raffleEntries
}
func exportDataFile(raffleData *list.List[RaffleEntry]) {
file, err := os.Create("data_new.txt")
if err != nil {
log.Fatal(err)
}
defer file.Close()
writer := bufio.NewWriter(file)
defer writer.Flush()
for entry := raffleData.Front; entry != nil; entry = entry.Next {
writer.WriteString(fmt.Sprintf("%s: %d\n", entry.Value.Patron, entry.Value.Points))
}
}
func readData() *list.List[RaffleEntry] {
raffleData := list.New[RaffleEntry]()
content, err := fs.ReadFile(os.DirFS("."), "points.csv")
if err != nil {
log.Fatal(err)
}
r := csv.NewReader(strings.NewReader(string(content)))
_, err = r.Read()
if err == io.EOF {
return raffleData
} else if err != nil {
log.Fatal(err)
}
for {
raffleEntry, err := r.Read()
if err == io.EOF {
break
} else if err != nil {
log.Fatal(err)
}
points, err := strconv.Atoi(raffleEntry[1])
if err != nil {
log.Fatal(err)
}
raffleData.PushBack(RaffleEntry{Patron: raffleEntry[0], Points: points})
}
return raffleData
}
func writeData(raffleData *list.List[RaffleEntry], override bool) {
var fileName string
if override {
fileName = "points.csv"
} else {
fileName = "points_new.csv"
}
file, err := os.Create(fileName)
if err != nil {
log.Fatal(err)
}
defer file.Close()
writer := csv.NewWriter(file)
defer writer.Flush()
writer.Write([]string{"patron", "points"})
for entry := raffleData.Front; entry != nil; entry = entry.Next {
writer.Write([]string{entry.Value.Patron, strconv.Itoa(entry.Value.Points)})
}
}