go_dreamfactory/modules/friend/module.go
2023-06-15 20:25:46 +08:00

222 lines
6.1 KiB
Go

// package
// 好友
// 赵长远
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"
"go_dreamfactory/lego/sys/redis"
cfg "go_dreamfactory/sys/configure/structs"
"github.com/spf13/cast"
"go.mongodb.org/mongo-driver/mongo"
)
var _ comm.IFriend = (*Friend)(nil)
func NewModule() core.IModule {
m := new(Friend)
return m
}
type Friend struct {
modules.ModuleBase
api *apiComp
modelFriend *ModelFriend
ModelFriendQiecuo *ModelFriendQiecuo
configure *modules.MCompConfigure
service base.IRPCXService
globalConf *cfg.GameGlobalData
}
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)
this.ModelFriendQiecuo = this.RegisterComp(new(ModelFriendQiecuo)).(*ModelFriendQiecuo)
}
func (this *Friend) Start() (err error) {
err = this.ModuleBase.Start()
this.service.RegisterFunctionName(string(comm.Rpc_ModuleFriendUseAssitHero), this.RpcUseAssisHero)
this.service.RegisterFunctionName(string(comm.Rpc_ModuleFriendDB), this.RpcFriendDB)
this.globalConf = this.ModuleTools.GetGlobalConf()
if this.globalConf == nil {
err = errors.New("global config not found")
}
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("重置玩家点赞数据", log.Field{Key: "uid", Value: uid}, 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("重置今日友情点", log.Field{Key: "uid", Value: uid}, 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.String()})
hero, err := this.UseAssistHero(req.Param1, req.Param2)
if err != nil {
return err
}
*reply = *hero
return nil
}
func (this *Friend) RpcFriendDB(ctx context.Context, req *pb.RPCGeneralReqA1, reply *pb.DBFriend) error {
this.Debug("Rpc_ModuleFriendDB", log.Field{Key: "req", Value: req.String()})
friend := &pb.DBFriend{Uid: req.Param1}
err := this.modelFriend.Get(req.Param1, friend)
if err != nil {
if err == redis.RedisNil || err == mongo.ErrNoDocuments {
if err := this.modelFriend.Add(req.Param1, friend); err != nil {
return nil
}
} else {
// log.Error("未获得好友数据", log.Fields{"uid": req.Param1, "err": err})
}
}
*reply = *friend
return nil
}
// 使用好友助战英雄
// friendId 好友Id
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.Warn("今日已助战",
log.Field{Key: "uid", Value: uid},
log.Field{Key: "friendId", Value: friend},
log.Field{Key: "assistHeroId", Value: 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("非好友")
}
// 挑战完成通知
func (this *Friend) QiecuoFinishNotify(redUid, matchId string) error {
qr := this.ModelFriendQiecuo.getQiecuo(redUid)
if qr != nil && qr.MatchId == matchId {
log.Debug("清理切磋记录",
log.Field{Key: "redUid", Value: redUid},
log.Field{Key: "matchId", Value: matchId})
//更新时间戳
update := map[string]interface{}{
"endTime": configure.Now().Unix(),
}
if err := this.ModelFriendQiecuo.Change(redUid, update); err != nil {
return err
}
return nil
// if err := this.ModelFriendQiecuo.DelByUId(redUid); err != nil {
// log.Error("删除", log.Field{Key: "redUid", Value: redUid})
// }
}
return nil
}
func (this *Friend) Reddot(session comm.IUserSession, rid ...comm.ReddotType) (reddot map[comm.ReddotType]bool) {
reddot = make(map[comm.ReddotType]bool)
friend := this.modelFriend.GetFriend(session.GetUserId())
if friend == nil {
for _, v := range rid {
reddot[v] = false
}
} else {
for _, v := range rid {
switch v {
case comm.Reddot21101:
if len(friend.ApplyIds) > 0 {
reddot[comm.Reddot15102] = true
}
}
}
}
return
}