Exemplo n.º 1
0
        public List <PlayerDatas> getTopXPlayers(int numberOfTops, PlayerDatas PlayerToInsert)
        {
            List <PlayerDatas> historics         = new List <PlayerDatas>();
            List <PlayerDatas> historicsToReturn = new List <PlayerDatas>();

            // insere ou atualiza

            /*
             * if (existThePlayer(PlayerToInsert))
             * {
             *  editPalyerHistoric(PlayerToInsert);
             * }
             * else
             * {
             *  addPlayerToHistoric(PlayerToInsert);
             * }
             */
            historics = getPlayersHistoric();
            historics = sort(historics);
            int control;

            if (numberOfTops > historics.Count())
            {
                control = historics.Count();
            }
            else
            {
                control = numberOfTops;
            }
            for (int i = 0; i < control; i++)
            {
                historicsToReturn.Add(historics[i]);
            }
            return(historicsToReturn);
        }
Exemplo n.º 2
0
        private static void addPlayerToHistoric(PlayerDatas p)
        {
            byte[]       byteArray = Encoding.ASCII.GetBytes("PlayersHistoric.xml");
            MemoryStream stream    = new MemoryStream(byteArray);
            XElement     x         = new XElement("player");

            x.Add(new XAttribute("name", p.getName()));
            x.Add(new XAttribute("points", p.getPoints().ToString()));
            XElement xml = XElement.Load("PlayersHistoric.xml");

            xml.Add(x);
            xml.Save(stream);
        }
Exemplo n.º 3
0
        private static List <PlayerDatas> getPlayersHistoric()
        {
            List <PlayerDatas> historics = new List <PlayerDatas>();
            XElement           xml       = XElement.Load("PlayersHistoric.xml");

            foreach (XElement x in xml.Elements())
            {
                PlayerDatas p = new PlayerDatas();
                p.setName(x.Attribute("name").Value);
                p.setPoints(int.Parse(x.Attribute("points").Value));
                historics.Add(p);
            }
            return(historics);
        }
Exemplo n.º 4
0
        private static List <PlayerDatas> sort(List <PlayerDatas> list)
        {
            PlayerDatas temp = new PlayerDatas();

            for (int i = 0; i < list.Count(); i++)
            {
                for (int j = 0; j < list.Count() - 1; j++)
                {
                    if (list[j].getPoints() < list[j + 1].getPoints())
                    {
                        temp        = list[j + 1];
                        list[j + 1] = list[j];
                        list[j]     = temp;
                    }
                }
            }
            return(list);
        }
Exemplo n.º 5
0
        private static void editPalyerHistoric(PlayerDatas player)
        {
            byte[]       byteArray = Encoding.ASCII.GetBytes("PlayersHistoric.xml");
            MemoryStream stream    = new MemoryStream(byteArray);
            XElement     xml       = XElement.Load("PlayersHistoric.xml");

            if (xml.HasElements)
            {
                XElement x = xml.Elements().Where(p => p.Attribute("name").Value.Equals(player.getName())).First();
                if (x != null)
                {
                    if ((int.Parse(x.Attribute("points").Value)) < player.getPoints())
                    {
                        x.Attribute("points").SetValue(player.getPoints().ToString());
                    }
                }
                xml.Save(stream);
            }
        }
Exemplo n.º 6
0
        private static bool existThePlayer(PlayerDatas player)
        {
            byte[]       byteArray = Encoding.UTF8.GetBytes("PlayersHistoric.xml");
            MemoryStream stream    = new MemoryStream(byteArray);
            XElement     xml       = XElement.Load("PlayersHistoric.xml");

            if (xml.HasElements)
            {
                XElement x = xml.Elements().Where(p => p.Attribute("name").Value.Equals(player.getName())).First();
                if (x != null)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }