192 lines
5.5 KiB
Go
192 lines
5.5 KiB
Go
package plunder
|
|
|
|
import (
|
|
"fmt"
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/pb"
|
|
|
|
"go_dreamfactory/sys/configure"
|
|
cfg "go_dreamfactory/sys/configure/structs"
|
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
)
|
|
|
|
// 参数校验
|
|
func (this *apiComp) ChallengeOverCheck(session comm.IUserSession, req *pb.PlunderChallengeOverReq) (errdata *pb.ErrorData) {
|
|
if req.Report == nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ReqParameterError,
|
|
Title: pb.ErrorCode_ReqParameterError.ToString(),
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// /挑战主线关卡
|
|
func (this *apiComp) ChallengeOver(session comm.IUserSession, req *pb.PlunderChallengeOverReq) (errdata *pb.ErrorData) {
|
|
var (
|
|
conf *cfg.GamePlunderData
|
|
err error
|
|
list *pb.DBPlunder
|
|
isWin bool
|
|
changExp map[string]int32
|
|
heroupaward []*cfg.Gameatn
|
|
battleConf *cfg.GamePlunderBattleData
|
|
atno []*pb.UserAtno // atno 类型
|
|
res []*cfg.Gameatn // 最后获得的资源
|
|
land *pb.DBPlunderLand // 岛屿数据
|
|
shipData *pb.ShipData // 船
|
|
users []string
|
|
update map[string]interface{} //
|
|
newShip map[string]*pb.ShipData
|
|
)
|
|
update = make(map[string]interface{})
|
|
changExp = make(map[string]int32, 0)
|
|
newShip = make(map[string]*pb.ShipData, 0)
|
|
if errdata = this.ChallengeOverCheck(session, req); errdata != nil {
|
|
return // 参数校验失败直接返回
|
|
}
|
|
if list, err = this.module.modelPlunder.getPlunderData(session.GetUserId()); err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
if len(list.Source) < int(req.Index) { // 数组长度校验
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ReqParameterError,
|
|
Title: pb.ErrorCode_ReqParameterError.ToString(),
|
|
Message: fmt.Sprintf("list.Source len:%d,req.index:%d", len(list.Source), req.Index),
|
|
}
|
|
return
|
|
}
|
|
// 配置校验
|
|
if conf, err = this.module.configure.getGamePlunderDataById(list.Source[req.Index]); err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ConfigNoFound,
|
|
Title: pb.ErrorCode_ConfigNoFound.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
if battleConf, err = this.module.configure.getGamePlunderBattleById(conf.Battleid); err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ConfigNoFound,
|
|
Title: pb.ErrorCode_ConfigNoFound.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
if req.Pos >= int32(len(list.Line)) {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ReqParameterError,
|
|
Title: pb.ErrorCode_ReqParameterError.ToString(),
|
|
}
|
|
return
|
|
}
|
|
if list.Line[req.Pos].Closetime == -1 { // 队列没解锁
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_PlunderLineUnlock,
|
|
Title: pb.ErrorCode_PlunderLineUnlock.ToString(),
|
|
}
|
|
return
|
|
}
|
|
// 校验通过
|
|
errdata, isWin = this.module.battle.CheckBattleReport(session, req.Report)
|
|
if errdata != nil {
|
|
return
|
|
}
|
|
if !isWin { // 战斗失败直接返回
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_BattleValidationFailed,
|
|
Title: pb.ErrorCode_BattleValidationFailed.ToString(),
|
|
Message: "battle is defeated",
|
|
}
|
|
return
|
|
}
|
|
if land, err = this.module.modelLand.getPlunderLandData(list.Landid); err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
for _, v := range land.Uinfo {
|
|
users = append(users, v.Uid)
|
|
}
|
|
_id := primitive.NewObjectID().Hex()
|
|
// 创建一条船的信息
|
|
shipData = &pb.ShipData{
|
|
Uid: session.GetUserId(),
|
|
Line: &pb.PlunderLine{
|
|
Itype: req.Itype,
|
|
Etime: configure.Now().Unix() + int64(conf.Extime),
|
|
Cid: conf.Id,
|
|
Oid: _id,
|
|
},
|
|
Status: 0,
|
|
Cd: 0,
|
|
Client: false,
|
|
Defend: req.Report.Info.Buleflist,
|
|
}
|
|
list.Count++
|
|
update["count"] = list.Count
|
|
|
|
list.Line[req.Pos].Oid = _id
|
|
update["line"] = list.Line
|
|
list.Setout = append(list.Setout, req.Index)
|
|
update["setout"] = list.Setout
|
|
// 广播用
|
|
newShip[_id] = shipData
|
|
this.module.modelPlunder.changePlunderData(session.GetUserId(), update)
|
|
// 此处需要redis 锁
|
|
land.Ship[_id] = shipData // 一条新船
|
|
|
|
this.module.modelLand.changePlunderLandData(land.Id, map[string]interface{}{
|
|
"ship": land.Ship,
|
|
})
|
|
// 通知大家
|
|
this.module.SendMsgToUsers(string(this.module.GetType()), "change", &pb.PlunderChangePush{
|
|
Ship: newShip,
|
|
}, users...)
|
|
if battleConf.Carexe > 0 {
|
|
var heroObjs []string
|
|
if req.Report != nil && req.Report.Info != nil && len(req.Report.Info.Redflist) > 0 {
|
|
for _, v := range req.Report.Info.Redflist[0].Team {
|
|
if v.HeroID != "" {
|
|
if !v.Ishelp { // 助战英雄不加经验
|
|
heroObjs = append(heroObjs, v.Oid)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if changExp, heroupaward, errdata = this.module.ModuleHero.AddHerosExp(session, heroObjs, battleConf.Carexe); errdata != nil {
|
|
return
|
|
}
|
|
res = append(res, heroupaward...)
|
|
}
|
|
res = append(res, battleConf.Playexp)
|
|
res = append(res, battleConf.Exreward...)
|
|
if battleConf.Reward > 0 {
|
|
if user, err := this.module.GetUserForSession(session); err == nil {
|
|
reward := this.module.ModuleTools.GetGroupDataByLottery(battleConf.Reward, user.Vip, user.Lv)
|
|
res = append(res, reward...)
|
|
}
|
|
}
|
|
|
|
if errdata, atno = this.module.DispenseAtno(session, res, true); errdata != nil {
|
|
return
|
|
}
|
|
session.SendMsg(string(this.module.GetType()), "challengeover", &pb.PlunderChallengeOverResp{
|
|
Line: list.Line,
|
|
Ship: land.Ship,
|
|
Atno: atno,
|
|
Heroexp: changExp,
|
|
})
|
|
go this.module.AsynHandleSession(session.Clone(), func(session comm.IUserSession) {
|
|
this.module.WriteUserLog(session.GetUserId(), req, comm.GMResAddType, "PlunderChallengeOverReq", atno)
|
|
})
|
|
return
|
|
}
|