129 lines
3.1 KiB
Go
129 lines
3.1 KiB
Go
package sociaty
|
|
|
|
import (
|
|
"errors"
|
|
"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 err := this.module.modelSociaty.isNameExist(req.Name); err != nil {
|
|
var customErr = new(comm.CustomError)
|
|
if errors.As(err, &customErr) {
|
|
if customErr.Code == pb.ErrorCode_SociatyNameExist {
|
|
code = pb.ErrorCode_SociatyNameExist
|
|
return
|
|
}
|
|
}
|
|
code = pb.ErrorCode_DBError
|
|
return
|
|
}
|
|
|
|
//检查钻石
|
|
if code = this.module.ConsumeRes(session, []*cfg.Gameatn{
|
|
this.module.globalConf.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
|
|
}
|