Exemplo n.º 1
0
    public void LoadLeaderBoard()
    {
        LeaderBoard = new List <LeaderBoardItem>();

        for (int i = 0; i < 10; i++)
        {
            string keyName  = string.Format("LeaderBoard[{0}].Name", i);
            string keyScore = string.Format("LeaderBoard[{0}].Score", i);

            if (PlayerPrefs.HasKey(keyName))
            {
                LeaderBoardItem item = new LeaderBoardItem()
                {
                    Name  = PlayerPrefs.GetString(keyName),
                    Score = PlayerPrefs.GetInt(keyScore)
                };

                LeaderBoard.Add(item);
            }
            else
            {
                break;
            }
        }

        LeaderBoard = LeaderBoard.OrderByDescending(x => x.Score).ToList();
    }
Exemplo n.º 2
0
        public void OnLeaderboardEvent(IEventBase obj)
        {
            if (content == null || itemPrefab == null)
            {
                return;
            }

            if (content != null)
            {
                LeaderBoardItem[] lbis = content.GetComponentsInChildren <LeaderBoardItem>();
                for (int i = 0; i < lbis.Length; i++)
                {
                    GameObject.Destroy(lbis[i].gameObject);
                }
            }

            if (obj is SetLeaderboardEvent)
            {
                SetLeaderboardEvent lb = (SetLeaderboardEvent)obj;

                if (lb.leaderboard == null)
                {
                    Debug.LogError("Empty leaderboard");
                    return;
                }

                if (lb.leaderboard.Items == null)
                {
                    Debug.LogError("Empty leaderboard Items");
                    return;
                }

                //if (lb.leaderboard.Items.Count > 1)
                //{
                //    lb.leaderboard.Items.Sort((x, y) =>
                //    {
                //        return string.Compare(y.Score, x.Score);
                //    });
                //}

                for (int i = 0; i < lb.leaderboard.Items.Count; i++)
                {
                    GameObject      newItem = Instantiate(itemPrefab, content.transform) as GameObject;
                    LeaderBoardItem lbi     = newItem.GetComponent <LeaderBoardItem>();
                    if (lbi != null)
                    {
                        lbi.txt_Rank.text   = (i + 1).ToString(); //lb.leaderboard.Items[i].Rank.ToString();
                        lbi.txt_Name.text   = lb.leaderboard.Items[i].Username;
                        lbi.txt_Points.text = lb.leaderboard.Items[i].Score.ToString();
                        // lbi.transform.parent = content.transform;
                    }
                }
            }
        }
Exemplo n.º 3
0
 public void AddToLeaderBoard(LeaderBoardItem val)
 {
     if (val.PlayerName == "")
     {
         val.PlayerName = "Player" + leaderboardItems.Count.ToString();
     }
     leaderboardItems.Add(val);
     leaderboardItems.Sort(delegate(LeaderBoardItem x, LeaderBoardItem y) {
         return(x.HighScore.CompareTo(y.HighScore));
     });
     if (leaderboardItems.Count > TotalHighScores && TotalHighScores != 0)
     {
         leaderboardItems.RemoveRange(TotalHighScores, leaderboardItems.Count - 1);
     }
     SaveLeaderBoardItems();
 }
Exemplo n.º 4
0
        public async Task <bool> ShowAsync(LeaderBoardItem scoresItem,
                                           CancellationToken cancellationToken = default)
        {
            await UpdateScoresAsync(scoresItem, cancellationToken);

            bool?playAgain = null;

            while (playAgain == null)
            {
                await DrawAsync(cancellationToken);

                playAgain = await PlayerInputAsync(cancellationToken);
            }

            return(await Task.FromResult((bool)playAgain));
        }
Exemplo n.º 5
0
        private async Task UpdateScoresAsync(LeaderBoardItem scoresItem, CancellationToken cancellationToken = default)
        {
            _leaderBoard.Clear();
            // init serializer
            var options = new JsonSerializerOptions
            {
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
                WriteIndented        = true
            };

            //load data
            string jsonString;
            var    fileName = $"{nameof(Tetris)}.letterBoard";

            if (File.Exists(fileName))
            {
                jsonString = await File.ReadAllTextAsync(fileName, cancellationToken);

                _leaderBoard = JsonSerializer.Deserialize <IList <LeaderBoardItem> >(jsonString, options);
            }

            // normalize
            _leaderBoard = _leaderBoard.Where(x => x.Player != null).ToList();

            // join actual scores
            var item = _leaderBoard.FirstOrDefault(x => x.Player.Equals(scoresItem.Player,
                                                                        StringComparison.OrdinalIgnoreCase));

            if (item != null)
            {
                _leaderBoard.Remove(item);
            }

            _leaderBoard.Add(scoresItem);

            // taking tops
            _leaderBoard = _leaderBoard
                           .OrderByDescending(x => x.Level)
                           .ThenByDescending(x => x.Score)
                           .Take(Constants.MaxTopPlayers)
                           .ToList();

            // storing back to file
            jsonString = JsonSerializer.Serialize(_leaderBoard, options);
            await File.WriteAllTextAsync(fileName, jsonString, cancellationToken);
        }
Exemplo n.º 6
0
    public void SaveLeaderBoard()
    {
        for (int i = 0; i < 10; i++)
        {
            if (i >= LeaderBoard.Count)
            {
                break;
            }

            LeaderBoardItem item = LeaderBoard[i];

            string keyName  = string.Format("LeaderBoard[{0}].Name", i);
            string keyScore = string.Format("LeaderBoard[{0}].Score", i);

            PlayerPrefs.SetString(keyName, item.Name);
            PlayerPrefs.SetInt(keyScore, item.Score);
        }
    }
Exemplo n.º 7
0
    public bool AddScoreToLeaderBoard(int score)
    {
        bool result = false;

        for (int i = 0; i < 10; i++)
        {
            if (LeaderBoard.Count <= i)
            {
                LeaderBoard.Add(new LeaderBoardItem()
                {
                    Name  = CurrentGame.PlayerName,
                    Score = score
                });

                result = true;
                break;
            }
            else
            {
                LeaderBoardItem item = LeaderBoard[i];

                if (score > item.Score)
                {
                    LeaderBoard.Insert(i, new LeaderBoardItem()
                    {
                        Name  = CurrentGame.PlayerName,
                        Score = score
                    });

                    result = true;
                    break;
                }
            }
        }

        if (result)
        {
            SaveLeaderBoard();
        }

        return(result);
    }
Exemplo n.º 8
0
        public async Task <LeaderBoardRS> GetLeaderBoardAsync()
        {
            LeaderBoardRS response = new LeaderBoardRS();

            DataSet    dataSet = new DataSet();
            DataTable  table   = new DataTable();
            SqlCommand cmd     = await ExecuteProcdureAsync(connection, "Leaderboard");

            sqlDataAdapter.SelectCommand = cmd;
            sqlDataAdapter.Fill(dataSet);
            table = dataSet.Tables[0];

            foreach (DataRow row in table.Rows)
            {
                LeaderBoardItem leaderBoardItem = new LeaderBoardItem()
                {
                    UserName = row.ItemArray[0].ToString().Trim(),
                    Score    = Convert.ToInt32(row.ItemArray[1].ToString().Trim())
                };
                response.leaderBoardItems.Add(leaderBoardItem);
            }
            return(response);
        }