/** * Perform game turn. * * @param turnCommand command to perform turn * @return turn result */ @RequestMapping(value = "/game/turn.do" /*, method = RequestMethod.POST*/) @ResponseBody public TurnResult doTurn(@Valid TurnCommand turnCommand) { physicProcessor.fire(turnCommand.getAngle()); physicProcessor.runPhysics(); final Iterable<ContactInfo> contacts = physicProcessor.processContactsHistory(); final TurnResult result = new TurnResult(); result.setSuccess(true); result.setContacts(contacts); result.setBubbles(gameWorld.getBubbles()); return result; }
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { HttpSession session = req.getSession(); TextGame gameModel = null; GameStates stateModel = null; GameState gameState = null; if (session.isNew()) { // initialize the model and state of the game gameModel = new Vampire(); stateModel = new GameStates(); stateModel.addState(new GameStateUninitialized(stateModel)); stateModel.addState(new GameStateInitialized(stateModel)); stateModel.addState(new GameStatePlaying(stateModel)); stateModel.addState(new GameStateQuitting(stateModel)); stateModel.addState(new GameStateDone(stateModel)); gameState = stateModel.getState(GameStates.UNINITIALIZED); // store in the session session.setAttribute("gameModel", gameModel); session.setAttribute("stateModel", stateModel); session.setAttribute("gameState", gameState); } else { // retrieve the model and state from the session gameModel = (TextGame) session.getAttribute("gameModel"); stateModel = (GameStates) session.getAttribute("stateModel"); gameState = (GameState) session.getAttribute("gameState"); } String action = req.getParameter("action"); if (action != null && action != "") { action = action.toLowerCase(); } else { action = "look"; // default to "look" action } resp.setContentType("text/html"); out = resp.getWriter(); out.println( "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">"); out.println("<HTML>"); out.println( "<HEAD><META http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">"); out.println("<TITLE>Vampire</TITLE></HEAD>"); out.println("<BODY>"); GameContext context = new GameContext(gameModel, new ActionBasic(action)); try { TurnResult turnResult = gameState.takeTurn(context); prompt(turnResult.getOutput()); gameState = (GameState) turnResult.getNextState(); // save modified model and state in the session session.setAttribute("gameModel", gameModel); session.setAttribute("gameState", gameState); } catch (IllegalArgumentException e) { // context was invalid somehow, report the problem and skip turn prompt("Something odd happened that cannot be explained... try again."); } out.println("</BODY></HTML>"); }