league.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package rankBase
  2. import (
  3. "f1-game/internal/code"
  4. "f1-game/internal/constant"
  5. "f1-game/internal/data"
  6. "f1-game/internal/enum"
  7. "f1-game/internal/event"
  8. nameEvent "f1-game/internal/name/event"
  9. nameLocal "f1-game/internal/name/local"
  10. nameRemote "f1-game/internal/name/remote"
  11. "f1-game/internal/pb"
  12. "f1-game/internal/sessions"
  13. "f1-game/nodes/map/internal/db"
  14. "time"
  15. cfacade "github.com/cherry-game/cherry/facade"
  16. clog "github.com/cherry-game/cherry/logger"
  17. cactor "github.com/cherry-game/cherry/net/actor"
  18. "github.com/cherry-game/cherry/net/parser/pomelo"
  19. cproto "github.com/cherry-game/cherry/net/proto"
  20. )
  21. type League struct {
  22. RankBase
  23. table *db.MapLeagueRankTable
  24. actorBase pomelo.ActorBase
  25. }
  26. func (p *League) Init(actorBase pomelo.ActorBase, event cactor.IEvent, rankStr string, saveInterval time.Duration) {
  27. p.RankBase.Init(actorBase, rankStr, saveInterval, func(rankID int32) (RankTable, bool) {
  28. table, found := db.LoadMapLeagueRankTableFromDB(rankID)
  29. if found {
  30. p.table = table
  31. return table, true
  32. }
  33. return nil, false
  34. })
  35. p.actorBase = actorBase
  36. p.actorBase.Local().Register(nameLocal.MapRank_LeagueList, p.rankList)
  37. p.actorBase.Remote().Register(nameRemote.UpdateRank, p.updateRank)
  38. p.actorBase.Remote().Register(nameRemote.GetRank, p.getRank)
  39. // 注册事件
  40. event.Register(nameEvent.Map_LeagueSync, p.onLeagueSync)
  41. event.Register(nameEvent.League_Upgrade, p.onLeagueUpgrade)
  42. event.Register(nameEvent.Map_WatcherSync, p.onPlayerSync)
  43. event.Register(nameEvent.MapLeague_LeaderChange, p.onLeagueLeaderChange)
  44. event.Register(nameEvent.League_Build, p.onLeagueBuild)
  45. clog.Warnf("init rank finish. rankID=%d", p.rankID)
  46. }
  47. // 获取排行榜列表
  48. func (p *League) rankList(session *cproto.Session, req *pb.I32) {
  49. leagueID := sessions.GetLeagueID(session)
  50. rsp := p.GetRankListWithLeague(leagueID)
  51. p.actorBase.Response(session, rsp)
  52. }
  53. func (p *League) GetRankListWithLeague(leagueID int64) *pb.LeagueRanklist {
  54. myRank := int32(0)
  55. if leagueID > 0 {
  56. myRank = p.table.GetRank(leagueID)
  57. }
  58. return &pb.LeagueRanklist{
  59. List: p.table.GetRankList(p.GetShowCount()),
  60. MyRank: myRank,
  61. }
  62. }
  63. func (p *League) updateRank(rank *pb.RankLeague) {
  64. leagueRank := p.table.GetRank(rank.LeagueID)
  65. // 联盟不在榜内 且 未达上榜所需条件
  66. if leagueRank <= 0 && rank.Data.Value < p.GetMinRankValue() {
  67. return
  68. }
  69. if update := p.table.AddOrUpdateRank(rank.LeagueID, rank, p.GetRankLimit()); update {
  70. p.MarkChange()
  71. }
  72. }
  73. func (p *League) getRank(leagueID int64) (*pb.I32, int32) {
  74. rank := p.table.GetRank(leagueID)
  75. resp := &pb.I32{
  76. Value: rank,
  77. }
  78. return resp, code.OK
  79. }
  80. func (p *League) onLeagueSync(e cfacade.IEventData) {
  81. clog.Debugf("[%d] league sync req.", p.rankID)
  82. evt, ok := e.(*event.MapLeagueSync)
  83. if !ok {
  84. clog.Errorf("[%d] league sync error: %v", p.rankID, e)
  85. return
  86. }
  87. var (
  88. leagueID = evt.UniqueID()
  89. league = evt.MapLeague()
  90. )
  91. rankLeague, found := p.table.GetRankLeague(leagueID)
  92. if !found {
  93. return
  94. }
  95. rankLeague.LeagueName = league.LeagueName
  96. rankLeague.LeagueAbbName = league.LeagueAbbName
  97. p.MarkChange()
  98. }
  99. func (p *League) onLeagueUpgrade(e cfacade.IEventData) {
  100. clog.Debugf("[%d] league sync req.", p.rankID)
  101. evt, ok := e.(*event.LeagueUpgrade)
  102. if !ok {
  103. clog.Errorf("[%d] league sync error: %v", p.rankID, e)
  104. return
  105. }
  106. var (
  107. leagueID = evt.UniqueID()
  108. leagueLevel = evt.GetLevel()
  109. )
  110. rankLeague, found := p.table.GetRankLeague(leagueID)
  111. if !found {
  112. return
  113. }
  114. leagueLevelRow, found := data.LeagueLevel.GetByLevel(leagueLevel)
  115. if !found {
  116. clog.Errorf("[%d] league level not found. leagueID = %d, leagueLevel = %d", p.rankID, leagueID, leagueLevel)
  117. return
  118. }
  119. rankLeague.MemberLimit = leagueLevelRow.LevelAttrs.GetI32(enum.LeagueAttrMemberCountLimit)
  120. p.MarkChange()
  121. }
  122. func (p *League) onPlayerSync(e cfacade.IEventData) {
  123. clog.Debugf("[%d] league player sync req.", p.rankID)
  124. evt, ok := e.(*event.MapWatcherSync)
  125. if !ok {
  126. clog.Errorf("[%d] league player sync error: %v", p.rankID, e)
  127. return
  128. }
  129. if evt.Data().IsFacilitySync {
  130. return
  131. }
  132. var (
  133. playerID = evt.UniqueID()
  134. playerName = evt.Data().PlayerName
  135. )
  136. rankLeague, found := p.table.GetRankLeagueByLeaderID(playerID)
  137. if found && playerName != rankLeague.PlayerName {
  138. rankLeague.PlayerName = evt.Data().PlayerName
  139. p.MarkChange()
  140. }
  141. }
  142. func (p *League) onLeagueLeaderChange(e cfacade.IEventData) {
  143. clog.Debugf("[%d] league player sync req.", p.rankID)
  144. evt, ok := e.(*event.LeagueLeaderChange)
  145. if !ok {
  146. clog.Errorf("[%d] league leader change error: %v", p.rankID, e)
  147. return
  148. }
  149. rankLeague, found := p.table.GetRankLeague(evt.UniqueID())
  150. if found {
  151. rankLeague.LeaderID = evt.LeaderID()
  152. rankLeague.PlayerName = evt.LeaderName()
  153. p.MarkChange()
  154. }
  155. }
  156. func (p *League) onLeagueBuild(e cfacade.IEventData) {
  157. clog.Debugf("[%d] league build req.", p.rankID)
  158. evt, ok := e.(*event.LeagueBuild)
  159. if !ok {
  160. clog.Errorf("[%d] league build error: %v", p.rankID, e)
  161. return
  162. }
  163. // 不是联盟驻地
  164. if evt.BuildID() != constant.MapBuild_LeaguePoint {
  165. return
  166. }
  167. rankLeague, found := p.table.GetRankLeague(evt.UniqueID())
  168. if found {
  169. rankLeague.Point = evt.Point()
  170. p.MarkChange()
  171. }
  172. }
  173. func (p *League) OnStop() {
  174. p.RankBase.OnStop()
  175. }