refactor(model): 重构Player模型,新增PlayerEX扩展结构并优化数据存储逻辑
This commit is contained in:
@@ -14,6 +14,10 @@ type Player struct {
|
||||
PlayerID uint64 `gorm:"not null;index:idx_pet_by_player_id;comment:'所属玩家ID'" json:"player_id"`
|
||||
Data string `gorm:"type:text;not null;comment:'全部数据'" json:"data"`
|
||||
}
|
||||
type PlayerEX struct {
|
||||
Player
|
||||
Data *PlayerInfo `orm:"data" json:"data"`
|
||||
}
|
||||
type Pos struct {
|
||||
X uint32 `struc:"uint32" default:"0"`
|
||||
Y uint32 `struc:"uint32" default:"0"`
|
||||
@@ -132,9 +136,11 @@ func (*Player) GroupName() string {
|
||||
}
|
||||
|
||||
// NewPlayerInfo create a new PlayerInfo
|
||||
func NewPlayer() *Player {
|
||||
return &Player{
|
||||
Model: cool.NewModel(),
|
||||
func NewPlayer() *PlayerEX {
|
||||
return &PlayerEX{
|
||||
Player: Player{
|
||||
Model: cool.NewModel(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"blazing/cool"
|
||||
"blazing/modules/blazing/model"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -34,18 +33,13 @@ func (s *UserService) Reg(nick string, color uint32) {
|
||||
t := model.NewPlayer()
|
||||
t.PlayerID = uint64(s.userid)
|
||||
//设置用户信息
|
||||
t1 := model.NewPlayerInfo()
|
||||
t1.Nick = nick
|
||||
t.Data = model.NewPlayerInfo()
|
||||
t.Data.Nick = nick
|
||||
|
||||
t1.Color = color
|
||||
t1.RegisterTime = uint32(time.Now().Unix()) //写入注册时间
|
||||
t22, err := json.Marshal(t1)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
t.Data.Color = color
|
||||
t.Data.RegisterTime = uint32(time.Now().Unix()) //写入注册时间
|
||||
|
||||
t.Data = string(t22)
|
||||
_, err = cool.DBM(s.info.Model).Data(t).FieldsEx("id").Insert()
|
||||
_, err := cool.DBM(s.info.Model).Data(t).FieldsEx("id").Insert()
|
||||
if err != nil {
|
||||
glog.Error(context.Background(), err)
|
||||
return
|
||||
@@ -55,52 +49,27 @@ func (s *UserService) Reg(nick string, color uint32) {
|
||||
|
||||
func (s *UserService) Person() (ret *model.PlayerInfo) {
|
||||
|
||||
m := cool.DBM(s.info.Model).Where("player_id", s.userid)
|
||||
var tt model.Player
|
||||
m.Scan(&tt)
|
||||
json.Unmarshal([]byte(tt.Data), &ret)
|
||||
|
||||
return
|
||||
return s.PersonOther(0)
|
||||
|
||||
}
|
||||
func (s *UserService) PersonOther(userid uint32) (ret *model.PlayerInfo) {
|
||||
func (s *UserService) PersonOther(userid uint32) *model.PlayerInfo {
|
||||
|
||||
m := cool.DBM(s.info.Model).Where("player_id", userid)
|
||||
var tt model.Player
|
||||
var tt model.PlayerEX
|
||||
m.Scan(&tt)
|
||||
json.Unmarshal([]byte(tt.Data), &ret)
|
||||
|
||||
return
|
||||
return tt.Data
|
||||
|
||||
}
|
||||
func (s *UserService) Save(data *model.PlayerInfo) {
|
||||
|
||||
m := cool.DBM(s.info.Model).Where("player_id", data.UserID)
|
||||
var tt model.Player
|
||||
var tt model.PlayerEX
|
||||
m.Scan(&tt)
|
||||
//todo 待测试
|
||||
|
||||
temp, _ := json.Marshal(data)
|
||||
tt.Data = string(temp)
|
||||
tt.Data = data
|
||||
_, err := m.Update(tt)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
func (s *UserService) RegExec(processFunc func(*model.PlayerInfo) bool) bool {
|
||||
//todo待测试
|
||||
var player model.Player
|
||||
m1 := cool.DBM(s.info.Model).Where("player_id", s.userid)
|
||||
m1.Scan(&player)
|
||||
var tt model.PlayerInfo
|
||||
json.Unmarshal([]byte(player.Data), &tt)
|
||||
processFunc(&tt)
|
||||
tmep, _ := json.Marshal(tt)
|
||||
player.Data = string(tmep)
|
||||
m1.Save(player)
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user