| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- package rankBase
- import (
- "f1-game/internal/code"
- "f1-game/internal/constant"
- "f1-game/internal/data"
- "f1-game/internal/enum"
- "f1-game/internal/event"
- nameEvent "f1-game/internal/name/event"
- nameLocal "f1-game/internal/name/local"
- nameRemote "f1-game/internal/name/remote"
- "f1-game/internal/pb"
- "f1-game/internal/sessions"
- "f1-game/nodes/map/internal/db"
- "time"
- cfacade "github.com/cherry-game/cherry/facade"
- clog "github.com/cherry-game/cherry/logger"
- cactor "github.com/cherry-game/cherry/net/actor"
- "github.com/cherry-game/cherry/net/parser/pomelo"
- cproto "github.com/cherry-game/cherry/net/proto"
- )
- type League struct {
- RankBase
- table *db.MapLeagueRankTable
- actorBase pomelo.ActorBase
- }
- func (p *League) Init(actorBase pomelo.ActorBase, event cactor.IEvent, rankStr string, saveInterval time.Duration) {
- p.RankBase.Init(actorBase, rankStr, saveInterval, func(rankID int32) (RankTable, bool) {
- table, found := db.LoadMapLeagueRankTableFromDB(rankID)
- if found {
- p.table = table
- return table, true
- }
- return nil, false
- })
- p.actorBase = actorBase
- p.actorBase.Local().Register(nameLocal.MapRank_LeagueList, p.rankList)
- p.actorBase.Remote().Register(nameRemote.UpdateRank, p.updateRank)
- p.actorBase.Remote().Register(nameRemote.GetRank, p.getRank)
- // 注册事件
- event.Register(nameEvent.Map_LeagueSync, p.onLeagueSync)
- event.Register(nameEvent.League_Upgrade, p.onLeagueUpgrade)
- event.Register(nameEvent.Map_WatcherSync, p.onPlayerSync)
- event.Register(nameEvent.MapLeague_LeaderChange, p.onLeagueLeaderChange)
- event.Register(nameEvent.League_Build, p.onLeagueBuild)
- clog.Warnf("init rank finish. rankID=%d", p.rankID)
- }
- // 获取排行榜列表
- func (p *League) rankList(session *cproto.Session, req *pb.I32) {
- leagueID := sessions.GetLeagueID(session)
- rsp := p.GetRankListWithLeague(leagueID)
- p.actorBase.Response(session, rsp)
- }
- func (p *League) GetRankListWithLeague(leagueID int64) *pb.LeagueRanklist {
- myRank := int32(0)
- if leagueID > 0 {
- myRank = p.table.GetRank(leagueID)
- }
- return &pb.LeagueRanklist{
- List: p.table.GetRankList(p.GetShowCount()),
- MyRank: myRank,
- }
- }
- func (p *League) updateRank(rank *pb.RankLeague) {
- leagueRank := p.table.GetRank(rank.LeagueID)
- // 联盟不在榜内 且 未达上榜所需条件
- if leagueRank <= 0 && rank.Data.Value < p.GetMinRankValue() {
- return
- }
- if update := p.table.AddOrUpdateRank(rank.LeagueID, rank, p.GetRankLimit()); update {
- p.MarkChange()
- }
- }
- func (p *League) getRank(leagueID int64) (*pb.I32, int32) {
- rank := p.table.GetRank(leagueID)
- resp := &pb.I32{
- Value: rank,
- }
- return resp, code.OK
- }
- func (p *League) onLeagueSync(e cfacade.IEventData) {
- clog.Debugf("[%d] league sync req.", p.rankID)
- evt, ok := e.(*event.MapLeagueSync)
- if !ok {
- clog.Errorf("[%d] league sync error: %v", p.rankID, e)
- return
- }
- var (
- leagueID = evt.UniqueID()
- league = evt.MapLeague()
- )
- rankLeague, found := p.table.GetRankLeague(leagueID)
- if !found {
- return
- }
- rankLeague.LeagueName = league.LeagueName
- rankLeague.LeagueAbbName = league.LeagueAbbName
- p.MarkChange()
- }
- func (p *League) onLeagueUpgrade(e cfacade.IEventData) {
- clog.Debugf("[%d] league sync req.", p.rankID)
- evt, ok := e.(*event.LeagueUpgrade)
- if !ok {
- clog.Errorf("[%d] league sync error: %v", p.rankID, e)
- return
- }
- var (
- leagueID = evt.UniqueID()
- leagueLevel = evt.GetLevel()
- )
- rankLeague, found := p.table.GetRankLeague(leagueID)
- if !found {
- return
- }
- leagueLevelRow, found := data.LeagueLevel.GetByLevel(leagueLevel)
- if !found {
- clog.Errorf("[%d] league level not found. leagueID = %d, leagueLevel = %d", p.rankID, leagueID, leagueLevel)
- return
- }
- rankLeague.MemberLimit = leagueLevelRow.LevelAttrs.GetI32(enum.LeagueAttrMemberCountLimit)
- p.MarkChange()
- }
- func (p *League) onPlayerSync(e cfacade.IEventData) {
- clog.Debugf("[%d] league player sync req.", p.rankID)
- evt, ok := e.(*event.MapWatcherSync)
- if !ok {
- clog.Errorf("[%d] league player sync error: %v", p.rankID, e)
- return
- }
- if evt.Data().IsFacilitySync {
- return
- }
- var (
- playerID = evt.UniqueID()
- playerName = evt.Data().PlayerName
- )
- rankLeague, found := p.table.GetRankLeagueByLeaderID(playerID)
- if found && playerName != rankLeague.PlayerName {
- rankLeague.PlayerName = evt.Data().PlayerName
- p.MarkChange()
- }
- }
- func (p *League) onLeagueLeaderChange(e cfacade.IEventData) {
- clog.Debugf("[%d] league player sync req.", p.rankID)
- evt, ok := e.(*event.LeagueLeaderChange)
- if !ok {
- clog.Errorf("[%d] league leader change error: %v", p.rankID, e)
- return
- }
- rankLeague, found := p.table.GetRankLeague(evt.UniqueID())
- if found {
- rankLeague.LeaderID = evt.LeaderID()
- rankLeague.PlayerName = evt.LeaderName()
- p.MarkChange()
- }
- }
- func (p *League) onLeagueBuild(e cfacade.IEventData) {
- clog.Debugf("[%d] league build req.", p.rankID)
- evt, ok := e.(*event.LeagueBuild)
- if !ok {
- clog.Errorf("[%d] league build error: %v", p.rankID, e)
- return
- }
- // 不是联盟驻地
- if evt.BuildID() != constant.MapBuild_LeaguePoint {
- return
- }
- rankLeague, found := p.table.GetRankLeague(evt.UniqueID())
- if found {
- rankLeague.Point = evt.Point()
- p.MarkChange()
- }
- }
- func (p *League) OnStop() {
- p.RankBase.OnStop()
- }
|