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"
)
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()
type CountdownPlugin struct {

View File

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

View File

@ -35,11 +35,7 @@ func (is Items) Titles() string {
out := ""
for _, v := range is {
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)
}
}
return out
}

View File

@ -3,6 +3,7 @@ package webshit
import (
"bytes"
"fmt"
"math"
"net/url"
"strconv"
"time"
@ -19,7 +20,12 @@ type Config struct {
HNFeed string
HNLimit int
BalanceReferesh int
HouseName string
}
var DefaultConfig = Config{
HNFeed: "topstories",
HNLimit: 10,
BalanceReferesh: 100,
}
type Webshit struct {
@ -65,6 +71,10 @@ 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()
@ -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 {
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 {
@ -178,23 +186,26 @@ 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 {
rec := wr[b.User]
rec.Won += b.Bid
rec.Score += b.Bid
wr[b.User] = rec
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)
}
wr[houseName] = WeeklyResult{
User: houseName,
Score: w.GetScore(houseName) + houseScore,
Won: houseScore,
payout := float64(b.Bid) / totalWinning * total * ratio
rec := wr[b.User]
rec.Won += int(payout)
rec.Score += int(payout)
wr[b.User] = rec
}
return wrMapToSlice(wr)
@ -339,8 +350,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(`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.Exec(`update webshit_balances set score=? where user=?`,
res.Score, res.User); err != nil {
if err := tx.Rollback(); err != nil {
return err
}