refactor(user): 重构用户服务数据库操作,将reg服务重命名为info并新增talk挖矿服务
This commit is contained in:
@@ -46,6 +46,7 @@ func (h *Controller) Login(data *user.MAIN_LOGIN_IN, c *player.Conn) (result *us
|
||||
if !IsToday(t.Info.LastResetTime) { //判断是否是今天
|
||||
t.Info.LastResetTime = time.Now()
|
||||
//每天login时候检查重置时间,然后把电池,任务,挖矿重置
|
||||
//挖矿需要单独存,因为防止多开挖矿
|
||||
t.Info.TimeToday = 0 //重置电池
|
||||
go func() {
|
||||
for i := 400; i < 500; i++ { //每日任务区段
|
||||
@@ -58,6 +59,10 @@ func (h *Controller) Login(data *user.MAIN_LOGIN_IN, c *player.Conn) (result *us
|
||||
})
|
||||
}
|
||||
}
|
||||
for i := 400; i < 50; i++ { //每日任务区段
|
||||
t.Info.DailyResArr[i] = 0 //重置每日任务
|
||||
|
||||
}
|
||||
}()
|
||||
|
||||
}
|
||||
|
||||
@@ -114,7 +114,7 @@ type PlayerInfo struct {
|
||||
Reserved byte `struc:"byte" json:"reserved"` // 1字节无内容
|
||||
Badge uint32 `struc:"uint32" default:"0" json:"badge"` // 默认0
|
||||
Reserved1 [27]byte `struc:"[27]byte" default:"3" json:"reserved1"` // 27字节默认3
|
||||
TaskList [600]byte `struc:"[500]byte" default:"0" json:"task_list"` // 任务状态数组500字节,默认3
|
||||
TaskList [500]byte `struc:"[500]byte" default:"0" json:"task_list"` // 任务状态数组500字节,默认3
|
||||
PetListCount uint32 `struc:"sizeof=PetList" json:"pet_list_count"` // 精灵列表长度
|
||||
PetList []PetInfo ` json:"pet_list"` // 精灵背包内信息
|
||||
ClothesCount uint32 `struc:"sizeof=Clothes" json:"clothes_count"` // 穿戴装备数量
|
||||
|
||||
79
modules/blazing/model/talk.go
Normal file
79
modules/blazing/model/talk.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"blazing/cool"
|
||||
)
|
||||
|
||||
// 资源采集计数表名
|
||||
const TableNameResourceCollection = "resource_collection"
|
||||
|
||||
func NewTalk() *ResourceCollection {
|
||||
return &ResourceCollection{
|
||||
Model: cool.NewModel(),
|
||||
}
|
||||
}
|
||||
|
||||
// ResourceCollection 记录玩家每种资源的采集计数
|
||||
type ResourceCollection struct {
|
||||
*cool.Model
|
||||
PlayerID uint64 `gorm:"not null;index:idx_player_resource;comment:'所属玩家ID'" json:"player_id"`
|
||||
Data string `gorm:"type:text;not null;comment:'全部数据'" json:"data"` //map[uint32]uint32
|
||||
}
|
||||
type Talk struct {
|
||||
ResourceCollection
|
||||
Data map[uint32]uint32 `orm:"data" dc:"资源规格"`
|
||||
}
|
||||
|
||||
// TableName 资源采集表名
|
||||
func (*ResourceCollection) TableName() string {
|
||||
return TableNameResourceCollection
|
||||
}
|
||||
|
||||
// GroupName 资源采集表分组
|
||||
func (*ResourceCollection) GroupName() string {
|
||||
return "default"
|
||||
}
|
||||
|
||||
// // 检查是否可以采集(未超过每日上限)
|
||||
// func (rc *ResourceCollection) CanCollect(maxDaily uint32) bool {
|
||||
// // 先检查是否需要重置计数
|
||||
// rc.checkAndReset()
|
||||
// return rc.CollectCnt < maxDaily
|
||||
// }
|
||||
|
||||
// // 增加采集计数,返回是否成功
|
||||
// func (rc *ResourceCollection) AddCollectCount(maxDaily uint32) bool {
|
||||
// if !rc.CanCollect(maxDaily) {
|
||||
// return false
|
||||
// }
|
||||
// rc.CollectCnt++
|
||||
// return true
|
||||
// }
|
||||
|
||||
// // 检查并重置每日计数
|
||||
// func (rc *ResourceCollection) checkAndReset() {
|
||||
// now := time.Now()
|
||||
// if now.After(rc.DailyReset) {
|
||||
// rc.CollectCnt = 0
|
||||
// // 重置为明天24点
|
||||
// rc.DailyReset = now.Truncate(24 * time.Hour).Add(24 * time.Hour)
|
||||
// }
|
||||
// }
|
||||
|
||||
// ResourceConfig 资源配置信息(对应XML中的配置)
|
||||
type ResourceConfig struct {
|
||||
Type uint32 `json:"type"` // 资源类型ID
|
||||
MapID uint32 `json:"map_id"` // 所在地图ID
|
||||
Name string `json:"name"` // 资源名称
|
||||
CollectType string `json:"collect_type"` // 采集类型
|
||||
MaxDailyCnt uint32 `json:"max_daily_cnt"` // 每日最大采集次数
|
||||
Unit string `json:"unit"` // 单位
|
||||
Dir uint32 `json:"dir"` // 方向
|
||||
}
|
||||
|
||||
// 初始化创建表
|
||||
func init() {
|
||||
cool.CreateTable(&ResourceCollection{})
|
||||
// 可以在这里加载资源配置
|
||||
// LoadResourceConfigsFromXML()
|
||||
}
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
func (s *UserService) ItemExec(t func([]model.SingleItemInfo) []model.SingleItemInfo) {
|
||||
//todo待测试
|
||||
var player model.Item
|
||||
m1 := cool.DBM(s.reg.Model).Where("player_id", s.userid)
|
||||
m1 := cool.DBM(s.info.Model).Where("player_id", s.userid)
|
||||
m1.Scan(&player)
|
||||
var tt []model.SingleItemInfo
|
||||
json.Unmarshal([]byte(player.Data), &tt)
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
// 是否注册,如果注册过,那么就会产生用户player信息
|
||||
func (s *UserService) IsReg() bool {
|
||||
|
||||
m := cool.DBM(s.reg.Model).Where("player_id", s.userid)
|
||||
m := cool.DBM(s.info.Model).Where("player_id", s.userid)
|
||||
|
||||
record, err := m.One()
|
||||
if err != nil {
|
||||
@@ -45,7 +45,7 @@ func (s *UserService) Reg(nick string, color uint32) {
|
||||
}
|
||||
|
||||
t.Data = string(t22)
|
||||
_, err = cool.DBM(s.reg.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,7 +55,7 @@ func (s *UserService) Reg(nick string, color uint32) {
|
||||
|
||||
func (s *UserService) Person() (ret *model.PlayerInfo) {
|
||||
|
||||
m := cool.DBM(s.reg.Model).Where("player_id", s.userid)
|
||||
m := cool.DBM(s.info.Model).Where("player_id", s.userid)
|
||||
var tt model.Player
|
||||
m.Scan(&tt)
|
||||
json.Unmarshal([]byte(tt.Data), &ret)
|
||||
@@ -65,7 +65,7 @@ func (s *UserService) Person() (ret *model.PlayerInfo) {
|
||||
}
|
||||
func (s *UserService) PersonOther(userid uint32) (ret *model.PlayerInfo) {
|
||||
|
||||
m := cool.DBM(s.reg.Model).Where("player_id", userid)
|
||||
m := cool.DBM(s.info.Model).Where("player_id", userid)
|
||||
var tt model.Player
|
||||
m.Scan(&tt)
|
||||
json.Unmarshal([]byte(tt.Data), &ret)
|
||||
@@ -75,7 +75,7 @@ func (s *UserService) PersonOther(userid uint32) (ret *model.PlayerInfo) {
|
||||
}
|
||||
func (s *UserService) Save(data *model.PlayerInfo) {
|
||||
|
||||
m := cool.DBM(s.reg.Model).Where("player_id", data.UserID)
|
||||
m := cool.DBM(s.info.Model).Where("player_id", data.UserID)
|
||||
var tt model.Player
|
||||
m.Scan(&tt)
|
||||
//todo 待测试
|
||||
@@ -94,7 +94,7 @@ func (s *UserService) Save(data *model.PlayerInfo) {
|
||||
func (s *UserService) RegExec(processFunc func(*model.PlayerInfo) bool) bool {
|
||||
//todo待测试
|
||||
var player model.Player
|
||||
m1 := cool.DBM(s.reg.Model).Where("player_id", s.userid)
|
||||
m1 := cool.DBM(s.info.Model).Where("player_id", s.userid)
|
||||
m1.Scan(&player)
|
||||
var tt model.PlayerInfo
|
||||
json.Unmarshal([]byte(player.Data), &tt)
|
||||
|
||||
47
modules/blazing/service/talk.go
Normal file
47
modules/blazing/service/talk.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"blazing/cool"
|
||||
"blazing/modules/blazing/model"
|
||||
)
|
||||
|
||||
// TalkCheck 获取玩家当前的Talk数据
|
||||
// todo 待实现xml解析判断是否溢出
|
||||
func (s *UserService) TalkCheck() map[uint32]uint32 {
|
||||
|
||||
m1 := cool.DBM(s.talk.Model).Where("player_id", s.userid)
|
||||
|
||||
var talks []model.Talk
|
||||
m1.Scan(&talks)
|
||||
|
||||
return talks[0].Data
|
||||
}
|
||||
|
||||
// TalkAdd 添加或更新玩家的Talk数据
|
||||
func (s *UserService) TalkAdd(id, count uint32) {
|
||||
var player model.Talk
|
||||
m1 := cool.DBM(s.talk.Model).Where("player_id", s.userid)
|
||||
m1.Scan(&player)
|
||||
|
||||
// // 如果玩家没有记录,则初始化
|
||||
// if len(talks) == 0 {
|
||||
// player.PlayerID = uint64(s.userid)
|
||||
// player.Data = "{}"
|
||||
// _, err := m1.Insert(player)
|
||||
// if err != nil {
|
||||
// panic(err)
|
||||
// }
|
||||
// }
|
||||
|
||||
// // 更新或新增
|
||||
// if existing, ok := talks[id]; ok {
|
||||
|
||||
// talks[id] = existing + count
|
||||
// } else {
|
||||
// talks[id] = count
|
||||
// }
|
||||
|
||||
// data, _ := json.Marshal(talks)
|
||||
// player.Data = string(data)
|
||||
// m1.Update(player)
|
||||
}
|
||||
@@ -7,11 +7,11 @@ import (
|
||||
|
||||
type UserService struct {
|
||||
userid uint32
|
||||
|
||||
task *cool.Service //任务
|
||||
reg *cool.Service //注册
|
||||
pet *cool.Service //精灵
|
||||
item *cool.Service //物品
|
||||
talk *cool.Service //挖矿
|
||||
task *cool.Service //任务
|
||||
info *cool.Service //信息
|
||||
pet *cool.Service //精灵
|
||||
item *cool.Service //物品
|
||||
}
|
||||
|
||||
func NewUserService(id uint32) *UserService {
|
||||
@@ -20,11 +20,12 @@ func NewUserService(id uint32) *UserService {
|
||||
task: &cool.Service{
|
||||
Model: model.NewTask(),
|
||||
},
|
||||
reg: &cool.Service{
|
||||
info: &cool.Service{
|
||||
Model: model.NewPlayer(),
|
||||
},
|
||||
pet: &cool.Service{Model: model.NewPet()},
|
||||
item: &cool.Service{Model: model.NewPlayerBag()},
|
||||
talk: &cool.Service{Model: model.NewTalk()},
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user