package mapLeague import ( "f1-game/internal/code" "f1-game/internal/constant" "f1-game/internal/enum" "f1-game/internal/event" "f1-game/internal/extend/utils" nameEvent "f1-game/internal/name/event" nameLocal "f1-game/internal/name/local" nameRoute "f1-game/internal/name/route" "f1-game/internal/pb" "f1-game/internal/sessions" "f1-game/internal/types" mapCall "f1-game/nodes/map/internal/call" "f1-game/nodes/map/internal/db" dbLeague "f1-game/nodes/map/internal/db/data/league" mapTypes "f1-game/nodes/map/internal/types" leagueBase "f1-game/nodes/map/league/base" leagueMail "f1-game/nodes/map/league/mail" ctime "github.com/cherry-game/cherry/extend/time" cfacade "github.com/cherry-game/cherry/facade" cproto "github.com/cherry-game/cherry/net/proto" ) func (p *actorLeague) initWar() { p.Local().Register(nameLocal.MapLeague_StartWar, p.startWar) p.Local().Register(nameLocal.MapLeague_StopWar, p.stopWar) p.Local().Register(nameLocal.MapLeague_WarList, p.warList) p.EventRegister(nameEvent.Map_WarUpdate, p.warUpdate) } // warList 获取联盟战斗列表 func (p *actorLeague) warList(session *cproto.Session, _ *pb.None) { // 获取联盟战斗列表 proto := pb.WarList{} for _, war := range p.leagueTable.Wars { proto.List = append(proto.List, war.ToProto()) } p.Response(session, &proto) } // startWar 宣战 func (p *actorLeague) startWar(session *cproto.Session, req *pb.I64I64) { p.Debug("[startWar] req = %v", req) var ( playerID = sessions.GetPlayerID(session) castleObjectID = req.Key waitTime = req.Value ) // 权限检查 leagueMemberTable, found := db.GetMapLeagueMemberTable(p.leagueID) if !found { p.ResponseCode(session, code.LeagueMembersEmpty) return } if found = leagueMemberTable.CheckMemberExist(playerID); !found { p.ResponseCode(session, code.LeagueNotJoin) return } if !leagueMemberTable.CheckPermission(playerID, enum.League_Permission_Castle) { p.ResponseCode(session, code.LeaguePermissionNotExist) return } if war, ok := p.leagueTable.Wars.Get(castleObjectID); ok { // 是否已经宣战过了 if war.Stage > enum.SiegeStage_None { p.ResponseCode(session, code.MapStartWar_AlreadyStart) return } // 宣战冷却 if war.Cooldown > ctime.Now().ToMillisecond() { p.ResponseCode(session, code.MapDeclareWar_Cooldown) return } } // 最少一个小时 startWarTime := constant.GetStartWarWaitTime(waitTime) rsp, errCode := mapCall.Logic.StartWar(&mapTypes.StartWar{ LeagueID: p.leagueID, CastleObjectID: castleObjectID, Time: startWarTime, }) if code.IsFail(errCode) { p.ResponseCode(session, errCode) return } // 保存战争数据 p.leagueTable.Wars.Put(castleObjectID, &dbLeague.War{ CastleObjectID: castleObjectID, CastleConfigID: rsp.CastleConfigID, CastlePoint: types.NewPoint(rsp.CastlePoint.X, rsp.CastlePoint.Y), SiegeBaseObjectID: rsp.SiegeBaseObjectID, SiegeBaseConfigID: rsp.SiegeBaseConfigID, SiegeBasePoint: types.NewPoint(rsp.SiegeBasePoint.X, rsp.SiegeBasePoint.Y), Stage: enum.SiegeStage(rsp.Stage), StageEndTime: rsp.StageEndTime, MainForceNum: rsp.MainForceNum, SiegeSquadNum: rsp.SiegeSquadNum, }) p.leagueTable.Save2Queue() p.Debug("declare war sucess: CastleObjectID = %d, Stage = %s, EndTime = %s", castleObjectID, enum.GetSiegeStageName(enum.SiegeStage(rsp.Stage)), ctime.CreateFromTimestamp(rsp.StageEndTime).ToDateTimeFormat()) p.ResponseCode(session, code.OK) // 通知所有成员 p.pushToMembers(nameRoute.PushMapLeague_WarUpdate, &pb.I32{Value: 1}) opPlayerName := leagueBase.Service().GetPlayerName(playerID) nowSec := ctime.Now().ToSecond() // 宣战邮件 leagueMail.Service().SendAllMemberMail(p.leagueID, constant.MailID_LeagueStartWar, nil, opPlayerName, nowSec, rsp.CastleConfigID, rsp.SiegeBaseConfigID) // 写入日志 leagueLogTable, _ := db.GetMapLeagueLogTable(p.leagueID) leagueLogTable.AddLeagueLog(enum.Log_LeagueDeclareWar, utils.ToStringSlice(opPlayerName, rsp.CastleConfigID)...) } // stopWar 取消宣战 func (p *actorLeague) stopWar(session *cproto.Session, req *pb.I64) { p.Debug("[stopWar] req = %v", req) var ( playerID = sessions.GetPlayerID(session) castleObjectID = req.Value ) // 权限检查 leagueMemberTable, found := db.GetMapLeagueMemberTable(p.leagueID) if !found { p.ResponseCode(session, code.LeagueMembersEmpty) return } if found = leagueMemberTable.CheckMemberExist(playerID); !found { p.ResponseCode(session, code.LeagueNotJoin) return } if !leagueMemberTable.CheckPermission(playerID, enum.League_Permission_Castle) { p.ResponseCode(session, code.LeaguePermissionNotExist) return } // 未宣战 war, ok := p.leagueTable.Wars.Get(castleObjectID) if !ok { p.ResponseCode(session, code.MapStopWar_NotInWar) return } // 通知地图取消宣战 errCode := mapCall.Logic.StopWar(p.leagueID, castleObjectID) if code.IsFail(errCode) { p.ResponseCode(session, errCode) return } // 清除战争数据 p.leagueTable.Wars.Remove(castleObjectID) p.leagueTable.Save2Queue() p.Debug("stop war success: CastleObjectID = %d", castleObjectID) p.ResponseCode(session, code.OK) // 通知所有成员 p.pushToMembers(nameRoute.PushMapLeague_WarUpdate, &pb.I32{Value: -1}) opPlayerName := leagueBase.Service().GetPlayerName(playerID) // 宣战取消邮件 leagueMail.Service().SendAllMemberMail(p.leagueID, constant.MailID_LeagueStopWar, nil, opPlayerName, war.CastleConfigID) // 添加日志 leagueLogTable, _ := db.GetMapLeagueLogTable(p.leagueID) leagueLogTable.AddLeagueLog(enum.Log_LeagueDeclareCancel, utils.ToStringSlice(opPlayerName, war.CastleConfigID)...) } // warUpdate 战争更新事件处理 func (p *actorLeague) warUpdate(eventData cfacade.IEventData) { warUpdateEvent, ok := eventData.(*event.WarUpdate) if !ok { return } data := warUpdateEvent.Data() p.Debug("[warUpdate] eventData = %+v", data) war, ok := p.leagueTable.Wars.Get(data.CastleObjectID) if !ok { return } // 更新战争数据 if data.Stage == enum.SiegeStage_End { p.leagueTable.Wars.Remove(data.CastleObjectID) p.leagueTable.Save2Queue() p.Debug("war end: CastleObjectID = %d", data.CastleObjectID) p.pushToMembers(nameRoute.PushMapLeague_WarUpdate, &pb.I32{Value: -1}) return } if data.Stage != enum.SiegeStage_None { war.Stage = data.Stage war.StageEndTime = data.StageEndTime p.leagueTable.Save2Queue() // 通知所有成员 p.pushToMembers(nameRoute.PushMapLeague_WarUpdate, &pb.I32{}) return } if data.MainForceNum >= 0 || data.SiegeSquadNum >= 0 { war.MainForceNum = data.MainForceNum war.SiegeSquadNum = data.SiegeSquadNum p.leagueTable.Save2Queue() // 通知所有成员 p.pushToMembers(nameRoute.PushMapLeague_WarUpdate, &pb.I32{}) return } }