예제 #1
0
        public static void Main()
        {
            var context = new DiabloEntities();
            var charactersNames = context.Characters
                .Select(c => c.Name);

            foreach (var charactersName in charactersNames)
            {
                Console.WriteLine(charactersName);
            }
        }
        public static void Main()
        {
            var context = new DiabloEntities();
            var charactersWithPlayers = context.Characters
                .OrderBy(c => c.Name)
                .Select(c => new
                {
                    name = c.Name,
                    playedBy = c.UsersGames
                        .Select(ug => ug.User.Username)
                });

            string json = JsonConvert.SerializeObject(charactersWithPlayers, Formatting.Indented);
            File.WriteAllText("../../characters.json", json);
        }
        private static bool CheckIfAllGamesExists(IEnumerable<XElement> games, DiabloEntities context)
        {
            foreach (var game in games)
            {
                string gameName = game.Element("game-name").Value;
                var gameInDatabase = context.Games
                    .FirstOrDefault(g => g.Name == gameName);

                if (gameInDatabase == null)
                {
                    return false;
                }
            }

            return true;
        }
        public static void Main()
        {
            var context = new DiabloEntities();
            var finishedGamesWithPlayers = context.Games
                .Where(g => g.IsFinished)
                .OrderBy(g => g.Name)
                .ThenBy(g => g.Duration)
                .Select(g => new
                {
                    g.Name,
                    g.Duration,
                    Users = g.UsersGames
                        .Select(ug => new
                        {
                            ug.User.Username,
                            ug.User.IpAddress
                        })
                });

            XElement rootElement = new XElement("games");
            foreach (var game in finishedGamesWithPlayers)
            {
                XElement xGame = new XElement(
                    "game",
                    new XAttribute("name", game.Name));
                if (game.Duration != null)
                {
                    xGame.SetAttributeValue("duration", game.Duration);
                }

                XElement xUsers = new XElement("users");
                foreach (var user in game.Users)
                {
                    XElement xUser = new XElement(
                        "user",
                        new XAttribute("username", user.Username),
                        new XAttribute("ip-address", user.IpAddress));

                    xUsers.Add(xUser);
                }

                xGame.Add(xUsers);
                rootElement.Add(xGame);
            }

            rootElement.Save("../../finished-games.xml");
        }
        public static void Main()
        {
            var context = new DiabloEntities();
            XDocument doc = XDocument.Load("../../users-and-games.xml");
            var xmlUsers = doc.XPathSelectElements("users/user");

            foreach (var xmlUser in xmlUsers)
            {
                var games = xmlUser.XPathSelectElements("games/game");
                bool allGamesExistsInDatabases = CheckIfAllGamesExists(games, context);

                if (allGamesExistsInDatabases)
                {
                    User userInDatabase = CreateUserIfNotExists(xmlUser, context);
                    AddUserToGames(games, userInDatabase, context);
                }
            }
        }
        private static void AddUserToGames(IEnumerable<XElement> games, User userInDatabase, DiabloEntities context)
        {
            foreach (var game in games)
            {
                string gameName = game.Element("game-name").Value;
                string characterName = game.Element("character").Attribute("name").Value;
                int characterId = context.Characters
                    .FirstOrDefault(ch => ch.Name == characterName).Id;
                decimal characterCash = decimal.Parse(game.Element("character").Attribute("cash").Value);
                int characterLevel = int.Parse(game.Element("character").Attribute("level").Value);
                DateTime joinedOn = DateTime.Parse(game.Element("joined-on").Value);

                int gameId = context.Games
                    .FirstOrDefault(g => g.Name == gameName).Id;
                var usersGameInDatabase = context.UsersGames
                    .FirstOrDefault(ug => ug.GameId == gameId && ug.UserId == userInDatabase.Id);

                if (usersGameInDatabase == null)
                {
                    UsersGame newUsersGame = new UsersGame()
                    {
                        GameId = gameId,
                        UserId = userInDatabase.Id,
                        CharacterId = characterId,
                        Level = characterLevel,
                        JoinedOn = joinedOn,
                        Cash = characterCash
                    };

                    context.UsersGames.Add(newUsersGame);
                    Console.WriteLine("User {0} successfully added to game {1}", userInDatabase.Username, gameName);
                }
            }

            context.SaveChanges();
        }
        private static User CreateUserIfNotExists(XElement xmlUser, DiabloEntities context)
        {
            string username = xmlUser.Attribute("username").Value;
            var userInDatabase = context.Users.FirstOrDefault(u => u.Username == username);

            if (userInDatabase != null)
            {
                Console.WriteLine("User {0} already exists", username);
                return userInDatabase;
            }

            int isDeletedValue = int.Parse(xmlUser.Attribute("is-deleted").Value);
            bool isDeleted = isDeletedValue != 0;
            string ipAddress = xmlUser.Attribute("ip-address").Value;
            DateTime registrationDate = DateTime.Parse(xmlUser.Attribute("registration-date").Value);
            string firstName =
                xmlUser.Attribute("first-name") != null ? xmlUser.Attribute("first-name").Value : null;
            string lastName =
                xmlUser.Attribute("last-name") != null ? xmlUser.Attribute("last-name").Value : null;
            string email =
                xmlUser.Attribute("email") != null ? xmlUser.Attribute("email").Value : null;

            User newUser = new User()
            {
                Username = username,
                FirstName = firstName,
                LastName = lastName,
                Email = email,
                RegistrationDate = registrationDate,
                IsDeleted = isDeleted,
                IpAddress = ipAddress
            };

            context.Users.Add(newUser);
            context.SaveChanges();
            Console.WriteLine("Successfully added user {0}", username);

            return newUser;
        }