2023-12-01 15:26:25 +00:00
|
|
|
package plugins
|
2019-11-13 00:45:56 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
aoc "git.bullercodeworks.com/brian/go-adventofcode"
|
2023-12-01 15:26:25 +00:00
|
|
|
"git.bullercodeworks.com/brian/helperbot/models"
|
2023-11-29 23:54:12 +00:00
|
|
|
"github.com/slack-go/slack"
|
2019-11-13 00:45:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
/* Plugin State */
|
|
|
|
type AoCState struct {
|
2023-12-01 15:26:25 +00:00
|
|
|
model *models.BotModel
|
2019-11-15 19:50:22 +00:00
|
|
|
boardId string
|
|
|
|
sessionCookie string
|
|
|
|
sessionNeedsUpdate bool
|
2019-11-13 00:45:56 +00:00
|
|
|
|
2019-11-22 18:37:15 +00:00
|
|
|
aoc *aoc.AoC
|
|
|
|
lastYear int
|
2019-11-13 00:45:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Plugin Interface Functions */
|
|
|
|
func (s *AoCState) Name() string { return "advent-of-code" }
|
2023-12-01 15:26:25 +00:00
|
|
|
func (s *AoCState) Initialize(m *models.BotModel) error {
|
2019-11-13 00:45:56 +00:00
|
|
|
// Initialize AoC stuff
|
|
|
|
var err error
|
|
|
|
var boardId, aocSession, aocChannelId string
|
|
|
|
s.model = m
|
|
|
|
|
|
|
|
boardId, err = s.getAoCBoardId()
|
|
|
|
boardId = strings.TrimSpace(boardId)
|
|
|
|
if err != nil || boardId == "" {
|
|
|
|
s.RequestBoardId()
|
|
|
|
}
|
|
|
|
|
|
|
|
aocSession, err = s.getAoCSessionCookie()
|
|
|
|
aocSession = strings.TrimSpace(aocSession)
|
|
|
|
if err != nil || aocSession == "" {
|
|
|
|
s.RequestSessionCookie()
|
2019-11-15 19:50:22 +00:00
|
|
|
} else {
|
|
|
|
s.sessionCookie = aocSession
|
2019-11-13 00:45:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
aocChannelId, err = s.getChannelId()
|
|
|
|
aocChannelId = strings.TrimSpace(aocChannelId)
|
|
|
|
if err != nil || aocChannelId == "" {
|
|
|
|
s.RequestChannelId()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = s.NewAoC(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-01 15:26:25 +00:00
|
|
|
func (s *AoCState) ProcessMessage(m models.BotMessage) bool {
|
|
|
|
if m.Source == models.MsgSrcSlack {
|
|
|
|
if len(m.Target) == 0 {
|
|
|
|
return false
|
2019-11-13 00:45:56 +00:00
|
|
|
}
|
2023-12-01 15:26:25 +00:00
|
|
|
switch m.Target[0] {
|
2019-11-22 18:37:15 +00:00
|
|
|
case 'C', 'G':
|
2023-12-01 15:26:25 +00:00
|
|
|
return s.ProcessChannelMessage(m)
|
2019-11-15 19:50:22 +00:00
|
|
|
case 'D':
|
2023-12-01 15:26:25 +00:00
|
|
|
admin := s.model.GetSlackAdminDMId()
|
|
|
|
if m.Target == admin {
|
|
|
|
return s.ProcessAdminDirectMessage(m)
|
2019-11-13 00:45:56 +00:00
|
|
|
} else {
|
2023-12-01 15:26:25 +00:00
|
|
|
return s.ProcessDirectMessage(m)
|
2019-11-13 00:45:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-12-01 15:26:25 +00:00
|
|
|
return false
|
2019-11-13 00:45:56 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 15:26:25 +00:00
|
|
|
func (s *AoCState) ProcessRTMEvent(msg *slack.RTMEvent) bool {
|
|
|
|
return false
|
|
|
|
}
|
2019-11-22 18:37:15 +00:00
|
|
|
|
2019-11-13 00:45:56 +00:00
|
|
|
func (s *AoCState) Run() {
|
|
|
|
go s.runLoop()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *AoCState) Exit() {}
|
|
|
|
|
|
|
|
/* Other Functions */
|
|
|
|
func (s *AoCState) NewAoC() error {
|
|
|
|
board, err := s.getAoCBoardId()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sess, err := s.getAoCSessionCookie()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if s.aoc, err = aoc.NewAoC(board, sess); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *AoCState) runLoop() {
|
2021-12-01 17:41:19 +00:00
|
|
|
_, err := s.getChannelId()
|
|
|
|
// This plugin fails without a channel id
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Don't do anything until we've done an initial update of all earlier years
|
|
|
|
for _, yr := range s.GetListOfAoCYears() {
|
|
|
|
if yr == s.GetLatestYear() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !s.sessionNeedsUpdate {
|
|
|
|
fmt.Printf("Startup: Checking if board needs update (%d)\n", yr)
|
|
|
|
s.AoCSilentBoardCheckAndUpdate(yr)
|
|
|
|
}
|
|
|
|
}
|
2019-11-13 00:45:56 +00:00
|
|
|
for {
|
2019-11-15 19:50:22 +00:00
|
|
|
_, err := s.getChannelId()
|
2019-11-22 18:37:15 +00:00
|
|
|
// This plugin fails without a channel id
|
2019-11-13 00:45:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, yr := range s.GetListOfAoCYears() {
|
2019-11-15 19:50:22 +00:00
|
|
|
if !s.sessionNeedsUpdate {
|
2019-11-22 18:37:15 +00:00
|
|
|
if s.GetLatestYear() != s.lastYear {
|
|
|
|
// Latest year changed. Grab that board first.
|
|
|
|
s.lastYear = s.GetLatestYear()
|
2023-12-01 15:26:25 +00:00
|
|
|
admin := s.model.GetSlackAdminDMId()
|
|
|
|
if admin == "" {
|
2022-12-01 13:28:02 +00:00
|
|
|
s.SendAdminIdError()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.SendSlackMessage(fmt.Sprintf(":christmas_tree: AoC Set latest leaderboard to %d", s.lastYear), admin)
|
2019-11-22 18:37:15 +00:00
|
|
|
s.AoCBoardCheckAndUpdate(s.lastYear)
|
|
|
|
time.Sleep(time.Minute)
|
|
|
|
}
|
2019-11-15 19:50:22 +00:00
|
|
|
s.AoCBoardCheckAndUpdate(yr)
|
|
|
|
time.Sleep(time.Minute)
|
2019-11-13 00:45:56 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-15 19:50:22 +00:00
|
|
|
time.Sleep(time.Minute)
|
2019-11-13 00:45:56 +00:00
|
|
|
}
|
2019-11-21 23:45:04 +00:00
|
|
|
|
2023-12-01 15:26:25 +00:00
|
|
|
s.model.SendMessage(models.NewBotMessage(s.Name(), models.MsgSrcApp, "status", "done", ""))
|
2019-11-13 00:45:56 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 15:26:25 +00:00
|
|
|
func (s *AoCState) ProcessDirectMessage(m models.BotMessage) bool {
|
|
|
|
msgPts := strings.Fields(m.Text)
|
2019-11-15 19:50:22 +00:00
|
|
|
if len(msgPts) < 2 || msgPts[0] != "!aoc" {
|
2023-12-01 15:26:25 +00:00
|
|
|
return false
|
2019-11-15 19:50:22 +00:00
|
|
|
}
|
|
|
|
switch msgPts[1] {
|
|
|
|
case "help":
|
2023-12-01 15:26:25 +00:00
|
|
|
return s.DoHelpCmd(m)
|
2019-11-15 19:50:22 +00:00
|
|
|
case "ping":
|
2023-12-01 15:26:25 +00:00
|
|
|
return s.DoPingCmd(m)
|
2019-11-15 19:50:22 +00:00
|
|
|
case "top":
|
2023-12-01 15:26:25 +00:00
|
|
|
return s.DoTopCmd(m)
|
2019-11-15 19:50:22 +00:00
|
|
|
}
|
2023-12-01 15:26:25 +00:00
|
|
|
return false
|
2019-11-15 19:50:22 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 15:26:25 +00:00
|
|
|
func (s *AoCState) ProcessAdminDirectMessage(m models.BotMessage) bool {
|
|
|
|
msgPts := strings.Fields(m.Text)
|
2019-11-15 19:50:22 +00:00
|
|
|
if len(msgPts) < 2 || msgPts[0] != "!aoc" {
|
2023-12-01 15:26:25 +00:00
|
|
|
return false
|
2019-11-15 19:50:22 +00:00
|
|
|
}
|
|
|
|
switch msgPts[1] {
|
|
|
|
case "help":
|
2023-12-01 15:26:25 +00:00
|
|
|
s.DoHelpAdminCmd(m)
|
|
|
|
return true
|
2019-11-15 19:50:22 +00:00
|
|
|
case "ping":
|
2023-12-01 15:26:25 +00:00
|
|
|
s.DoPingCmd(m)
|
|
|
|
return true
|
2019-11-15 19:50:22 +00:00
|
|
|
case "top":
|
2023-12-01 15:26:25 +00:00
|
|
|
s.DoTopCmd(m)
|
|
|
|
return true
|
2019-11-15 19:50:22 +00:00
|
|
|
case "session":
|
2023-12-01 15:26:25 +00:00
|
|
|
s.DoSessionCmd(m)
|
|
|
|
return true
|
2019-11-15 19:50:22 +00:00
|
|
|
}
|
2023-12-01 15:26:25 +00:00
|
|
|
return false
|
2019-11-15 19:50:22 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 15:26:25 +00:00
|
|
|
func (s *AoCState) ProcessChannelMessage(m models.BotMessage) bool {
|
|
|
|
msgPts := strings.Fields(m.Text)
|
2019-11-15 19:50:22 +00:00
|
|
|
if len(msgPts) < 2 || msgPts[0] != "!aoc" {
|
2023-12-01 15:26:25 +00:00
|
|
|
return false
|
2019-11-15 19:50:22 +00:00
|
|
|
}
|
|
|
|
switch msgPts[1] {
|
|
|
|
case "top":
|
2023-12-01 15:26:25 +00:00
|
|
|
return s.DoTopCmd(m)
|
2019-11-15 19:50:22 +00:00
|
|
|
}
|
2023-12-01 15:26:25 +00:00
|
|
|
return false
|
2019-11-15 19:50:22 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 15:26:25 +00:00
|
|
|
func (s *AoCState) DoHelpCmd(m models.BotMessage) bool {
|
2019-11-15 19:50:22 +00:00
|
|
|
txt := fmt.Sprint(":christmas_tree: AoC Help :christmas_tree:\n",
|
|
|
|
"-- WiP --",
|
|
|
|
)
|
2023-12-01 15:26:25 +00:00
|
|
|
s.SendSlackMessage(txt, m.Target)
|
|
|
|
return true
|
2019-11-15 19:50:22 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 15:26:25 +00:00
|
|
|
func (s *AoCState) DoHelpAdminCmd(m models.BotMessage) bool {
|
2019-11-15 19:50:22 +00:00
|
|
|
txt := fmt.Sprint(":christmas_tree: AoC Help :christmas_tree:\n",
|
|
|
|
"-- WiP --",
|
|
|
|
)
|
2023-12-01 15:26:25 +00:00
|
|
|
s.SendSlackMessage(txt, m.Target)
|
|
|
|
return true
|
2019-11-15 19:50:22 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 15:26:25 +00:00
|
|
|
func (s *AoCState) DoPingCmd(m models.BotMessage) bool {
|
|
|
|
s.SendSlackMessage(":christmas_tree: PONG :christmas_tree:", m.Target)
|
|
|
|
return true
|
2019-11-15 19:50:22 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 15:26:25 +00:00
|
|
|
func (s *AoCState) DoSessionCmd(m models.BotMessage) bool {
|
|
|
|
msgPts := strings.Fields(m.Text)
|
2019-11-15 19:50:22 +00:00
|
|
|
if len(msgPts) == 3 && msgPts[1] == "session" {
|
|
|
|
// Set the session cookie
|
2023-12-01 15:26:25 +00:00
|
|
|
admin := s.model.GetSlackAdminDMId()
|
|
|
|
if admin == "" {
|
2019-11-21 23:45:04 +00:00
|
|
|
s.SendAdminIdError()
|
2023-12-01 15:26:25 +00:00
|
|
|
return true
|
2019-11-15 19:50:22 +00:00
|
|
|
}
|
|
|
|
aocSession := msgPts[2]
|
|
|
|
s.setAoCSessionCookie(strings.TrimSpace(aocSession))
|
|
|
|
s.sessionCookie = aocSession
|
|
|
|
s.sessionNeedsUpdate = false
|
2019-11-21 23:45:04 +00:00
|
|
|
s.SendSlackMessage(":christmas_tree: New Session: "+s.sessionCookie, admin)
|
2019-11-15 19:50:22 +00:00
|
|
|
} else if len(msgPts) == 2 && msgPts[1] == "session" {
|
|
|
|
// Print the session cookie
|
2023-12-01 15:26:25 +00:00
|
|
|
admin := s.model.GetSlackAdminDMId()
|
|
|
|
if admin == "" {
|
2019-11-21 23:45:04 +00:00
|
|
|
s.SendAdminIdError()
|
2023-12-01 15:26:25 +00:00
|
|
|
return true
|
2019-11-15 19:50:22 +00:00
|
|
|
}
|
|
|
|
// We only send the session cookie to the admin
|
2019-11-21 23:45:04 +00:00
|
|
|
s.SendSlackMessage(":christmas_tree: session: "+s.sessionCookie, admin)
|
2019-11-15 19:50:22 +00:00
|
|
|
}
|
2023-12-01 15:26:25 +00:00
|
|
|
return true
|
2019-11-15 19:50:22 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 15:26:25 +00:00
|
|
|
func (s *AoCState) DoTopCmd(m models.BotMessage) bool {
|
|
|
|
msgPts := strings.Fields(m.Text)
|
2019-11-15 19:50:22 +00:00
|
|
|
var err error
|
|
|
|
var yr int
|
|
|
|
if len(msgPts) > 2 {
|
|
|
|
yr, err = strconv.Atoi(msgPts[2])
|
|
|
|
if err != nil {
|
|
|
|
if msgPts[2] == "all" {
|
|
|
|
yr = -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
yr = s.GetLatestYear()
|
|
|
|
}
|
|
|
|
var txt string
|
|
|
|
if yr == -1 {
|
|
|
|
var err error
|
|
|
|
txt, err = s.DoTopForAll()
|
|
|
|
if err != nil {
|
|
|
|
txt = "Error calculating all-time tops"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
txt = s.DoTopForYear(yr)
|
|
|
|
}
|
2023-12-01 15:26:25 +00:00
|
|
|
s.SendSlackMessage(txt, m.Target)
|
|
|
|
return true
|
2019-11-15 19:50:22 +00:00
|
|
|
}
|
|
|
|
|
2019-11-13 00:45:56 +00:00
|
|
|
func (s *AoCState) DoTopForAll() (string, error) {
|
|
|
|
mbrMap := make(map[string]aoc.Member)
|
|
|
|
for _, yr := range s.GetListOfAoCYears() {
|
|
|
|
l, err := s.aoc.GetLeaderboard(yr)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
for k, v := range l.Members {
|
|
|
|
if m, ok := mbrMap[k]; ok {
|
|
|
|
m.Stars = m.Stars + v.Stars
|
|
|
|
m.LocalScore = m.LocalScore + v.LocalScore
|
|
|
|
mbrMap[k] = m
|
|
|
|
} else {
|
|
|
|
mbrMap[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var mbrs []aoc.Member
|
|
|
|
for _, v := range mbrMap {
|
|
|
|
mbrs = append(mbrs, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(mbrs) == 0 {
|
|
|
|
return "", errors.New("No member data")
|
|
|
|
}
|
|
|
|
sort.Sort(aoc.ByStarsThenScore(mbrs))
|
|
|
|
txt := ":christmas_tree: AoC All-Time Top Five! :christmas_tree:"
|
|
|
|
var num int
|
|
|
|
for k := 0; k < len(mbrs); k++ {
|
|
|
|
v := mbrs[len(mbrs)-k-1]
|
|
|
|
txt = fmt.Sprintf("%s\n%s (%d :star:, %d)", txt, v.Name, v.Stars, v.LocalScore)
|
|
|
|
num++
|
|
|
|
if num >= 5 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return txt, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *AoCState) DoTopForYear(yr int) string {
|
|
|
|
mbrs := s.GetMembers(yr)
|
|
|
|
if len(mbrs) == 0 {
|
|
|
|
return "No data for that year"
|
|
|
|
}
|
|
|
|
sort.Sort(aoc.ByStarsThenScore(mbrs))
|
|
|
|
txt := fmt.Sprintf(":christmas_tree: AoC Top Five for %d! :christmas_tree:", yr)
|
|
|
|
var num int
|
|
|
|
for k := 0; k < len(mbrs); k++ {
|
|
|
|
v := mbrs[len(mbrs)-k-1]
|
|
|
|
txt = fmt.Sprintf("%s\n%s (%d :star:, %d)", txt, v.Name, v.Stars, v.LocalScore)
|
|
|
|
num++
|
|
|
|
if num >= 5 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return txt
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *AoCState) GetMembers(yr int) []aoc.Member {
|
|
|
|
var ret []aoc.Member
|
|
|
|
l, err := s.aoc.GetLeaderboard(yr)
|
|
|
|
if err != nil {
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
for _, v := range l.Members {
|
|
|
|
ret = append(ret, v)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *AoCState) GetLatestYear() int {
|
|
|
|
latestYear := time.Now().Year()
|
|
|
|
if time.Now().Month() < 12 {
|
|
|
|
latestYear--
|
|
|
|
}
|
|
|
|
return latestYear
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *AoCState) GetListOfAoCYears() []int {
|
|
|
|
var ret []int
|
|
|
|
for k := s.GetLatestYear(); k > 2014; k-- {
|
|
|
|
ret = append(ret, k)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *AoCState) GetAoCBoard(yr int) (*aoc.Leaderboard, error) {
|
|
|
|
return s.aoc.GetLeaderboard(yr)
|
|
|
|
}
|
|
|
|
|
2021-12-01 17:41:19 +00:00
|
|
|
func (s *AoCState) AoCSilentBoardCheckAndUpdate(yr int) {
|
|
|
|
if s.AoCBoardNeedsUpdate(yr) {
|
|
|
|
l, err := s.aoc.GetLeaderboard(yr)
|
|
|
|
if err != nil {
|
2023-12-01 15:26:25 +00:00
|
|
|
admin := s.model.GetSlackAdminDMId()
|
|
|
|
if admin == "" {
|
2021-12-01 17:41:19 +00:00
|
|
|
s.SendAdminIdError()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err.Error() == "Invalid Session Cookie" {
|
|
|
|
s.sessionNeedsUpdate = true
|
|
|
|
}
|
|
|
|
s.SendSlackMessage(fmt.Sprintf(":warning: AoC Error processing leaderboard (%d) - %s", yr, err.Error()), admin)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-01 15:26:25 +00:00
|
|
|
s.model.SendMessage(models.NewBotMessage(s.Name(), models.MsgSrcApp, models.MsgTpStatus, "", fmt.Sprintf("Received leaderboard (%d)", yr)))
|
2021-12-01 17:41:19 +00:00
|
|
|
// Save the leaderboard to the db
|
|
|
|
s.saveLeaderboard(l)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 19:50:22 +00:00
|
|
|
func (s *AoCState) AoCBoardCheckAndUpdate(yr int) {
|
2021-12-02 17:44:30 +00:00
|
|
|
stYr := strconv.Itoa(yr)
|
2019-11-15 19:50:22 +00:00
|
|
|
channelId, _ := s.getChannelId()
|
|
|
|
if s.AoCBoardNeedsUpdate(yr) {
|
|
|
|
l, err := s.aoc.GetLeaderboard(yr)
|
|
|
|
if err != nil {
|
2023-12-01 15:26:25 +00:00
|
|
|
admin := s.model.GetSlackAdminDMId()
|
|
|
|
if admin == "" {
|
2019-11-21 23:45:04 +00:00
|
|
|
s.SendAdminIdError()
|
2019-11-15 19:50:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err.Error() == "Invalid Session Cookie" {
|
|
|
|
s.sessionNeedsUpdate = true
|
|
|
|
}
|
2019-11-21 23:45:04 +00:00
|
|
|
s.SendSlackMessage(fmt.Sprintf(":warning: AoC Error processing leaderboard (%d) - %s", yr, err.Error()), admin)
|
2019-11-15 19:50:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-01 15:26:25 +00:00
|
|
|
s.model.SendMessage(models.NewBotMessage(s.Name(), models.MsgSrcApp, models.MsgTpStatus, "", fmt.Sprintf("Received leaderboard (%d)", yr)))
|
2019-11-15 19:50:22 +00:00
|
|
|
// Compare the new leaderboard to the saved one
|
|
|
|
for _, v := range l.Members {
|
|
|
|
mbr, err := s.getMember(l.Event, v.ID)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if mbr.Stars != v.Stars {
|
2020-12-02 14:45:51 +00:00
|
|
|
plural := "star"
|
|
|
|
if v.Stars > 1 {
|
|
|
|
plural = "stars"
|
|
|
|
}
|
2021-12-01 17:41:19 +00:00
|
|
|
mbrAll := s.getMemberAllYears(v.ID)
|
2021-12-02 13:30:17 +00:00
|
|
|
// Gather all stars from all leaderboards
|
2021-12-01 17:41:19 +00:00
|
|
|
var totalStars int
|
2021-12-02 17:44:30 +00:00
|
|
|
for k, m := range mbrAll {
|
|
|
|
if k != stYr {
|
|
|
|
totalStars += m.Stars
|
|
|
|
}
|
2021-12-01 17:41:19 +00:00
|
|
|
}
|
2021-12-02 13:30:17 +00:00
|
|
|
// Add this new one into the total
|
2021-12-02 17:44:30 +00:00
|
|
|
allYearsText := fmt.Sprintf(" (%d for all years)", totalStars+v.Stars)
|
2020-12-02 14:40:07 +00:00
|
|
|
if yr == s.GetLatestYear() {
|
2021-12-01 17:41:19 +00:00
|
|
|
s.SendSlackMessage(fmt.Sprintf(":christmas_tree: %s now has %d %s! :christmas_tree:%s", v.Name, v.Stars, plural, allYearsText), channelId)
|
2020-12-02 14:40:07 +00:00
|
|
|
} else {
|
2021-12-01 17:41:19 +00:00
|
|
|
s.SendSlackMessage(fmt.Sprintf(":christmas_tree: %s now has %d %s! (%d) :christmas_tree:%s", v.Name, v.Stars, plural, yr, allYearsText), channelId)
|
2020-12-02 14:40:07 +00:00
|
|
|
}
|
2019-11-15 19:50:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Save the leaderboard to the db
|
|
|
|
s.saveLeaderboard(l)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *AoCState) AoCBoardNeedsUpdate(yr int) bool {
|
|
|
|
var freshDt time.Duration
|
|
|
|
if yr == s.GetLatestYear() {
|
2020-12-02 14:40:07 +00:00
|
|
|
freshDt, _ = time.ParseDuration("15m") // AoC asks that we not update more than once every 15 minutes
|
2019-11-15 19:50:22 +00:00
|
|
|
} else {
|
|
|
|
freshDt, _ = time.ParseDuration("1h")
|
|
|
|
}
|
|
|
|
l, err := s.aoc.GetCachedLeaderboard(yr)
|
|
|
|
if err != nil {
|
|
|
|
if err.Error() == "Invalid Year" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// This board is not cached, we need to pull it
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return time.Since(l.LastFetch) > freshDt
|
|
|
|
}
|
|
|
|
|
2019-11-22 18:37:15 +00:00
|
|
|
func (s *AoCState) AoCBoardIsNew(yr int) bool {
|
|
|
|
l, err := s.aoc.GetCachedLeaderboard(yr)
|
|
|
|
if err != nil || l == nil {
|
|
|
|
if err.Error() == "Invalid Year" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-11-13 00:45:56 +00:00
|
|
|
func (s *AoCState) RequestBoardId() {
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
fmt.Print("Advent of Code Board ID: ")
|
|
|
|
boardId, _ := reader.ReadString('\n')
|
|
|
|
s.setAoCBoardId(strings.TrimSpace(boardId))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *AoCState) RequestSessionCookie() {
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
fmt.Print("Advent of Code Session Cookie: ")
|
|
|
|
aocSession, _ := reader.ReadString('\n')
|
|
|
|
s.setAoCSessionCookie(strings.TrimSpace(aocSession))
|
2019-11-15 19:50:22 +00:00
|
|
|
s.sessionCookie = aocSession
|
2019-11-13 00:45:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The channel that we post updates to
|
|
|
|
func (s *AoCState) RequestChannelId() {
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
fmt.Print("Advent of Code Slack Channel ID: ")
|
|
|
|
chn, _ := reader.ReadString('\n')
|
|
|
|
s.setChannelId(strings.TrimSpace(chn))
|
|
|
|
}
|
|
|
|
|
2021-12-01 17:41:19 +00:00
|
|
|
// Returns true if we have all AoC years saved to the DB
|
|
|
|
func (s *AoCState) haveAllYearsCached() bool {
|
|
|
|
for _, yr := range s.GetListOfAoCYears() {
|
|
|
|
_, err := s.aoc.GetCachedLeaderboard(yr)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-11-13 00:45:56 +00:00
|
|
|
/* DB Functions */
|
|
|
|
func (s *AoCState) setAoCBoardId(brdId string) error {
|
|
|
|
return s.model.SetString([]string{"aoc", "config", "board_id"}, brdId)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *AoCState) getAoCBoardId() (string, error) {
|
|
|
|
return s.model.GetString([]string{"aoc", "config", "board_id"})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *AoCState) setAoCSessionCookie(sess string) error {
|
|
|
|
return s.model.SetString([]string{"aoc", "config", "session"}, sess)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *AoCState) getAoCSessionCookie() (string, error) {
|
|
|
|
return s.model.GetString([]string{"aoc", "config", "session"})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *AoCState) setChannelId(chn string) error {
|
|
|
|
return s.model.SetString([]string{"aoc", "config", "channel_id"}, chn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *AoCState) getChannelId() (string, error) {
|
|
|
|
return s.model.GetString([]string{"aoc", "config", "channel_id"})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *AoCState) saveLeaderboard(l *aoc.Leaderboard) error {
|
2022-12-01 13:28:02 +00:00
|
|
|
err := s.model.SetInt([]string{"aoc", "leaderboards", l.Event, "owner_id"}, l.OwnerID)
|
2019-11-13 00:45:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = s.model.SetString([]string{"aoc", "leaderboards", l.Event, "last_fetch"}, l.LastFetch.Format(time.RFC3339))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, v := range l.Members {
|
|
|
|
if err = s.saveMember(l.Event, &v); err != nil {
|
2023-12-01 15:26:25 +00:00
|
|
|
s.model.SendMessage(models.NewErrorBotMessage(s.Name(), models.MsgSrcApp, fmt.Sprintf("Error Saving Member (%s)", v.Name)))
|
2019-11-13 00:45:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *AoCState) saveMember(event string, m *aoc.Member) error {
|
2022-12-01 13:28:02 +00:00
|
|
|
strId := strconv.Itoa(m.ID)
|
|
|
|
err := s.model.SetString([]string{"aoc", "leaderboards", event, "members", strId, "id"}, strId)
|
2019-11-13 00:45:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-01 13:28:02 +00:00
|
|
|
err = s.model.SetString([]string{"aoc", "leaderboards", event, "members", strId, "stars"}, strconv.Itoa(m.Stars))
|
2019-11-13 00:45:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-01 13:28:02 +00:00
|
|
|
err = s.model.SetString([]string{"aoc", "leaderboards", event, "members", strId, "last_star_ts"}, m.LastStarTs.Format(time.RFC3339))
|
2019-11-13 00:45:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-01 13:28:02 +00:00
|
|
|
err = s.model.SetString([]string{"aoc", "leaderboards", event, "members", strId, "name"}, m.Name)
|
2019-11-13 00:45:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-01 13:28:02 +00:00
|
|
|
err = s.model.SetString([]string{"aoc", "leaderboards", event, "members", strId, "local_score"}, strconv.Itoa(m.LocalScore))
|
2019-11-13 00:45:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-01 13:28:02 +00:00
|
|
|
err = s.model.SetString([]string{"aoc", "leaderboards", event, "members", strId, "global_score"}, strconv.Itoa(m.GlobalScore))
|
2019-11-13 00:45:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-01 13:28:02 +00:00
|
|
|
func (s *AoCState) getMemberAllYears(memberId int) map[string]*aoc.Member {
|
2021-12-01 17:41:19 +00:00
|
|
|
ret := make(map[string]*aoc.Member)
|
|
|
|
for _, yr := range s.GetListOfAoCYears() {
|
|
|
|
stYr := strconv.Itoa(yr)
|
|
|
|
m, err := s.getMember(stYr, memberId)
|
|
|
|
if err == nil {
|
|
|
|
ret[stYr] = m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2022-12-01 13:28:02 +00:00
|
|
|
func (s *AoCState) getMember(event string, memberId int) (*aoc.Member, error) {
|
2019-11-13 00:45:56 +00:00
|
|
|
var err error
|
|
|
|
var wrk string
|
|
|
|
mbr := new(aoc.Member)
|
2022-12-01 13:28:02 +00:00
|
|
|
mbrPath := []string{"aoc", "leaderboards", event, "members", strconv.Itoa(memberId)}
|
|
|
|
mbr.ID, err = s.model.GetInt(append(mbrPath, "id"))
|
2019-11-13 00:45:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
wrk, err = s.model.GetString(append(mbrPath, "stars"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
mbr.Stars, err = strconv.Atoi(wrk)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
wrk, err = s.model.GetString(append(mbrPath, "last_star_ts"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
mbr.LastStarTs, err = time.Parse(time.RFC3339, wrk)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
mbr.Name, err = s.model.GetString(append(mbrPath, "name"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
wrk, err = s.model.GetString(append(mbrPath, "local_score"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
mbr.LocalScore, err = strconv.Atoi(wrk)
|
|
|
|
wrk, err = s.model.GetString(append(mbrPath, "global_score"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
mbr.GlobalScore, err = strconv.Atoi(wrk)
|
|
|
|
return mbr, nil
|
|
|
|
}
|
2019-11-21 23:45:04 +00:00
|
|
|
|
|
|
|
func (s *AoCState) SendAdminIdError() {
|
2023-12-01 15:26:25 +00:00
|
|
|
s.model.SendMessage(models.NewBotMessage(s.Name(), models.MsgSrcApp, models.MsgTpError, models.MsgSrcApp, "No Admin ID Found"))
|
2019-11-21 23:45:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *AoCState) SendSlackMessage(text, dest string) {
|
2023-12-01 15:26:25 +00:00
|
|
|
s.model.SendMessage(models.NewBotMessage(s.Name(), models.MsgSrcSlack, models.MsgTpMessage, dest, text))
|
2019-11-21 23:45:04 +00:00
|
|
|
}
|