league_castle.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package mapLeague
  2. import (
  3. "f1-game/internal/code"
  4. "f1-game/internal/event"
  5. nameEvent "f1-game/internal/name/event"
  6. nameLocal "f1-game/internal/name/local"
  7. "f1-game/internal/pb"
  8. "f1-game/internal/sessions"
  9. "f1-game/nodes/map/internal/db"
  10. dbLeague "f1-game/nodes/map/internal/db/data/league"
  11. ctime "github.com/cherry-game/cherry/extend/time"
  12. cfacade "github.com/cherry-game/cherry/facade"
  13. clog "github.com/cherry-game/cherry/logger"
  14. cproto "github.com/cherry-game/cherry/net/proto"
  15. )
  16. func (p *actorLeague) initCastle() {
  17. p.Local().Register(nameLocal.MapLeague_CastleList, p.castleList)
  18. p.Local().Register(nameLocal.MapLeague_CastleAppoint, p.castleAppoint)
  19. p.EventRegister(nameEvent.League_CastleUpdate, p.castleUpdate)
  20. }
  21. // 获取城池列表
  22. func (p *actorLeague) castleList(session *cproto.Session, _ *pb.None) {
  23. p.Response(session, p.leagueTable.Castles.ToProto())
  24. }
  25. // 城池任命或取消任命
  26. func (p *actorLeague) castleAppoint(session *cproto.Session, req *pb.I64I64) {
  27. var (
  28. objectID = req.GetKey() // 城池对象ID
  29. targetPlayerID = req.GetValue() // 目标玩家ID 0 取消任命
  30. )
  31. if objectID < 1 {
  32. p.ResponseCode(session, code.IllegalArgument)
  33. return
  34. }
  35. // 判断城池是否存在
  36. castle, found := p.leagueTable.Castles.Castles.Get(objectID)
  37. if !found {
  38. p.ResponseCode(session, code.MapLeague_CastleNotExist)
  39. return
  40. }
  41. // 判断操作者权限 只有盟主有权任命
  42. leagueTable, found := db.GetMapLeagueTable(p.leagueID)
  43. if !found {
  44. p.ResponseCode(session, code.LeagueNotExist)
  45. return
  46. }
  47. // 当前操作者ID
  48. opPlayerID := sessions.GetPlayerID(session)
  49. if leagueTable.LeagueInfo.LeaderID != opPlayerID {
  50. p.ResponseCode(session, code.LeaguePermissionDenied)
  51. return
  52. }
  53. // 任命
  54. if targetPlayerID > 0 {
  55. // 判断是不是本联盟成员
  56. leagueMemberTable, found := db.GetMapLeagueMemberTable(p.leagueID)
  57. if !found {
  58. p.ResponseCode(session, code.LeagueMembersEmpty)
  59. return
  60. }
  61. found = leagueMemberTable.CheckMemberExist(targetPlayerID)
  62. if !found {
  63. p.ResponseCode(session, code.LeagueMemberNotExist)
  64. return
  65. }
  66. // 判断目标玩家是否已经是其他城池的城主
  67. if p.leagueTable.Castles.IsAppointed(targetPlayerID) {
  68. p.ResponseCode(session, code.MapLeague_CastleAlreadyHasOther)
  69. return
  70. }
  71. // 已经有城主
  72. if castle.OwnerID > 0 {
  73. p.ResponseCode(session, code.MapLeague_CastleAlreadyHasLord)
  74. return
  75. }
  76. castle.SetCastleLord(targetPlayerID)
  77. } else {
  78. // 取消任命
  79. castle.SetCastleLord(0)
  80. }
  81. p.leagueTable.Save2Queue()
  82. p.ResponseCode(session, code.OK)
  83. clog.Debugf("castleAppoint success: %+v", castle)
  84. }
  85. // 联盟城池更新
  86. func (p *actorLeague) castleUpdate(eventData cfacade.IEventData) {
  87. evt, ok := eventData.(*event.LeagueCastleUpdate)
  88. if !ok {
  89. p.Warn("[castleUpdate] eventData error. leagueID:%d", p.leagueID)
  90. return
  91. }
  92. var (
  93. objectID = evt.GetObjectID()
  94. configID = evt.GetConfigID()
  95. point = evt.GetPoint()
  96. isDel = evt.IsDel()
  97. )
  98. if isDel {
  99. p.leagueTable.Castles.DelCastle(objectID)
  100. } else {
  101. newCastle := dbLeague.NewCastle(objectID, configID, point, ctime.Now().ToMillisecond())
  102. p.leagueTable.Castles.AddCastle(newCastle)
  103. }
  104. p.leagueTable.Save2Queue()
  105. p.Debug("[castleUpdate] objectID = %d, configID = %d, point = %v, isDel = %v", objectID, configID, point, isDel)
  106. }