auto hour earning

This commit is contained in:
yangduo 2024-12-06 12:08:58 +08:00
parent 361d2d76bc
commit 9c5c6e5281
3 changed files with 126 additions and 17 deletions

View File

@ -6,9 +6,9 @@ import (
"main/constant" "main/constant"
"main/model" "main/model"
"main/mt" "main/mt"
"main/service"
"main/vo" "main/vo"
"q5" "q5"
"wheelserver/service"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -24,13 +24,13 @@ func (this *UserApi) Login(c *gin.Context) {
user := new(model.User) user := new(model.User)
rspObj := struct { rspObj := struct {
vo.BaseVo vo.BaseVo
UserInfo vo.User `json:"user_info"` UserInfo vo.User `json:"user_info"`
ServerTime int32 `json:"server_time"` ServerTime int32 `json:"server_time"`
}{} }{}
nowTime := f5.GetApp().GetRealSeconds() nowTime := f5.GetApp().GetRealSeconds()
rspObj.ServerTime = int32(nowTime) rspObj.ServerTime = int32(nowTime)
err, found := user.Find(s.GetAccountId(), nowTime) err, found := user.Find(s.GetAccountId(), nowTime)
if err != nil { if err != nil {
f5.RspErr(c, 500, "server internal error") f5.RspErr(c, 500, "server internal error")
return return
} else if !found { } else if !found {
@ -52,7 +52,13 @@ func (this *UserApi) Login(c *gin.Context) {
return return
} }
f5.GetMsgQueue().FireEvent(constant.MSG_CREATE_USER, q5.Args{user}) f5.GetMsgQueue().FireEvent(constant.MSG_CREATE_USER, q5.Args{user})
} else {
earning := service.Chip.CalcScore(s.GetAccountId())
if earning > 0 {
user.AddScore(earning)
}
} }
if user.NickName != s.GetNickName() { if user.NickName != s.GetNickName() {
user.UpdateName() user.UpdateName()
} }
@ -75,7 +81,7 @@ func (this *UserApi) Info(c *gin.Context) {
UserInfo vo.User `json:"user_info"` UserInfo vo.User `json:"user_info"`
}{} }{}
nowTime := f5.GetApp().GetRealSeconds() nowTime := f5.GetApp().GetRealSeconds()
if err, found := user.Find(s.GetAccountId(), nowTime); err != nil { if err, found := user.Find(s.GetAccountId(), nowTime); err != nil {
f5.RspErr(c, 500, "server internal error") f5.RspErr(c, 500, "server internal error")
return return
} else if !found { } else if !found {
@ -86,3 +92,34 @@ func (this *UserApi) Info(c *gin.Context) {
rspObj.UserInfo.HourlyEarnings = service.Chip.GetHourEarning(user.AccountId) rspObj.UserInfo.HourlyEarnings = service.Chip.GetHourEarning(user.AccountId)
c.JSON(200, rspObj) c.JSON(200, rspObj)
} }
func (this *UserApi) ClaimEarning(c *gin.Context) {
s := c.MustGet(constant.SESSION_KEY).(common.Session)
if s == nil {
return
}
rspObj := struct {
vo.BaseVo
Earning int32 `json:"earning"`
Score int64 `json:"score"`
}{}
rspObj.Earning = service.Chip.CalcScore(s.GetAccountId())
if rspObj.Earning > 0 {
user := new(model.User)
nowTime := f5.GetApp().GetRealSeconds()
if err, found := user.Find(s.GetAccountId(), nowTime); err != nil {
f5.RspErr(c, 500, "server internal error")
return
} else if !found {
f5.RspErr(c, 1, "server internal error")
return
}
user.AddScore(rspObj.Earning)
rspObj.Score = user.Score
}
c.JSON(200, rspObj)
}

View File

@ -6,13 +6,14 @@ import (
) )
type Chip struct { type Chip struct {
Idx int64 `gorm:"column:idx;AUTO_INCREMENT;primaryKey"` Idx int64 `gorm:"column:idx;AUTO_INCREMENT;primaryKey"`
AccountId string `gorm:"column:account_id"` AccountId string `gorm:"column:account_id"`
ItemId int32 `gorm:"column:item_id"` ItemId int32 `gorm:"column:item_id"`
ItemNum int32 `gorm:"column:item_num"` ItemNum int32 `gorm:"column:item_num"`
ExpireTime int32 `gorm:"column:expiretime;<-:create"` ExpireTime int32 `gorm:"column:expiretime;<-:create"`
CreateTime int32 `gorm:"column:createtime;<-:create"` CalcTime int32 `gorm:"column:calc_time"`
ModifyTime int32 `gorm:"column:modifytime"` CreateTime int32 `gorm:"column:createtime;<-:create"`
ModifyTime int32 `gorm:"column:modifytime"`
} }
func (this *Chip) TableName() string { func (this *Chip) TableName() string {
@ -25,3 +26,11 @@ func (this *Chip) Create() error {
} }
return nil return nil
} }
func (this *Chip) UpdateCalcTime() error {
if result := f5.GetApp().GetOrmDb(constant.WHEEL_DB).Model(this).Select(
"calc_time", "modifytime").Updates(this); result.Error != nil {
return result.Error
}
return nil
}

