Files
bl/modules/blazing/service/task.go
2025-08-28 17:13:54 +00:00

119 lines
2.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package service
import (
"blazing/cool"
"blazing/modules/blazing/model"
"encoding/json"
"reflect"
"time"
)
func Exec[T, F any](userid uint32, s *cool.Service, processFunc func(F) bool) bool {
//todo待测试
var player T
// 方法2使用反射获取
// 获取反射值对象
val := reflect.ValueOf(player)
var dataField reflect.Value
// 检查是否为结构体
if val.Kind() == reflect.Struct {
// 通过字段名获取Data字段
dataField = val.FieldByName("Data")
}
m1 := cool.DBM(s.Model).Where("player_id", userid)
m1.Scan(&player)
var tt F
json.Unmarshal([]byte(dataField.Interface().(string)), &tt)
processFunc(tt)
tmep, _ := json.Marshal(tt)
dataField.SetString(string(tmep))
m1.Save(player)
return false
}
func (s *UserService) TaskExec(t func(map[uint32]model.TaskInfo) bool) (ret bool) {
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)
// 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
}
// /**
// * 完成任务
// */
// func (s *TaskService) CompleteTask(task uint32) bool {
// return s.Exec(func(ttt map[uint32]model.TaskInfo) bool {
// if conditions, ok := ttt[task]; ok {
// conditions.Status = 3
// ttt[task] = conditions
// }
// return false
// })
// }
// /**
// * 校验任务是否已经完成
// */
// func (s *TaskService) CheckTaskCompleted(task uint32) bool {
// return s.Exec(func(ttt map[uint32]model.TaskInfo) bool {
// if conditions, ok := ttt[task]; ok {
// return conditions.Status == 3
// }
// return false
// })
// }