コード例 #1
0
 private static void ProcessGames(IEnumerable<XElement> games, User user, DiabloEntities context)
 {
     foreach (var gm in games)
     {
         var gameName = gm.Element("game-name").Value;
         var characterName = gm.Element("character").Attribute("name").Value;
         var characterCash = gm.Element("character").Attribute("cash").Value;
         var characterLevel = gm.Element("character").Attribute("level").Value;
         var joinedOn = gm.Element("joined-on").Value;
         var game = context.Games.FirstOrDefault(g => g.Name == gameName);
         var character = context.Characters.FirstOrDefault(c => c.Name == characterName);
         if (game != null && character != null)
         {
             var userGame = new UsersGame()
             {
                 Game = game,
                 User = user,
                 Character = character,
                 Level = int.Parse(characterLevel),
                 JoinedOn = DateTime.Parse(joinedOn),
                 Cash = decimal.Parse(characterCash)
             };
             if (
                 !context.UsersGames.Any(
                     ug =>
                         ug.Game.Name == game.Name && ug.User.Username == user.Username &&
                         ug.Character.Name == character.Name))
             {
                 context.UsersGames.Add(userGame);
                 context.SaveChanges();
                 Console.WriteLine("User {0} successfully added to game {1}", userGame.User.Username, userGame.Game.Name);
             }
         }
     }
 }
コード例 #2
0
        static void Main()
        {
            var db = new DiabloEntities();

            var xmlDoc = XDocument.Load("../../users-and-games.xml");
            var xUsers = xmlDoc.XPathSelectElements("users/user");

            foreach (var user in xUsers)
            {
                var dbUser = new User();
                var userName = user.Attribute("username").Value;
                if (db.Users.Any(u => u.Username == userName))
                {
                    Console.WriteLine("User {0} already exists", userName);
                    dbUser = db.Users.FirstOrDefault(u => u.Username == userName);
                    continue;
                }
                else
                {
                    dbUser.Username = userName;
                    dbUser.IsDeleted = Convert.ToBoolean(int.Parse(user.Attribute("is-deleted").Value) == 0 ? "false" : "true");
                    dbUser.IpAddress = user.Attribute("ip-address").Value;
                    dbUser.RegistrationDate = DateTime.Parse(user.Attribute("registration-date").Value);
                    var firstName = user.Attribute("first-name");
                    if (firstName != null) dbUser.FirstName = firstName.Value;

                    var lastName = user.Attribute("last-name");
                    if (lastName != null) dbUser.FirstName = lastName.Value;

                    var email = user.Attribute("email");
                    if (email != null) dbUser.FirstName = email.Value;

                    db.Users.Add(dbUser);
                    db.SaveChanges();
                    Console.WriteLine("Successfully added user {0}", userName);
                }

                var games = user.XPathSelectElements("games/game");
                foreach (var gameXml in games)
                {
                    var game = new UsersGame();
                    var gameName = gameXml.Element("game-name").Value;
                    var characterName = gameXml.Element("character").Attribute("name").Value;
                    var currGame = db.Games.FirstOrDefault(g => g.Name == gameName);
                    var currCharacter = db.Characters.FirstOrDefault(g => g.Name == characterName);

                    game.GameId = currGame.Id;
                    game.UserId = dbUser.Id;
                    game.CharacterId = currCharacter.Id;
                    game.Cash = decimal.Parse(gameXml.Element("character").Attribute("cash").Value);
                    game.Level = int.Parse(gameXml.Element("character").Attribute("level").Value);
                    game.JoinedOn = DateTime.Parse(gameXml.Element("joined-on").Value);
                    db.SaveChanges();
                    Console.WriteLine("User {0} successfully added to game {1}", userName, gameXml.Element("game-name").Value);
                }
            }
        }
コード例 #3
0
 static void Main()
 {
     var db = new DiabloEntities();
     var characters = db.Characters.Select(c => c.Name).ToList();
     foreach (var character in characters)
     {
         Console.WriteLine(character);
     }
 }
コード例 #4
0
        static void Main()
        {
            var context = new DiabloEntities();

            var characterNames = context.Characters.Select(c => c.Name).ToList();
            foreach (var name in characterNames)
            {
                Console.WriteLine(name);
            }
        }
コード例 #5
0
        static void Main()
        {
            var context = new DiabloEntities();

            var doc = XDocument.Load("../../users-and-games.xml");
            var users = doc.XPathSelectElements("/users/user");
            foreach (var us in users)
            {
                User user = ProcessUsers(us, context);
                var games = us.XPathSelectElements("games/game");
                ProcessGames(games, user, context);
            }
        }
