You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
1.9 KiB
Go
85 lines
1.9 KiB
Go
package games
|
|
|
|
import (
|
|
"joybuywork/internal/app"
|
|
"joybuywork/internal/consts"
|
|
"joybuywork/internal/consts/ckey"
|
|
"joybuywork/internal/plugins/hashid"
|
|
"joybuywork/internal/plugins/jsonm"
|
|
"joybuywork/internal/repository/dbcheck"
|
|
"joybuywork/internal/repository/model"
|
|
"joybuywork/internal/repository/mtype"
|
|
"joybuywork/internal/service/queue/producer"
|
|
"joybuywork/internal/service/queue/types"
|
|
"joybuywork/internal/utils/logic"
|
|
pb "joybuywork/proto/go_proto/w_games"
|
|
"time"
|
|
)
|
|
|
|
type Trace struct {
|
|
logic.Logic
|
|
Req *pb.TraceReq
|
|
Res *pb.TraceRes
|
|
}
|
|
|
|
func (s *Trace) Run() error {
|
|
return logic.Run(s)
|
|
}
|
|
|
|
func (s *Trace) Data() (data jsonm.JsonM) {
|
|
data, _ = jsonm.Marshal(s.Req)
|
|
return
|
|
}
|
|
|
|
func (s *Trace) Exec() (err error) {
|
|
user, err := s.user()
|
|
if err != nil || user == nil {
|
|
return
|
|
}
|
|
|
|
switch s.Req.GetType() {
|
|
case mtype.GameActionTypeStay:
|
|
return s.stay(user)
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func (s *Trace) user() (user *model.GameUser, err error) {
|
|
m := s.GetDao().GameUser
|
|
user, err = m.WithContext(s.Ctx).Where(m.ID.Eq(s.Req.GetUserId())).First()
|
|
if err = dbcheck.DbError(err); err != nil {
|
|
err = s.BE().MysqlError(err)
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func (s *Trace) stay(user *model.GameUser) (err error) {
|
|
id := hashid.IdBigint()
|
|
var stayId int64
|
|
key := ckey.GameActionStayId(user.ID, s.Req.GetLinkId())
|
|
if !app.Redis.Cache.WithContext(s.Ctx).SetNX(s.Ctx, key, id, 2*consts.TimeDay).Val() {
|
|
stayId, _ = app.Redis.Cache.WithContext(s.Ctx).Get(s.Ctx, key).Int64()
|
|
}
|
|
|
|
producer.NewCampaignNotice(s.Ctx).GameAction(types.GameAction{
|
|
ID: id,
|
|
ProjectId: user.ProjectID,
|
|
GameId: user.GameID,
|
|
UserId: user.ID,
|
|
LinkId: s.Req.GetLinkId(),
|
|
Type: mtype.GameActionTypeStay,
|
|
ActionTime: time.Now().Unix(),
|
|
Dimension: mtype.GameDimension{
|
|
GameDimensionOption: mtype.GameDimensionOption{
|
|
StaySec: s.Req.GetStaySec(),
|
|
StayId: stayId,
|
|
},
|
|
},
|
|
})
|
|
|
|
return
|
|
}
|