- 简化 ClientData 结构体,移除不必要的方法 - 优化 Player 结构体,调整 Conn 类型 - 更新 wscodec.go 中的 Conn 结构体 - 删除未使用的 XML 相关文件和代码 - 调整 ServerEvent 和 controller 中的相关逻辑
108 lines
2.3 KiB
Go
108 lines
2.3 KiB
Go
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, isdaliy bool) (ret bool) {
|
||
|
||
if isdaliy {
|
||
Exec[model.Task](s.userid, s.task, func(tt map[uint32]model.TaskInfo) bool {
|
||
|
||
//先重置每日
|
||
for _, v := range tt {
|
||
if v.TaskType == 1 && !IsToday(v.LastResetTime) {
|
||
|
||
v.Status = 0 //重置+自动接受每日任务
|
||
v.LastResetTime = time.Now().UTC()
|
||
}
|
||
|
||
}
|
||
|
||
return true
|
||
})
|
||
}
|
||
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 *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
|
||
// })
|
||
// }
|