public void Save(FinishedGameDto finishedGame) { using (var db = new FifteenGameDbFirstEntities()) { db.FinishedGames.Add(GameMapper.Instance.Map <FinishedGame>(finishedGame)); db.SaveChanges(); } }
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(); } }
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(); } }
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)); } }