From 44159c26fc4d483b582b3e63487bda50da833347 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Thu, 31 Dec 2020 14:08:38 -0500 Subject: [PATCH] newsbid: update for season 2 Fixes #326 --- plugins/newsbid/newsbid.go | 7 ++++- plugins/newsbid/webshit/hn/api.go | 6 ++++- plugins/newsbid/webshit/webshit.go | 41 +++++++++++------------------- 3 files changed, 26 insertions(+), 28 deletions(-) diff --git a/plugins/newsbid/newsbid.go b/plugins/newsbid/newsbid.go index d9daa73..d5191fb 100644 --- a/plugins/newsbid/newsbid.go +++ b/plugins/newsbid/newsbid.go @@ -19,7 +19,12 @@ type NewsBid struct { } func New(b bot.Bot) *NewsBid { - ws := webshit.New(b.DB()) + ws := webshit.NewConfig(b.DB(), webshit.Config{ + 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"), + }) p := &NewsBid{ bot: b, db: b.DB(), diff --git a/plugins/newsbid/webshit/hn/api.go b/plugins/newsbid/webshit/hn/api.go index 5cce8bc..86caf95 100644 --- a/plugins/newsbid/webshit/hn/api.go +++ b/plugins/newsbid/webshit/hn/api.go @@ -35,7 +35,11 @@ func (is Items) Titles() string { out := "" for _, v := range is { hnURL := fmt.Sprintf("https://news.ycombinator.com/item?id=%d", v.ID) - out += fmt.Sprintf("• %s <%s|%s> (<%s|Comments>)\n", v.Bid, v.URL, v.Title, hnURL) + if v.URL == "" { + out += fmt.Sprintf("• %s %s (<%s|Comments>)\n", v.Bid, v.Title, hnURL) + } else { + out += fmt.Sprintf("• %s <%s|%s> (<%s|Comments>)\n", v.Bid, v.URL, v.Title, hnURL) + } } return out } diff --git a/plugins/newsbid/webshit/webshit.go b/plugins/newsbid/webshit/webshit.go index beac1ee..8697285 100644 --- a/plugins/newsbid/webshit/webshit.go +++ b/plugins/newsbid/webshit/webshit.go @@ -3,7 +3,6 @@ package webshit import ( "bytes" "fmt" - "math" "net/url" "strconv" "time" @@ -20,12 +19,7 @@ type Config struct { HNFeed string HNLimit int BalanceReferesh int -} - -var DefaultConfig = Config{ - HNFeed: "topstories", - HNLimit: 10, - BalanceReferesh: 100, + HouseName string } type Webshit struct { @@ -71,10 +65,6 @@ type WeeklyResult struct { Score int } -func New(db *sqlx.DB) *Webshit { - return NewConfig(db, DefaultConfig) -} - func NewConfig(db *sqlx.DB, cfg Config) *Webshit { w := &Webshit{db: db, config: cfg} w.setup() @@ -158,11 +148,13 @@ func (w *Webshit) Check(last int64) ([]WeeklyResult, int64, error) { } func (w *Webshit) checkBids(bids []Bid, storyMap map[string]hn.Item) []WeeklyResult { - var wins []Bid total, totalWinning := 0.0, 0.0 wr := map[string]WeeklyResult{} + houseName := w.config.HouseName + houseScore := 0 + for _, b := range bids { score := w.GetScore(b.User) if _, ok := wr[b.User]; !ok { @@ -186,28 +178,25 @@ func (w *Webshit) checkBids(bids []Bid, storyMap map[string]hn.Item) []WeeklyRes Bid: b.BidStr, } rec.LosingArticles = append(rec.LosingArticles, bid) + houseScore += b.Bid } total += float64(b.Bid) wr[b.User] = rec } for _, b := range wins { - u, _ := url.Parse(b.URL) - id, _ := strconv.Atoi(u.Query().Get("id")) - item, err := hn.GetItem(id) - score := item.Score - comments := item.Descendants - ratio := 1.0 - if err != nil { - ratio = float64(score) / math.Max(float64(comments), 1.0) - } - payout := float64(b.Bid) / totalWinning * total * ratio rec := wr[b.User] - rec.Won += int(payout) - rec.Score += int(payout) + rec.Won += b.Bid + rec.Score += b.Bid wr[b.User] = rec } + wr[houseName] = WeeklyResult{ + User: houseName, + Score: w.GetScore(houseName) + houseScore, + Won: houseScore, + } + return wrMapToSlice(wr) } @@ -350,8 +339,8 @@ func (w *Webshit) getStoryByURL(URL string) (hn.Item, error) { func (w *Webshit) updateScores(results []WeeklyResult) error { tx := w.db.MustBegin() for _, res := range results { - if _, err := tx.Exec(`update webshit_balances set score=? where user=?`, - res.Score, res.User); err != nil { + if _, err := tx.Exec(`insert into webshit_balances (user, balance, score) values (?, 0, ?) on conflict(user) do update set score=excluded.score`, + res.User, res.Score); err != nil { if err := tx.Rollback(); err != nil { return err }