mirror of https://github.com/velour/catbase.git
Compare commits
4 Commits
0aaa3cb136
...
92da19e5a8
Author | SHA1 | Date |
---|---|---|
Chris Sexton | 92da19e5a8 | |
Chris Sexton | 44159c26fc | |
Chris Sexton | f7340983e9 | |
Chris Sexton | c20b4c8b17 |
|
@ -12,7 +12,7 @@ import (
|
|||
"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()
|
||||
|
||||
type CountdownPlugin struct {
|
||||
|
|
|
@ -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(),
|
||||
|
|
|
@ -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)
|
||||
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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue