Пример #1
0
        public List <RowData> GetNears(string AccountId, int Count)
        {
            List <RowData> result = new List <RowData>();

            lock (sync)
            {
                LblItem playerData = GetItemByAccountId(AccountId);
                if (playerData != null)
                {
                    List <RowData> upList   = new List <RowData>();
                    List <RowData> downList = new List <RowData>();

                    int midRank = GetRank(new LeaderboardKey(playerData.Score, playerData.AccountId));

                    #region UP_LIST
                    for (int rank = midRank - Count / 2; rank < midRank - 1; rank++)
                    {
                        if (rank >= 0 && rank < sortList.Count)
                        {
                            string  uId  = sortList.Values[rank];
                            LblItem item = GetItemByAccountId(uId);
                            if (item != null)
                            {
                                upList.Add(item.GetView());
                            }
                        }
                    }
                    #endregion
                    #region DOWN_LIST
                    for (int rank = midRank; rank < midRank + Count / 2; rank++)
                    {
                        if (rank >= 0 && rank < sortList.Count)
                        {
                            string  uId  = sortList.Values[rank];
                            LblItem item = GetItemByAccountId(uId);
                            if (item != null)
                            {
                                downList.Add(item.GetView());
                            }
                        }
                    }
                    #endregion

                    result.AddRange(upList);
                    result.Add(playerData.GetView());
                    result.AddRange(downList);
                }
            }
            return(result);
        }
Пример #2
0
        public void AddOrUpdate(string AccountId, float Score, int DeltaRank = 0)
        {
            lock (sync)
            {
                if (isInit)
                {
                    if (id_data_dic.TryGetValue(AccountId, out LblItem item))
                    {
                        int prevRank = GetRank(new LeaderboardKey(item.Score, AccountId));


                        sortList.Remove(new LeaderboardKey(item.Score, AccountId));

                        item.Score = Score;

                        sortList.Add(new LeaderboardKey(Score, AccountId), AccountId);

                        int curRank = GetRank(new LeaderboardKey(item.Score, AccountId));

                        item.DeltaRank = prevRank - curRank;
                    }
                    else
                    {
                        item = new LblItem()
                        {
                            AccountId = AccountId,
                            Score     = Score
                        };

                        id_data_dic.TryAdd(AccountId, item);

                        sortList.Add(new LeaderboardKey(Score, AccountId), AccountId);

                        item.DeltaRank = DeltaRank;
                    }
                }
                else
                {
                    preUpdate.Add(new LeaderboardKey(Score, AccountId, DeltaRank));
                }
            }
        }
Пример #3
0
 private int GetRank(LblItem item)
 {
     lock (sync)
         return(sortList.IndexOfKey(new LeaderboardKey(item.Score, item.AccountId)) + 1);
 }