league_castle.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package mapLeague
  2. import (
  3. "f1-game/internal/code"
  4. "f1-game/internal/enum"
  5. "f1-game/internal/event"
  6. "f1-game/internal/extend/utils"
  7. nameEvent "f1-game/internal/name/event"
  8. nameLocal "f1-game/internal/name/local"
  9. nameRemote "f1-game/internal/name/remote"
  10. "f1-game/internal/pb"
  11. "f1-game/internal/sessions"
  12. mapCall "f1-game/nodes/map/internal/call"
  13. "f1-game/nodes/map/internal/db"
  14. dbLeague "f1-game/nodes/map/internal/db/data/league"
  15. cstring "github.com/cherry-game/cherry/extend/string"
  16. ctime "github.com/cherry-game/cherry/extend/time"
  17. cfacade "github.com/cherry-game/cherry/facade"
  18. clog "github.com/cherry-game/cherry/logger"
  19. cproto "github.com/cherry-game/cherry/net/proto"
  20. )
  21. func (p *actorLeague) initCastle() {
  22. p.Local().Register(nameLocal.MapLeague_CastleList, p.castleList)
  23. p.Local().Register(nameLocal.MapLeague_CastleAppoint, p.castleAppoint)
  24. p.EventRegister(nameEvent.League_CastleUpdate, p.castleUpdate)
  25. }
  26. // 获取城池列表
  27. func (p *actorLeague) castleList(session *cproto.Session, _ *pb.None) {
  28. p.Response(session, p.leagueTable.Castles.ToProto())
  29. }
  30. // 城池任命或取消任命
  31. func (p *actorLeague) castleAppoint(session *cproto.Session, req *pb.I64I64) {
  32. var (
  33. objectID = req.GetKey() // 城池对象ID
  34. targetPlayerID = req.GetValue() // 目标玩家ID 0 取消任命
  35. )
  36. if objectID < 1 {
  37. p.ResponseCode(session, code.IllegalArgument)
  38. return
  39. }
  40. // 判断城池是否存在
  41. castle, found := p.leagueTable.Castles.Castles.Get(objectID)
  42. if !found {
  43. p.ResponseCode(session, code.MapLeague_CastleNotExist)
  44. return
  45. }
  46. // 判断操作者权限 只有盟主有权任命
  47. leagueTable, found := db.GetMapLeagueTable(p.leagueID)
  48. if !found {
  49. p.ResponseCode(session, code.LeagueNotExist)
  50. return
  51. }
  52. // 当前操作者ID
  53. opPlayerID := sessions.GetPlayerID(session)
  54. if leagueTable.LeagueInfo.LeaderID != opPlayerID {
  55. p.ResponseCode(session, code.LeaguePermissionDenied)
  56. return
  57. }
  58. // 任命
  59. if targetPlayerID > 0 {
  60. // 判断是不是本联盟成员
  61. leagueMemberTable, found := db.GetMapLeagueMemberTable(p.leagueID)
  62. if !found {
  63. p.ResponseCode(session, code.LeagueMembersEmpty)
  64. return
  65. }
  66. found = leagueMemberTable.CheckMemberExist(targetPlayerID)
  67. if !found {
  68. p.ResponseCode(session, code.LeagueMemberNotExist)
  69. return
  70. }
  71. // 判断目标玩家是否已经是其他城池的城主
  72. if p.leagueTable.Castles.IsAppointed(targetPlayerID) {
  73. p.ResponseCode(session, code.MapLeague_CastleAlreadyHasOther)
  74. return
  75. }
  76. // 已经有城主
  77. if castle.OwnerID > 0 {
  78. p.ResponseCode(session, code.MapLeague_CastleAlreadyHasLord)
  79. return
  80. }
  81. castle.SetCastleLord(targetPlayerID)
  82. } else {
  83. // 取消任命
  84. castle.SetCastleLord(0)
  85. }
  86. p.leagueTable.Save2Queue()
  87. p.ResponseCode(session, code.OK)
  88. clog.Debugf("castleAppoint success: %+v", castle)
  89. }
  90. // 联盟城池更新
  91. func (p *actorLeague) castleUpdate(eventData cfacade.IEventData) {
  92. evt, ok := eventData.(*event.LeagueCastleUpdate)
  93. if !ok {
  94. p.Warn("[castleUpdate] eventData error. leagueID:%d", p.leagueID)
  95. return
  96. }
  97. var (
  98. objectID = evt.GetObjectID()
  99. configID = evt.GetConfigID()
  100. point = evt.GetPoint()
  101. isDel = evt.IsDel()
  102. )
  103. if isDel {
  104. p.leagueTable.Castles.DelCastle(objectID)
  105. // 通知所有联盟成员减少产量
  106. p.callToMembers(nameRemote.MapPlayer_UpdateCastleResYield, &pb.I32Bool{
  107. Key: configID,
  108. Value: true,
  109. })
  110. // 添加被攻占日志
  111. leagueLogTable, _ := db.GetMapLeagueLogTable(p.leagueID)
  112. leagueLogTable.AddLeagueLog(enum.Log_LeagueCaptureCity_Enemy, utils.ToStringSlice(evt.GetNewLeagueName(), configID)...)
  113. } else {
  114. newCastle := dbLeague.NewCastle(objectID, configID, point, ctime.Now().ToMillisecond())
  115. p.leagueTable.Castles.AddCastle(newCastle)
  116. // 通知所有联盟成员增加产量
  117. p.callToMembers(nameRemote.MapPlayer_UpdateCastleResYield, &pb.I32Bool{
  118. Key: configID,
  119. Value: false,
  120. })
  121. // 添加攻占日志
  122. leagueLogTable, _ := db.GetMapLeagueLogTable(p.leagueID)
  123. leagueLogTable.AddLeagueLog(enum.Log_LeagueCaptureCity, cstring.ToString(configID))
  124. }
  125. p.leagueTable.Save2Queue()
  126. // 刷新霸业排行榜
  127. seasonScore := p.leagueTable.Castles.GetSeasonScore()
  128. rank := p.leagueTable.ToRankLeague(seasonScore)
  129. mapCall.Rank.UpdateLeagueRank(enum.Rank_202, rank)
  130. p.Debug("[castleUpdate] objectID = %d, configID = %d, point = %v, isDel = %v", objectID, configID, point, isDel)
  131. }