Compare commits

...

4 Commits

Author SHA1 Message Date
Chris Sexton 92da19e5a8
Merge pull request #327 from velour/omnibus
newsbid: update for season 2
2020-12-31 14:14:20 -05:00
Chris Sexton 44159c26fc newsbid: update for season 2
Fixes #326
2020-12-31 14:08:38 -05:00
Chris Sexton f7340983e9
Merge pull request #325 from velour/omnibus
countdown: fix timer
2020-12-31 12:40:18 -05:00
Chris Sexton c20b4c8b17 countdown: fix timer 2020-12-31 12:36:48 -05:00
4 changed files with 27 additions and 29 deletions

View File

@ -12,7 +12,7 @@ import (
"github.com/velour/catbase/config" "github.com/velour/catbase/config"
) )
var nextYear = time.Date(time.Now().Year()+1, time.January, 0, 0, 0, 0, 0, time.Local) var nextYear = time.Date(time.Now().Year()+1, time.January, 1, 0, 0, 0, 1, time.Local)
var thisYear = time.Now().Year() var thisYear = time.Now().Year()
type CountdownPlugin struct { type CountdownPlugin struct {

View File

@ -19,7 +19,12 @@ type NewsBid struct {
} }
func New(b bot.Bot) *NewsBid { 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{ p := &NewsBid{
bot: b, bot: b,
db: b.DB(), db: b.DB(),

View File

@ -35,7 +35,11 @@ func (is Items) Titles() string {
out := "" out := ""
for _, v := range is { for _, v := range is {
hnURL := fmt.Sprintf("https://news.ycombinator.com/item?id=%d", v.ID) 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 return out
} }

View File

@ -3,7 +3,6 @@ package webshit
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"math"
"net/url" "net/url"
"strconv" "strconv"
"time" "time"
@ -20,12 +19,7 @@ type Config struct {
HNFeed string HNFeed string
HNLimit int HNLimit int
BalanceReferesh int BalanceReferesh int
} HouseName string
var DefaultConfig = Config{
HNFeed: "topstories",
HNLimit: 10,
BalanceReferesh: 100,
} }
type Webshit struct { type Webshit struct {
@ -71,10 +65,6 @@ type WeeklyResult struct {
Score int Score int
} }
func New(db *sqlx.DB) *Webshit {
return NewConfig(db, DefaultConfig)
}
func NewConfig(db *sqlx.DB, cfg Config) *Webshit { func NewConfig(db *sqlx.DB, cfg Config) *Webshit {
w := &Webshit{db: db, config: cfg} w := &Webshit{db: db, config: cfg}
w.setup() 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 { func (w *Webshit) checkBids(bids []Bid, storyMap map[string]hn.Item) []WeeklyResult {
var wins []Bid var wins []Bid
total, totalWinning := 0.0, 0.0 total, totalWinning := 0.0, 0.0
wr := map[string]WeeklyResult{} wr := map[string]WeeklyResult{}
houseName := w.config.HouseName
houseScore := 0
for _, b := range bids { for _, b := range bids {
score := w.GetScore(b.User) score := w.GetScore(b.User)
if _, ok := wr[b.User]; !ok { 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, Bid: b.BidStr,
} }
rec.LosingArticles = append(rec.LosingArticles, bid) rec.LosingArticles = append(rec.LosingArticles, bid)
houseScore += b.Bid
} }
total += float64(b.Bid) total += float64(b.Bid)
wr[b.User] = rec wr[b.User] = rec
} }
for _, b := range wins { 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 := wr[b.User]
rec.Won += int(payout) rec.Won += b.Bid
rec.Score += int(payout) rec.Score += b.Bid
wr[b.User] = rec wr[b.User] = rec
} }
wr[houseName] = WeeklyResult{
User: houseName,
Score: w.GetScore(houseName) + houseScore,
Won: houseScore,
}
return wrMapToSlice(wr) return wrMapToSlice(wr)
} }
@ -350,8 +339,8 @@ func (w *Webshit) getStoryByURL(URL string) (hn.Item, error) {
func (w *Webshit) updateScores(results []WeeklyResult) error { func (w *Webshit) updateScores(results []WeeklyResult) error {
tx := w.db.MustBegin() tx := w.db.MustBegin()
for _, res := range results { for _, res := range results {
if _, err := tx.Exec(`update webshit_balances set score=? where user=?`, if _, err := tx.Exec(`insert into webshit_balances (user, balance, score) values (?, 0, ?) on conflict(user) do update set score=excluded.score`,
res.Score, res.User); err != nil { res.User, res.Score); err != nil {
if err := tx.Rollback(); err != nil { if err := tx.Rollback(); err != nil {
return err return err
} }