forked from connorkuehl/popple
-
Notifications
You must be signed in to change notification settings - Fork 0
/
popple.go
140 lines (114 loc) · 2.71 KB
/
popple.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
package popple
import (
"bufio"
"database/sql"
"errors"
"io"
"strconv"
"strings"
"github.com/connorkuehl/popple/adapter"
"github.com/connorkuehl/popple/internal/karma"
"github.com/connorkuehl/popple/internal/popple"
)
var (
ErrInvalidLimit = errors.New("limit must be positive non-zero integer")
ErrInvalidAnnounceSetting = errors.New("announce setting is invalid")
ErrMissingArgument = errors.New("expected argument")
)
var (
defaultLeaderboardSize = 10
)
type Popple struct {
pl adapter.PersistenceLayer
}
func New(pl adapter.PersistenceLayer) *Popple {
p := Popple{
pl: pl,
}
return &p
}
func (p *Popple) BumpKarma(serverID string, body io.Reader) (map[string]int, bool, error) {
cfgf := popple.GetConfig(p.pl, serverID)
var text strings.Builder
_, err := io.Copy(&text, body)
if err != nil {
return nil, false, err
}
bumps := karma.Parse(text.String())
newlvlsr := <-popple.AddKarmaToEntities(p.pl, serverID, bumps)
levels, err := newlvlsr.Levels, newlvlsr.Err
if err != nil {
return nil, false, err
}
cfgr := <-cfgf
cfg, err := cfgr.C, cfgr.Err
if err != nil {
if !errors.Is(err, sql.ErrNoRows) {
return nil, false, err
}
err = nil
}
return levels, cfg.NoAnnounce, err
}
func (p *Popple) SetAnnounce(serverID string, body io.Reader) error {
scanner := bufio.NewScanner(body)
scanner.Split(bufio.ScanWords)
if ok := scanner.Scan(); !ok {
err := scanner.Err()
if err == nil {
return ErrMissingArgument
}
return err
}
setting := scanner.Text()
var on bool
switch setting {
case "on", "yes":
on = true
case "off", "no":
on = false
default:
return ErrInvalidAnnounceSetting
}
return <-popple.SetAnnounce(p.pl, serverID, on)
}
func (p *Popple) Karma(serverID string, body io.Reader) (map[string]int, error) {
var text strings.Builder
_, err := io.Copy(&text, body)
if err != nil {
return nil, err
}
who := karma.Parse(text.String())
levelsr := <-popple.GetLevels(p.pl, serverID, who)
levels, err := levelsr.Levels, levelsr.Err
if err != nil {
return nil, err
}
return levels, nil
}
func (p *Popple) Leaderboard(serverID string, top bool, body io.Reader) ([]adapter.LeaderboardEntry, error) {
limit := defaultLeaderboardSize
scanner := bufio.NewScanner(body)
scanner.Split(bufio.ScanWords)
if ok := scanner.Scan(); !ok {
err := scanner.Err()
if err != nil {
return nil, err
}
} else {
parsedLimit, err := strconv.Atoi(scanner.Text())
if err != nil {
return nil, err
}
if parsedLimit < 1 {
return nil, ErrInvalidLimit
}
limit = parsedLimit
}
lbr := <-popple.GetLeaderboard(p.pl, serverID, top, uint(limit))
entries, err := lbr.Entries, lbr.Err
if err != nil {
return nil, err
}
return entries, nil
}