refactor to get a base abstract GameHandler since a bunch of handlers will need to get the game object
This commit is contained in:
parent
e21fd8ed00
commit
2ce38e4222
|
@ -8,8 +8,7 @@ import java.util.List;
|
|||
|
||||
public class UpdateHandlerList {
|
||||
|
||||
private static final List<String> EXCLUDE = Arrays.asList("UpdateHandlerList", "Handler",
|
||||
"Handlers");
|
||||
private static final List<String> EXCLUDE = Arrays.asList("GameHandler", "Handler", "Handlers");
|
||||
|
||||
/**
|
||||
* @param args
|
||||
|
|
|
@ -0,0 +1,53 @@
|
|||
package net.socialgamer.cah.handlers;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.HttpSession;
|
||||
|
||||
import net.socialgamer.cah.Constants.AjaxRequest;
|
||||
import net.socialgamer.cah.Constants.ErrorCode;
|
||||
import net.socialgamer.cah.Constants.ReturnableData;
|
||||
import net.socialgamer.cah.Constants.SessionAttribute;
|
||||
import net.socialgamer.cah.RequestWrapper;
|
||||
import net.socialgamer.cah.data.Game;
|
||||
import net.socialgamer.cah.data.GameManager;
|
||||
import net.socialgamer.cah.data.User;
|
||||
|
||||
|
||||
public abstract class GameHandler extends Handler {
|
||||
|
||||
protected GameManager gameManager;
|
||||
|
||||
public GameHandler(final GameManager gameManager) {
|
||||
this.gameManager = gameManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<ReturnableData, Object> handle(final RequestWrapper request, final HttpSession session) {
|
||||
final User user = (User) session.getAttribute(SessionAttribute.USER);
|
||||
assert (user != null);
|
||||
|
||||
final int gameId;
|
||||
|
||||
if (request.getParameter(AjaxRequest.GAME_ID) == null) {
|
||||
return error(ErrorCode.NO_GAME_SPECIFIED);
|
||||
}
|
||||
try {
|
||||
gameId = Integer.parseInt(request.getParameter(AjaxRequest.GAME_ID));
|
||||
} catch (final NumberFormatException nfe) {
|
||||
return error(ErrorCode.INVALID_GAME);
|
||||
}
|
||||
|
||||
final Game game = gameManager.getGame(gameId);
|
||||
if (game == null) {
|
||||
return error(ErrorCode.INVALID_GAME);
|
||||
}
|
||||
|
||||
assert game.getId() == gameId : "Got a game with id not what we asked for.";
|
||||
|
||||
return handle(request, session, user, game);
|
||||
}
|
||||
|
||||
public abstract Map<ReturnableData, Object> handle(final RequestWrapper request,
|
||||
final HttpSession session, final User user, final Game game);
|
||||
}
|
|
@ -6,11 +6,8 @@ import java.util.Map;
|
|||
import javax.servlet.http.HttpSession;
|
||||
|
||||
import net.socialgamer.cah.Constants.AjaxOperation;
|
||||
import net.socialgamer.cah.Constants.AjaxRequest;
|
||||
import net.socialgamer.cah.Constants.AjaxResponse;
|
||||
import net.socialgamer.cah.Constants.ErrorCode;
|
||||
import net.socialgamer.cah.Constants.ReturnableData;
|
||||
import net.socialgamer.cah.Constants.SessionAttribute;
|
||||
import net.socialgamer.cah.RequestWrapper;
|
||||
import net.socialgamer.cah.data.Game;
|
||||
import net.socialgamer.cah.data.GameManager;
|
||||
|
@ -19,46 +16,22 @@ import net.socialgamer.cah.data.User;
|
|||
import com.google.inject.Inject;
|
||||
|
||||
|
||||
public class GetGameInfoHandler extends Handler {
|
||||
public class GetGameInfoHandler extends GameHandler {
|
||||
|
||||
public static final String OP = AjaxOperation.GET_GAME_INFO.toString();
|
||||
|
||||
private final GameManager gameManager;
|
||||
|
||||
@Inject
|
||||
public GetGameInfoHandler(final GameManager gameManager) {
|
||||
this.gameManager = gameManager;
|
||||
super(gameManager);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<ReturnableData, Object> handle(final RequestWrapper request, final HttpSession session) {
|
||||
public Map<ReturnableData, Object> handle(final RequestWrapper request,
|
||||
final HttpSession session, final User user, final Game game) {
|
||||
final Map<ReturnableData, Object> data = new HashMap<ReturnableData, Object>();
|
||||
|
||||
final User user = (User) session.getAttribute(SessionAttribute.USER);
|
||||
assert (user != null);
|
||||
|
||||
final int gameId;
|
||||
|
||||
if (request.getParameter(AjaxRequest.GAME_ID) == null) {
|
||||
return error(ErrorCode.NO_GAME_SPECIFIED);
|
||||
}
|
||||
try {
|
||||
gameId = Integer.parseInt(request.getParameter(AjaxRequest.GAME_ID));
|
||||
} catch (final NumberFormatException nfe) {
|
||||
return error(ErrorCode.INVALID_GAME);
|
||||
}
|
||||
|
||||
final Game game = gameManager.getGame(gameId);
|
||||
if (game == null) {
|
||||
return error(ErrorCode.INVALID_GAME);
|
||||
}
|
||||
|
||||
assert game.getId() == gameId : "Got a game with id not what we asked for.";
|
||||
|
||||
data.put(AjaxResponse.GAME_INFO, game.getInfo());
|
||||
data.put(AjaxResponse.PLAYER_INFO, game.getPlayerInfo());
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -6,11 +6,9 @@ import java.util.Map;
|
|||
import javax.servlet.http.HttpSession;
|
||||
|
||||
import net.socialgamer.cah.Constants.AjaxOperation;
|
||||
import net.socialgamer.cah.Constants.AjaxRequest;
|
||||
import net.socialgamer.cah.Constants.AjaxResponse;
|
||||
import net.socialgamer.cah.Constants.ErrorCode;
|
||||
import net.socialgamer.cah.Constants.ReturnableData;
|
||||
import net.socialgamer.cah.Constants.SessionAttribute;
|
||||
import net.socialgamer.cah.RequestWrapper;
|
||||
import net.socialgamer.cah.data.Game;
|
||||
import net.socialgamer.cah.data.Game.TooManyPlayersException;
|
||||
|
@ -20,43 +18,20 @@ import net.socialgamer.cah.data.User;
|
|||
import com.google.inject.Inject;
|
||||
|
||||
|
||||
public class JoinGameHandler extends Handler {
|
||||
public class JoinGameHandler extends GameHandler {
|
||||
|
||||
public static final String OP = AjaxOperation.JOIN_GAME.toString();
|
||||
|
||||
private final GameManager gameManager;
|
||||
|
||||
@Inject
|
||||
public JoinGameHandler(final GameManager gameManager) {
|
||||
this.gameManager = gameManager;
|
||||
super(gameManager);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<ReturnableData, Object> handle(final RequestWrapper request,
|
||||
final HttpSession session) {
|
||||
final HttpSession session, final User user, final Game game) {
|
||||
final Map<ReturnableData, Object> data = new HashMap<ReturnableData, Object>();
|
||||
|
||||
final User user = (User) session.getAttribute(SessionAttribute.USER);
|
||||
assert (user != null);
|
||||
|
||||
final int gameId;
|
||||
|
||||
if (request.getParameter(AjaxRequest.GAME_ID) == null) {
|
||||
return error(ErrorCode.NO_GAME_SPECIFIED);
|
||||
}
|
||||
try {
|
||||
gameId = Integer.parseInt(request.getParameter(AjaxRequest.GAME_ID));
|
||||
} catch (final NumberFormatException nfe) {
|
||||
return error(ErrorCode.INVALID_GAME);
|
||||
}
|
||||
|
||||
final Game game = gameManager.getGame(gameId);
|
||||
if (game == null) {
|
||||
return error(ErrorCode.INVALID_GAME);
|
||||
}
|
||||
|
||||
assert game.getId() == gameId : "Got a game with id not what we asked for.";
|
||||
|
||||
try {
|
||||
game.addPlayer(user);
|
||||
} catch (final IllegalStateException e) {
|
||||
|
|
Loading…
Reference in New Issue