add get more lottery func, chenge lottery act
This commit is contained in:
parent
e0323e964f
commit
38bc2f7639
@ -17,9 +17,20 @@ type Lottery struct {
|
||||
|
||||
// GetRandomLotteryByType -
|
||||
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)
|
||||
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 {
|
||||
return nil, nil
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package msgcmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
@ -338,10 +339,29 @@ func lottery(sub, txt string, s *lineobj.SourceObject) (res string) {
|
||||
if len(sub) == 0 {
|
||||
return ""
|
||||
}
|
||||
data, err := model.GetRandomLotteryByType(sub)
|
||||
if err != nil || data == nil {
|
||||
return
|
||||
arr, err := model.GetRandomLotteryByTypeAndLimit(sub, 100)
|
||||
if err != nil || arr == nil {
|
||||
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()
|
||||
u := conf.URL
|
||||
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"
|
||||
thumbURL := "/image/thumbnail"
|
||||
if len(data.Message) == 0 {
|
||||
return
|
||||
return ""
|
||||
}
|
||||
o := u + oriURL + "/" + data.Message + "?d=" + sub
|
||||
t := u + thumbURL + "/" + data.Message + "?d=" + sub
|
||||
|
Loading…
Reference in New Issue
Block a user