コード例 #6
0
ファイル: ImportXml.cs プロジェクト: peterkirilov/SoftUni-1
        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;
        }
コード例 #7
0
ファイル: ExportXml.cs プロジェクト: peterkirilov/SoftUni-1
        public static void Main(string[] args)
        {
            var directory = new FileInfo("../../../finished-games.xml");
            var context = new DiabloEntities();
            var finishedGames = context.Games
                .Where(g => g.IsFinished == true)
                .Select(g => new
                {
                    gameName = g.Name,
                    gameDuration = g.Duration,
                    users = g.UsersGames.Select(u => new
                    {
                        u.User.Username,
                        u.User.IpAddress
                    })
                })
                .OrderBy(g => g.gameName)
                .ThenBy(g => g.gameDuration)
                .ToList();

            var xml = new XDocument();
            var root = new XElement("games");
            xml.Add(root);

            finishedGames.ForEach(g =>
            {
                var gameNode = new XElement("game");
                gameNode.SetAttributeValue("name", g.gameName);
                if (g.gameDuration != null)
                {
                    gameNode.SetAttributeValue("duration", g.gameDuration);
                }

                var usersNode = new XElement("users");
                foreach (var user in g.users)
                {
                    var userNode = new XElement("user");
                    userNode.SetAttributeValue("username", user.Username);
                    userNode.SetAttributeValue("ip-address", user.IpAddress);
                    usersNode.Add(userNode);
                }

                gameNode.Add(usersNode);
                root.Add(gameNode);
            });

            xml.Save(directory.FullName);
            Console.WriteLine("XML saved : " + directory.FullName);
        }
コード例 #8
0
ファイル: ImportXml.cs プロジェクト: peterkirilov/SoftUni-1
 static void Main(string[] args)
 {
     var context = new DiabloEntities();
     var doc = XDocument.Load("../../../data/users-and-games.xml");
     var users = doc.Root.Elements();
     foreach (var user in users)
     {
         try
         {
             ProcessUserBatch(context, user);
         }
         catch (ArgumentException ex)
         {
             Console.Error.WriteLine(ex.Message);
         }
     }
 }
コード例 #9
0
        static void Main()
        {
            var context = new DiabloEntities();

            var charactersPlayers = context.Characters.Select(c => new
            {
                name = c.Name,
                playedBy =
                    context.Users.Where(u => c.UsersGames.Select(ug => ug.UserId).Contains(u.Id))
                        .Select(u => u.Username)
            }).OrderBy(c => c.name)
                .ToList();

            var serializer = new JavaScriptSerializer();
            var json = serializer.Serialize(charactersPlayers);
            File.WriteAllText("../../characters.json", json);
        }
コード例 #10
0
ファイル: ExportJson.cs プロジェクト: peterkirilov/SoftUni-1
        public static void Main(string[] args)
        {
            var directory = new FileInfo("../../../characters.json");
            var context = new DiabloEntities();
            var characters = context.Characters
                .Select(c => new
                {
                    name = c.Name,
                    playedBy = c.UsersGames.Select(ug => ug.User.Username)
                })
                .OrderBy(c => c.name)
                .ToList();

            var serializaer = new JavaScriptSerializer();
            var json = serializaer.Serialize(characters);
            File.WriteAllText(directory.FullName, json);
            Console.WriteLine("Json saved: " + directory.FullName);
        }
コード例 #11
0
 private static User ProcessUsers(XElement us, DiabloEntities context)
 {
     var username = us.Attribute("username").Value;
     var regDate = us.Attribute("registration-date").Value;
     var isDeleted = us.Attribute("is-deleted").Value;
     var ipAddress = us.Attribute("ip-address").Value;
     string firstName = null;
     string lastName = null;
     string email = null;
     if (us.Attribute("first-name") != null)
     {
         firstName = us.Attribute("first-name").Value;
     }
     if (us.Attribute("last-name") != null)
     {
         lastName = us.Attribute("last-name").Value;
     }
     if (us.Attribute("email") != null)
     {
         email = us.Attribute("email").Value;
     }
     var user = context.Users.FirstOrDefault(u => u.Username == username);
     if (user == null)
     {
         user = new User()
         {
             Username = username,
             FirstName = firstName,
             LastName = lastName,
             RegistrationDate = DateTime.Parse(regDate),
             IsDeleted = int.Parse(isDeleted) != 0,
             IpAddress = ipAddress,
             Email = email
         };
         context.Users.Add(user);
         context.SaveChanges();
         Console.WriteLine("Successfully added user {0}", username);
     }
     else
     {
         Console.WriteLine("User {0} already exists", username);
     }
     return user;
 }
