143 lines
4.0 KiB
Go
143 lines
4.0 KiB
Go
package plunder
|
|
|
|
import (
|
|
"fmt"
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/sys/redis"
|
|
"go_dreamfactory/pb"
|
|
"go_dreamfactory/sys/configure"
|
|
)
|
|
|
|
// 参数校验
|
|
func (this *apiComp) PvpChallengeCheck(session comm.IUserSession, req *pb.PlunderPvpChallengeReq) (errdata *pb.ErrorData) {
|
|
if req.Oid == "" || req.Battle.Format == nil || len(req.Battle.Format) != 5 {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ReqParameterError,
|
|
Title: pb.ErrorCode_ReqParameterError.ToString(),
|
|
}
|
|
}
|
|
|
|
for _, v := range req.Battle.Format {
|
|
if v != "" {
|
|
return
|
|
}
|
|
}
|
|
errdata = &pb.ErrorData{ //没有英雄
|
|
Code: pb.ErrorCode_ReqParameterError,
|
|
Title: pb.ErrorCode_ReqParameterError.ToString(),
|
|
Message: "no hero",
|
|
}
|
|
return
|
|
}
|
|
|
|
// / 状态 0 运输 1 正在被攻击 2 战败cd中 3 掠夺成功
|
|
// /挑战
|
|
func (this *apiComp) PvpChallenge(session comm.IUserSession, req *pb.PlunderPvpChallengeReq) (errdata *pb.ErrorData) {
|
|
var (
|
|
record *pb.DBBattleRecord
|
|
err error
|
|
land *pb.DBPlunderLand // 岛屿数据
|
|
list *pb.DBPlunder
|
|
lock *redis.RedisMutex
|
|
users []string
|
|
changeShip map[string]*pb.ShipData
|
|
)
|
|
if errdata = this.PvpChallengeCheck(session, req); errdata != nil {
|
|
return
|
|
}
|
|
changeShip = make(map[string]*pb.ShipData)
|
|
if list, err = this.module.modelPlunder.getPlunderData(session.GetUserId()); err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
if lock, err = this.module.modelLand.landMutexLock(list.Landid); err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Title: pb.ErrorCode_DBError.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
defer lock.Unlock()
|
|
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)
|
|
}
|
|
// 校验oid
|
|
if _, ok := land.Ship[req.Oid]; !ok {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_PlunderNotFoundShip,
|
|
Title: pb.ErrorCode_PlunderNotFoundShip.ToString(),
|
|
}
|
|
return
|
|
}
|
|
// 品质校验
|
|
if int64(land.Ship[req.Oid].Line.Itype) == 0 { // 普通船不能被掠夺
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_PlundeNormalShip,
|
|
Title: pb.ErrorCode_PlundeNormalShip.ToString(),
|
|
Message: fmt.Sprintf("ErrorCode_PlundeNormalShip :%d", land.Ship[req.Oid].Line.Itype),
|
|
}
|
|
return
|
|
}
|
|
if land.Ship[req.Oid].Line.Etime > configure.Now().Unix() { // 船到达不能被掠夺
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_PlundeShipReach,
|
|
Title: pb.ErrorCode_PlundeShipReach.ToString(),
|
|
Message: fmt.Sprintf("PlundeShipReach :%d", land.Ship[req.Oid].Line.Etime),
|
|
}
|
|
return
|
|
}
|
|
// 状态校验
|
|
if land.Ship[req.Oid].Status != 0 { // return
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_PlunderNotFoundShip,
|
|
Title: pb.ErrorCode_PlunderNotFoundShip.ToString(),
|
|
Message: fmt.Sprintf("Status err,curStutus :%d", land.Ship[req.Oid].Status),
|
|
}
|
|
return
|
|
}
|
|
if errdata, record = this.module.battle.CreatePlunderPvpBattle(session, &pb.BattlePVEPlunderReq{
|
|
Ptype: pb.PlayType_plunderpvp,
|
|
Title: "",
|
|
Rulesid: 105,
|
|
Format: req.Battle,
|
|
}); errdata != nil {
|
|
return
|
|
}
|
|
land.Ship[req.Oid].Status = 1 // 修改状态
|
|
changeShip[req.Oid] = land.Ship[req.Oid] // 广播数据
|
|
this.module.modelLand.changePlunderLandData(land.Id, map[string]interface{}{
|
|
"ship": land.Ship,
|
|
})
|
|
session.SendMsg(string(this.module.GetType()), "pvpchallenge", &pb.PlunderPvpChallengeResp{
|
|
Oid: req.Oid,
|
|
Info: &pb.BattleInfo{
|
|
Id: record.Id,
|
|
Title: record.Title,
|
|
Rulesid: 105,
|
|
Btype: record.Btype,
|
|
Ptype: record.Ptype,
|
|
RedCompId: record.RedCompId,
|
|
Redflist: record.Redflist,
|
|
BlueCompId: land.Ship[req.Oid].Uid,
|
|
Buleflist: land.Ship[req.Oid].Defend,
|
|
Tasks: record.Tasks,
|
|
},
|
|
})
|
|
// 通知大家
|
|
this.module.SendMsgToUsers(string(this.module.GetType()), "change", &pb.PlunderChangePush{
|
|
Ship: changeShip,
|
|
}, users...)
|
|
return
|
|
}
|