Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a80ddbb

Browse files
committed
活跃用户铜币奖励
1 parent 5b308fe commit a80ddbb

File tree

11 files changed

+111
-9
lines changed

11 files changed

+111
-9
lines changed

data/programming.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ vscode 3 n
5151
eclipse 3 n
5252
docker 3 n
5353
dockerfile 3 n
54+
kubernetes 3 n
5455
goroot 3 n
5556
etcd 3 n
5657
consul 3 n

src/logic/rank.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,17 @@ func (self RankLogic) FindMonthRank(ctx context.Context, objtype, num int, needE
119119
return self.findModelsByRank(resultSlice, objtype, num, needExt...)
120120
}
121121

122-
func (self RankLogic) FindDAURank(ctx context.Context, num int) []*model.User {
122+
// FindDAURank DAU 排名,默认获取当天的
123+
func (self RankLogic) FindDAURank(ctx context.Context, num int, ymds ...string) []*model.User {
123124
objLog := GetLogger(ctx)
124125

126+
ymd := times.Format("ymd")
127+
if len(ymds) > 0 {
128+
ymd = ymds[0]
129+
}
130+
125131
redisClient := nosql.NewRedisClient()
126-
key := self.getDAURankKey(times.Format("ymd"))
132+
key := self.getDAURankKey(ymd)
127133
resultSlice, err := redisClient.ZREVRANGE(key, 0, num-1, true)
128134
redisClient.Close()
129135
if err != nil {

src/logic/user_rich.go

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,80 @@ import (
1010
"errors"
1111
"fmt"
1212
"model"
13+
"time"
14+
"util"
1315

1416
. "db"
1517

18+
"github.com/garyburd/redigo/redis"
1619
"github.com/go-xorm/xorm"
1720
"github.com/polaris1119/logger"
21+
"github.com/polaris1119/nosql"
22+
"github.com/polaris1119/times"
1823
"golang.org/x/net/context"
1924
)
2025

26+
var (
27+
beginAwardWeight = 50
28+
)
29+
2130
type UserRichLogic struct{}
2231

2332
var DefaultUserRich = UserRichLogic{}
2433

25-
func (self UserRichLogic) Add(ctx context.Context) error {
26-
return nil
34+
func (self UserRichLogic) AwardCooper() {
35+
redisClient := nosql.NewRedisClient()
36+
defer redisClient.Close()
37+
ymd := times.Format("ymd", time.Now().Add(-86400*time.Second))
38+
key := DefaultRank.getDAURankKey(ymd)
39+
40+
var (
41+
cursor uint64
42+
err error
43+
resultSlice []interface{}
44+
count = 20
45+
)
46+
47+
for {
48+
cursor, resultSlice, err = redisClient.ZSCAN(key, cursor, "COUNT", count)
49+
if err != nil {
50+
logger.Errorln("AwardCooper ZSCAN error:", err)
51+
break
52+
}
53+
54+
for len(resultSlice) > 0 {
55+
var (
56+
uid, weight int
57+
err error
58+
)
59+
resultSlice, err = redis.Scan(resultSlice, &uid, &weight)
60+
if err != nil {
61+
logger.Errorln("AwardCooper redis Scan error:", err)
62+
continue
63+
}
64+
65+
if weight < beginAwardWeight {
66+
continue
67+
}
68+
69+
award := util.Max((weight-500)*5, 0) +
70+
util.UMin((weight-400), 100)*4 +
71+
util.UMin((weight-300), 100)*3 +
72+
util.UMin((weight-200), 100)*2 +
73+
util.UMin((weight-100), 100) +
74+
int(float64(util.UMin((weight-beginAwardWeight), beginAwardWeight))*0.5)
75+
76+
userRank := redisClient.ZREVRANK(key, uid)
77+
desc := fmt.Sprintf("%s 的活跃度为 %d,排名第 %d,奖励 %d 铜币", ymd, weight, userRank, award)
78+
fmt.Println(uid, desc)
79+
user := DefaultUser.FindOne(nil, "uid", uid)
80+
self.IncrUserRich(user, model.MissionTypeActive, award, desc)
81+
}
82+
83+
if cursor == 0 {
84+
break
85+
}
86+
}
2787
}
2888

2989
// IncrUserRich 增加或减少用户财富
@@ -33,8 +93,9 @@ func (self UserRichLogic) IncrUserRich(user *model.User, typ, award int, desc st
3393
err error
3494
)
3595

36-
if award > 0 && typ == model.MissionTypeReplied {
96+
if award > 0 && (typ == model.MissionTypeReplied || typ == model.MissionTypeActive) {
3797
// 老用户,因为之前的主题被人回复而增加财富,自动帮其领取初始资本
98+
// 因为活跃奖励铜币,自动帮其领取初始资本
3899
total, err = MasterDB.Where("uid=?", user.Uid).Count(new(model.UserBalanceDetail))
39100
if err != nil {
40101
logger.Errorln("IncrUserRich count error:", err)

src/logic/user_rich_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package logic_test
2+
3+
import "testing"
4+
5+
func TestAwardCooper(t *testing.T) {
6+
// logic.DefaultUserRich.AwardCooper()
7+
}

src/model/mission.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ const (
2727
MissionTypeReplied = 70
2828
// 额外奖励
2929
MissionTypeAward = 80
30+
// 活跃奖励
31+
MissionTypeActive = 81
3032
)
3133

3234
const (

src/model/user_rich.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var BalanceTypeMap = map[int]string{
2424
MissionTypeProject: "发布项目",
2525
MissionTypeBook: "分享图书",
2626
MissionTypeReplied: "回复收益",
27+
MissionTypeActive: "活跃奖励",
2728
}
2829

2930
type UserBalanceDetail struct {

src/server/studygolang/background.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ func ServeBackGround() {
5959
// 给用户发邮件,如通知网站最近的动态,每周的晨读汇总等
6060
c.AddFunc("0 0 4 * * 1", logic.DefaultEmail.EmailNotice)
6161
}
62+
63+
// 每天对活跃用户奖励铜币
64+
c.AddFunc("@daily", logic.DefaultUserRich.AwardCooper)
6265
}
6366

6467
// 两分钟刷一次浏览数(TODO:重启丢失问题?信号控制重启?)

src/util/tool.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package util
99
import (
1010
"fmt"
1111
"global"
12+
"math"
1213
"regexp"
1314
"strings"
1415

@@ -34,6 +35,18 @@ func Gravatar(avatar string, emailI interface{}, size uint16, isHttps bool) stri
3435
return fmt.Sprintf("%s/avatar/%s?s=%d", gravatarDomain, goutils.Md5(email), size)
3536
}
3637

38+
func Max(x, y int) int {
39+
return int(math.Max(float64(x), float64(y)))
40+
}
41+
42+
// 最小值,但不会小于0
43+
func UMin(x, y int) int {
44+
if x < 0 || y < 0 {
45+
return 0
46+
}
47+
return int(math.Min(float64(x), float64(y)))
48+
}
49+
3750
// 内嵌 Wide iframe 版
3851
func EmbedWide(content string) string {
3952
if !strings.Contains(content, "&lt;iframe") {

template/common/layout.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
<li class="first"><a href="/user/{{.me.Username}}">我的主页</a></li>
9999
<li><a href="/account/edit">个人资料设置</a></li>
100100
<li><a href="/favorites/{{.me.Username}}">我的收藏</a></li>
101+
<li><a href="/balance">我的财富</a></li>
101102
<li role="presentation" class="divider"></li>
102103
{{if .me.IsAdmin}}
103104
<li><a href="/admin" target="_blank">管理后台</a></li>

template/common/my_info.html

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
<span class="avatar"><a href="/account/edit#change-avatar"><img alt="个人首页" width="80" height="80" src="{{gravatar .me.Avatar .me.Email 80 .is_https}}" class="img-circle tool-tip" data-toggle="tooltip" data-placement="left" title="更换头像"></a></span>
66
</div>
77
<div class="profile-show pull-left">
8-
<p></p>
9-
<p class="user-name"><a href="/user/{{.me.Username}}">{{.me.Username}}</a></p>
8+
<div class="user-name"><a href="/user/{{.me.Username}}">{{.me.Username}}</a></div>
9+
<small class="f12 cc">{{.me.Monlog}}</small>
10+
<div class="sep5"></div>
1011
<p><a href="/account/edit">个人资料设置</a></p>
1112
</div>
1213
</div>
@@ -19,7 +20,13 @@
1920
<a class="btn btn-default btn-sm" href="/resources/new">分享资源</a>&nbsp;
2021
</div>
2122
<div class="inner_content">
22-
<a class="pull-left" href="/message/system" style="margin-left: 10px">{{.me.MsgNum}} 条未读消息</a>
23+
<a class="pull-left" href="/message/system" style="margin-left: 10px">
24+
{{if .me.MsgNum}}
25+
<strong>{{.me.MsgNum}} 条未读消息</strong>
26+
{{else}}
27+
{{.me.MsgNum}} 条未读消息
28+
{{end}}
29+
</a>
2330
<div style="text-align: right; margin-right: 20px;">
2431
<a href="/balance" class="balance_area">
2532
{{if .me.Gold}}

template/resources/detail.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ <h1>
3232
<div class="outdated">这是一个分享于 <span title="{{.resource.ctime}}" class="timeago"></span> 的资源,其中的信息可能已经有所发展或是发生改变。</div>
3333
{{end}}
3434
<div class="cell">
35-
<div class="content">{{if eq .resource.form "只是链接"}}传送门: {{.resource.url}}{{else}}{{.resource.content}}{{end}}</div>
35+
<div class="content">{{if eq .resource.form "只是链接"}}传送门: {{if hasPrefix .resource.url "/r/golang/"}}https://reddit.com{{end}}{{.resource.url}}{{else}}{{.resource.content}}{{end}}</div>
3636
</div>
3737

3838
<div class="content-buttons">

0 commit comments

Comments
 (0)