package main import ( "C" ) import ( "bufio" "errors" "fmt" "os" "sort" "strconv" "strings" "time" aoc "git.bullercodeworks.com/brian/go-adventofcode" "git.bullercodeworks.com/brian/helperbot" "github.com/nlopes/slack" ) /* Plugin State */ type AoCState struct { model helperbot.Model boardId string sessionCookie string sessionNeedsUpdate bool aoc *aoc.AoC lastYear int } var State AoCState /* Plugin Interface Functions */ func (s *AoCState) Name() string { return "advent-of-code" } func (s *AoCState) Initialize(m helperbot.Model) error { // 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() } else { s.sessionCookie = aocSession } aocChannelId, err = s.getChannelId() aocChannelId = strings.TrimSpace(aocChannelId) if err != nil || aocChannelId == "" { s.RequestChannelId() } if err = s.NewAoC(); err != nil { return err } return nil } func (s *AoCState) ProcessMessage(m helperbot.Message) { if m.GetSource() == "slack" { slackMsg := m.GetMessage() if len(slackMsg.Channel) == 0 { return } switch slackMsg.Channel[0] { case 'C', 'G': s.ProcessChannelMessage(slackMsg) case 'D': admin, err := s.model.GetSlackAdminDMId() if err != nil { s.SendAdminIdError() return } if slackMsg.Channel == admin { s.ProcessAdminDirectMessage(slackMsg) } else { s.ProcessDirectMessage(slackMsg) } } } } func (s *AoCState) ProcessRTMEvent(msg slack.RTMEvent) {} 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() { for { _, err := s.getChannelId() // This plugin fails without a channel id if err != nil { return } for _, yr := range s.GetListOfAoCYears() { if !s.sessionNeedsUpdate { if s.GetLatestYear() != s.lastYear { // Latest year changed. Grab that board first. s.lastYear = s.GetLatestYear() s.AoCBoardCheckAndUpdate(s.lastYear) time.Sleep(time.Minute) } s.AoCBoardCheckAndUpdate(yr) time.Sleep(time.Minute) } } time.Sleep(time.Minute) } s.model.SendMessage(s.Name(), "main", s.BuildMessage("status", "done", "")) } func (s *AoCState) ProcessDirectMessage(slackMsg slack.Message) { msgPts := strings.Fields(slackMsg.Text) if len(msgPts) < 2 || msgPts[0] != "!aoc" { return } switch msgPts[1] { case "help": s.DoHelpCmd(slackMsg) case "ping": s.DoPingCmd(slackMsg) case "top": s.DoTopCmd(slackMsg) } } func (s *AoCState) ProcessAdminDirectMessage(slackMsg slack.Message) { msgPts := strings.Fields(slackMsg.Text) if len(msgPts) < 2 || msgPts[0] != "!aoc" { return } switch msgPts[1] { case "help": s.DoHelpAdminCmd(slackMsg) case "ping": s.DoPingCmd(slackMsg) case "top": s.DoTopCmd(slackMsg) case "session": s.DoSessionCmd(slackMsg) } } func (s *AoCState) ProcessChannelMessage(slackMsg slack.Message) { msgPts := strings.Fields(slackMsg.Text) if len(msgPts) < 2 || msgPts[0] != "!aoc" { return } switch msgPts[1] { case "top": s.DoTopCmd(slackMsg) } } func (s *AoCState) DoHelpCmd(slackMsg slack.Message) { txt := fmt.Sprint(":christmas_tree: AoC Help :christmas_tree:\n", "-- WiP --", ) s.SendSlackMessage(txt, slackMsg.Channel) } func (s *AoCState) DoHelpAdminCmd(slackMsg slack.Message) { txt := fmt.Sprint(":christmas_tree: AoC Help :christmas_tree:\n", "-- WiP --", ) s.SendSlackMessage(txt, slackMsg.Channel) } func (s *AoCState) DoPingCmd(slackMsg slack.Message) { s.SendSlackMessage(":christmas_tree: PONG :christmas_tree:", slackMsg.Channel) } func (s *AoCState) DoSessionCmd(slackMsg slack.Message) { msgPts := strings.Fields(slackMsg.Text) if len(msgPts) == 3 && msgPts[1] == "session" { // Set the session cookie admin, err := s.model.GetSlackAdminDMId() if err != nil { s.SendAdminIdError() return } aocSession := msgPts[2] s.setAoCSessionCookie(strings.TrimSpace(aocSession)) s.sessionCookie = aocSession s.sessionNeedsUpdate = false s.SendSlackMessage(":christmas_tree: New Session: "+s.sessionCookie, admin) } else if len(msgPts) == 2 && msgPts[1] == "session" { // Print the session cookie admin, err := s.model.GetSlackAdminDMId() if err != nil { s.SendAdminIdError() return } // We only send the session cookie to the admin s.SendSlackMessage(":christmas_tree: session: "+s.sessionCookie, admin) } } func (s *AoCState) DoTopCmd(slackMsg slack.Message) { msgPts := strings.Fields(slackMsg.Text) 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) } s.SendSlackMessage(txt, slackMsg.Channel) } 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) } func (s *AoCState) AoCBoardCheckAndUpdate(yr int) { channelId, _ := s.getChannelId() if s.AoCBoardNeedsUpdate(yr) { l, err := s.aoc.GetLeaderboard(yr) if err != nil { admin, adminErr := s.model.GetSlackAdminDMId() if adminErr != nil { 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 } s.model.SendMessage(s.Name(), "main", s.BuildMessage("success", fmt.Sprintf("Received leaderboard (%d)", yr), "")) // 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 { if yr == s.GetLatestYear() { s.SendSlackMessage(fmt.Sprintf(":christmas_tree: %s now has %d stars! :christmas_tree:", v.Name, v.Stars), channelId) } else { s.SendSlackMessage(fmt.Sprintf(":christmas_tree: %s now has %d stars! (%d) :christmas_tree:", v.Name, v.Stars, yr), channelId) } } } // Save the leaderboard to the db s.saveLeaderboard(l) } } func (s *AoCState) AoCBoardNeedsUpdate(yr int) bool { var freshDt time.Duration if yr == s.GetLatestYear() { freshDt, _ = time.ParseDuration("15m") // AoC asks that we not update more than once every 15 minutes } 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 } func (s *AoCState) AoCBoardIsNew(yr int) bool { l, err := s.aoc.GetCachedLeaderboard(yr) fmt.Println("Checking if board is new") if err != nil || l == nil { if err.Error() == "Invalid Year" { return false } fmt.Println("YUP!") return true } return false } 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)) s.sessionCookie = aocSession } // 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)) } /* 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 { err := s.model.SetString([]string{"aoc", "leaderboards", l.Event, "owner_id"}, l.OwnerID) 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 { s.model.SendMessage(s.Name(), "error", s.BuildMessage("error", fmt.Sprintf("Error Saving Member (%s)", v.Name), "")) } } return nil } func (s *AoCState) saveMember(event string, m *aoc.Member) error { err := s.model.SetString([]string{"aoc", "leaderboards", event, "members", m.ID, "id"}, m.ID) if err != nil { return err } err = s.model.SetString([]string{"aoc", "leaderboards", event, "members", m.ID, "stars"}, strconv.Itoa(m.Stars)) if err != nil { return err } err = s.model.SetString([]string{"aoc", "leaderboards", event, "members", m.ID, "last_star_ts"}, m.LastStarTs.Format(time.RFC3339)) if err != nil { return err } err = s.model.SetString([]string{"aoc", "leaderboards", event, "members", m.ID, "name"}, m.Name) if err != nil { return err } err = s.model.SetString([]string{"aoc", "leaderboards", event, "members", m.ID, "local_score"}, strconv.Itoa(m.LocalScore)) if err != nil { return err } err = s.model.SetString([]string{"aoc", "leaderboards", event, "members", m.ID, "global_score"}, strconv.Itoa(m.GlobalScore)) if err != nil { return err } return nil } func (s *AoCState) getMember(event string, memberId string) (*aoc.Member, error) { var err error var wrk string mbr := new(aoc.Member) mbrPath := []string{"aoc", "leaderboards", event, "members", memberId} mbr.ID, err = s.model.GetString(append(mbrPath, "id")) 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 } func (s *AoCState) SendAdminIdError() { s.model.SendMessage(s.Name(), "error", s.BuildMessage("error", "Error getting Admin DM Id", "")) } func (s *AoCState) SendSlackMessage(text, dest string) { s.model.SendMessage(s.Name(), "slack", s.BuildMessage("message", text, dest)) } func (s *AoCState) BuildMessage(tp, text, ch string) slack.Message { ret := slack.Message{} ret.Type = tp ret.Text = text ret.Channel = ch return ret }