Пример #1
0
        static void Main()
        {
            var context = new DiabloEntities();
            var characters = context.Characters
                .Select(c => c.Name);

            foreach (var character in characters)
            {
                Console.WriteLine(character);
            }
        }
Пример #2
0
        static void Main()
        {
            var context    = new DiabloEntities();
            var characters = context.Characters
                             .Select(c => c.Name);

            foreach (var character in characters)
            {
                Console.WriteLine(character);
            }
        }
        static void Main()
        {
            var context = new DiabloEntities();

            var finishedGames = context.Games
                .Where(g => g.IsFinished)
                .OrderBy(g => g.Name)
                .ThenBy(g => g.Duration)
                .Select(g => new
                {
                    GameName = g.Name,
                    Duration = g.Duration,
                    Users = g.UsersGames
                        .Select(ug => new
                        {   
                            UserName = ug.User.Username,
                            IpAddress = ug.User.IpAddress
                        })
                });

            string file = "../../finished-games.xml";
            Encoding encoding = Encoding.GetEncoding("utf-8");

            using (XmlTextWriter writer = new XmlTextWriter(file, encoding))
            {
                writer.Formatting = Formatting.Indented;
                writer.IndentChar = '\t';
                writer.Indentation = 1;

                writer.WriteStartDocument();
                writer.WriteStartElement("games");
                foreach (var game in finishedGames)
                {
                    WriteGame(writer, game.GameName, game.Duration);
                    writer.WriteStartElement("users");
                    foreach (var user in game.Users)
                    {
                        WriteUser(writer, user.UserName, user.IpAddress);
                        writer.WriteEndElement();
                    }
                    writer.WriteEndElement();
                    writer.WriteEndElement();
                }
            }
        }
        static void Main()
        {
            var context = new DiabloEntities();
            string fileName = "../../characters.json";

            var characters = context.Characters
                .OrderBy(c => c.Name)
                .Select(c => new
                {
                    name = c.Name,
                    playedBy = c.UsersGames
                                .Select(ug => ug.User.Username)
                });

            var json = JsonConvert.SerializeObject(characters, 
                                            Formatting.Indented);
            File.WriteAllText(fileName, json);
        }
        static void Main()
        {
            string fileName = "../../users-and-games.xml";

            var context = new DiabloEntities();
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(fileName);

            XmlNode rootNode = xmlDocument.DocumentElement;

            foreach (XmlNode user in rootNode.ChildNodes)
            {
                try
                {
                    if (user.Attributes.Count > 0)
                    {
                        string firstName = null, lastName = null, email = null, username = null, ip = null;
                        DateTime regDate = DateTime.MinValue;
                        bool isDeleted = false;
                        User userFromDb = new User();
                        foreach (XmlAttribute attribute in user.Attributes)
                        {
                            if (attribute.Name == "first-name")
                            {
                                firstName = attribute.Value;
                            }

                            if (attribute.Name == "last-name")
                            {
                                lastName = attribute.Value;
                            }

                            if (attribute.Name == "username")
                            {
                                username = attribute.Value;
                            }

                            if (attribute.Name == "email")
                            {
                                email = attribute.Value;
                            }

                            if (attribute.Name == "ip-address")
                            {
                                ip = attribute.Value;
                            }

                            if (attribute.Name == "is-deleted")
                            {
                                isDeleted = attribute.Value == "1";
                            }

                            if (attribute.Name == "registration-date")
                            {
                                regDate = DateTime.ParseExact(attribute.Value, "dd/mm/yyyy",
                                    CultureInfo.InvariantCulture);
                            }
                        }

                        userFromDb = context.Users
                            .FirstOrDefault(u => u.Username == username);
                        if (userFromDb == null)
                        {
                            userFromDb = new User()
                            {
                                Username = username,
                                FirstName = firstName,
                                LastName = lastName,
                                Email = email,
                                RegistrationDate = regDate,
                                IsDeleted = isDeleted,
                                IpAddress = ip
                            };
                            context.Users.Add(userFromDb);
                            Console.WriteLine("Successfully added user {0}", username);
                        }
                        else
                        {
                            Console.WriteLine("User {0} already exists", username);
                            throw new ArgumentException("User already exists");
                        }

                        foreach (XmlNode game in user.FirstChild.ChildNodes)
                        {
                            string gameName = "", characterName = "";
                            int? gameId, userId, characterId, level = null;
                            DateTime? joinedOn = null;
                            decimal? cash = null;
                            Game gameFromDb = new Game();
                            if (game["game-name"] != null)
                            {
                                gameName = game["game-name"].InnerText;
                            }
                            if (game["character"] != null)
                            {
                                foreach (XmlAttribute attribute in game["character"].Attributes)
                                {
                                    if (attribute.Name == "name")
                                    {
                                        characterName = attribute.Value;
                                    }

                                    if (attribute.Name == "cash")
                                    {
                                        cash = decimal.Parse(attribute.Value);
                                    }

                                    if (attribute.Name == "level")
                                    {
                                        level = int.Parse(attribute.Value);
                                    }
                                }
                            }

                            if (game["joined-on"] != null)
                            {
                                joinedOn = DateTime.ParseExact(game["joined-on"].InnerText, "dd/mm/yyyy",
                                    CultureInfo.InvariantCulture);
                            }

                            gameFromDb = context.Games
                                .FirstOrDefault(g => g.Name == gameName);
                            
                            if (gameFromDb == null)
                            {
                                throw new ArgumentException("No such game exists");
                            }
                            gameId = gameFromDb.Id;

                            if (userFromDb == null)
                            {
                                throw new ArgumentException("User already exists");
                            }
                            userId = userFromDb.Id;

                            Character character = context.Characters
                                .FirstOrDefault(c => c.Name == characterName);
                            characterId = character.Id;

                            context.UsersGames.Add(new UsersGame()
                            {
                                GameId = gameId.Value,
                                User = userFromDb,
                                CharacterId = characterId.Value,
                                Level = level.Value,
                                JoinedOn = joinedOn.Value,
                                Cash = cash.Value
                            });
                            Console.WriteLine("User {0} successfully added to game {1}", username, gameName);
                        }
                    }
                    context.SaveChanges();
                }
                catch (ArgumentException ex)
                {

                }
            }
        }