示例#1
0
        private static void addUserToGames(XmlNode user, int userId, string username, DiabloContext context)
        {
            XmlNodeList userGames = user.SelectNodes("games/game");

            foreach (XmlNode userGame in userGames)
            {
                string   gameName      = userGame["game-name"].InnerText;
                XmlNode  character     = userGame.SelectSingleNode("character");
                string   characterName = character.Attributes["name"].InnerText;
                decimal  cash          = decimal.Parse(character.Attributes["cash"].InnerText);
                int      level         = int.Parse(character.Attributes["level"].InnerText);
                DateTime joinedOn      = DateTime.Parse(userGame["joined-on"].InnerText);

                int gameId      = context.Games.FirstOrDefault(g => g.Name == gameName).Id;
                int characterId = context.Characters.FirstOrDefault(c => c.Name == characterName).Id;

                UsersGame currentUsersGame = new UsersGame
                {
                    GameId      = gameId,
                    UserId      = userId,
                    CharacterId = characterId,
                    Level       = level,
                    JoinedOn    = joinedOn,
                    Cash        = cash
                };

                context.UsersGames.Add(currentUsersGame);

                Console.WriteLine($"User {username} successfully added to game {gameName}");
            }

            context.SaveChanges();
        }
示例#2
0
        private static UsersGame ProcessGame(DiabloEntities context, XElement game)
        {
            if (game.Element("game-name") == null)
            {
                throw new ArgumentException("game-name is required!");
            }

            var gameName = game.Element("game-name").Value;
            var dbGame   = context.Games.FirstOrDefault(g => g.Name == gameName);

            if (dbGame == null)
            {
                var message = string.Format("{0} game not existing!", gameName);
                throw new ArgumentException(message);
            }

            var characterNode = game.Element("character");

            if (characterNode == null)
            {
                throw new ArgumentException("character is required!");
            }

            var charName = characterNode.Attribute("name").Value;
            var cash     = decimal.Parse(characterNode.Attribute("cash").Value);
            var dbChar   = context.Characters.FirstOrDefault(c => c.Name == charName);

            if (dbChar == null)
            {
                dbChar = new Character()
                {
                    Name = charName
                };
            }

            var level      = int.Parse(characterNode.Attribute("level").Value);
            var joinedNode = game.Element("joined-on");

            if (joinedNode == null)
            {
                throw new ArgumentException("joined-on is required!");
            }

            var joinedOn = DateTime.ParseExact(joinedNode.Value, DateTimeFormat,
                                               CultureInfo.InvariantCulture);

            var userGame = new UsersGame()
            {
                Cash      = cash,
                Game      = dbGame,
                JoinedOn  = joinedOn,
                Level     = level,
                Items     = new List <Item>(),
                Character = dbChar
            };

            return(userGame);
        }
示例#3
0
        static void Main()
        {
            var context = new DiabloEntities();

            var xml = new XmlDocument();

            xml.Load("../../users-and-games.xml");
            var rootNode = xml.DocumentElement;

            foreach (XmlNode user in rootNode.ChildNodes)
            {
                var      username         = user.Attributes["username"].Value;
                var      ipAddress        = user.Attributes["ip-address"].Value;
                DateTime registrationDate = DateTime.Parse(user.Attributes["registration-date"].Value);
                bool     isDeleted        = false;
                string   lastName         = null;
                string   firstName        = null;
                string   email            = null;

                if (user.Attributes["first-name"] != null)
                {
                    firstName = user.Attributes["first-name"].Value;
                }
                if (user.Attributes["last-name"] != null)
                {
                    lastName = user.Attributes["last-name"].Value;
                }
                if (user.Attributes["email"] != null)
                {
                    email = user.Attributes["email"].Value;
                }
                if (int.Parse(user.Attributes["is-deleted"].Value) == 1)
                {
                    isDeleted = true;
                }

                if (context.Users.Any(u => u.Username == username))
                {
                    Console.WriteLine("User {0} already exists", username);
                    continue;
                }

                var userGames = rootNode.SelectSingleNode("user/games");


                // Loop through all games for the given user
                foreach (XmlNode game in userGames)
                {
                    var gameName  = game["game-name"].InnerText;
                    var character = game["character"];

                    var characterName  = character.Attributes["name"].Value;
                    var characterCash  = decimal.Parse(character.Attributes["cash"].Value);
                    var characterLevel = int.Parse(character.Attributes["level"].Value);
                    var joinedOn       = DateTime.Parse(game["joined-on"].InnerText);
                    Console.WriteLine(joinedOn);

                    if (game["joined-on"] != null && user.Attributes["registration-date"] != null)
                    {
                        var userGame = new UsersGame()
                        {
                            Cash      = characterCash,
                            Character = context.Characters.FirstOrDefault(c => c.Name == characterName),
                            Game      = context.Games.FirstOrDefault(g => g.Name == gameName),
                            JoinedOn  = joinedOn,
                            Level     = characterLevel,
                            User      = new User()
                            {
                                FirstName        = firstName,
                                LastName         = lastName,
                                Email            = email,
                                Username         = username,
                                IpAddress        = ipAddress,
                                RegistrationDate = registrationDate,
                                IsDeleted        = isDeleted
                            }
                        };

                        context.UsersGames.Add(userGame);

                        Console.WriteLine("Successfully added user {0}", username);
                        Console.WriteLine("User {0} successfully added to game {1}", username, gameName);
                    }
                }

                context.SaveChanges();
            }
        }
