diff --git a/comm/const.go b/comm/const.go index 5382bb3a3..cd61c3756 100644 --- a/comm/const.go +++ b/comm/const.go @@ -40,6 +40,7 @@ const ( ModuleForum core.M_Modules = "forum" //论坛模块 ModuleItems core.M_Modules = "items" //道具模块 ModuleShop core.M_Modules = "shop" //商店模块 + ModuleTask core.M_Modules = "task" //任务模块 ) //RPC服务接口定义处 diff --git a/modules/hero/module.go b/modules/hero/module.go index 0acb29557..2d8590ad4 100644 --- a/modules/hero/module.go +++ b/modules/hero/module.go @@ -3,6 +3,7 @@ package hero import ( "go_dreamfactory/comm" "go_dreamfactory/lego/core" + "go_dreamfactory/lego/sys/log" "go_dreamfactory/modules" "go_dreamfactory/pb" ) @@ -46,6 +47,11 @@ func (this *Hero) CreateHero(uid string, heroCfgId ...int32) error { //消耗英雄卡 func (this *Hero) ConsumeCard(uId string, heroCfgId int32, count int32) (code pb.ErrorCode) { + if count <= 0 { + log.Errorf("attr no changed,uid: %s heroCfgId: %s count: %d", uId, heroCfgId, count) + code = pb.ErrorCode_ReqParameterError + return + } heroes := this.GetHeroList(uId) var curList []*pb.DBHero for _, v := range heroes { diff --git a/modules/task/api.go b/modules/task/api.go new file mode 100644 index 000000000..f640ee51c --- /dev/null +++ b/modules/task/api.go @@ -0,0 +1,11 @@ +package task + +import ( + "go_dreamfactory/lego/base" + "go_dreamfactory/modules" +) + +type apiComp struct { + modules.MCompGate + service base.IRPCXService +} diff --git a/modules/task/model_task.go b/modules/task/model_task.go new file mode 100644 index 000000000..0f73947ed --- /dev/null +++ b/modules/task/model_task.go @@ -0,0 +1,20 @@ +package task + +import ( + "go_dreamfactory/lego/core" + "go_dreamfactory/modules" +) + +const ( //Redis + TableTask core.SqlTable = "task" //任务表 +) + +type ModelTask struct { + modules.MCompModel +} + +func (this *ModelTask) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { + err = this.MCompModel.Init(service, module, comp, options) + this.TableName = string(TableTask) + return +} diff --git a/modules/task/module.go b/modules/task/module.go new file mode 100644 index 000000000..50620b7ca --- /dev/null +++ b/modules/task/module.go @@ -0,0 +1,48 @@ +package task + +import ( + "go_dreamfactory/comm" + "go_dreamfactory/lego/core" + "go_dreamfactory/modules" + "time" +) + +type TaskModule struct { + modules.ModuleBase + modelTask *ModelTask + api *apiComp +} + +func NewModule() core.IModule { + return &TaskModule{} +} + +func (this *TaskModule) GetType() core.M_Modules { + return comm.ModuleTask +} + +func (this *TaskModule) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) { + err = this.ModuleBase.Init(service, module, options) + + go func() { + tickC := time.NewTicker(time.Second * 1) + for { + select { + case tc := <-tickC.C: + this.update(tc) + } + } + }() + + return +} + +func (this *TaskModule) OnInstallComp() { + this.ModuleBase.OnInstallComp() + this.api = this.RegisterComp(new(apiComp)).(*apiComp) + this.modelTask = this.RegisterComp(new(ModelTask)).(*ModelTask) +} + +func (this *TaskModule) update(t time.Time) { + +} diff --git a/modules/user/module.go b/modules/user/module.go index 5064faf09..9dda79c16 100644 --- a/modules/user/module.go +++ b/modules/user/module.go @@ -6,6 +6,7 @@ import ( "go_dreamfactory/pb" "go_dreamfactory/lego/core" + "go_dreamfactory/lego/sys/log" ) func NewModule() core.IModule { @@ -74,12 +75,35 @@ func (this *User) QueryAttributeValue(uid string, attr string) (value int32) { //用户资源 func (this *User) AddAttributeValue(uid string, attr string, add int32) (code pb.ErrorCode) { + if add == 0 { + log.Errorf("attr no changed,uid: %s attr: %s add: %d", uid, attr, add) + code = pb.ErrorCode_ReqParameterError + return + } + user := this.GetUser(uid) + if user == nil { + code = pb.ErrorCode_UserSessionNobeing + return + } + update := make(map[string]interface{}) switch attr { case comm.ResGold: - update[comm.ResGold] = add + if add < 0 { + if user.Gold+add < 0 { + code = pb.ErrorCode_GoldNoEnough + return + } + } + update[comm.ResGold] = user.Gold + add case comm.ResExp: - update[comm.ResExp] = add + if add < 0 { + if user.Exp+add < 0 { + code = pb.ErrorCode_ResNoEnough + return + } + } + update[comm.ResExp] = user.Gold + add } if err := this.modelUser.updateUserAttr(uid, update); err != nil { diff --git a/services/worker/main.go b/services/worker/main.go index 3afb7c4b9..ebb295b45 100644 --- a/services/worker/main.go +++ b/services/worker/main.go @@ -8,6 +8,7 @@ import ( "go_dreamfactory/modules/hero" "go_dreamfactory/modules/items" "go_dreamfactory/modules/mail" + "go_dreamfactory/modules/task" "go_dreamfactory/modules/user" "go_dreamfactory/services" "go_dreamfactory/sys/cache" @@ -44,6 +45,7 @@ func main() { friend.NewModule(), hero.NewModule(), equipment.NewModule(), + task.NewModule(), ) }