Пример #1
0
        static void Main()
        {
            var context = new DiabloEntities();

            var finishedGames = context.Games
                .Where(g => g.IsFinished == true)
                .OrderBy(g => g.Name)
                .ThenBy(g => g.Duration)
                .Select(g => new
                {
                    name = g.Name,
                    duration = g.Duration, //.ToString() ?? "N/A",
                    users = g.UsersGames
                        .Select(u => new
                        {
                            username = u.User.Username,
                            ipAddress = u.User.IpAddress
                        })
                }).ToList();

            //foreach (var game in finishedGames)
            //{
            //    Console.WriteLine("--" + game.name + "--" + game.duration);
            //    foreach (var user in game.users)
            //    {
            //        Console.WriteLine(user.username + "; " + user.ipAddress);
            //    }
            //}

            XElement games = new XElement("games");
            foreach (var game in finishedGames)
            {
                XElement xmlGame =
                    new XElement("game",
                        new XAttribute("name", game.name));
                         XElement
                xmlUsers = new XElement("users");
                        foreach (var user in game.users)
                        {
                            XElement xmlUser =
                                new XElement("user",
                                    new XAttribute("username", user.username),
                                    new XAttribute("ip-address", user.ipAddress));
                           xmlUsers.Add(xmlUser);
                        }
                        xmlGame.Add(xmlUsers);

                //check if game has duration
                if (game.duration != null)
                {
                    xmlGame.Add(new XAttribute("duration", game.duration));
                }
                games.Add(xmlGame);
            }

            Console.WriteLine(games);

            //export data to .xml file
            games.Save("../../finished-games.xml");
        }
Пример #2
0
        static void Main()
        {
            var context = new DiabloEntities();

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

            //foreach (var character in characters)
            //{
            //    Console.WriteLine("--" + character.name);
            //    foreach (var player in character.playedBy)
            //    {
            //        Console.WriteLine(player);
            //    }
            //}

            var json = JsonConvert.SerializeObject(characters, Formatting.Indented);

            //write the output in a JSON file named leagues-and-teams.json in the directory of the project
            File.WriteAllText("../../characters.json", json);
            Console.WriteLine(json);
        }
        static void Main()
        {
            var context = new DiabloEntities();

            //list all character names
            var names = context.Characters.Select(c => c.Name);
            foreach (var name in names)
            {
                Console.WriteLine(name);
            }
        }
Пример #4
0
        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;
        }
Пример #5
0
        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;
        }
Пример #6
0
        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();
        }
Пример #7
0
        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);
                }
            }
        }