Brian Buller
14e2827377
* Fix issue with parsing AOC leaderboard JSON * Add aoc-util for different things * go mod * Makefile
212 lines
4.2 KiB
Go
212 lines
4.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"sort"
|
|
"strconv"
|
|
"time"
|
|
|
|
"git.bullercodeworks.com/brian/boltease"
|
|
)
|
|
|
|
var db *boltease.DB
|
|
|
|
func main() {
|
|
_, err := os.Stat("helperbot.db")
|
|
if err != nil {
|
|
fmt.Println("Couldn't find helperbot.db (is it in the CWD?)")
|
|
os.Exit(1)
|
|
}
|
|
if len(os.Args) == 1 {
|
|
printUsage([]string{})
|
|
os.Exit(1)
|
|
}
|
|
run(os.Args[1:])
|
|
}
|
|
|
|
func run(args []string) int {
|
|
var cmd string
|
|
if len(args) == 0 {
|
|
printUsage(args)
|
|
return 1
|
|
}
|
|
var err error
|
|
db, err = boltease.Create("helperbot.db", 0600, nil)
|
|
if err != nil {
|
|
fmt.Println("Error opening database")
|
|
return 1
|
|
}
|
|
err = db.OpenDB()
|
|
if err != nil {
|
|
fmt.Println("Error opening database")
|
|
return 1
|
|
}
|
|
defer db.CloseDB()
|
|
|
|
cmd, args = args[0], args[1:]
|
|
switch cmd {
|
|
case "clear-stars":
|
|
return clearYearStars(args)
|
|
case "print-member-list":
|
|
return printMemberList(args)
|
|
case "view-member":
|
|
return viewMember(args)
|
|
}
|
|
return 0
|
|
}
|
|
|
|
func clearYearStars(args []string) int {
|
|
if len(args) < 1 {
|
|
printUsage([]string{"clear-stars"})
|
|
return 1
|
|
}
|
|
yr := args[0]
|
|
if !isValidYear(yr) {
|
|
fmt.Println("Invalid year provided")
|
|
return 1
|
|
}
|
|
var err error
|
|
var members []string
|
|
membersPath := []string{"aoc", "leaderboards", yr, "members"}
|
|
members, err = db.GetBucketList(membersPath)
|
|
if err != nil {
|
|
fmt.Println("Error getting member list for year", yr)
|
|
return 1
|
|
}
|
|
for _, v := range members {
|
|
memberPath := append(membersPath, v)
|
|
err = db.SetInt(memberPath, "stars", 0)
|
|
if err != nil {
|
|
fmt.Printf("Error resetting stars for [%s].\n", v)
|
|
fmt.Println(err)
|
|
fmt.Println("")
|
|
}
|
|
}
|
|
|
|
return 0
|
|
}
|
|
|
|
func printMemberList(args []string) int {
|
|
memberMap := make(map[string]string)
|
|
memberIdMap := make(map[string]string)
|
|
var memberIds []string
|
|
var memberNames []string
|
|
years := getYearsInDB()
|
|
for _, v := range years {
|
|
var err error
|
|
var members []string
|
|
yearMembersPath := []string{"aoc", "leaderboards", v, "members"}
|
|
members, err = db.GetBucketList(yearMembersPath)
|
|
if err != nil {
|
|
fmt.Println("Error getting member list for year:", v)
|
|
continue
|
|
}
|
|
for _, mid := range members {
|
|
memberPath := append(yearMembersPath, mid)
|
|
_, ok := memberIdMap[mid]
|
|
if !ok {
|
|
memberIds = append(memberIds, mid)
|
|
name, err := db.GetValue(memberPath, "name")
|
|
if err != nil {
|
|
continue
|
|
}
|
|
memberNames = append(memberNames, name)
|
|
memberMap[name] = mid
|
|
memberIdMap[mid] = name
|
|
}
|
|
}
|
|
}
|
|
|
|
sort.Strings(memberNames)
|
|
for _, v := range memberNames {
|
|
fmt.Printf("%s %s\n", v, memberMap[v])
|
|
}
|
|
return 0
|
|
}
|
|
|
|
func viewMember(args []string) int {
|
|
if len(args) == 0 {
|
|
printUsage([]string{"view-member"})
|
|
return 1
|
|
}
|
|
years := getYearsInDB()
|
|
mId := args[0]
|
|
pInfo := false
|
|
for _, v := range years {
|
|
memberPath := []string{"aoc", "leaderboards", v, "members", mId}
|
|
if !pInfo {
|
|
name, err := db.GetValue(memberPath, "name")
|
|
if err != nil {
|
|
continue
|
|
}
|
|
fmt.Println(name, mId)
|
|
pInfo = true
|
|
}
|
|
stars, err := db.GetValue(memberPath, "stars")
|
|
if err != nil {
|
|
continue
|
|
}
|
|
fmt.Println(v, "stars:", stars)
|
|
}
|
|
|
|
return 0
|
|
}
|
|
|
|
func printUsage(args []string) {
|
|
if len(args) == 0 {
|
|
fmt.Println("Usage: aoc-util [command]")
|
|
fmt.Println("Commands:")
|
|
fmt.Println(" clear-stars [year] Reset all member's star count for the given year")
|
|
return
|
|
}
|
|
switch args[0] {
|
|
case "clear-stars":
|
|
fmt.Println("Usage: aoc-util clear-stars [year]")
|
|
case "print-member-list":
|
|
fmt.Println("Usage: aoc-util print-member-list")
|
|
case "print-member":
|
|
fmt.Println("Usage: aoc-util view-member [id]")
|
|
}
|
|
}
|
|
|
|
func getLatestYear() int {
|
|
latestYear := time.Now().Year()
|
|
if time.Now().Month() < 12 {
|
|
latestYear--
|
|
}
|
|
return latestYear
|
|
}
|
|
|
|
func getListOfAoCYears() []int {
|
|
var ret []int
|
|
for k := getLatestYear(); k > 2014; k-- {
|
|
ret = append(ret, k)
|
|
}
|
|
return ret
|
|
}
|
|
|
|
func getYearsInDB() []string {
|
|
leaderboardsPath := []string{"aoc", "leaderboards"}
|
|
years, err := db.GetBucketList(leaderboardsPath)
|
|
if err != nil {
|
|
fmt.Println("Error getting year list")
|
|
return []string{}
|
|
}
|
|
return years
|
|
}
|
|
|
|
func isValidYear(yr string) bool {
|
|
i, err := strconv.Atoi(yr)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
yrs := getListOfAoCYears()
|
|
for k := range yrs {
|
|
if yrs[k] == i {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|