From 2ce38e42227f03ef1a6557c250ffc635e18b3aa6 Mon Sep 17 00:00:00 2001 From: Andy Janata Date: Mon, 23 Jan 2012 12:52:33 -0800 Subject: [PATCH] refactor to get a base abstract GameHandler since a bunch of handlers will need to get the game object --- .../socialgamer/cah/UpdateHandlerList.java | 3 +- .../socialgamer/cah/handlers/GameHandler.java | 53 +++++++++++++++++++ .../cah/handlers/GetGameInfoHandler.java | 35 ++---------- .../cah/handlers/JoinGameHandler.java | 31 ++--------- 4 files changed, 61 insertions(+), 61 deletions(-) create mode 100644 src/net/socialgamer/cah/handlers/GameHandler.java diff --git a/src/net/socialgamer/cah/UpdateHandlerList.java b/src/net/socialgamer/cah/UpdateHandlerList.java index f232cf3..22c128a 100644 --- a/src/net/socialgamer/cah/UpdateHandlerList.java +++ b/src/net/socialgamer/cah/UpdateHandlerList.java @@ -8,8 +8,7 @@ import java.util.List; public class UpdateHandlerList { - private static final List EXCLUDE = Arrays.asList("UpdateHandlerList", "Handler", - "Handlers"); + private static final List EXCLUDE = Arrays.asList("GameHandler", "Handler", "Handlers"); /** * @param args diff --git a/src/net/socialgamer/cah/handlers/GameHandler.java b/src/net/socialgamer/cah/handlers/GameHandler.java new file mode 100644 index 0000000..d8e702e --- /dev/null +++ b/src/net/socialgamer/cah/handlers/GameHandler.java @@ -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 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 handle(final RequestWrapper request, + final HttpSession session, final User user, final Game game); +} diff --git a/src/net/socialgamer/cah/handlers/GetGameInfoHandler.java b/src/net/socialgamer/cah/handlers/GetGameInfoHandler.java index b8a752f..a707dd5 100644 --- a/src/net/socialgamer/cah/handlers/GetGameInfoHandler.java +++ b/src/net/socialgamer/cah/handlers/GetGameInfoHandler.java @@ -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 handle(final RequestWrapper request, final HttpSession session) { + public Map handle(final RequestWrapper request, + final HttpSession session, final User user, final Game game) { final Map data = new HashMap(); - 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; } - } diff --git a/src/net/socialgamer/cah/handlers/JoinGameHandler.java b/src/net/socialgamer/cah/handlers/JoinGameHandler.java index a695b92..b3e49e6 100644 --- a/src/net/socialgamer/cah/handlers/JoinGameHandler.java +++ b/src/net/socialgamer/cah/handlers/JoinGameHandler.java @@ -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 handle(final RequestWrapper request, - final HttpSession session) { + final HttpSession session, final User user, final Game game) { final Map data = new HashMap(); - 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) {