Exemplo n.º 1
0
        /// <summary>
        /// adds a score to the local leaderboards
        /// </summary>
        /// <param name="score">the score to add</param>
        /// <param name="leaderboard">the leaderboard to add it to</param>
        public static void AddScoreLocaly(HighScore score, string leaderboard)
        {
            int index = Find(score, leaderboard, true);

            NEX_Category?category = CategoryHelper.GetCategory(leaderboard);

            if (category == null)
            {
                throw new Exception("Category does not exits!");
            }

            if (index == -1)
            {
                if (!_localLeaderboards.ContainsKey(leaderboard)) //create the leaderboard if it doesnt exits
                {
                    _localLeaderboards.Add(leaderboard, new List <Networking.HighScore>());
                }

                _localLeaderboards[leaderboard].Add(score); //add the highscore;
                PrintLine("Score Added");
            }
            else
            { //now we know the score exits we need to know if we need to replace the old score or not
                PrintLine("Score Found");
                if (category.Value.SortBy == SortOrder.Accending)
                {
                    if (score.Score < _localLeaderboards[leaderboard][index].Score)
                    {
                        _localLeaderboards[leaderboard][index] = score;
                        PrintLine("Score Replaced");
                        return;
                    }
                    PrintLine("Score Not Replaced");
                }
                else
                {
                    if (score.Score > _localLeaderboards[leaderboard][index].Score)
                    {
                        _localLeaderboards[leaderboard][index] = score;
                        PrintLine("Score Replaced");
                        return;
                    }
                    PrintLine("Score Not Replaced");
                }
            }
        }
Exemplo n.º 2
0
 public QueuededScore(HighScore score, string leaderboard, HighScoreType type)
 {
     Score       = score;
     Leaderboard = leaderboard;
     Type        = type;
 }
Exemplo n.º 3
0
        private static void UploadFailed(HighScore score, string leaderboard, HighScoreType type)
        {
#if DEBUG
            Console.WriteLine($"Uploading Failed. Adding to Scores to send");
#endif
            //make sure that we dont have the score in the list already

            NEX_Category?category = CategoryHelper.GetCategory(leaderboard);
            if (category == null)
            {
                throw new Exception("Category does not exits!");
            }

            int index = -1;
            if (type == HighScoreType.Score)
            {
                index = _scoresToSend.FindIndex(x => x.Leaderboard == leaderboard && x.Score.Principal_ID == score.Principal_ID &&
                                                x.Score.Nex_Unique_ID == score.Nex_Unique_ID && x.Score.Score == score.Score);
            }
            else
            {
                index = _scoresToSend.FindIndex(x => x.Leaderboard == leaderboard && x.Score.Principal_ID == score.Principal_ID &&
                                                x.Score.Nex_Unique_ID == score.Nex_Unique_ID); // && x.Score.CommonUserData == score.CommonUserData); <-- nonsense
            }
            if (index != -1)                                                                   //if the score already exits
            {
                return;
            }

            //here we dont have one of theses scores add it
            int scoreIndex = _scoresToSend.FindIndex(x => x.Leaderboard == leaderboard &&
                                                     x.Score.Principal_ID == score.Principal_ID && x.Score.Nex_Unique_ID == score.Nex_Unique_ID);
            if (scoreIndex == -1)
            {
                _scoresToSend.Add(new QueuededScore(score, leaderboard, type));
                _saveSoon = true;
            }
            else
            {
                bool isBetter = false;
                //check to see if score is better or not
                if (category.Value.SortBy == SortOrder.Accending)
                {
                    if (_scoresToSend[scoreIndex].Score.Score > score.Score)
                    {
                        isBetter = true;
                    }
                }
                else
                if (_scoresToSend[scoreIndex].Score.Score > score.Score)
                {
                    isBetter = true;
                }

                if (isBetter)
                {
                    QueuededScore qs = new QueuededScore(score, _scoresToSend[scoreIndex].Leaderboard, type);
                    _scoresToSend[scoreIndex] = qs;
                }
            }
        }
