add get more lottery func, chenge lottery act

This commit is contained in:
Jay 2018-10-09 23:45:45 +08:00
parent e0323e964f
commit 38bc2f7639
2 changed files with 37 additions and 6 deletions

View File

@ -17,9 +17,20 @@ type Lottery struct {
// GetRandomLotteryByType - // GetRandomLotteryByType -
func GetRandomLotteryByType(t string) (p *Lottery, err error) { func GetRandomLotteryByType(t string) (p *Lottery, err error) {
p = &Lottery{} parr, err := GetRandomLotteryByTypeAndLimit(t, 1)
if err != nil {
return nil, err
}
if parr == nil {
return nil, nil
}
return parr[0], err
}
// GetRandomLotteryByTypeAndLimit -
func GetRandomLotteryByTypeAndLimit(t string, limit int) (p []*Lottery, err error) {
offset := rand.Intn(10) offset := rand.Intn(10)
err = x.Get(p, `select * from "public"."lottery" where "type" = $1 order by random() offset $2 limit 1`, t, offset) err = x.Select(p, `select * from "public"."lottery" where "type" = $1 order by random() offset $2 limit $3`, t, offset, limit)
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
return nil, nil return nil, nil
} }

View File

@ -2,6 +2,7 @@ package msgcmd
import ( import (
"fmt" "fmt"
"math/rand"
"strconv" "strconv"
"strings" "strings"
@ -338,10 +339,29 @@ func lottery(sub, txt string, s *lineobj.SourceObject) (res string) {
if len(sub) == 0 { if len(sub) == 0 {
return "" return ""
} }
data, err := model.GetRandomLotteryByType(sub) arr, err := model.GetRandomLotteryByTypeAndLimit(sub, 100)
if err != nil || data == nil { if err != nil || arr == nil {
return return ""
} }
// shuffle array
dest := make([]*model.Lottery, len(arr))
perm := rand.Perm(len(arr))
for idx, val := range perm {
dest[val] = arr[idx]
arr[idx] = nil
}
data := dest[0]
// set arr to nil
for i := range dest {
dest[i] = nil
}
arr = nil
dest = nil
perm = nil
conf := config.GetConf() conf := config.GetConf()
u := conf.URL u := conf.URL
if last := len(u); last > 0 && u[last-1] == '/' { if last := len(u); last > 0 && u[last-1] == '/' {
@ -350,7 +370,7 @@ func lottery(sub, txt string, s *lineobj.SourceObject) (res string) {
oriURL := "/image/origin" oriURL := "/image/origin"
thumbURL := "/image/thumbnail" thumbURL := "/image/thumbnail"
if len(data.Message) == 0 { if len(data.Message) == 0 {
return return ""
} }
o := u + oriURL + "/" + data.Message + "?d=" + sub o := u + oriURL + "/" + data.Message + "?d=" + sub
t := u + thumbURL + "/" + data.Message + "?d=" + sub t := u + thumbURL + "/" + data.Message + "?d=" + sub