achievements: add a list function

This commit is contained in:
Chris Sexton 2021-06-17 16:21:30 -04:00 committed by Chris Sexton
parent 825a8d267f
commit 1619a7f7d4
1 changed files with 52 additions and 7 deletions

View File

@ -32,14 +32,24 @@ func New(b bot.Bot) *AchievementsPlugin {
log.Fatal().Err(err).Msg("unable to create achievements tables") log.Fatal().Err(err).Msg("unable to create achievements tables")
} }
b.RegisterRegexCmd(ap, bot.Message, grantRegex, ap.grantCmd) ap.register()
b.RegisterRegexCmd(ap, bot.Message, createRegex, ap.createCmd)
b.RegisterRegexCmd(ap, bot.Message, greatRegex, ap.greatCmd)
b.Register(ap, bot.Help, ap.help)
return ap return ap
} }
var grantRegex = regexp.MustCompile(`(?i)grant (?P<emojy>(?::[[:word:][:punct:]]+:\s?)+) to :?(?P<nick>[[:word:]]+):?`)
var createRegex = regexp.MustCompile(`(?i)create trophy (?P<emojy>(?::[[:word:][:punct:]]+:\s?)+) (?P<description>.+)`)
var greatRegex = regexp.MustCompile(`(?i)how great (?:am i|is :?(?P<who>[[:word:]]+))[[:punct:]]*`)
var listRegex = regexp.MustCompile(`(?i)^list (?P<whos>.+)?\s?trophies$`)
func (p *AchievementsPlugin) register() {
p.bot.RegisterRegexCmd(p, bot.Message, grantRegex, p.grantCmd)
p.bot.RegisterRegexCmd(p, bot.Message, createRegex, p.createCmd)
p.bot.RegisterRegexCmd(p, bot.Message, greatRegex, p.greatCmd)
p.bot.RegisterRegexCmd(p, bot.Message, listRegex, p.listCmd)
p.bot.Register(p, bot.Help, p.help)
}
func (p *AchievementsPlugin) mkDB() error { func (p *AchievementsPlugin) mkDB() error {
trophiesTable := `create table if not exists trophies ( trophiesTable := `create table if not exists trophies (
emojy string primary key, emojy string primary key,
@ -80,9 +90,37 @@ func (p *AchievementsPlugin) GetAwards(nick string) []Award {
return awards return awards
} }
var grantRegex = regexp.MustCompile(`(?i)grant (?P<emojy>(?::[[:word:][:punct:]]+:\s?)+) to :?(?P<nick>[[:word:]]+):?`) func (p *AchievementsPlugin) listCmd(r bot.Request) bool {
var createRegex = regexp.MustCompile(`(?i)create trophy (?P<emojy>(?::[[:word:][:punct:]]+:\s?)+) (?P<description>.+)`) log.Debug().Msgf("Values: %+v", r.Values)
var greatRegex = regexp.MustCompile(`(?i)how great (?:am i|is :?(?P<who>[[:word:]]+))[[:punct:]]*`) whos := strings.TrimSpace(r.Values["whos"])
if whos == "my" {
whos = r.Msg.User.Name
}
log.Debug().Msgf("whos = %s", whos)
ts, err := p.AllTrophies()
if err != nil {
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "Some problem happened.")
log.Error().Err(err).Msg("could not retrieve trophies")
return true
}
if len(ts) == 0 {
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "There are no trophies.")
return true
}
msg := "Current trophies:\n"
log.Debug().Msgf("Trophies: %s", ts)
for _, t := range ts {
log.Debug().Msgf("t.Creator = %s", t.Creator)
if strings.TrimSpace(t.Creator) == whos {
log.Debug().Msgf("adding %s", t)
msg += fmt.Sprintf("%s - %s\n", t.Emojy, t.Description)
} else if whos == "" {
msg += fmt.Sprintf("%s - %s (by %s)\n", t.Emojy, t.Description, t.Creator)
}
}
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, strings.TrimSpace(msg))
return true
}
func (p *AchievementsPlugin) grantCmd(r bot.Request) bool { func (p *AchievementsPlugin) grantCmd(r bot.Request) bool {
nick := r.Msg.User.Name nick := r.Msg.User.Name
@ -226,6 +264,13 @@ func (a *Award) Save() error {
return nil return nil
} }
func (p *AchievementsPlugin) AllTrophies() ([]Trophy, error) {
q := `select * from trophies order by creator`
var t []Trophy
err := p.db.Select(&t, q)
return t, err
}
func (p *AchievementsPlugin) FindTrophy(emojy string) (Trophy, error) { func (p *AchievementsPlugin) FindTrophy(emojy string) (Trophy, error) {
q := `select * from trophies where emojy=?` q := `select * from trophies where emojy=?`
var t Trophy var t Trophy