Esempio n. 1
0
 public IEnumerable <FinishedGameDto> GetAll()
 {
     using (var db = new FifteenGameDbFirstEntities())
     {
         return(GameMapper.Instance.Map <IEnumerable <FinishedGameDto> >(db.FinishedGames));
     }
 }
Esempio n. 2
0
 public void Save(FinishedGameDto finishedGame)
 {
     using (var db = new FifteenGameDbFirstEntities())
     {
         db.FinishedGames.Add(GameMapper.Instance.Map <FinishedGame>(finishedGame));
         db.SaveChanges();
     }
 }
Esempio n. 3
0
        public UserDto Login(string login, string password)
        {
            using (var db = new FifteenGameDbFirstEntities())
            {
                var user = db.Users.Where(u => u.Login == login && u.Password == password).FirstOrDefault();
                if (user == null)
                {
                    return(null);
                }

                return(GameMapper.Instance.Map <UserDto>(user));
            }
        }
Esempio n. 4
0
        public void RemoveCurrentGame(int userId)
        {
            using (var db = new FifteenGameDbFirstEntities())
            {
                var currentGame = db.CurrentGames.FirstOrDefault(cg => cg.UserId == userId);
                if (currentGame == null)
                {
                    return;
                }

                while (currentGame.CurrentGameCells.Any())
                {
                    db.CurrentGameCells.Remove(currentGame.CurrentGameCells.First());
                }

                db.CurrentGames.Remove(currentGame);
                db.SaveChanges();
            }
        }
Esempio n. 5
0
        public void SaveCurrentGameState(int userId, GameStateDto state)
        {
            using (var db = new FifteenGameDbFirstEntities())
            {
                var currentGame = db.CurrentGames.FirstOrDefault(cg => cg.UserId == userId);
                if (currentGame == null)
                {
                    currentGame = new CurrentGame
                    {
                        UserId        = userId,
                        MoveCount     = state.MoveCount,
                        GameStartTime = state.GameStartTime,
                    };

                    db.CurrentGames.Add(currentGame);
                }
                else
                {
                    currentGame.MoveCount     = state.MoveCount;
                    currentGame.GameStartTime = state.GameStartTime;
                }

                while (currentGame.CurrentGameCells.Any())
                {
                    db.CurrentGameCells.Remove(currentGame.CurrentGameCells.First());
                }

                int i = 1;
                foreach (var cell in state.State)
                {
                    currentGame.CurrentGameCells.Add(new CurrentGameCell
                    {
                        CellIndex = i,
                        CellValue = cell,
                    });

                    i++;
                }

                db.SaveChanges();
            }
        }
Esempio n. 6
0
        public GameStateDto GetCurrentGameState(int userId)
        {
            using (var db = new FifteenGameDbFirstEntities())
            {
                var currentGame = db.CurrentGames.FirstOrDefault(cg => cg.UserId == userId);
                if (currentGame == null)
                {
                    return(null);
                }

                var result = new GameStateDto
                {
                    MoveCount     = currentGame.MoveCount,
                    GameStartTime = currentGame.GameStartTime,
                    State         = currentGame.CurrentGameCells.OrderBy(cgs => cgs.CellIndex).Select(cgs => cgs.CellValue).ToList(),
                };

                return(result);
            }
        }
Esempio n. 7
0
        public UserDto Register(string login, string password, string userName)
        {
            using (var db = new FifteenGameDbFirstEntities())
            {
                if (db.Users.Any(u => u.Login == login))
                {
                    return(null);
                }

                var newUser = new User
                {
                    UserName = userName,
                    Login    = login,
                    Password = password,
                };

                db.Users.Add(newUser);
                db.SaveChanges();
                return(GameMapper.Instance.Map <UserDto>(newUser));
            }
        }