package sociaty import ( "go_dreamfactory/comm" "go_dreamfactory/pb" "go_dreamfactory/utils" "strings" "go_dreamfactory/sys/configure" cfg "go_dreamfactory/sys/configure/structs" "google.golang.org/protobuf/proto" ) // 公会创建 func (this *apiComp) CreateCheck(session comm.IUserSession, req *pb.SociatyCreateReq) (code pb.ErrorCode) { if len(req.Notice) > 150 || strings.TrimSpace(req.Name) == "" { code = pb.ErrorCode_ReqParameterError } return } func (this *apiComp) Create(session comm.IUserSession, req *pb.SociatyCreateReq) (code pb.ErrorCode, data proto.Message) { if code = this.CreateCheck(session, req); code != pb.ErrorCode_Success { return } uid := session.GetUserId() user, err := this.module.ModuleUser.GetRemoteUser(uid) if err != nil { this.module.Errorf("GetRmoteUser err:%v", err) code = pb.ErrorCode_DBError return } if user.Uid == "" { this.module.Errorf("GetRmoteUser not found uid:%v", uid) code = pb.ErrorCode_UserSessionNobeing return } userExpand, err := this.module.ModuleUser.GetRemoteUserExpand(uid) if err != nil { this.module.Errorf("GetRemoteUserExpand err:%v", err) code = pb.ErrorCode_DBError return } // CD校验 if utils.IsInCDHour(userExpand.SociatyCd) { code = pb.ErrorCode_SociatyCDLimit return } //检查是否已加入公会 if userExpand.SociatyId != "" { code = pb.ErrorCode_SociatyAdded return } // 验证公会名是否重复 if ok, err := this.module.modelSociaty.isNameExist(req.Name); ok { if err != nil { code = pb.ErrorCode_DBError } else { code = pb.ErrorCode_SociatyNameExist } return } //检查钻石 ggd := this.module.modelSociaty.moduleSociaty.configure.GetGlobalConf() if ggd == nil { code = pb.ErrorCode_ConfigNoFound return } if code = this.module.ConsumeRes(session, []*cfg.Gameatn{ggd.GuildBuildCos}, true); code != pb.ErrorCode_Success { return } //创建公会 sociaty := &pb.DBSociaty{ Creater: user.Uid, Name: req.Name, Icon: req.Icon, Notice: req.Notice, IsApplyCheck: req.IsApplyCheck, ApplyLv: req.ApplyLv, } //会长 sociaty.Members = append(sociaty.Members, &pb.SociatyMember{ Uid: user.Uid, Job: pb.SociatyJob_PRESIDENT, //创建人是会长 Ctime: configure.Now().Unix(), }) if err := this.module.modelSociaty.create(sociaty); err != nil { code = pb.ErrorCode_DBError this.module.Errorf("创建公会 err:%v", err) return } // 更新玩家公会 update := map[string]interface{}{ "sociatyId": sociaty.Id, } if err = this.module.ModuleUser.ChangeRemoteUserExpand(user.Uid, update); err != nil { code = pb.ErrorCode_DBError this.module.Errorf("更新玩家公会ID err:%v", err) return } // 初始化任务 if err := this.module.modelSociatyTask.initSociatyTask(user.Uid, sociaty.Id); err != nil { this.module.Errorf("初始化玩家任务 err:%v", err) } if err := session.SendMsg(string(this.module.GetType()), SociatySubTypeCreate, &pb.SociatyCreateResp{ Id: sociaty.Id, Uid: uid, }); err != nil { code = pb.ErrorCode_SystemError return } return }