每日任务添加

This commit is contained in:
1
2025-08-28 17:13:54 +00:00
parent e550502e36
commit 02c0582fe5
7 changed files with 307 additions and 87 deletions

View File

@@ -0,0 +1,4 @@
package model
// /这里实现周期性任务,比如每日任务,每周任务,每月任务,每年任务,一次性任务
type PeriodType int

View File

@@ -0,0 +1,48 @@
package model
import (
"blazing/cool"
"time"
)
const TableNameDailyTask = "DailyTask"
// DailyTask mapped from table <DailyTask>
type DailyTask struct {
*cool.Model
PlayerID uint64 `gorm:"not null;index:idx_DailyTask_by_player_id;comment:'所属玩家ID'" json:"player_id"`
Data string `gorm:"type:text;not null;comment:'全部数据'" json:"data"`
}
// DailyTaskInfo 单个任务的详细信息,包含任务步骤状态和整体状态
type DailyTaskInfo struct {
// DailyTaskInfo 任务步骤信息对应Java的@ArraySerialize(FIXED_LENGTH=20)注解
// struc:"[20]byte" 确保二进制序列化时固定20字节长度json标签指定JSON字段名
DailyTaskInfo []uint32 `struc:"[20]byte" json:"DailyTask_info"`
LastResetTime time.Time `gorm:"not null;comment:'上次重置时间UTC'" json:"last_reset_time"` //这里是每天重置
// Status 任务整体状态0-未接受1-已接受2-已完成未领取3-已完成已领取
// json标签指定JSON字段名与业务状态说明保持一致
Status byte `json:"status"`
}
// TableName PlayerInfo's table name
func (*DailyTask) TableName() string {
return TableNamePlayerInfo
}
// GroupName PlayerInfo's table group
func (*DailyTask) GroupName() string {
return "default"
}
// NewPlayerInfo create a new PlayerInfo
func NewDailyTask() *DailyTask {
return &DailyTask{
Model: cool.NewModel(),
}
}
// init 创建表
func init() {
cool.CreateTable(&DailyTask{})
}

View File

@@ -5,6 +5,7 @@ import (
"blazing/modules/blazing/model"
"encoding/json"
"reflect"
"time"
)
func Exec[T, F any](userid uint32, s *cool.Service, processFunc func(F) bool) bool {
@@ -33,7 +34,46 @@ func Exec[T, F any](userid uint32, s *cool.Service, processFunc func(F) bool) bo
return false
}
func (s *UserService) TaskExec(t func(map[uint32]model.TaskInfo) bool) (ret bool) {
return Exec[model.Task, map[uint32]model.TaskInfo](s.userid, s.task, t)
return Exec[model.Task](s.userid, s.task, t)
// m := cool.DBM(s.task.Model).Where("player_id", s.userid)
// var tt model.Task
// m.Scan(&tt)
// var ttt map[uint32]model.TaskInfo
// json.Unmarshal([]byte(tt.Data), &ttt)
// ret = t(ttt)
// t1, _ := json.Marshal(&ttt)
// tt.Data = string(t1)
// m.Save(&tt) //退出时保存
// return
}
// IsToday 判断给定时间是否是今天
func IsToday(t time.Time) bool {
// 获取当前时间
now := time.Now()
// 比较年、月、日是否相同
return t.Year() == now.Year() &&
t.Month() == now.Month() &&
t.Day() == now.Day()
}
func (s *UserService) DailyTaskExec(t func(map[uint32]model.DailyTaskInfo) bool) (ret bool) {
Exec[model.DailyTask](s.userid, s.task, func(tt map[uint32]model.DailyTaskInfo) bool {
//先重置每日
for _, v := range tt {
if !IsToday(v.LastResetTime) {
v.Status = 0 //重置+自动接受每日任务
v.LastResetTime = time.Now().UTC()
}
}
return true
})
return Exec[model.DailyTask](s.userid, s.task, t)
// m := cool.DBM(s.task.Model).Where("player_id", s.userid)
// var tt model.Task
// m.Scan(&tt)