go_dreamfactory/modules/friend/module.go
2022-10-20 18:14:20 +08:00

122 lines
3.0 KiB
Go

package friend
import (
"context"
"go_dreamfactory/comm"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"time"
"go_dreamfactory/lego/base"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/log"
"github.com/spf13/cast"
)
var _ comm.IFriend = (*Friend)(nil)
func NewModule() core.IModule {
m := new(Friend)
return m
}
type Friend struct {
modules.ModuleBase
api *apiComp
modelFriend *ModelFriend
configure *modules.MCompConfigure
service base.IRPCXService
}
func (this *Friend) GetType() core.M_Modules {
return comm.ModuleFriend
}
func (this *Friend) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) {
err = this.ModuleBase.Init(service, module, options)
this.service = service.(base.IRPCXService)
return
}
func (this *Friend) OnInstallComp() {
this.ModuleBase.OnInstallComp()
this.api = this.RegisterComp(new(apiComp)).(*apiComp)
this.modelFriend = this.RegisterComp(new(ModelFriend)).(*ModelFriend)
}
func (this *Friend) Start() (err error) {
err = this.ModuleBase.Start()
this.service.RegisterFunctionName(string(comm.Rpc_ModuleFriendUseAssitHero), this.RpcUseAssisHero)
return
}
func (this *Friend) ResetFriend(uid string) {
// 重置点赞列表
zanUpdate := map[string]interface{}{
"zanIds": []string{},
"getZandIds": []string{},
"received": 0, //奖励状态重置
}
if err := this.modelFriend.Change(uid, zanUpdate); err != nil {
log.Error("resetZanFriend err", log.Field{Key: "err", Value: err})
}
// 重置今日友情点
update := map[string]interface{}{
"friendPointID": 0,
"friendPointOD": 0,
}
if err := this.ModuleUser.ChangeUserExpand(uid, update); err != nil {
log.Error("resetFriend err", log.Field{Key: "err", Value: err})
}
}
func (this *Friend) GetFriendCount(uid string) (count int32) {
if friend := this.modelFriend.GetFriend(uid); friend != nil {
count = cast.ToInt32(len(friend.FriendIds))
}
return
}
func (this *Friend) GetFriendList(uid string) (uids []string) {
if friend := this.modelFriend.GetFriend(uid); friend != nil {
uids = friend.FriendIds
}
return
}
func (this *Friend) RpcUseAssisHero(ctx context.Context, req *pb.RPCGeneralReqA2, reply *pb.DBHero) error {
hero, err := this.UseAssistHero(req.Param1, req.Param2)
if err != nil {
return err
}
reply = hero
return nil
}
// 使用好友助战英雄
func (this *Friend) UseAssistHero(uid, friendId string) (*pb.DBHero, error) {
//指定好友
friend := this.modelFriend.GetFriend(friendId)
for _, fId := range friend.FriendIds {
// 判断uid是否在指定玩家的好友列表中
if fId == uid {
//更新助战记录和助战分数
friend.ZhuzhanScore++
friend.Record = append(friend.Record, &pb.ZhuZhanRecord{
Uid: uid,
Zhuzhantime: time.Now().Unix(),
})
update := map[string]interface{}{
"zhuzhanScore": friend.ZhuzhanScore,
"record": friend.Record,
}
return friend.Hero, this.modelFriend.Change(friendId, update)
}
}
return nil, nil
}