コード例 #12
0
        static void Main(string[] args)
        {
            var db = new DiabloEntities();
            var finishedGames = db.Games
            .Where(g => g.IsFinished)
            .Select(g => new
            {
                GameName = g.Name,
                Darution = g.Duration,
                Users = g.UsersGames.Select(u => new
                {
                    UserName = u.User.Username,
                    IpAddress = u.User.IpAddress
                })
            })
            .OrderBy(g => g.GameName)
            .ThenBy(g => g.Darution)
            .ToList();

            var root = new XElement("games");
            foreach (var finishedGame in finishedGames)
            {
                var game = new XElement("game", new XAttribute("name", finishedGame.GameName));
                if (finishedGame.Darution != null)
                {
                    game.Add(new XAttribute("duration", finishedGame.Darution));
                }
                var users = new XElement("users");
                foreach (var user in finishedGame.Users)
                {
                    var userXml = new XElement("user");
                    userXml.Add(new XAttribute("username", user.UserName));
                    userXml.Add(new XAttribute("ip-address", user.IpAddress));
                    users.Add(userXml);
                }

                game.Add(users);
                root.Add(game);
            }

            var xmlDoc = new XDocument(root);
            xmlDoc.Save("../../finished-games.xml");
        }
コード例 #13
0
        static void Main()
        {
            var context = new DiabloEntities();

            var finishedGames = context.Games
                .Where(g => g.IsFinished == true)
                .Select(g => new
                {
                    name = g.Name,
                    duration = g.Duration,
                    users = context.Users.Where(u => g.UsersGames.Select(ug => ug.UserId).Contains(u.Id))
                    .Select(u => new
                    {
                        username = u.Username,
                        ip = u.IpAddress
                    })
                }).OrderBy(g => g.name)
                .ThenBy(g => g.duration)
                .ToList();

            var root = new XElement("games");
            foreach (var fGame in finishedGames)
            {
                var game = new XElement("game", new XAttribute("name", fGame.name));
                if (fGame.duration != null)
                {
                    game.Add(new XAttribute("duration", fGame.duration));
                }
                var users = new XElement("users");
                foreach (var us in fGame.users)
                {
                    var user = new XElement("user", new XAttribute("username", us.username),
                        new XAttribute("ip-address", us.ip));
                    users.Add(user);
                }
                game.Add(users);
                root.Add(game);
            }
            var doc = new XDocument();
            doc.Add(root);
            doc.Save("../../finished-games.xml");
        }
コード例 #14
0
 public static void Main(string[] args)
 {
     var context = new DiabloEntities();
     var characterNames = context.Characters.Select(c => c.Name).ToList();
     characterNames.ForEach(Console.WriteLine);
 }
コード例 #15
0
ファイル: ImportXml.cs プロジェクト: peterkirilov/SoftUni-1
        private static void ProcessUserBatch(DiabloEntities context, XElement user)
        {
            // Optional
            string firstName = null;
            string lastName = null;
            string email = null;
            if (user.Attribute("first-name") != null)
            {
                firstName = user.Attribute("first-name").Value;
            }

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

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

            // Required
            ValidateRequired(user);

            var username = user.Attribute("username").Value;
            var ip = user.Attribute("ip-address").Value;
            var deleted = int.Parse(user.Attribute("is-deleted").Value);
            var isDeleted = true;
            if (deleted == 0)
            {
                isDeleted = false;
            }

            var registrationDate = DateTime.ParseExact(user.Attribute("registration-date").Value, DateTimeFormat,
                CultureInfo.InvariantCulture);

            var dbUser = context.Users.FirstOrDefault(u => u.Username == username);
            if (dbUser != null)
            {
                var message = string.Format("User {0} already exists", username);
                throw new ArgumentException(message);
            }

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

            var gameMessages = new List<string>();
            var games = user.Elements();
            foreach (var game in games.Elements())
            {
                var userGame = ProcessGame(context, game);

                xmlUser.UsersGames.Add(userGame);
                var gameMessage = string.Format("User {0} successfully added to game {1}", xmlUser.Username, userGame.Game.Name);
                gameMessages.Add(gameMessage);
            }

            context.Users.Add(xmlUser);
            context.SaveChanges();
            Console.WriteLine("Successfully added user {0}", xmlUser.Username);
            Console.WriteLine(string.Join("\n", gameMessages));
        }