-
Notifications
You must be signed in to change notification settings - Fork 1
/
tell.go
57 lines (45 loc) · 911 Bytes
/
tell.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
package joke
import (
"fmt"
"math/rand"
"time"
"github.com/gabrie30/joke/configs"
)
// Tell tells n jokes
func Tell(count int) {
var jokeIDs []int
if configs.LastNJokesToTell > 0 {
jokeIDs = LastNJokeIDs(count)
} else {
// find a random number between 1 and lastjoke id
jokeIDs = randomJokeIDs(count)
}
for _, id := range jokeIDs {
j := GetJokeByID(id)
fmt.Printf("================== #%v ==================\n", j.ID)
fmt.Println(j.Setup)
fmt.Println(j.Punchline)
fmt.Println("")
}
}
func randomJokeIDs(count int) []int {
counts := map[int]bool{}
min := 1
max := LastJokeID()
if count > max {
count = max
}
for len(counts) < count {
rand.Seed(time.Now().UnixNano())
seed := rand.Intn((max - min + 1) + min)
if seed == 0 {
continue
}
counts[seed] = true
}
retKeys := []int{}
for k := range counts {
retKeys = append(retKeys, k)
}
return retKeys
}