public void Save(FinishedGameDto finishedGame)
 {
     using (var db = new FifteenGameModel())
     {
         var finishedGameEntity = GameMapper.Instance.Map <FinishedGame>(finishedGame);
         finishedGameEntity.User = db.Users.First(u => u.Id == finishedGame.UserId);
         db.FinishedGames.Add(finishedGameEntity);
         db.SaveChanges();
     }
 }
        public void RemoveCurrentGame(int userId)
        {
            using (var db = new FifteenGameModel())
            {
                var currentGame = db.CurrentGames.FirstOrDefault(cg => cg.User.Id == userId);
                if (currentGame == null)
                {
                    return;
                }

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

                db.CurrentGames.Remove(currentGame);
                db.SaveChanges();
            }
        }
        public void SaveCurrentGameState(int userId, GameStateDto state)
        {
            using (var db = new FifteenGameModel())
            {
                var currentGame = db.CurrentGames.FirstOrDefault(cg => cg.User.Id == userId);
                if (currentGame == null)
                {
                    var user = db.Users.FirstOrDefault(u => u.Id == userId);
                    currentGame               = db.CurrentGames.Create();
                    currentGame.User          = user;
                    currentGame.MoveCount     = state.MoveCount;
                    currentGame.GameStartTime = state.GameStartTime;

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

                if (currentGame.CurrentGameCells?.Any() ?? false)
                {
                    db.CurrentGameCells.RemoveRange(currentGame.CurrentGameCells);
                }

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

                    i++;
                }

                db.SaveChanges();
            }
        }
示例#4
0
        public UserDto Register(string login, string password, string userName)
        {
            using (var db = new FifteenGameModel())
            {
                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));
            }
        }