go_dreamfactory/modules/friend/module.go
2022-11-09 14:18:03 +08:00

142 lines
3.7 KiB
Go

package friend
import (
"context"
"errors"
"go_dreamfactory/comm"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"go_dreamfactory/sys/configure"
"go_dreamfactory/utils"
"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 {
this.Debug("Rpc_ModuleFriendUseAssitHero", log.Field{Key: "req", Value: req})
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 {
//好友没有设置助战英雄
if friend.AssistHeroId == "" {
break
}
//遍历助战记录
for _, r := range friend.Record {
if r.AssistHeroId == friend.AssistHeroId {
if utils.IsToday(r.AssistTime) {
log.Warnf("今日已助战 uid:%v friendId:%v heroId:%v", uid, friendId, r.AssistHeroId)
return nil, errors.New("今日已助战")
}
}
}
//更新助战记录和助战分数
friend.AssistScore++
// 设置助战记录
friend.Record = append(friend.Record, &pb.AssistRecord{
Uid: uid,
AssistHeroId: friend.AssistHeroId,
AssistTime: configure.Now().Unix(),
})
update := map[string]interface{}{
"assistScore": friend.AssistScore,
"record": friend.Record,
"updateTime": configure.Now().Unix(),
}
return friend.Hero, this.modelFriend.Change(friendId, update)
}
}
log.Errorf("[friendId:%v]不是你[uid:%v]的好友", uid, friendId)
return nil, errors.New("非好友")
}