-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
167 lines (128 loc) · 3.26 KB
/
utils.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
package main
import (
"encoding/csv"
"fmt"
"io"
"log"
"os"
"strings"
"time"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"github.com/toferc/runequest"
"github.com/gorilla/sessions"
)
func getUserSessionValues(s *sessions.Session) map[string]string {
sessionMap := map[string]string{
"username": "",
"loggedin": "false",
"isAdmin": "false",
}
// Prep for user authentication
u := s.Values["username"]
l := s.Values["loggedin"]
a := s.Values["isAdmin"]
// Type assertation
if user, ok := u.(string); !ok {
} else {
fmt.Println(user)
sessionMap["username"] = user
}
// Type assertation
if loggin, ok := l.(string); !ok {
} else {
fmt.Println(loggin)
sessionMap["loggedin"] = loggin
}
// Type assertation
if admin, ok := a.(string); !ok {
} else {
fmt.Println(admin)
sessionMap["isAdmin"] = admin
}
return sessionMap
}
// numToArray takes and int and returns an array of [1:int]
func numToArray(m int) []int {
a := []int{}
for i := 1; i < m+1; i++ {
a = append(a, i)
}
return a
}
// CreateUpdate creates an update for a runequest Object
func CreateUpdate(text string, value int) *runequest.Update {
t := time.Now()
tString := t.Format("2006-01-02")
update := &runequest.Update{
Date: tString,
Event: text,
Value: value,
}
fmt.Printf("Add Update: %s %d\n", text, value)
return update
}
func createName(coreString string, userString string) string {
targetString := ""
if userString != "" {
targetString = fmt.Sprintf("%s (%s)", coreString, userString)
} else {
targetString = coreString
}
return targetString
}
// ProcessUserString returns a simplified
func ProcessUserString(s string) string {
trimmed := strings.TrimSpace(s)
lower := strings.ToLower(trimmed)
title_case := cases.Title(language.English)
return title_case.String(lower)
}
func readCSV(f string) []string {
var a []string
csvFile, err := os.Open(f)
if err != nil {
log.Println("Couldn't open CSV file", err)
}
r := csv.NewReader(csvFile)
for {
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
log.Panic(err)
}
a = append(a, record[0])
}
return a
}
// ChooseRandom returns a random number betweeen 0 and l
func ChooseRandom(l int) int {
return randomSeed.Intn(l)
}
// ChooseFromStringArray takes an array of strings to choose from and an array of strings already chosen
// it returns a selected string and an updated array of chosen stings
func ChooseFromStringArray(stringArray, chosenStrings []string) string {
fmt.Println("Choose string")
choice := ChooseRandom(len(stringArray))
target := stringArray[choice]
for isInString(chosenStrings, target) {
fmt.Println("String already chosen")
choice = ChooseRandom(len(stringArray))
target = stringArray[choice]
}
//chosenStrings = append(chosenStrings, target)
return target
}
// ChooseFromSkillArray takes an array of Skills to choose from and an array of ints already chosen
// it returns a target index and an updated array of chosen indexes
func ChooseFromSkillArray(skillArray []*runequest.Skill, chosenInts []int) int {
choice := ChooseRandom(len(skillArray))
for isIn(chosenInts, choice) {
fmt.Println("Int already chosen")
choice = ChooseRandom(len(skillArray))
}
//chosenInts = append(chosenInts, choice)
return choice
}