View File

@ -6,13 +6,16 @@ import (
"main/model" "main/model"
"main/mt" "main/mt"
"q5" "q5"
"time"
) )
type chip struct { type chip struct {
earningUsers *q5.ConcurrentMap[string, int64] //[accountid, loginTime]
} }
func (this *chip) init() { func (this *chip) init() {
this.earningUsers = new(q5.ConcurrentMap[string, int64])
go this.checkEarningLoop()
} }
func (this *chip) unInit() { func (this *chip) unInit() {
@ -26,14 +29,43 @@ func (this *chip) List(accountId string) (error, []*model.Chip) {
return result.Error, chips return result.Error, chips
} }
func (this *chip) CalcScore(accountId string) { func (this *chip) CalcScore(accountId string) int32 {
//nowTime := f5.GetApp().GetRealSeconds()
chips := []*model.Chip{} chips := []*model.Chip{}
result := f5.GetApp().GetOrmDb(constant.WHEEL_DB).Table(new(model.Chip).TableName()).Where( result := f5.GetApp().GetOrmDb(constant.WHEEL_DB).Table(new(model.Chip).TableName()).Where(
"account_id = ? AND expiretime > calc_time", accountId).Find(&chips) "account_id = ? AND expiretime > calc_time + createtime", accountId).Find(&chips)
if result.Error == nil { if result.Error == nil {
var earningscore int32 = 0
nowtime := int32(f5.GetApp().GetRealSeconds())
for _, chipitem := range chips {
itemcfg := mt.Table.Item.GetById(int64(chipitem.ItemId))
if itemcfg == nil {
continue
}
var eclapse int32 = 0
if nowtime < chipitem.ExpireTime {
if nowtime > chipitem.CreateTime+chipitem.CalcTime {
eclapse = nowtime - chipitem.CreateTime - chipitem.CalcTime
}
} else {
if chipitem.CalcTime < itemcfg.GetTime() {
eclapse = itemcfg.GetTime() - chipitem.CalcTime
}
}
itemearning := (eclapse / 3600) * itemcfg.GetProduce() / (itemcfg.GetTime() / int32(60))
if itemearning > 0 {
earningscore += itemearning
chipitem.CalcTime += eclapse / 3600 * 3600
chipitem.ModifyTime = nowtime
chipitem.UpdateCalcTime()
}
}
return earningscore
} }
return 0
} }
func (this *chip) GetHourEarning(accountId string) string { func (this *chip) GetHourEarning(accountId string) string {
@ -44,7 +76,38 @@ func (this *chip) GetHourEarning(accountId string) string {
itemcfg := mt.Table.Item.GetById(int64(chipitem.ItemId)) itemcfg := mt.Table.Item.GetById(int64(chipitem.ItemId))
earnings += itemcfg.GetProduce() / (itemcfg.GetTime() / int32(60)) earnings += itemcfg.GetProduce() / (itemcfg.GetTime() / int32(60))
} }
if earnings > 0 {
this.earningUsers.Store(accountId, f5.GetApp().GetRealSeconds())
}
return q5.SafeToString(earnings) return q5.SafeToString(earnings)
} }
return "0" return "0"
}
func (this *chip) checkEarningLoop() {
for {
time.Sleep(time.Hour)
nowtime := f5.GetApp().GetRealSeconds()
deletelist := map[string]int32{}
this.earningUsers.Range(func(accountId string, lastlogin int64) bool {
if q5.GetDaySeconds(nowtime, 0) == q5.GetDaySeconds(lastlogin, 0) {
user := new(model.User)
err, found := user.Find(accountId, nowtime)
if err == nil && found {
userearning := this.CalcScore(accountId)
user.AddScore(userearning)
}
} else {
deletelist[accountId] = 0
}
return true
})
for accoutid := range deletelist {
this.earningUsers.Delete(accoutid)
}
}
} }