提交修改反射部分

This commit is contained in:
1
2025-08-28 14:38:13 +00:00
parent 7481182371
commit 921a121e11
9 changed files with 94 additions and 90 deletions

View File

@@ -62,7 +62,7 @@ func (c *BaseSysUserController) GetSession(ctx context.Context, req *SessionReq)
res.Session = retsid
if !blazing_service.NewPlayerService().IsReg(t1.ID) {
if !blazing_service.NewUserService(uint32(t1.ID)).IsReg() {
res.UserID = int(t1.ID)
}

View File

@@ -37,6 +37,11 @@ func (*Boss) GroupName() string {
return "default"
}
// GroupName 定义分组名
func (s *Boss) GetData() string {
return s.Data
}
// NewBoss 创建新的BOSS主表实例
func NewBoss() *Boss {
return &Boss{

View File

@@ -10,22 +10,10 @@ import (
"github.com/gogf/gf/v2/os/glog"
)
type PlayerService struct {
*cool.Service
}
func NewPlayerService() *PlayerService {
return &PlayerService{
&cool.Service{
Model: model.NewPlayer(),
},
}
}
// 是否注册,如果注册过,那么就会产生用户player信息
func (s *PlayerService) IsReg(accountID uint) bool {
func (s *UserService) IsReg() bool {
m := cool.DBM(s.Model).Where("player_id", accountID)
m := cool.DBM(s.reg.Model).Where("player_id", s.userid)
record, err := m.One()
if err != nil {
@@ -38,12 +26,12 @@ func (s *PlayerService) IsReg(accountID uint) bool {
}
// 实现注册,id+昵称+颜色
func (s *PlayerService) Reg(accountID uint, nick string, color uint32) {
func (s *UserService) Reg(nick string, color uint32) {
nick = strings.Trim(nick, "\x00")
t := model.NewPlayer()
t.PlayerID = uint64(accountID)
t.PlayerID = uint64(s.userid)
//设置用户信息
t1 := model.NewPlayerInfo()
t1.Nick = nick
@@ -56,16 +44,17 @@ func (s *PlayerService) Reg(accountID uint, nick string, color uint32) {
}
t.Data = string(t22)
_, err = cool.DBM(s.Model).Data(t).FieldsEx("id").Insert()
_, err = cool.DBM(s.reg.Model).Data(t).FieldsEx("id").Insert()
if err != nil {
glog.Error(context.Background(), err)
return
}
}
func (s *PlayerService) Person(accountID uint) (ret *model.PlayerInfo) {
m := cool.DBM(s.Model).Where("player_id", accountID)
func (s *UserService) Person() (ret *model.PlayerInfo) {
m := cool.DBM(s.reg.Model).Where("player_id", s.userid)
var tt model.Player
m.Scan(&tt)
json.Unmarshal([]byte(tt.Data), &ret)
@@ -73,9 +62,9 @@ func (s *PlayerService) Person(accountID uint) (ret *model.PlayerInfo) {
return
}
func (s *PlayerService) Save(data *model.PlayerInfo) {
func (s *UserService) Save(data *model.PlayerInfo) {
m := cool.DBM(s.Model).Where("player_id", data.UserID)
m := cool.DBM(s.reg.Model).Where("player_id", data.UserID)
var tt model.Player
m.Scan(&tt)
//todo 待测试
@@ -88,10 +77,10 @@ func (s *PlayerService) Save(data *model.PlayerInfo) {
}
func (s *PlayerService) ProcessAndSave(playerID uint32, processFunc func(*model.PlayerInfo) error) error {
func (s *UserService) RegExec(processFunc func(*model.PlayerInfo) bool) bool {
//todo待测试
var player model.Player
m1 := cool.DBM(s.Model).Where("player_id", playerID)
m1 := cool.DBM(s.reg.Model).Where("player_id", s.userid)
m1.Scan(&player)
var tt model.PlayerInfo
json.Unmarshal([]byte(player.Data), &tt)
@@ -99,5 +88,5 @@ func (s *PlayerService) ProcessAndSave(playerID uint32, processFunc func(*model.
tmep, _ := json.Marshal(tt)
player.Data = string(tmep)
m1.Save(player)
return nil
return false
}

View File

@@ -4,61 +4,75 @@ import (
"blazing/cool"
"blazing/modules/blazing/model"
"encoding/json"
"reflect"
)
type TaskService struct {
*UserService
}
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")
func NewTaskService(id uint32) *TaskService {
return &TaskService{
&UserService{
userid: id,
Service: &cool.Service{
Model: model.NewTask(),
},
},
}
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 *TaskService) Exec(t func(map[uint32]model.TaskInfo) bool) (ret bool) {
var tt model.Task
s.GetModel().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)
s.GetModel().Save(&tt) //退出时保存
return
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)
// 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 {
// /**
// * 完成任务
// */
// 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 s.Exec(func(ttt map[uint32]model.TaskInfo) bool {
// if conditions, ok := ttt[task]; ok {
// conditions.Status = 3
// ttt[task] = conditions
}
// }
return false
})
}
// return false
// })
// }
/**
* 校验任务是否已经完成
*/
func (s *TaskService) CheckTaskCompleted(task uint32) bool {
// /**
// * 校验任务是否已经完成
// */
// func (s *TaskService) CheckTaskCompleted(task uint32) bool {
return s.Exec(func(ttt map[uint32]model.TaskInfo) bool {
if conditions, ok := ttt[task]; ok {
// return s.Exec(func(ttt map[uint32]model.TaskInfo) bool {
// if conditions, ok := ttt[task]; ok {
return conditions.Status == 3
}
// return conditions.Status == 3
// }
return false
})
}
// return false
// })
// }

View File

@@ -3,29 +3,24 @@ package service
import (
"blazing/cool"
"blazing/modules/blazing/model"
"encoding/json"
"github.com/gogf/gf/v2/database/gdb"
)
type UserService struct {
userid uint32
*cool.Service
task *cool.Service
reg *cool.Service
}
func (s *UserService) GetModel() *gdb.Model {
return cool.DBM(s.Model).Where("player_id", s.userid)
func NewUserService(id uint32) *UserService {
return &UserService{
userid: id,
task: &cool.Service{
Model: model.NewTask(),
},
reg: &cool.Service{
Model: model.NewPlayer(),
},
}
}
func (s *UserService) Exec(t func(map[uint32]model.TaskInfo) bool) {
var tt model.Task
s.GetModel().Scan(&tt)
var ttt map[uint32]model.TaskInfo
json.Unmarshal([]byte(tt.Data), &ttt)
t(ttt)
t1, _ := json.Marshal(&ttt)
tt.Data = string(t1)
s.GetModel().Save(&tt) //退出时保存
}