go_dreamfactory/modules/sociaty/module.go
2024-02-23 15:35:51 +08:00

449 lines
12 KiB
Go

// package
// 公会
// 赵长远
package sociaty
import (
"context"
"errors"
"fmt"
"go_dreamfactory/comm"
"go_dreamfactory/lego/base"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"go_dreamfactory/sys/configure"
cfg "go_dreamfactory/sys/configure/structs"
"go_dreamfactory/sys/db"
"go.mongodb.org/mongo-driver/bson"
)
var _ comm.ISociaty = (*Sociaty)(nil)
type Sociaty struct {
modules.ModuleBase
//maincity comm.IMainCity
api *apiComp
service base.IRPCXService
modelSociaty *ModelSociaty
modelSociatyTask *ModelSociatyTask
modelSociatyLog *ModelSociatyLog
configure *configureComp
globalConf *cfg.GameGlobalData
sociatyActivityConf *cfg.GameGuildActivity
sociatyTaskConf *cfg.GameGuildTask
sociatySignConf *cfg.GameGuildSign
}
func NewModule() core.IModule {
return &Sociaty{}
}
func (this *Sociaty) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) {
if err = this.ModuleBase.Init(service, module, options); err != nil {
return
}
this.service = service.(base.IRPCXService)
return
}
func (this *Sociaty) GetType() core.M_Modules {
return comm.ModuleSociaty
}
func (this *Sociaty) OnInstallComp() {
this.ModuleBase.OnInstallComp()
this.api = this.RegisterComp(new(apiComp)).(*apiComp)
this.modelSociaty = this.RegisterComp(new(ModelSociaty)).(*ModelSociaty)
this.modelSociatyTask = this.RegisterComp(new(ModelSociatyTask)).(*ModelSociatyTask)
this.modelSociatyLog = this.RegisterComp(new(ModelSociatyLog)).(*ModelSociatyLog)
this.configure = this.RegisterComp(new(configureComp)).(*configureComp)
// event.Register(comm.EventBuriedComplete, this.TCondFinishNotify)
}
func (this *Sociaty) Start() (err error) {
if err = this.ModuleBase.Start(); err != nil {
return
}
this.service.RegisterFunctionName(string(comm.Rpc_ModuleSociaty), this.RpcGetSociaty)
this.service.RegisterFunctionName(string(comm.Rpc_ModuleSociatyUpdate), this.RpcUpdateSociaty)
this.service.RegisterFunctionName(string(comm.Rpc_ModuleSociatyTask), this.RpcUpdateUserTask)
this.service.RegisterFunctionName(string(comm.Rpc_ModuleSociatyGetTask), this.RpcGetUserTask)
this.globalConf = this.ModuleTools.GetGlobalConf()
if this.globalConf == nil {
err = errors.New("global config not found")
return
}
if this.sociatyActivityConf, err = this.configure.getSociatyActivityCfg(); err != nil {
return err
}
if this.sociatyTaskConf, err = this.configure.getSociatyTaskCfg(); err != nil {
return err
}
if this.sociatySignConf, err = this.configure.getSociatySignCfg(); err != nil {
return err
}
if err = this.checkSociatyConf(); err != nil {
return err
}
// var module core.IModule
// if module, err = this.service.GetModule(comm.ModuleMaincity); err != nil {
// return
// }
// this.maincity = module.(comm.IMainCity)
return
}
var errs []string
func (this *Sociaty) checkSociatyConf() (err error) {
buriedCondConf, err := this.configure.getBuriedCondCfg()
if err != nil {
return err
}
for _, data := range this.sociatyTaskConf.GetDataList() {
if _, ok := buriedCondConf.GetDataMap()[data.TypeId]; !ok {
errs = append(errs, fmt.Sprintf("condId:%v 可能无效,检查guild_task表字段type_id值是否存在于buried_condi", data.TypeId))
}
}
return
}
// 会长弹劾处理
// Deprecated
func (this *Sociaty) ProcessAccuse(uid, sociatyId string) {
t := this.globalConf.GuildImpeachmentCountDown
if t == 0 {
return
}
sociaty, _ := this.modelSociaty.getSociaty(sociatyId)
if sociaty != nil {
if sociaty.AccuseTime > 0 {
now := configure.Now().Unix()
if now-sociaty.AccuseTime >= int64(3600*t) {
//TODO 选新会长
} else {
//结束弹劾
update := map[string]interface{}{
"accuseTime": 0,
}
if err := this.modelSociaty.updateSociaty(sociatyId, update); err != nil {
this.Errorf("弹劾时间更新失败 %v", err)
}
}
}
}
}
// 获取我的公会成员
func (this *Sociaty) MembersByUid(uid string) (list []*pb.SociatyMemberInfo) {
sociaty := this.modelSociaty.getUserSociaty(uid)
return this.modelSociaty.members(sociaty)
}
// 获取公会成员
func (this *Sociaty) MembersBySociatyId(sociatyId string) (list []*pb.SociatyMemberInfo, errdata *pb.ErrorData) {
var (
sociaty *pb.DBSociaty
err error
)
if sociaty, err = this.modelSociaty.getSociaty(sociatyId); err != nil {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_DBError,
Title: pb.ErrorCode_DBError.ToString(),
Message: err.Error(),
}
return
}
return this.modelSociaty.members(sociaty), nil
}
// 批量获取公户信息
func (this *Sociaty) GetSociatys(sociatyIds []string) (result []*pb.DBSociaty, errdata *pb.ErrorData) {
var (
err error
)
if result, err = this.modelSociaty.querySociatys(sociatyIds); err != nil {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_DBError,
Title: pb.ErrorCode_DBError.String(),
Message: err.Error(),
}
}
return
}
// 公会红点
func (this *Sociaty) Reddot(session comm.IUserSession, rid map[comm.ReddotType]struct{}) (items map[comm.ReddotType]*pb.ReddotItem) {
var (
selfrid []comm.ReddotType = []comm.ReddotType{comm.Reddot15102, comm.Reddot15201, comm.Reddot15301}
applyReddot bool
tasks *pb.DBSociatyTask
comdi []int32
condisProgress []*pb.ConIProgress
err error
ok bool
)
items = make(map[comm.ReddotType]*pb.ReddotItem)
for _, v := range selfrid {
if _, ok = rid[v]; ok {
break
}
}
if ok {
return
}
sociaty := this.modelSociaty.getUserSociaty(session.GetUserId())
if sociaty == nil || sociaty.Id == "" {
return
}
for _, v := range selfrid {
if _, ok = rid[v]; ok {
switch v {
case comm.Reddot15102:
progress := int32(0)
tf := this.modelSociaty.IsSign(session.GetUserId(), sociaty)
if tf {
progress = 1
}
items[comm.Reddot15102] =
&pb.ReddotItem{
Rid: int32(comm.Reddot15102),
Activated: !tf,
Nextchanagetime: 0,
Progress: progress,
Total: 1,
}
case comm.Reddot15201:
if this.modelSociaty.isRight(session.GetUserId(), sociaty,
pb.SociatyJob_PRESIDENT,
pb.SociatyJob_VICEPRESIDENT,
pb.SociatyJob_ADMIN) {
if len(sociaty.ApplyRecord) > 0 {
applyReddot = true
}
}
items[comm.Reddot15201] = &pb.ReddotItem{
Rid: int32(comm.Reddot15201),
Activated: applyReddot,
Nextchanagetime: 0,
}
case comm.Reddot15301:
if tasks, err = this.modelSociatyTask.getUserTask(session.GetUserId(), sociaty.Id); err != nil {
continue
}
for k, _ := range tasks.TaskList {
comdi = append(comdi, k)
}
if _, condisProgress, err = this.ModuleBuried.CheckCondition(session, comdi...); err != nil {
this.Errorln(err)
return
}
for _, v := range condisProgress {
if v.State == pb.BuriedItemFinishState_buried_finish {
if value, ok := tasks.TaskList[v.Conid]; !ok || value == 0 {
items[comm.Reddot15301] = &pb.ReddotItem{
Rid: int32(comm.Reddot15301),
Activated: true,
}
break
}
}
}
case comm.Reddot15400:
for _, v := range this.sociatyActivityConf.GetDataMap() {
if sociaty.Activity >= v.Activity {
if value, ok := tasks.ActivityList[v.Id]; !ok || value == 0 {
items[comm.Reddot15301] = &pb.ReddotItem{
Rid: int32(comm.Reddot15400),
Activated: true,
}
break
}
}
}
}
}
}
return
}
// 跨服获取公会
func (this *Sociaty) RpcGetSociaty(ctx context.Context, req *pb.RPCGeneralReqA1, reply *pb.DBSociaty) error {
this.Debug("Rpc_ModuleSociaty", log.Field{Key: "req", Value: req.String()})
var (
sociaty *pb.DBSociaty
err error
)
sociaty, err = this.modelSociaty.getSociaty(req.Param1)
reply.Id = sociaty.Id
reply.Lv = sociaty.Lv
reply.Exp = sociaty.Exp
reply.Members = sociaty.Members
reply.DismissCD = sociaty.DismissCD
reply.Name = sociaty.Name
reply.Icon = sociaty.Icon
reply.Notice = sociaty.Notice
reply.DismissTime = sociaty.DismissTime
reply.IsApplyCheck = sociaty.IsApplyCheck
reply.SignIds = sociaty.SignIds
reply.LastSignCount = sociaty.LastSignCount
reply.ApplyLv = sociaty.ApplyLv
reply.Activity = sociaty.Activity
reply.AccuseTime = sociaty.AccuseTime
reply.ApplyRecord = sociaty.ApplyRecord
return err
}
type SociatyUpdateParam struct {
SociatyId string
Update map[string]interface{}
}
// 跨服更新数据
func (this *Sociaty) RpcUpdateSociaty(ctx context.Context, req *SociatyUpdateParam, reply *pb.DBSociaty) error {
return this.modelSociaty.ChangeList(comm.RDS_EMPTY, req.SociatyId, req.Update)
}
// 设置工会经验
func (this *Sociaty) BingoSetExp(session comm.IUserSession, exp int32) (err error) {
sociaty := this.modelSociaty.getUserSociaty(session.GetUserId())
if sociaty == nil || sociaty.Id == "" {
log.Warn("未获得公会信息", log.Field{Key: "uid", Value: session.GetUserId()})
return comm.NewCustomError(pb.ErrorCode_SociatyNoFound)
}
sociaty.Exp += exp
update := map[string]interface{}{
"exp": sociaty.Exp,
}
if _, err = this.modelSociaty.changeLv(sociaty, update); err != nil {
return
}
err = this.modelSociaty.updateSociaty(sociaty.Id, update)
return
}
// 设置工会活跃度
func (this *Sociaty) BingoSetActivity(session comm.IUserSession, activity int32) error {
sociaty := this.modelSociaty.getUserSociaty(session.GetUserId())
if sociaty == nil || sociaty.Id == "" {
log.Warn("未获得公会信息", log.Field{Key: "uid", Value: session.GetUserId()})
return comm.NewCustomError(pb.ErrorCode_SociatyNoFound)
}
activity += sociaty.Activity
update := map[string]interface{}{
"activity": activity,
}
return this.modelSociaty.updateSociaty(sociaty.Id, update)
}
type TaskParams struct {
SociatyId string
Uid string
Data interface{}
}
// 任务条件达成通知
// 废弃 不需要主动推送任务变化 前端主动刷新页面即可
func (this *Sociaty) BuriedsNotify(session comm.IUserSession, conds []*pb.ConIProgress) {
log.Debug("公会任务通知",
log.Field{Key: "uid", Value: session.GetUserId()},
log.Field{Key: "condIds", Value: conds})
ok := false
for _, v := range conds {
if v.State == pb.BuriedItemFinishState_buried_finish {
ok = true
}
}
if ok {
session.SendMsg(string(comm.ModuleReddot), "change", &pb.ReddotChangePush{Rids: []*pb.ReddotItem{
{
Rid: int32(comm.Reddot15301),
Activated: true,
},
}})
}
}
func (this *Sociaty) RpcUpdateUserTask(ctx context.Context, p *TaskParams, reply *pb.EmptyResp) error {
conn, err := db.Local()
if err != nil {
return err
}
model := db.NewDBModel(this.service.GetTag(), comm.TableSociatyTask, conn)
if err := model.Redis.HMSet(fmt.Sprintf("%s-%s:%s-%s", model.ServiceId, this.modelSociatyTask.TableName,
p.SociatyId, p.Uid), p.Data); err != nil {
log.Error("DBModel ChangeList", log.Field{Key: "err", Value: err.Error()})
return err
}
if err := model.UpdateModelLogs(this.modelSociatyTask.TableName,
p.Uid, bson.M{"sociatyid": p.SociatyId, "uid": p.Uid}, p.Data); err != nil {
return err
}
return nil
}
func (this *Sociaty) RpcGetUserTask(ctx context.Context, p *pb.RPCGeneralReqA2, reply *pb.DBSociatyTask) error {
dt, err := this.modelSociatyTask.getUserTask(p.Param2, p.Param1)
if err != nil {
return errors.New("not found")
}
reply.Uid = dt.Uid
reply.TaskList = dt.TaskList
reply.ActivityList = dt.ActivityList
reply.SociatyId = dt.SociatyId
reply.LastUpdateTime = dt.LastUpdateTime
return nil
}
// 创建公会3.10
func (this *Sociaty) CreateSociaty(uid, sociatyName string) error {
//创建公会
sociaty := &pb.DBSociaty{
Creater: uid,
Name: sociatyName,
ApplyLv: 1,
}
//会长
sociaty.Members = append(sociaty.Members, &pb.SociatyMember{
Uid: uid,
Job: pb.SociatyJob_PRESIDENT, //创建人是会长
Ctime: configure.Now().Unix(),
})
return this.modelSociaty.create(sociaty)
}
func (this *Sociaty) GetSociaty(uid string) *pb.DBSociaty {
return this.modelSociaty.getUserSociaty(uid)
}
func (this *Sociaty) ModifySociatyLv(uid string, lv int32) (err error) {
var (
sociaty *pb.DBSociaty
)
sociaty = this.modelSociaty.getUserSociaty(uid)
if sociaty != nil {
sociaty.Lv = lv
this.modelSociaty.updateSociaty(sociaty.Id, map[string]interface{}{
"lv": lv,
})
}
err = fmt.Errorf("no fund sociaty!")
return
}