public static void Main()
        {
            var context = new DiabloEntities();

            var xDocument = XDocument.Load(@"..\..\users-and-games.xml");

            var xUsers = xDocument.XPathSelectElements("/users/user");

            foreach (var xUser in xUsers)
            {
                var xGames = xUser.XPathSelectElements("games/game");

                bool gameFail = false;

                using (var dbContextTransactions = context.Database.BeginTransaction())
                {
                    var user = CreateUser(xUser, context);

                    foreach (var xGame in xGames)
                    {
                        gameFail = CreateGame(xGame, context, user, gameFail);
                    }

                    if (gameFail)
                    {
                        dbContextTransactions.Rollback();
                    }
                    else
                    {
                        dbContextTransactions.Commit();
                    }
                }
            }
        }
        public static void Main()
        {
            var context = new DiabloEntities();

            var characterNames = context.Characters.Select(ch => ch.Name);

            foreach (var characterName in characterNames)
            {
                Console.WriteLine(characterName);
            }
        }
Exemplo n.º 3
0
        public 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(u => new
                                                                      {
                                                                          Username = u.User.Username,
                                                                          IpAddress = u.User.IpAddress
                                                                      })
                             });

            var xGames = new XElement("games");

            foreach (var finishedGame in finishedGames)
            {
                var xGame = new XElement("game");
                xGame.Add(new XAttribute("name", finishedGame.GameName));

                if (finishedGame.Duration != null)
                {
                    xGame.Add(new XAttribute("duration", finishedGame.Duration));
                }

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

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

            new XDocument(xGames).Save(@"..\..\finished-games.xml");
            Console.WriteLine("Export finished-games.xml complited!");
        }
        public static void Main()
        {
            var context = new DiabloEntities();

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

            JavaScriptSerializer js = new JavaScriptSerializer();

            var charactersJson = js.Serialize(characters);

            File.AppendAllText(@"..\..\characters.json", charactersJson);
            Console.WriteLine("Export characters.json complited!");
        }
        private static User CreateUser(XElement xUser, DiabloEntities context)
        {
            User user;
            var xUsername = xUser.Attribute("username").Value;

            if (!context.Users.Any(u => u.Username == xUsername))
            {
                user = new User();

                user.FirstName = null;
                if (xUser.Attribute("first-name") != null)
                {
                    user.FirstName = xUser.Attribute("first-name").Value;
                }

                user.LastName = null;
                if (xUser.Attribute("last-name") != null)
                {
                    user.LastName = xUser.Attribute("last-name").Value;
                }

                user.Email = null;
                if (xUser.Attribute("email") != null)
                {
                    user.Email = xUser.Attribute("email").Value;
                }

                user.Username = xUsername;
                user.IsDeleted = xUser.Attribute("is-deleted").Value != "0";
                user.IpAddress = xUser.Attribute("ip-address").Value;
                user.RegistrationDate = Convert.ToDateTime(xUser.Attribute("registration-date").Value);

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

            Console.WriteLine("User {0} already exists", xUsername);
            return null;
        }
        private static bool CreateGame(XElement xGame, DiabloEntities context, User user, bool gameFail)
        {
            var gameName = xGame.Element("game-name").Value;
            var characterName = xGame.Element("character").Attribute("name").Value;
            var characterCash = decimal.Parse(xGame.Element("character").Attribute("cash").Value);
            var characterLevel = int.Parse(xGame.Element("character").Attribute("level").Value);
            DateTime gameJoinedOn;
            bool isValidDate = DateTime.TryParse(xGame.Element("joined-on").Value, out gameJoinedOn);

            Game game = context.Games.FirstOrDefault(g => g.Name == gameName);
            var characterId = context.Characters
                .Where(ch => ch.Name == characterName)
                .Select(ch => ch.Id)
                .FirstOrDefault();

            if (game != null && user != null && characterCash >= 0 && isValidDate)
            {
                UsersGame usersGame = new UsersGame();
                usersGame.GameId = game.Id;
                usersGame.CharacterId = characterId;
                usersGame.Cash = characterCash;
                usersGame.Level = characterLevel;
                usersGame.JoinedOn = gameJoinedOn;
                usersGame.UserId = user.Id;

                context.UsersGames.Add(usersGame);
                context.SaveChanges();
                Console.WriteLine("User {0} successfully added to game {1}", user.Username, game.Name);
            }
            else
            {
                gameFail = true;
            }

            return gameFail;
        }