2019-07-15 16:46:24 +00:00
|
|
|
package newsbid
|
|
|
|
|
|
|
|
import (
|
2019-07-15 20:55:35 +00:00
|
|
|
"fmt"
|
2019-11-02 22:18:36 +00:00
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2019-07-15 16:46:24 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
"github.com/velour/catbase/bot"
|
|
|
|
"github.com/velour/catbase/bot/msg"
|
2019-07-15 20:55:35 +00:00
|
|
|
"github.com/velour/catbase/plugins/newsbid/webshit"
|
2019-07-15 16:46:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type NewsBid struct {
|
|
|
|
bot bot.Bot
|
|
|
|
db *sqlx.DB
|
2019-07-15 20:55:35 +00:00
|
|
|
ws *webshit.Webshit
|
2019-07-15 16:46:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(b bot.Bot) *NewsBid {
|
2019-07-15 20:55:35 +00:00
|
|
|
ws := webshit.New(b.DB())
|
2019-07-15 16:46:24 +00:00
|
|
|
p := &NewsBid{
|
|
|
|
bot: b,
|
|
|
|
db: b.DB(),
|
2019-07-15 20:55:35 +00:00
|
|
|
ws: ws,
|
2019-07-15 16:46:24 +00:00
|
|
|
}
|
|
|
|
p.bot.Register(p, bot.Message, p.message)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *NewsBid) message(conn bot.Connector, k bot.Kind, message msg.Message, args ...interface{}) bool {
|
2019-07-15 20:55:35 +00:00
|
|
|
body := strings.ToLower(message.Body)
|
|
|
|
ch := message.Channel
|
|
|
|
if message.Command && body == "balance" {
|
|
|
|
bal := p.ws.GetBalance(message.User.Name)
|
|
|
|
p.bot.Send(conn, bot.Message, ch, fmt.Sprintf("%s, your current balance is %d.",
|
|
|
|
message.User.Name, bal))
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if message.Command && body == "bids" {
|
|
|
|
bids, err := p.ws.GetAllBids()
|
|
|
|
if err != nil {
|
|
|
|
p.bot.Send(conn, bot.Message, ch, fmt.Sprintf("Error getting bids: %s", err))
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if len(bids) == 0 {
|
|
|
|
p.bot.Send(conn, bot.Message, ch, "No bids to report.")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
sort.Slice(bids, func(i, j int) bool { return bids[i].User < bids[j].User })
|
|
|
|
out := "Bids:\n"
|
|
|
|
for _, b := range bids {
|
2019-11-02 22:18:36 +00:00
|
|
|
out += fmt.Sprintf("%s bid %d on <%s|%s> \n", b.User, b.Bid, b.URL, b.Title)
|
2019-07-15 20:55:35 +00:00
|
|
|
}
|
|
|
|
p.bot.Send(conn, bot.Message, ch, out)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if message.Command && body == "scores" {
|
|
|
|
bals, err := p.ws.GetAllBalances()
|
|
|
|
if err != nil {
|
|
|
|
p.bot.Send(conn, bot.Message, ch, fmt.Sprintf("Error getting bids: %s", err))
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if len(bals) == 0 {
|
|
|
|
p.bot.Send(conn, bot.Message, ch, "No balances to report.")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
out := "NGate balances:\n"
|
|
|
|
for _, b := range bals {
|
|
|
|
out += fmt.Sprintf("%s has a total score of %d with %d left to bid this session\n", b.User, b.Score, b.Balance)
|
|
|
|
}
|
|
|
|
p.bot.Send(conn, bot.Message, ch, out)
|
|
|
|
return true
|
|
|
|
|
|
|
|
}
|
|
|
|
if message.Command && strings.HasPrefix(body, "bid") {
|
|
|
|
parts := strings.Fields(body)
|
|
|
|
if len(parts) != 3 {
|
|
|
|
p.bot.Send(conn, bot.Message, ch, "You must bid with an amount and a URL.")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
amount, _ := strconv.Atoi(parts[1])
|
|
|
|
url := parts[2]
|
2019-07-16 19:55:38 +00:00
|
|
|
if bid, err := p.ws.Bid(message.User.Name, amount, url); err != nil {
|
2019-07-15 20:55:35 +00:00
|
|
|
p.bot.Send(conn, bot.Message, ch, fmt.Sprintf("Error placing bid: %s", err))
|
|
|
|
} else {
|
2019-07-16 19:55:38 +00:00
|
|
|
p.bot.Send(conn, bot.Message, ch, fmt.Sprintf("Your bid has been placed on %s", bid.Title))
|
2019-07-15 20:55:35 +00:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if message.Command && body == "check ngate" {
|
|
|
|
p.check(conn, ch)
|
|
|
|
return true
|
|
|
|
}
|
2019-07-15 16:46:24 +00:00
|
|
|
return false
|
|
|
|
}
|
2019-07-15 20:55:35 +00:00
|
|
|
|
|
|
|
func (p *NewsBid) check(conn bot.Connector, ch string) {
|
|
|
|
wr, err := p.ws.Check()
|
|
|
|
if err != nil {
|
|
|
|
p.bot.Send(conn, bot.Message, ch, fmt.Sprintf("Error checking ngate: %s", err))
|
|
|
|
return
|
|
|
|
}
|
2019-08-24 18:36:24 +00:00
|
|
|
|
|
|
|
topWon := 0
|
|
|
|
topSpread := 0
|
|
|
|
|
2019-07-15 20:55:35 +00:00
|
|
|
for _, res := range wr {
|
2019-08-24 18:36:24 +00:00
|
|
|
if res.Won > topWon {
|
|
|
|
topWon = res.Won
|
|
|
|
}
|
|
|
|
if len(res.WinningArticles) > topSpread {
|
|
|
|
topSpread = len(res.WinningArticles)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, res := range wr {
|
|
|
|
icon := ""
|
|
|
|
if res.Won == topWon {
|
|
|
|
icon += "🏆 "
|
|
|
|
}
|
|
|
|
if len(res.WinningArticles) == topSpread {
|
|
|
|
icon += "⭐️ "
|
|
|
|
}
|
|
|
|
msg := fmt.Sprintf("%s%s won %d for a score of %d",
|
|
|
|
icon, res.User, res.Won, res.Score)
|
2019-07-16 02:00:19 +00:00
|
|
|
if len(res.WinningArticles) > 0 {
|
|
|
|
msg += "\nWinning articles: " + res.WinningArticles.Titles()
|
|
|
|
}
|
|
|
|
if len(res.LosingArticles) > 0 {
|
|
|
|
msg += "\nLosing articles: " + res.LosingArticles.Titles()
|
|
|
|
}
|
2019-07-15 20:55:35 +00:00
|
|
|
p.bot.Send(conn, bot.Message, ch, msg)
|
|
|
|
}
|
|
|
|
}
|