go_dreamfactory/modules/sociaty/api_cross_create.go
2023-06-06 11:08:14 +08:00

180 lines
4.7 KiB
Go

package sociaty
import (
"errors"
"go_dreamfactory/comm"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/pb"
"go_dreamfactory/utils"
"sort"
"strings"
"go_dreamfactory/sys/configure"
cfg "go_dreamfactory/sys/configure/structs"
)
// 公会创建
func (this *apiComp) CreateCheck(session comm.IUserSession, req *pb.SociatyCreateReq) (errdata *pb.ErrorData) {
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()})
errdata = &pb.ErrorData{
Code: pb.ErrorCode_ReqParameterError,
Title: pb.ErrorCode_ReqParameterError.ToString(),
}
}
return
}
func (this *apiComp) Create(session comm.IUserSession, req *pb.SociatyCreateReq) (errdata *pb.ErrorData) {
if errdata = this.CreateCheck(session, req); errdata != nil {
return
}
data = &pb.ErrorData{}
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 {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_DBError,
Title: pb.ErrorCode_DBError.ToString(),
Message: err.Error(),
}
data.Title = code.ToString()
return
}
// CD校验
if utils.IsInCDHour(userExpand.SociatyCd) {
code = pb.ErrorCode_SociatyCDLimit
data.Title = code.ToString()
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
}
errdata = &pb.ErrorData{
Code: pb.ErrorCode_DBError,
Title: pb.ErrorCode_DBError.ToString(),
Message: err.Error(),
}
return
}
//检查钻石
if errdata = this.module.ConsumeRes(session, []*cfg.Gameatn{this.module.globalConf.GuildBuildCos}, true); errdata != nil {
this.module.Warn("资源不足",
log.Field{Key: "uid", Value: uid},
log.Field{Key: "res", Value: this.module.globalConf.GuildBuildCos},
)
return
}
//公会申请等级校验
plvConf := this.module.configure.GetPlayerlvConfList()
sort.SliceStable(plvConf, func(i, j int) bool {
return plvConf[i].Lv > plvConf[j].Lv
})
var maxLv int32
if len(plvConf) > 0 {
maxLv = plvConf[0].Lv
}
if req.ApplyLv > maxLv {
code = pb.ErrorCode_SociatyApplyLvLimit
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 {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_DBError,
Title: pb.ErrorCode_DBError.ToString(),
Message: err.Error(),
}
}
}
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 {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_DBError,
Title: pb.ErrorCode_DBError.ToString(),
Message: err.Error(),
}
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
}
go this.module.ModuleBuried.TriggerBuried(session.GetUserId(), comm.GetBuriedParam(comm.Rtype188, 1))
// 初始化玩家公会任务
go func() {
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
}