142 lines
3.7 KiB
Go
142 lines
3.7 KiB
Go
package sociaty
|
|
|
|
import (
|
|
"errors"
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/sys/log"
|
|
"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) == "" {
|
|
this.module.Error("公会创建参数错误", log.Field{Key: "uid", Value: session.GetUserId()}, log.Field{Key: "params", Value: req.String()})
|
|
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 := this.module.ModuleUser.GetUser(uid)
|
|
if user == nil {
|
|
this.module.Error("GetRmoteUser not found", log.Field{Key: "uid", Value: uid})
|
|
code = pb.ErrorCode_UserSessionNobeing
|
|
return
|
|
}
|
|
|
|
userExpand, err := this.module.ModuleUser.GetUserExpand(uid)
|
|
if err != nil {
|
|
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) {
|
|
code = customErr.Code
|
|
return
|
|
}
|
|
code = pb.ErrorCode_DBError
|
|
return
|
|
}
|
|
|
|
//检查钻石
|
|
if code = this.module.ConsumeRes(session, []*cfg.Gameatn{this.module.globalConf.GuildBuildCos}, true); code != pb.ErrorCode_Success {
|
|
this.module.Warn("资源不足",
|
|
log.Field{Key: "uid", Value: uid},
|
|
log.Field{Key: "res", Value: this.module.globalConf.GuildBuildCos},
|
|
)
|
|
return
|
|
}
|
|
|
|
//创建公会
|
|
sociaty := &pb.DBSociaty{
|
|
Creater: user.Uid,
|
|
Name: strings.TrimSpace(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 {
|
|
var customError = new(comm.CustomError)
|
|
if errors.As(err, &customError) {
|
|
if customError.Code == pb.ErrorCode_SociatyNoFound {
|
|
code = pb.ErrorCode_SociatyNoFound
|
|
} else {
|
|
code = pb.ErrorCode_DBError
|
|
}
|
|
}
|
|
this.module.Error("创建公会",
|
|
log.Field{Key: "uid", Value: uid},
|
|
log.Field{Key: "params", Value: req.String()},
|
|
log.Field{Key: "err", Value: err.Error()},
|
|
)
|
|
return
|
|
}
|
|
|
|
// 更新玩家公会
|
|
update := map[string]interface{}{
|
|
"sociatyId": sociaty.Id,
|
|
}
|
|
|
|
if err = this.module.ModuleUser.ChangeUserExpand(user.Uid, update); err != nil {
|
|
code = pb.ErrorCode_DBError
|
|
this.module.Error("更新玩家公会ID",
|
|
log.Field{Key: "uid", Value: uid},
|
|
log.Field{Key: "sociatyId", Value: sociaty.Id},
|
|
log.Field{Key: "err", Value: err.Error()},
|
|
)
|
|
return
|
|
}
|
|
|
|
// 初始化玩家公会任务
|
|
if err := this.module.modelSociatyTask.initSociatyTask(user.Uid, sociaty.Id); err != nil {
|
|
this.module.Error("初始化玩家公会任务",
|
|
log.Field{Key: "uid", Value: uid},
|
|
log.Field{Key: "err", Value: err.Error()},
|
|
)
|
|
}
|
|
|
|
if err := session.SendMsg(string(this.module.GetType()), SociatySubTypeCreate, &pb.SociatyCreateResp{
|
|
Id: sociaty.Id,
|
|
Uid: uid,
|
|
}); err != nil {
|
|
code = pb.ErrorCode_SystemError
|
|
return
|
|
}
|
|
return
|
|
}
|