@SuppressWarnings("finally")
	public static Game acceptChallenge(EntityManager manager, Long challengeId,
			String mobileHash, boolean reject) throws Exception {

		Challenge challenge = manager.find(Challenge.class, challengeId);
		if (challenge == null) {
			throw new Exception("Challenge already accepted by other user");
		}

		if (reject) {
			if (mobileHash.equals(challenge.getOpponent())) {
				String msg="";
				EntityTransaction transaction = manager.getTransaction();
				try {
					transaction.begin();
					manager.remove(challenge);
					transaction.commit();
					msg="You have rejected your opponent challenge";
				} catch (Exception e) {
					try {
						transaction.rollback();
					} catch (Exception ex) {
						ex.printStackTrace();
					}
					msg=
							"unable to reject the challenge (Internal error)";
				}
				finally{
					throw new Exception(msg);
				}
			} else {
				throw new Exception(
						"It's an open challenge, you can't cancel it");
			}
		}

		if (challenge.getCreatedBy().equals(mobileHash)) {
			throw new Exception("You can't accept challenges created by you");
		}
		ChessUser user = manager.find(ChessUser.class, mobileHash);
		if (challenge != null
				&& challenge.getMin_rating() <= user.getRating()
				&& challenge.getMax_rating() >= user.getRating()
				&& (challenge.getOpponent() == null || mobileHash
						.equals(challenge.getOpponent()))) {
			EntityTransaction transaction = manager.getTransaction();
			try {

				Game game = new Game(
						ChessBoard.getNewInstance(),
						"white".equals(challenge.getCreatorColor()) ? challenge
								.getCreatedBy() : user.getMobileHash(),
						!"white".equals(challenge.getCreatorColor()) ? challenge
								.getCreatedBy() : user.getMobileHash());
				manager.persist(game);
				transaction.begin();
				manager.remove(challenge);
				transaction.commit();
				return game;
			} catch (Exception e) {
				try {
					transaction.rollback();
				} catch (Exception ex) {
					ex.printStackTrace();
				}
				throw new Exception(
						"Unable to accept challenge (Internal Error)", e);
			}

		}
		throw new Exception("Unable to Accept the Challenge");
	}