mirror of https://github.com/velour/catbase.git
bid: fix losing story mode; pretty it up a little
This commit is contained in:
parent
286582417b
commit
bdfd6060a0
|
@ -102,8 +102,14 @@ func (p *NewsBid) check(conn bot.Connector, ch string) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, res := range wr {
|
for _, res := range wr {
|
||||||
msg := fmt.Sprintf("%s: won %d and lost %d for a score of %d",
|
msg := fmt.Sprintf("%s won %d and lost %d for a score of %d",
|
||||||
res.User, res.Won, res.Lost, res.Score)
|
res.User, res.Won, res.Lost, res.Score)
|
||||||
|
if len(res.WinningArticles) > 0 {
|
||||||
|
msg += "\nWinning articles: " + res.WinningArticles.Titles()
|
||||||
|
}
|
||||||
|
if len(res.LosingArticles) > 0 {
|
||||||
|
msg += "\nLosing articles: " + res.LosingArticles.Titles()
|
||||||
|
}
|
||||||
p.bot.Send(conn, bot.Message, ch, msg)
|
p.bot.Send(conn, bot.Message, ch, msg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,8 +14,21 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
HNFeed string
|
||||||
|
HNLimit int
|
||||||
|
BalanceReferesh int
|
||||||
|
}
|
||||||
|
|
||||||
|
var DefaultConfig = Config{
|
||||||
|
HNFeed: "topstories",
|
||||||
|
HNLimit: 10,
|
||||||
|
BalanceReferesh: 100,
|
||||||
|
}
|
||||||
|
|
||||||
type Webshit struct {
|
type Webshit struct {
|
||||||
db *sqlx.DB
|
db *sqlx.DB
|
||||||
|
config Config
|
||||||
}
|
}
|
||||||
|
|
||||||
type Story struct {
|
type Story struct {
|
||||||
|
@ -23,6 +36,19 @@ type Story struct {
|
||||||
URL string
|
URL string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Stories []Story
|
||||||
|
|
||||||
|
func (s Stories) Titles() string {
|
||||||
|
out := ""
|
||||||
|
for i, v := range s {
|
||||||
|
if i > 0 {
|
||||||
|
out += ", "
|
||||||
|
}
|
||||||
|
out += v.Title
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
type Bid struct {
|
type Bid struct {
|
||||||
ID int
|
ID int
|
||||||
User string
|
User string
|
||||||
|
@ -45,12 +71,18 @@ type Balance struct {
|
||||||
type WeeklyResult struct {
|
type WeeklyResult struct {
|
||||||
User string
|
User string
|
||||||
Won int
|
Won int
|
||||||
|
WinningArticles Stories
|
||||||
Lost int
|
Lost int
|
||||||
|
LosingArticles Stories
|
||||||
Score int
|
Score int
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(db *sqlx.DB) *Webshit {
|
func New(db *sqlx.DB) *Webshit {
|
||||||
w := &Webshit{db}
|
return NewConfig(db, DefaultConfig)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewConfig(db *sqlx.DB, cfg Config) *Webshit {
|
||||||
|
w := &Webshit{db: db, config: cfg}
|
||||||
w.setup()
|
w.setup()
|
||||||
return w
|
return w
|
||||||
}
|
}
|
||||||
|
@ -108,7 +140,8 @@ func (w *Webshit) Check() ([]WeeklyResult, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set all balances to 100
|
// Set all balances to 100
|
||||||
if _, err = w.db.Exec(`update webshit_balances set balance=100`); err != nil {
|
if _, err = w.db.Exec(`update webshit_balances set balance=?`,
|
||||||
|
w.config.BalanceReferesh); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -118,35 +151,39 @@ func (w *Webshit) Check() ([]WeeklyResult, error) {
|
||||||
func (w *Webshit) checkBids(bids []Bid, storyMap map[string]Story) []WeeklyResult {
|
func (w *Webshit) checkBids(bids []Bid, storyMap map[string]Story) []WeeklyResult {
|
||||||
wr := map[string]WeeklyResult{}
|
wr := map[string]WeeklyResult{}
|
||||||
for _, b := range bids {
|
for _, b := range bids {
|
||||||
win, loss := 0, 0
|
|
||||||
score := w.GetScore(b.User)
|
score := w.GetScore(b.User)
|
||||||
if s, ok := storyMap[b.Title]; ok {
|
if _, ok := wr[b.User]; !ok {
|
||||||
log.Info().Interface("story", s).Msg("won bid")
|
|
||||||
win = b.Bid
|
|
||||||
} else {
|
|
||||||
log.Info().Interface("story", s).Msg("lost bid")
|
|
||||||
loss = b.Bid
|
|
||||||
}
|
|
||||||
if res, ok := wr[b.User]; !ok {
|
|
||||||
wr[b.User] = WeeklyResult{
|
wr[b.User] = WeeklyResult{
|
||||||
User: b.User,
|
User: b.User,
|
||||||
Won: win,
|
Won: 0,
|
||||||
Lost: loss,
|
Lost: 0,
|
||||||
Score: score + win - loss,
|
Score: score,
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
rec := wr[b.User]
|
||||||
|
|
||||||
|
if s, ok := storyMap[b.Title]; ok {
|
||||||
|
log.Debug().Interface("story", s).Msg("won bid")
|
||||||
|
rec.Won += b.Bid
|
||||||
|
rec.Score += b.Bid
|
||||||
|
rec.WinningArticles = append(rec.WinningArticles, s)
|
||||||
|
log.Debug().Interface("story", s).Msg("Appending to winning log")
|
||||||
} else {
|
} else {
|
||||||
res.Won += win
|
log.Debug().Interface("story", s).Msg("lost bid")
|
||||||
res.Lost += loss
|
rec.Lost += b.Bid
|
||||||
res.Score += win - loss
|
rec.Score -= b.Bid
|
||||||
wr[b.User] = res
|
rec.LosingArticles = append(rec.LosingArticles, Story{Title: b.Title, URL: b.URL})
|
||||||
|
log.Debug().Interface("story", s).Msg("Appending to losing log")
|
||||||
}
|
}
|
||||||
|
wr[b.User] = rec
|
||||||
|
log.Debug().Interface("WR User", wr[b.User]).Str("user", b.User).Msg("setting WR")
|
||||||
}
|
}
|
||||||
return wrMapToSlice(wr)
|
return wrMapToSlice(wr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetHeadlines will return the current possible news headlines for bidding
|
// GetHeadlines will return the current possible news headlines for bidding
|
||||||
func (w *Webshit) GetHeadlines() ([]Story, error) {
|
func (w *Webshit) GetHeadlines() ([]Story, error) {
|
||||||
news := hacknews.Initializer{Story: "topstories", NbPosts: 10}
|
news := hacknews.Initializer{Story: w.config.HNFeed, NbPosts: w.config.HNLimit}
|
||||||
ids, err := news.GetCodesStory()
|
ids, err := news.GetCodesStory()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -2,12 +2,19 @@ package webshit
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
|
"github.com/rs/zerolog"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
log.Logger = log.Logger.Output(zerolog.ConsoleWriter{Out: os.Stderr})
|
||||||
|
}
|
||||||
|
|
||||||
func make(t *testing.T) *Webshit {
|
func make(t *testing.T) *Webshit {
|
||||||
db := sqlx.MustOpen("sqlite3", "file::memory:?mode=memory&cache=shared")
|
db := sqlx.MustOpen("sqlite3", "file::memory:?mode=memory&cache=shared")
|
||||||
w := New(db)
|
w := New(db)
|
||||||
|
@ -51,3 +58,20 @@ func TestWebshit_GetBalance(t *testing.T) {
|
||||||
actual := w.GetBalance("foo")
|
actual := w.GetBalance("foo")
|
||||||
assert.Equal(t, expected, actual)
|
assert.Equal(t, expected, actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWebshit_checkBids(t *testing.T) {
|
||||||
|
w := make(t)
|
||||||
|
bids := []Bid{
|
||||||
|
Bid{User: "foo", Title: "bar", URL: "baz", Bid: 10},
|
||||||
|
Bid{User: "foo", Title: "bar2", URL: "baz2", Bid: 10},
|
||||||
|
}
|
||||||
|
storyMap := map[string]Story{
|
||||||
|
"bar": Story{Title: "bar", URL: "baz"},
|
||||||
|
}
|
||||||
|
result := w.checkBids(bids, storyMap)
|
||||||
|
assert.Len(t, result, 1)
|
||||||
|
if len(result) > 0 {
|
||||||
|
assert.Len(t, result[0].WinningArticles, 1)
|
||||||
|
assert.Len(t, result[0].LosingArticles, 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue