From a41fc350692bbec47fd48f021f4079bfee52744b Mon Sep 17 00:00:00 2001 From: "marcins78@gmail.com" Date: Sun, 13 May 2012 20:18:06 +0000 Subject: [PATCH] - Added a way to request a game to be cancelled, if both parties request, the game will be cancelled. --- .../lotro/logic/timing/DefaultLotroGame.java | 24 ++++++++++++++++++- .../lotro/game/LotroGameMediator.java | 11 +++++++++ .../gempukku/lotro/server/GameResource.java | 15 ++++++++++++ .../src/main/webapp/includes/changeLog.html | 1 + .../main/webapp/js/gemp-005/communication.js | 11 +++++++++ .../src/main/webapp/js/gemp-005/gameUi.js | 7 +++++- 6 files changed, 67 insertions(+), 2 deletions(-) diff --git a/gemp-lotr/gemp-lotr-logic/src/main/java/com/gempukku/lotro/logic/timing/DefaultLotroGame.java b/gemp-lotr/gemp-lotr-logic/src/main/java/com/gempukku/lotro/logic/timing/DefaultLotroGame.java index 10b4c90de..da79b998a 100644 --- a/gemp-lotr/gemp-lotr-logic/src/main/java/com/gempukku/lotro/logic/timing/DefaultLotroGame.java +++ b/gemp-lotr/gemp-lotr-logic/src/main/java/com/gempukku/lotro/logic/timing/DefaultLotroGame.java @@ -43,6 +43,8 @@ public class DefaultLotroGame implements LotroGame { private Set _gameResultListeners = new HashSet(); + private Set _requestedCancel = new HashSet(); + public DefaultLotroGame(LotroFormat format, Map decks, UserFeedback userFeedback, final LotroCardBlueprintLibrary library) { _format = format; _actionStack = new ActionStack(); @@ -130,7 +132,7 @@ public class DefaultLotroGame implements LotroGame { _cancelled = true; if (_gameState != null) { - _gameState.sendMessage("Game was cancelled do to an error, the error was logged and will be fixed soon."); + _gameState.sendMessage("Game was cancelled due to an error, the error was logged and will be fixed soon."); _gameState.sendMessage("Please post the replay game link and description of what happened on the TLHH forum."); } @@ -141,6 +143,20 @@ public class DefaultLotroGame implements LotroGame { } } + public void cancelGameRequested() { + if (!_finished) { + _cancelled = true; + + if (_gameState != null) + _gameState.sendMessage("Game was cancelled, as requested by all parties."); + + for (GameResultListener gameResultListener : _gameResultListeners) + gameResultListener.gameCancelled(); + + _finished = true; + } + } + public boolean isCancelled() { return _cancelled; } @@ -189,6 +205,12 @@ public class DefaultLotroGame implements LotroGame { } } + public void requestCancel(String playerId) { + _requestedCancel.add(playerId); + if (_requestedCancel.size() == _allPlayers.size()) + cancelGameRequested(); + } + @Override public GameState getGameState() { return _gameState; diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/LotroGameMediator.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/LotroGameMediator.java index 9c308802d..7684b0eda 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/LotroGameMediator.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/LotroGameMediator.java @@ -266,6 +266,17 @@ public class LotroGameMediator { } } + public void cancel(Player player) { + String playerId = player.getName(); + _writeLock.lock(); + try { + if (_playersPlaying.contains(playerId)) + _lotroGame.requestCancel(playerId); + } finally { + _writeLock.unlock(); + } + } + public void playerAnswered(Player player, int channelNumber, int decisionId, String answer) { String playerName = player.getName(); _writeLock.lock(); diff --git a/gemp-lotr/gemp-lotr-web-server/src/main/java/com/gempukku/lotro/server/GameResource.java b/gemp-lotr/gemp-lotr-web-server/src/main/java/com/gempukku/lotro/server/GameResource.java index 470350091..05a50a1e5 100644 --- a/gemp-lotr/gemp-lotr-web-server/src/main/java/com/gempukku/lotro/server/GameResource.java +++ b/gemp-lotr/gemp-lotr-web-server/src/main/java/com/gempukku/lotro/server/GameResource.java @@ -120,6 +120,21 @@ public class GameResource extends AbstractResource { gameMediator.concede(resourceOwner); } + @Path("/{gameId}/cancel") + @POST + public void cancel( + @PathParam("gameId") String gameId, + @FormParam("participantId") String participantId, + @Context HttpServletRequest request) throws ParserConfigurationException { + Player resourceOwner = getResourceOwnerSafely(request, participantId); + + LotroGameMediator gameMediator = _lotroServer.getGameById(gameId); + if (gameMediator == null) + sendError(Response.Status.NOT_FOUND); + + gameMediator.cancel(resourceOwner); + } + @Path("/{gameId}") @POST @Produces(MediaType.APPLICATION_XML) diff --git a/gemp-lotr/gemp-lotr-web/src/main/webapp/includes/changeLog.html b/gemp-lotr/gemp-lotr-web/src/main/webapp/includes/changeLog.html index d2c5e0f0b..776c2ef46 100644 --- a/gemp-lotr/gemp-lotr-web/src/main/webapp/includes/changeLog.html +++ b/gemp-lotr/gemp-lotr-web/src/main/webapp/includes/changeLog.html @@ -6,6 +6,7 @@ already carries one. - "Assault Commander" now gives -1 resistance, rather than +1. - "Shingle in a Storm" now correctly discards itself upon use. - "Armored Easterling" Dmg+1 bonus lasts only until the end of the skirmish now. +- Added a way to request a game to be cancelled, if both parties request, the game will be cancelled. 09 May 2012 - "Wrath of Harad" should now no longer freeze the game. diff --git a/gemp-lotr/gemp-lotr-web/src/main/webapp/js/gemp-005/communication.js b/gemp-lotr/gemp-lotr-web/src/main/webapp/js/gemp-005/communication.js index 8937e99a0..4dae618c6 100644 --- a/gemp-lotr/gemp-lotr-web/src/main/webapp/js/gemp-005/communication.js +++ b/gemp-lotr/gemp-lotr-web/src/main/webapp/js/gemp-005/communication.js @@ -180,6 +180,17 @@ var GempLotrCommunication = Class.extend({ dataType: "xml" }); }, + cancel: function(errorMap) { + $.ajax({ + type: "POST", + url: this.url + "/game/" + getUrlParam("gameId") + "/cancel", + cache: false, + data: { + participantId: getUrlParam("participantId")}, + error: this.errorCheck(errorMap), + dataType: "xml" + }); + }, getDeck: function(deckName, callback, errorMap) { $.ajax({ type: "GET", diff --git a/gemp-lotr/gemp-lotr-web/src/main/webapp/js/gemp-005/gameUi.js b/gemp-lotr/gemp-lotr-web/src/main/webapp/js/gemp-005/gameUi.js index 84557d643..f13d35201 100644 --- a/gemp-lotr/gemp-lotr-web/src/main/webapp/js/gemp-005/gameUi.js +++ b/gemp-lotr/gemp-lotr-web/src/main/webapp/js/gemp-005/gameUi.js @@ -463,11 +463,16 @@ var GempLotrGameUI = Class.extend({ this.chatBox.chatUpdateInterval = 3000; if (!this.spectatorMode && !this.replayMode) { - $("#gameOptionsBox").append(""); + $("#gameOptionsBox").append("
"); $("#concedeGame").button().click( function() { that.communication.concede(); }); + $("#gameOptionsBox").append(""); + $("#cancelGame").button().click( + function() { + that.communication.cancel(); + }); } },