Пример #1
0
        public Task <Player[]> GetAll()
        {
            string     jsonToBeDeserialized = System.IO.File.ReadAllText(path);
            Ratkaisija players = JsonConvert.DeserializeObject <Ratkaisija>(jsonToBeDeserialized);

            Player[] players1 = players.PlayerLista;
            // Console.WriteLine(players1[0].Name);
            return(Task.FromResult <Player[]>(players1));
        }
Пример #2
0
        public Task <Player> Create(Player player)
        {
            Player[] players1;
            string   jsonToBeDeserialized = System.IO.File.ReadAllText(path);

            if (jsonToBeDeserialized.Length > 0)
            {
                Ratkaisija players = JsonConvert.DeserializeObject <Ratkaisija>(jsonToBeDeserialized);
                players1 = new Player[players.PlayerLista.Length + 2];
                var newlycreatedPlayer = new Player
                {
                    Id           = player.Id,
                    Name         = player.Name,
                    Score        = 0,
                    Level        = 0,
                    IsBanned     = false,
                    CreationTime = DateTime.Now
                };
                players1 = players.PlayerLista;
                players1[players.PlayerLista.Length] = newlycreatedPlayer;
                Ratkaisija newplayers = new Ratkaisija(players1);
                string     output     = JsonConvert.SerializeObject(newplayers);
                File.WriteAllText(path, output);

                return(Task.FromResult <Player>(newlycreatedPlayer));
            }

            else
            {
                players1 = new Player[1000];

                var newlycreatedPlayer = new Player
                {
                    Id           = player.Id,
                    Name         = player.Name,
                    Score        = 0,
                    Level        = 0,
                    IsBanned     = false,
                    CreationTime = DateTime.Now
                };

                players1[0] = newlycreatedPlayer;
                Ratkaisija newplayers = new Ratkaisija(players1);
                string     output     = JsonConvert.SerializeObject(newplayers);
                File.WriteAllText(path, output);

                return(Task.FromResult <Player>(newlycreatedPlayer));
            }
        }