Exemplo n.º 4
0
        public static void UploadScore(string userName, uint score, string leaderboard, ulong misc = 0, byte[] commonData = null, Action <bool> onCompleate = null)
        {
            NEX_Category?category = CategoryHelper.GetCategory(leaderboard);

            if (category == null)
            {
                throw new Exception("Category does not exits!");
            }
#if DEBUG
            Console.WriteLine($"Uploading Score {userName}: {score}");
#endif
            HighScore hscore = new HighScore()
            {
                Score = score,
                Misc  = misc,
                CommonDataUserName = userName,
                CommonDataBinary   = commonData,
            };

            if (!CanCommunicateWithRankingsServer)
            {
                PrintLine("Not Allowed to connect to rankings server");
                UploadFailed(hscore, leaderboard, HighScoreType.Score); //on failed to upload score
                PrintLine("Adding Score Localy");
                AddScoreLocaly(hscore, leaderboard);
                PrintLine("Done");
                return;
            }

            Thread t = new System.Threading.Thread(() =>
            {
                int checkCout = 10;
                while (IsBussy) //make sure i dont send a score while we are current uploading a score
                {
                    System.Threading.Thread.Sleep(1000);
                    checkCout--;
                    if (checkCout <= 0)
                    {
                        PrintLine("Sending failed");
                        UploadFailed(hscore, leaderboard, HighScoreType.Score); //on failed to upload score
                        return;
                    }
                }
                IsBussy = true;
                //I guess you meant to upload the common data here too but never did
                //using hscore.CommonDataBinary....
                bool success = _uploadScoreToServer(hscore, leaderboard, misc);
                if (!success) //try to upload the scores to the server
                {
                    PrintLine("************************");
                    UploadFailed(hscore, leaderboard, HighScoreType.Score); //on failed to upload score
                    SendingFailed = true;                                   // dont allow to try to connect again
                }
                if (onCompleate != null)
                {
                    onCompleate(success);
                }
                IsBussy = false;
            });
            t.Start();

            PrintLine("Adding Score Localy");
            AddScoreLocaly(hscore, leaderboard);
            PrintLine("Done");
        }
Exemplo n.º 5
0
        public static void Load()
        {
            PrintLine("Atempting Load of leadeboards");
            FileStream fs = null;

            if (!UFile.Exits(_savePath))
            {
                PrintLine("no file found");
                return;
            }
            try
            {
                fs = UFile.Open(_savePath, FileMode.Open);
                PrintLine("Loading Scores");
                CustomXmlReader reader = CustomXmlReader.Create(fs);
                _scoresToSend.Clear();
                while (reader.Read())
                {
                    if (reader.Name == "ScoresToSend")
                    {
                        if (reader.IsStartElement())
                        {
                            HighScore hs = new Networking.HighScore()
                            {
                                Principal_ID       = reader.ReadAttributeULong("Id"),
                                Nex_Unique_ID      = reader.ReadAttributeULong("Nex_Id"),
                                Misc               = reader.ReadAttributeULong("Misc"),
                                Score              = reader.ReadAttributeUInt("Score"),
                                CommonDataBinary   = reader.ReadAttributeArrayOfByte("Cud"),
                                CommonDataUserName = reader.ReadAttributeString("UserName"),
                            };

                            QueuededScore qhs = new QueuededScore(hs, reader.ReadAttributeString("Leaderboard"),
                                                                  (HighScoreType)reader.ReadAttributeInt("Type"));
                            _scoresToSend.Add(qhs);
                        }
                    }
                    string currentLeaderboard;
                    if (reader.Name == "Leaderboard")
                    {
                        if (reader.IsStartElement())
                        {
                            currentLeaderboard = reader.ReadAttributeString("Name");

                            if (!_localLeaderboards.ContainsKey(currentLeaderboard))
                            {
                                _localLeaderboards.Add(currentLeaderboard, new List <HighScore>());
                            }
                            while (reader.Read())
                            {
                                if (reader.Name == "Leaderboard")
                                {
                                    if (!reader.IsStartElement())
                                    {
                                        break;
                                    }
                                }

                                if (reader.Name == "Score")
                                {
                                    if (reader.IsStartElement())
                                    {
                                        HighScore hs = new HighScore()
                                        {
                                            Principal_ID       = reader.ReadAttributeULong("Id"),
                                            Nex_Unique_ID      = reader.ReadAttributeULong("Nex_Id"),
                                            Misc               = reader.ReadAttributeULong("Misc"),
                                            Score              = reader.ReadAttributeUInt("Score"),
                                            CommonDataUserName = reader.ReadAttributeString("UserName"),
                                            CommonDataBinary   = reader.ReadAttributeArrayOfByte("Cud"),
                                        };


                                        _localLeaderboards[currentLeaderboard].Add(hs);
                                    }
                                }
                            }
                        }
                    }
                }

                foreach (QueuededScore qs in _scoresToSend)
                {
                    PrintLine($"{qs.Leaderboard} > {qs.Score.Principal_ID}, {qs.Score.Score}");
                }
                reader.Close();
                PrintLine("Loading Compleate");
            }
            catch (System.Exception x)
            {
                PrintLine($"Error Saving: {x.Message}");
//
            }
            fs.Close();
        }