cmd/vyvanse: keep track of user scores

This commit is contained in:
Cadey Ratio 2017-08-29 10:24:23 -07:00
parent 07b62886e3
commit 46545f2142
No known key found for this signature in database
GPG Key ID: D607EE27C2E7F89A
2 changed files with 35 additions and 7 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt"
"log"
"net/http"
"strings"
"git.xeserv.us/xena/gorqlite"
"git.xeserv.us/xena/vyvanse/bot"
@ -51,7 +52,7 @@ func main() {
}
mgs := []migrator{us}
sp, ctx := opentracing.StartSpan("migrations")
sp, ctx := opentracing.StartSpanFromContext(context.Background(), "migrations")
for _, mg := range mgs {
err := mg.Migrate(ctx)
if err != nil {
@ -133,14 +134,22 @@ func main() {
cs.AddCmd("splattus", "splatoon 2 map rotation status", bot.NoPermissions, spla2nMaps)
dg.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) {
err := cs.Run(s, m.Message)
sp, ctx := opentracing.StartSpanFromContext(context.Background(), "discordgo.message.create")
defer sp.Finish()
err := cs.Run(ctx, s, m.Message)
if err != nil {
ln.Error(context.Background(), err, ln.F{"action": "run commandSet on message"})
ln.Error(ctx, err, ln.F{"action": "run commandSet on message"})
}
_, err = us.Insert(context.Background(), m.Author.ID)
_, err = us.Insert(ctx, m.Author.ID)
if err != nil && !strings.Contains(err.Error(), "UNIQUE constraint failed") {
ln.Error(ctx, err, ln.F{"action": "insert user into database"})
}
err = us.IncScore(ctx, m.Author.ID)
if err != nil {
ln.Error(context.Background(), err, ln.F{"action": "insert user into database"})
ln.Error(ctx, err, ln.F{"action": "increment user score"})
}
})

View File

@ -21,7 +21,7 @@ func (u *Users) Migrate(ctx context.Context) error {
defer sp.Finish()
migrationDDL := []string{
`CREATE TABLE IF NOT EXISTS users(id INTEGER PRIMARY KEY, discord_id UNIQUE TEXT, score INTEGER)`,
`CREATE TABLE IF NOT EXISTS users(id INTEGER PRIMARY KEY, discord_id TEXT UNIQUE, score INTEGER)`,
}
res, err := u.conn.Write(migrationDDL)
@ -47,9 +47,10 @@ func (u *Users) Insert(ctx context.Context, discordID string) (int64, error) {
defer sp.Finish()
sp.LogFields(splog.String("discord.id", discordID))
re, err := u.conn.WriteOne(gorqlite.NewPreparedStatement("INSERT INTO users (discord_id) VALUES (%s)").Bind(discordID))
re, err := u.conn.WriteOne(gorqlite.NewPreparedStatement("INSERT INTO users (discord_id, score) VALUES (%s, 0)").Bind(discordID))
if err != nil {
sp.LogFields(splog.Error(err))
sp.LogFields(splog.Error(re.Err))
return -1, err
}
@ -60,3 +61,21 @@ func (u *Users) Insert(ctx context.Context, discordID string) (int64, error) {
return re.LastInsertID, nil
}
func (u *Users) IncScore(ctx context.Context, discordID string) error {
sp, ctx := opentracing.StartSpanFromContext(ctx, "users.inc.score")
defer sp.Finish()
sp.LogFields(splog.String("discord.id", discordID))
re, err := u.conn.WriteOne(gorqlite.NewPreparedStatement("UPDATE users SET score = score + 1 WHERE discord_id=%s").Bind(discordID))
if err != nil {
sp.LogFields(splog.Error(err))
}
if re.Err != nil {
sp.LogFields(splog.Error(re.Err))
return re.Err
}
return nil
}