示例#4
0
        static void Main(string[] args)
        {
            Console.BufferHeight = Int16.MaxValue - 1;
            var         db  = new DiabloContext();
            XmlDocument doc = new XmlDocument();

            doc.Load("../../users-and-games.xml");

            var root = doc.DocumentElement;

            foreach (XmlNode user in root)
            {
                try
                {
                    var userFirstName = user.Attributes["first-name"] == null ? null : user.Attributes["first-name"].Value;
                    var userLastName  = user.Attributes["last-name"] == null ? null : user.Attributes["last-name"].Value;
                    var userEmail     = user.Attributes["email"] == null ? null : user.Attributes["email"].Value;
                    var userUserName  = user.Attributes["username"] == null ? null : user.Attributes["username"].Value;

                    bool     userIsDeleted        = user.Attributes["is-deleted"].Value == "0" ? true : false;
                    var      userIpAddress        = user.Attributes["ip-address"] == null ? null : user.Attributes["ip-address"].Value;
                    DateTime userRegistracionDate = DateTime.ParseExact(user.Attributes["registration-date"].Value, "dd/MM/yyyy", null);

                    User userDb = null;

                    if (db.Users.Any(u => u.Username == userUserName))
                    {
                        Console.WriteLine("User {0} already exists", userUserName);
                    }
                    else
                    {
                        userDb = new User()
                        {
                            Username         = userUserName,
                            Email            = userEmail,
                            FirstName        = userFirstName,
                            IpAddress        = userIpAddress,
                            IsDeleted        = userIsDeleted,
                            LastName         = userLastName,
                            RegistrationDate = userRegistracionDate
                        };
                        db.Users.Add(userDb);

                        Console.WriteLine("Successfully added user {0}", userUserName);

                        foreach (XmlNode games in user.ChildNodes)
                        {
                            foreach (XmlNode game in games)
                            {
                                var gameName       = game["game-name"].InnerText;
                                var CharacterName  = game["character"].Attributes["name"].Value;
                                var CharacterCash  = game["character"].Attributes["cash"].Value;
                                var CharacterLevel = game["character"].Attributes["level"].Value;
                                var joinedOn       = game["joined-on"].InnerText;

                                var dbGame = db.Games.Where(g => g.Name == gameName).FirstOrDefault();



                                UsersGame ug = new UsersGame()
                                {
                                    Game      = db.Games.Where(g => g.Name == gameName).FirstOrDefault(),
                                    User      = userDb,
                                    Cash      = Convert.ToDecimal(CharacterCash),
                                    Level     = Convert.ToInt32(CharacterLevel),
                                    JoinedOn  = DateTime.ParseExact(joinedOn, "dd/MM/yyyy", null),
                                    Character = new Character()
                                    {
                                        Name = CharacterName
                                    }
                                };
                                db.UsersGames.Add(ug);
                                db.SaveChanges();
                                Console.WriteLine("User {0} successfully added to game {1}", userDb.Username, gameName);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                }
            }
        }
    public static void Main()
    {
        Thread.CurrentThread.CurrentCulture = new CultureInfo("DE-de");
        var context = new DiabloEntities();

        XmlDocument docUsers = new XmlDocument();

        docUsers.Load("..//..//..//..//data//users-and-games.xml");
        string      usersPath = "/users/user";
        XmlNodeList userNodes = docUsers.SelectNodes(usersPath);
        string      gamesPath = "games/game";

        foreach (XmlNode userNode in userNodes)
        {
            string username = userNode.Attributes["username"].Value;
            User   user     = context.Users.Where(u => u.Username == username).FirstOrDefault();

            if (user != null)
            {
                Console.WriteLine("User {0} already exists", user.Username);
                continue;
            }

            user = new User()
            {
                FirstName = userNode.Attributes["first-name"] != null ?
                            userNode.Attributes["first-name"].Value
                : null,
                LastName = userNode.Attributes["last-name"] != null ?
                           userNode.Attributes["last-name"].Value
                : null,
                Username = username,
                Email    = userNode.Attributes["email"] != null ?
                           userNode.Attributes["email"].Value
                : null,
                IpAddress        = userNode.Attributes["ip-address"].Value,
                IsDeleted        = Convert.ToBoolean(int.Parse(userNode.Attributes["is-deleted"].Value)),
                RegistrationDate = DateTime.Parse(userNode.Attributes["registration-date"].Value)
            };

            context.Users.Add(user);

            XmlNodeList xmlGames = userNode.SelectNodes(gamesPath);
            foreach (XmlNode xmlGame in xmlGames)
            {
                string   gameName      = xmlGame.SelectSingleNode("game-name").InnerText;
                string   characterName = xmlGame.SelectSingleNode("character").Attributes["name"].Value;
                decimal  cash          = decimal.Parse(xmlGame.SelectSingleNode("character").Attributes["cash"].Value);
                int      level         = int.Parse(xmlGame.SelectSingleNode("character").Attributes["level"].Value);
                DateTime joinedOn      = DateTime.Parse(xmlGame.SelectSingleNode("joined-on").InnerText);

                var userGame = new UsersGame()
                {
                    //Game name is a non-unique field in the database and duplicates exist. For the task we
                    //admit that we take the first game with this name.
                    Game      = context.Games.Where(g => g.Name == gameName).First(),
                    User      = user,
                    Character = context.Characters.Where(c => c.Name == characterName).First(),
                    Cash      = cash,
                    Level     = level,
                    JoinedOn  = joinedOn
                };

                context.UsersGames.Add(userGame);
            }

            context.SaveChanges();

            //As per the problem terms we can't add a user if any of his games fail to add.
            //That's why we save changes after all the UserGames are in context. If agame fails to add,
            //the user won't be added. We print the user and its games only in case of a successful add in
            //the context

            var addedUser = context.Users
                            .Where(u => u.Username == username)
                            .Select(u => new
            {
                u.Username,
                UserGames = u.UsersGames.Select(ug => ug.Game.Name)
            })
                            .First();

            Console.WriteLine("Successfully added user " + addedUser.Username);
            foreach (var game in user.UsersGames)
            {
                Console.WriteLine("User {0} successfully added to game {1}", user.Username, game.Game.Name);
            }
        }
    }
        static void Main(string[] args)
        {
            Console.BufferHeight = Int16.MaxValue - 1;
            var db = new DiabloContext();
            XmlDocument doc = new XmlDocument();
            doc.Load("../../users-and-games.xml");

            var root = doc.DocumentElement;
            foreach (XmlNode user in root)
            {

                try
                {

                var userFirstName = user.Attributes["first-name"] == null ? null : user.Attributes["first-name"].Value;
                var userLastName = user.Attributes["last-name"] == null ? null : user.Attributes["last-name"].Value;
                var userEmail = user.Attributes["email"] == null ? null : user.Attributes["email"].Value;
                var userUserName = user.Attributes["username"] == null ? null : user.Attributes["username"].Value;

                bool userIsDeleted = user.Attributes["is-deleted"].Value == "0" ? true : false;
                var userIpAddress = user.Attributes["ip-address"] == null ? null : user.Attributes["ip-address"].Value;
                DateTime userRegistracionDate = DateTime.ParseExact(user.Attributes["registration-date"].Value, "dd/MM/yyyy", null);

                User userDb = null;

                if (db.Users.Any(u => u.Username == userUserName))
                {
                    Console.WriteLine("User {0} already exists", userUserName);
                }
                else
                {
                    userDb = new User()
                    {
                        Username = userUserName,
                        Email = userEmail,
                        FirstName = userFirstName,
                        IpAddress = userIpAddress,
                        IsDeleted = userIsDeleted,
                        LastName = userLastName,
                        RegistrationDate = userRegistracionDate
                    };
                    db.Users.Add(userDb);
                    
                    Console.WriteLine("Successfully added user {0}", userUserName);

                    foreach (XmlNode games in user.ChildNodes)
                    {
                        foreach (XmlNode game in games)
                        {

                            var gameName = game["game-name"].InnerText;
                            var CharacterName = game["character"].Attributes["name"].Value;
                            var CharacterCash = game["character"].Attributes["cash"].Value;
                            var CharacterLevel = game["character"].Attributes["level"].Value;
                            var joinedOn = game["joined-on"].InnerText;

                            var dbGame = db.Games.Where(g => g.Name == gameName).FirstOrDefault();

                           
                                
                             UsersGame ug = new UsersGame()
                            {
                                Game = db.Games.Where(g => g.Name == gameName).FirstOrDefault(),
                                User = userDb,
                                Cash = Convert.ToDecimal(CharacterCash),
                                Level = Convert.ToInt32(CharacterLevel),
                                JoinedOn = DateTime.ParseExact(joinedOn, "dd/MM/yyyy", null),
                                Character = new Character()
                                {
                                    Name = CharacterName
                                }
                            };
                            db.UsersGames.Add(ug);
                            db.SaveChanges();
                            Console.WriteLine("User {0} successfully added to game {1}", userDb.Username, gameName);

                        }
                    }
                }
                
                }
                catch (Exception e )
                {

                }
              
            }
        }
        static void Main()
        {
            var context  = new DiabloEntities();
            var xmlDoc   = XDocument.Load(@"..\..\..\users-and-games.xml");
            var allUsers = xmlDoc.XPathSelectElements("users/user");

            foreach (var xElement in allUsers)
            {
                string firstName = null;
                if (xElement.Attribute("first-name") != null)
                {
                    firstName = xElement.Attribute("first-name").Value;
                }

                string lastName = null;
                if (xElement.Attribute("last-name") != null)
                {
                    lastName = xElement.Attribute("last-name").Value;
                }

                string email = null;
                if (xElement.Attribute("email") != null)
                {
                    email = xElement.Attribute("email").Value;
                }

                string   username     = xElement.Attribute("username").Value;
                string   ip           = xElement.Attribute("ip-address").Value;
                DateTime registeredOn = DateTime.Parse(xElement.Attribute("registration-date").Value);
                bool     isDeleted    = xElement.Attribute("is-deleted").Value.Equals("1");

                var dbUser = context.Users.FirstOrDefault(u => u.Username == username);
                if (dbUser == null)
                {
                    var newUser = new User()
                    {
                        FirstName        = firstName,
                        LastName         = lastName,
                        Email            = email,
                        Username         = username,
                        IpAddress        = ip,
                        RegistrationDate = registeredOn,
                        IsDeleted        = isDeleted
                    };
                    context.Users.Add(newUser);
                    context.SaveChanges();
                    Console.WriteLine("Successfully added user {0}", username);

                    var userGames = xElement.XPathSelectElements("games/game");
                    foreach (var userGame in userGames)
                    {
                        var      gameName  = userGame.Element("game-name").Value;
                        var      character = userGame.Element("character");
                        var      charName  = character.Attribute("name").Value;
                        var      cash      = Decimal.Parse(character.Attribute("cash").Value);
                        var      level     = int.Parse(character.Attribute("level").Value);
                        DateTime joinedOn  = DateTime.Parse(userGame.Element("joined-on").Value);

                        var addUserToGame = new UsersGame()
                        {
                            GameId      = context.Games.FirstOrDefault(g => g.Name.Equals(gameName)).Id,
                            UserId      = context.Users.FirstOrDefault(u => u.Username.Equals(username)).Id,
                            CharacterId = context.Characters.FirstOrDefault(c => c.Name.Equals(charName)).Id,
                            Level       = level,
                            JoinedOn    = joinedOn,
                            Cash        = cash
                        };
                        context.UsersGames.Add(addUserToGame);
                        context.SaveChanges();
                        Console.WriteLine("User {0} successfully added to game {1}", username, gameName);
                    }
                }
                else
                {
                    Console.WriteLine("User {0} already exists", username);
                }
            }
        }
示例#8
0
        static void Main()
        {
            var            diabloContext = new DiabloEntities();
            Queue <string> output        = new Queue <string>();

            var            xmlDocument   = XDocument.Load("../../users-and-games.xml");
            var            userNodes     = xmlDocument.XPathSelectElements("/users/user");
            List <UserDTO> users         = new List <UserDTO>();
            var            allUsers      = diabloContext.Users.ToList();
            var            allCharacters = diabloContext.Characters.ToList();
            var            allUsersGames = diabloContext.UsersGames.ToList();
            var            allGames      = diabloContext.Games.ToList();

            foreach (var userNode in userNodes)
            {
                UserDTO currentUser = null;

                string   firstName        = null;
                string   lastName         = null;
                string   email            = null;
                string   username         = userNode.Attribute("username").Value;
                bool     isDeleted        = userNode.Attribute("is-deleted").Value == "1";
                string   ipAddress        = userNode.Attribute("ip-address").Value;
                DateTime registrationDate = Convert.ToDateTime(userNode.Attribute("registration-date").Value);

                if (userNode.Attribute("first-name") != null)
                {
                    firstName = userNode.Attribute("first-name").Value;
                }

                if (userNode.Attribute("last-name") != null)
                {
                    lastName = userNode.Attribute("last-name").Value;
                }

                if (userNode.Attribute("email") != null)
                {
                    email = userNode.Attribute("email").Value;
                }

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

                var gameNodes = userNode.XPathSelectElements("games/game");
                foreach (var gameNode in gameNodes)
                {
                    XElement characterNode = gameNode.XPathSelectElement("character");

                    string   gameName       = gameNode.XPathSelectElement("game-name").Value;
                    DateTime joinedOn       = Convert.ToDateTime(gameNode.XPathSelectElement("joined-on").Value);
                    string   characterName  = characterNode.Attribute("name").Value;
                    decimal  characterCash  = Decimal.Parse(characterNode.Attribute("cash").Value);
                    int      characterLevel = int.Parse(characterNode.Attribute("level").Value);

                    CharacterDTO currentCharacter = new CharacterDTO()
                    {
                        Name  = characterName,
                        Cash  = characterCash,
                        Level = characterLevel
                    };

                    GameDTO currentGame = new GameDTO()
                    {
                        Name      = gameName,
                        JoinedOn  = joinedOn,
                        Character = currentCharacter
                    };

                    currentUser.Games.Add(currentGame);
                }

                users.Add(currentUser);
            }

            foreach (var user in users)
            {
                var currentUser = allUsers.Where(u => u.Username == user.Username).FirstOrDefault();

                if (currentUser == null)
                {
                    currentUser = new User()
                    {
                        Username         = user.Username,
                        FirstName        = user.FirstName,
                        LastName         = user.LastName,
                        Email            = user.Email,
                        RegistrationDate = user.RegistrationDate,
                        IsDeleted        = user.IsDeleted,
                        IpAddress        = user.IpAddress,
                        UsersGames       = new HashSet <UsersGame>()
                    };

                    diabloContext.Users.Add(currentUser);
                    output.Enqueue(string.Format("Successfully added user {0}", currentUser.Username));

                    foreach (var game in user.Games)
                    {
                        UsersGame currentGame = new UsersGame()
                        {
                            GameId    = allGames.Where(g => g.Name == game.Name).Select(g => g.Id).FirstOrDefault(),
                            Level     = game.Character.Level,
                            Cash      = game.Character.Cash,
                            Character = allCharacters.Where(c => c.Name == game.Character.Name).FirstOrDefault(),
                            JoinedOn  = game.JoinedOn
                        };

                        currentUser.UsersGames.Add(currentGame);
                        output.Enqueue(string.Format("User {0} successfully added to game {1}", user.Username, game.Name));
                    }
                }
                else
                {
                    output.Enqueue(string.Format("User {0} already exists", currentUser.Username));
                }

                diabloContext.SaveChanges();
            }

            while (output.Count > 0)
            {
                Console.WriteLine(output.Dequeue());
            }
        }