From e2352a214ba7125630ca79802b5f846f6cb677ab Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Wed, 11 Mar 2020 12:25:34 -0400 Subject: [PATCH] newsbid: sort scores by score --- plugins/newsbid/newsbid.go | 1 + plugins/newsbid/webshit/webshit.go | 10 ++++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/plugins/newsbid/newsbid.go b/plugins/newsbid/newsbid.go index f7dc7e7..d9daa73 100644 --- a/plugins/newsbid/newsbid.go +++ b/plugins/newsbid/newsbid.go @@ -73,6 +73,7 @@ func (p *NewsBid) message(conn bot.Connector, k bot.Kind, message msg.Message, a return true } out := "NGate balances:\n" + sort.Sort(bals) 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) } diff --git a/plugins/newsbid/webshit/webshit.go b/plugins/newsbid/webshit/webshit.go index 29344f5..46f49ad 100644 --- a/plugins/newsbid/webshit/webshit.go +++ b/plugins/newsbid/webshit/webshit.go @@ -57,6 +57,12 @@ type Balance struct { Score int } +type Balances []Balance + +func (b Balances) Len() int { return len(b) } +func (b Balances) Swap(i, j int) { b[i], b[j] = b[j], b[i] } +func (b Balances) Less(i, j int) bool { return b[i].Score < b[j].Score } + type WeeklyResult struct { User string Won int @@ -274,8 +280,8 @@ func (w *Webshit) GetAllBids() ([]Bid, error) { return bids, nil } -func (w *Webshit) GetAllBalances() ([]Balance, error) { - var balances []Balance +func (w *Webshit) GetAllBalances() (Balances, error) { + var balances Balances err := w.db.Select(&balances, `select * from webshit_balances`) if err != nil { return nil, err