-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
170 lines (136 loc) · 3.8 KB
/
main.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
168
169
170
package main
import (
"fmt"
"log"
"net/http"
"strings"
"github.com/boltdb/bolt"
"github.com/gin-gonic/gin"
"github.com/gowon-irc/go-gowon"
"github.com/imroc/req/v3"
"github.com/jessevdk/go-flags"
)
type Options struct {
APIKey string `short:"k" long:"api-key" env:"GOWON_RA_API_KEY" required:"true" description:"retroachievements api key"`
KVPath string `short:"K" long:"kv-path" env:"GOWON_RA_KV_PATH" default:"kv.db" description:"path to kv db"`
}
const (
moduleName = "retroachievements"
moduleHelp = "get players last achievements from retroachievements"
)
func setUser(kv *bolt.DB, nick, user []byte) error {
err := kv.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("retroachievements"))
return b.Put([]byte(nick), []byte(user))
})
return err
}
func getUser(kv *bolt.DB, nick []byte) (user []byte, err error) {
err = kv.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("retroachievements"))
v := b.Get([]byte(nick))
user = v
return nil
})
return user, err
}
func parseArgs(msg string) (command, user string) {
fields := strings.Fields(msg)
if len(fields) >= 1 {
command = fields[0]
}
if len(fields) >= 2 {
user = fields[1]
}
return command, user
}
func setUserHandler(kv *bolt.DB, nick, user string) (string, error) {
if user == "" {
return "Error: username needed", nil
}
err := setUser(kv, []byte(nick), []byte(user))
if err != nil {
return "", err
}
return fmt.Sprintf("set %s's user to %s", nick, user), nil
}
type commandFunc func(*req.Client, string) (string, error)
func CommandHandler(client *req.Client, kv *bolt.DB, nick, user string, f commandFunc) (string, error) {
if user != "" {
return f(client, user)
}
savedUser, err := getUser(kv, []byte(nick))
if err != nil {
return "", err
}
if len(savedUser) == 0 {
return "Error: username needed", nil
}
return f(client, string(savedUser))
}
func raHandler(client *req.Client, kv *bolt.DB, m *gowon.Message) (string, error) {
command, user := parseArgs(m.Args)
switch command {
case "s", "set":
return setUserHandler(kv, m.Nick, user)
case "a", "achievement":
return CommandHandler(client, kv, m.Nick, user, raNewestAchievement)
case "l", "last":
return CommandHandler(client, kv, m.Nick, user, raLastGames)
case "c", "current":
return CommandHandler(client, kv, m.Nick, user, raCurrentStatus)
case "p", "points":
return CommandHandler(client, kv, m.Nick, user, raPoints)
case "w", "awards":
return CommandHandler(client, kv, m.Nick, user, raAwards)
case "g", "game":
return CommandHandler(client, kv, m.Nick, user, raGameProgress)
}
return "one of [s]et, [a]chievement, [l]ast, [c]urrent, [p]oints, a[w]ards or [g]ame must be passed as a command", nil
}
func main() {
log.Printf("%s starting\n", moduleName)
opts := Options{}
if _, err := flags.Parse(&opts); err != nil {
log.Fatal(err)
}
kv, err := bolt.Open(opts.KVPath, 0666, nil)
if err != nil {
log.Fatal(err)
}
defer kv.Close()
err = kv.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte("retroachievements"))
return err
})
if err != nil {
log.Fatal(err)
}
httpClient := req.C().
SetCommonQueryParam("y", opts.APIKey)
r := gin.Default()
r.POST("/message", func(c *gin.Context) {
var m gowon.Message
if err := c.BindJSON(&m); err != nil {
log.Println("Error: unable to bind message to json", err)
return
}
out, err := raHandler(httpClient, kv, &m)
if err != nil {
log.Println(err)
m.Msg = "{red}Error when looking up retroachievements data{clear}"
c.IndentedJSON(http.StatusInternalServerError, &m)
}
m.Msg = out
c.IndentedJSON(http.StatusOK, &m)
})
r.GET("/help", func(c *gin.Context) {
c.IndentedJSON(http.StatusOK, &gowon.Message{
Module: moduleName,
Msg: moduleHelp,
})
})
if err := r.Run(":8080"); err != nil {
log.Fatal(err)
}
}