2019-07-15 16:46:24 +00:00
|
|
|
package newsbid
|
|
|
|
|
|
|
|
import (
|
2019-07-15 20:55:35 +00:00
|
|
|
"fmt"
|
2021-12-20 17:40:10 +00:00
|
|
|
bh "github.com/timshannon/bolthold"
|
2021-01-31 23:08:25 +00:00
|
|
|
"regexp"
|
2019-11-02 22:18:36 +00:00
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2019-07-15 16:46:24 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
2021-01-31 23:08:25 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2019-07-15 16:46:24 +00:00
|
|
|
"github.com/velour/catbase/bot"
|
2019-07-15 20:55:35 +00:00
|
|
|
"github.com/velour/catbase/plugins/newsbid/webshit"
|
2019-07-15 16:46:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type NewsBid struct {
|
2021-12-20 17:40:10 +00:00
|
|
|
bot bot.Bot
|
|
|
|
db *sqlx.DB
|
|
|
|
store *bh.Store
|
|
|
|
ws *webshit.Webshit
|
2019-07-15 16:46:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(b bot.Bot) *NewsBid {
|
2021-12-20 17:40:10 +00:00
|
|
|
ws := webshit.NewConfig(b.Store(), webshit.Config{
|
2020-12-31 19:08:38 +00:00
|
|
|
HNFeed: b.Config().GetString("webshit.hnfeed", "topstories"),
|
|
|
|
HNLimit: b.Config().GetInt("webshit.hnlimit", 10),
|
|
|
|
BalanceReferesh: b.Config().GetInt("webshit.balancerefresh", 100),
|
|
|
|
HouseName: b.Config().GetString("webshit.housename", "house"),
|
|
|
|
})
|
2019-07-15 16:46:24 +00:00
|
|
|
p := &NewsBid{
|
2021-12-20 17:40:10 +00:00
|
|
|
bot: b,
|
|
|
|
store: b.Store(),
|
|
|
|
ws: ws,
|
2019-07-15 16:46:24 +00:00
|
|
|
}
|
2021-01-31 23:08:25 +00:00
|
|
|
|
|
|
|
p.bot.RegisterRegexCmd(p, bot.Message, balanceRegex, p.balanceCmd)
|
|
|
|
p.bot.RegisterRegexCmd(p, bot.Message, bidsRegex, p.bidsCmd)
|
|
|
|
p.bot.RegisterRegexCmd(p, bot.Message, scoresRegex, p.scoresCmd)
|
|
|
|
p.bot.RegisterRegexCmd(p, bot.Message, bidRegex, p.bidCmd)
|
|
|
|
p.bot.RegisterRegexCmd(p, bot.Message, checkRegex, p.checkCmd)
|
|
|
|
|
2019-07-15 16:46:24 +00:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2021-01-31 23:08:25 +00:00
|
|
|
var balanceRegex = regexp.MustCompile(`(?i)^balance$`)
|
|
|
|
var bidsRegex = regexp.MustCompile(`(?i)^bids$`)
|
|
|
|
var scoresRegex = regexp.MustCompile(`(?i)^scores$`)
|
|
|
|
var bidRegex = regexp.MustCompile(`(?i)^bid (?P<amount>\S+) (?P<url>)\S+$`)
|
|
|
|
var checkRegex = regexp.MustCompile(`(?i)^check ngate$`)
|
|
|
|
|
|
|
|
func (p *NewsBid) balanceCmd(r bot.Request) bool {
|
|
|
|
if !r.Msg.Command {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
bal := p.ws.GetBalance(r.Msg.User.Name)
|
|
|
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("%s, your current balance is %d.",
|
2021-12-21 19:08:20 +00:00
|
|
|
r.Msg.User.Name, bal.Balance))
|
2021-01-31 23:08:25 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *NewsBid) bidsCmd(r bot.Request) bool {
|
|
|
|
ch := r.Msg.Channel
|
|
|
|
bids, err := p.ws.GetAllBids()
|
|
|
|
if err != nil {
|
|
|
|
p.bot.Send(r.Conn, bot.Message, ch, fmt.Sprintf("Error getting bids: %s", err))
|
2019-07-15 20:55:35 +00:00
|
|
|
return true
|
|
|
|
}
|
2021-01-31 23:08:25 +00:00
|
|
|
if len(bids) == 0 {
|
|
|
|
p.bot.Send(r.Conn, bot.Message, ch, "No bids to report.")
|
2019-07-15 20:55:35 +00:00
|
|
|
return true
|
|
|
|
}
|
2021-01-31 23:08:25 +00:00
|
|
|
sort.Slice(bids, func(i, j int) bool {
|
|
|
|
if bids[i].User == bids[j].User {
|
|
|
|
return bids[i].Bid > bids[j].Bid
|
2019-07-15 20:55:35 +00:00
|
|
|
}
|
2021-01-31 23:08:25 +00:00
|
|
|
return bids[i].User < bids[j].User
|
|
|
|
})
|
|
|
|
out := "Bids:\n"
|
|
|
|
for _, b := range bids {
|
|
|
|
hnURL := fmt.Sprintf("https://news.ycombinator.com/item?id=%d", b.HNID)
|
2021-09-18 14:24:39 +00:00
|
|
|
out += fmt.Sprintf("• %s bid %s on %s (%s)\n", b.User, b.BidStr, b.Title, hnURL)
|
2021-01-31 23:08:25 +00:00
|
|
|
}
|
|
|
|
p.bot.Send(r.Conn, bot.Message, ch, out)
|
|
|
|
return true
|
|
|
|
}
|
2019-07-15 20:55:35 +00:00
|
|
|
|
2021-01-31 23:08:25 +00:00
|
|
|
func (p *NewsBid) scoresCmd(r bot.Request) bool {
|
|
|
|
|
|
|
|
ch := r.Msg.Channel
|
|
|
|
bals, err := p.ws.GetAllBalances()
|
|
|
|
if err != nil {
|
|
|
|
p.bot.Send(r.Conn, bot.Message, ch, fmt.Sprintf("Error getting bids: %s", err))
|
|
|
|
return true
|
2019-07-15 20:55:35 +00:00
|
|
|
}
|
2021-01-31 23:08:25 +00:00
|
|
|
if len(bals) == 0 {
|
|
|
|
p.bot.Send(r.Conn, bot.Message, ch, "No balances to report.")
|
2019-07-15 20:55:35 +00:00
|
|
|
return true
|
|
|
|
}
|
2021-01-31 23:08:25 +00:00
|
|
|
out := "NGate balances:\n"
|
|
|
|
sort.Sort(bals)
|
|
|
|
for _, b := range bals {
|
2021-06-17 16:57:52 +00:00
|
|
|
if b.Score > 0 {
|
|
|
|
out += fmt.Sprintf("%s has a total score of %d with %d left to bid this session\n", b.User, b.Score, b.Balance)
|
|
|
|
}
|
2021-01-31 23:08:25 +00:00
|
|
|
}
|
|
|
|
p.bot.Send(r.Conn, bot.Message, ch, out)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *NewsBid) bidCmd(r bot.Request) bool {
|
|
|
|
body := strings.ToLower(r.Msg.Body)
|
|
|
|
ch := r.Msg.Channel
|
|
|
|
conn := r.Conn
|
|
|
|
|
|
|
|
log.Debug().Interface("request", r).Msg("bid request")
|
|
|
|
|
|
|
|
parts := strings.Fields(body)
|
|
|
|
if len(parts) != 3 {
|
|
|
|
p.bot.Send(conn, bot.Message, ch, "You must bid with an amount and a URL.")
|
2019-07-15 20:55:35 +00:00
|
|
|
return true
|
|
|
|
}
|
2021-01-31 23:08:25 +00:00
|
|
|
|
|
|
|
amount, _ := strconv.Atoi(parts[1])
|
|
|
|
url := parts[2]
|
|
|
|
if bid, err := p.ws.Bid(r.Msg.User.Name, amount, parts[1], url); err != nil {
|
|
|
|
p.bot.Send(conn, bot.Message, ch, fmt.Sprintf("Error placing bid: %s", err))
|
|
|
|
} else {
|
|
|
|
p.bot.Send(conn, bot.Message, ch, fmt.Sprintf("Your bid has been placed on %s", bid.Title))
|
|
|
|
}
|
|
|
|
return true
|
2019-07-15 16:46:24 +00:00
|
|
|
}
|
2019-07-15 20:55:35 +00:00
|
|
|
|
2021-01-31 23:08:25 +00:00
|
|
|
func (p *NewsBid) checkCmd(r bot.Request) bool {
|
|
|
|
ch := r.Msg.Channel
|
|
|
|
conn := r.Conn
|
2019-11-22 16:51:48 +00:00
|
|
|
last := p.bot.Config().GetInt64("newsbid.lastprocessed", 0)
|
|
|
|
wr, pubTime, err := p.ws.Check(last)
|
2019-07-15 20:55:35 +00:00
|
|
|
if err != nil {
|
|
|
|
p.bot.Send(conn, bot.Message, ch, fmt.Sprintf("Error checking ngate: %s", err))
|
2021-01-31 23:08:25 +00:00
|
|
|
return true
|
2019-07-15 20:55:35 +00:00
|
|
|
}
|
2019-11-22 16:51:48 +00:00
|
|
|
p.bot.Config().Set("newsbid.lastprocessed", strconv.FormatInt(pubTime, 10))
|
2019-08-24 18:36:24 +00:00
|
|
|
|
|
|
|
topWon := 0
|
|
|
|
topSpread := 0
|
|
|
|
|
2019-07-15 20:55:35 +00:00
|
|
|
for _, res := range wr {
|
2019-08-24 18:36:24 +00:00
|
|
|
if res.Won > topWon {
|
|
|
|
topWon = res.Won
|
|
|
|
}
|
|
|
|
if len(res.WinningArticles) > topSpread {
|
|
|
|
topSpread = len(res.WinningArticles)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, res := range wr {
|
|
|
|
icon := ""
|
|
|
|
if res.Won == topWon {
|
|
|
|
icon += "🏆 "
|
|
|
|
}
|
|
|
|
if len(res.WinningArticles) == topSpread {
|
|
|
|
icon += "⭐️ "
|
|
|
|
}
|
|
|
|
msg := fmt.Sprintf("%s%s won %d for a score of %d",
|
|
|
|
icon, res.User, res.Won, res.Score)
|
2019-07-16 02:00:19 +00:00
|
|
|
if len(res.WinningArticles) > 0 {
|
2019-12-20 18:31:05 +00:00
|
|
|
msg += "\nWinning articles: \n" + res.WinningArticles.Titles()
|
2019-07-16 02:00:19 +00:00
|
|
|
}
|
|
|
|
if len(res.LosingArticles) > 0 {
|
2019-12-20 18:31:05 +00:00
|
|
|
msg += "\nLosing articles: \n" + res.LosingArticles.Titles()
|
2019-07-16 02:00:19 +00:00
|
|
|
}
|
2019-07-15 20:55:35 +00:00
|
|
|
p.bot.Send(conn, bot.Message, ch, msg)
|
|
|
|
}
|
2021-01-31 23:08:25 +00:00
|
|
|
return true
|
2019-07-15 20:55:35 +00:00
|
|
|
}
|