Compare commits

..

No commits in common. "92da19e5a8fc8974010abf2baef659f3f9ee57ae" and "0aaa3cb1369220c6dd7605a85fcf48a25d55486d" have entirely different histories.

4 changed files with 29 additions and 27 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, 1, 0, 0, 0, 1, time.Local) var nextYear = time.Date(time.Now().Year()+1, time.January, 0, 0, 0, 0, 0, time.Local)
var thisYear = time.Now().Year() var thisYear = time.Now().Year()
type CountdownPlugin struct { type CountdownPlugin struct {

View File

@ -19,12 +19,7 @@ type NewsBid struct {
} }
func New(b bot.Bot) *NewsBid { func New(b bot.Bot) *NewsBid {
ws := webshit.NewConfig(b.DB(), webshit.Config{ ws := webshit.New(b.DB())
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,11 +35,7 @@ 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)
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) out += fmt.Sprintf("• %s <%s|%s> (<%s|Comments>)\n", v.Bid, v.URL, v.Title, hnURL)
} }
}
return out return out
} }

View File

@ -3,6 +3,7 @@ package webshit
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"math"
"net/url" "net/url"
"strconv" "strconv"
"time" "time"
@ -19,7 +20,12 @@ 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 {
@ -65,6 +71,10 @@ 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()
@ -148,13 +158,11 @@ 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 {
@ -178,23 +186,26 @@ 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 {
rec := wr[b.User] u, _ := url.Parse(b.URL)
rec.Won += b.Bid id, _ := strconv.Atoi(u.Query().Get("id"))
rec.Score += b.Bid item, err := hn.GetItem(id)
wr[b.User] = rec 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
wr[houseName] = WeeklyResult{ rec := wr[b.User]
User: houseName, rec.Won += int(payout)
Score: w.GetScore(houseName) + houseScore, rec.Score += int(payout)
Won: houseScore, wr[b.User] = rec
} }
return wrMapToSlice(wr) return wrMapToSlice(wr)
@ -339,8 +350,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(`insert into webshit_balances (user, balance, score) values (?, 0, ?) on conflict(user) do update set score=excluded.score`, if _, err := tx.Exec(`update webshit_balances set score=? where user=?`,
res.User, res.Score); err != nil { res.Score, res.User); err != nil {
if err := tx.Rollback(); err != nil { if err := tx.Rollback(); err != nil {
return err return err
} }