Пример #1
0
        // Adds a new record to the database
        public async Task <LeaderboardData> AddRecord(string username, int score)
        {
            // List<LeaderboardData> currentLeaderboardData = await this.GetLeaderboard();

            // Create a new object holding the data for the current record
            LeaderboardData leaderboardData = new LeaderboardData()
            {
                Username = username,
                Date     = DateTime.Now,
                Score    = score
            };

            // Crate the path to the record - Gamemode/username
            string path = $"{this.gameMode.ToString()}/" + leaderboardData.Username;

            LeaderboardData data = null;

            try
            {
                // Send the create request to the database
                SetResponse response = await this.firebaseClient.SetAsync(path, leaderboardData);

                data = response.ResultAs <LeaderboardData>();
            }
            catch (Exception exception) { }

            return(data);
        }
        // When the Enter button is clicked
        private async void enterButton_Click(object sender, EventArgs e)
        {
            // Get the data for the leaderboard from the database
            List <LeaderboardData> leaderboardData = await this.leaderboardManager.GetLeaderboard();

            // If the nickname entered is empty or null
            if (nicknameTextBox.Text.Trim().Length == 0 || string.IsNullOrEmpty(nicknameTextBox.Text.Trim()))
            {
                // Show error message
                errorMessage.Text    = "This username is invalid!";
                errorMessage.Visible = true;
            }
            else if (nicknameTextBox.Text.Trim().Length > 5)
            {
                // Show error message
                errorMessage.Text    = "This username is too long! Please enter a shorter one!";
                errorMessage.Visible = true;
            }
            else
            {
                // Get the entered username
                string username = nicknameTextBox.Text;

                // If someone on the leaderboard has the same name
                if (leaderboardData.Any(r => r.Username == username))
                {
                    // Show error message
                    errorMessage.Text    = "This username is already taken! Please use another!";
                    errorMessage.Visible = true;
                }
                else
                {
                    // If the people in the leaderboard are less than 10
                    if (leaderboardData.Count < 10)
                    {
                        // Just add the person's record to the database
                        await this.leaderboardManager.AddRecord(username, this.playerScore);
                    }
                    else
                    {
                        // Get the person in the last place with the lowest score
                        LeaderboardData lastPlace = leaderboardData.OrderByDescending(l => l.Score).Last();

                        // Remove him from the database
                        await this.leaderboardManager.RemoveRecord(lastPlace);

                        // Add the new record in the database
                        await this.leaderboardManager.AddRecord(username, this.playerScore);
                    }

                    // Then close the form
                    this.Close();
                }
            }
        }
Пример #3
0
        // Retrieves all the records inside the database for the current game mode
        public async Task <List <LeaderboardData> > GetLeaderboard()
        {
            List <LeaderboardData> currentLeaderboardData = new List <LeaderboardData>();

            try
            {
                // Send a get request to the database - getting all records for the current gamemode
                FirebaseResponse response = await this.firebaseClient.GetAsync($"{this.gameMode.ToString()}/");

                // If the request response is not null
                if (response.Body != "null")
                {
                    // Parse the JSON response to JObject
                    JObject jobject = JObject.Parse(response.Body);

                    // Go through each user record in the jobject
                    foreach (var user in jobject)
                    {
                        // Get the record's username
                        string username = user.Key;

                        // Get the record's data for the current user
                        JToken userData = user.Value;

                        // Get the record's date
                        DateTime date = DateTime.Parse(userData["Date"].ToString());

                        // Get the record's score
                        int score = int.Parse(userData["Score"].ToString());

                        // Form a object with the collected data
                        LeaderboardData currentLeaderBoardData = new LeaderboardData()
                        {
                            Username = username,
                            Date     = date,
                            Score    = score
                        };

                        // Add the created object to the leaderboard
                        currentLeaderboardData.Add(currentLeaderBoardData);
                    }
                }
            }
            catch (Exception exception) { }

            // Return the leaderboard data
            return(currentLeaderboardData);
        }
Пример #4
0
        // Removes a specific record from the database
        public async Task <FirebaseResponse> RemoveRecord(LeaderboardData leaderboardData)
        {
            // Crate the path to the record - Gamemode/username
            string path = $"{this.gameMode.ToString()}/{leaderboardData.Username}";

            FirebaseResponse response = null;

            try
            {
                // Send the remove request to the database
                response = await this.firebaseClient.DeleteAsync(path);
            }
            catch (Exception exception) { }

            return(response);
        }