Add auto-passing to server (rather than on client).

This commit is contained in:
marcins78@gmail.com
2012-01-11 16:37:23 +00:00
parent 59778785a1
commit 72ceff28b2
6 changed files with 104 additions and 29 deletions

View File

@@ -1,5 +1,6 @@
package com.gempukku.lotro.game.state;
import com.gempukku.lotro.common.Phase;
import com.gempukku.lotro.communication.UserFeedback;
import com.gempukku.lotro.game.ActionsEnvironment;
import com.gempukku.lotro.game.LotroFormat;
@@ -28,4 +29,6 @@ public interface LotroGame {
public String getWinnerPlayerId();
public LotroFormat getFormat();
public boolean shouldAutoPass(String playerId, Phase phase);
}

View File

@@ -34,6 +34,7 @@ public class DefaultLotroGame implements LotroGame {
private LotroFormat _format;
private Set<String> _allPlayers;
private Map<String, Set<Phase>> _autoPassConfiguration = new HashMap<String, Set<Phase>>();
private String _winnerPlayerId;
private Map<String, String> _losers = new HashMap<String, String>();
@@ -82,6 +83,14 @@ public class DefaultLotroGame implements LotroGame {
ruleSet.applyRuleSet();
}
@Override
public boolean shouldAutoPass(String playerId, Phase phase) {
final Set<Phase> passablePhases = _autoPassConfiguration.get(playerId);
if (passablePhases == null)
return false;
return passablePhases.contains(phase);
}
public void addGameResultListener(GameResultListener listener) {
_gameResultListeners.add(listener);
}
@@ -207,4 +216,8 @@ public class DefaultLotroGame implements LotroGame {
public void removeGameStateListener(GameStateListener gameStateListener) {
_gameState.removeGameStateListener(gameStateListener);
}
public void setPlayerAutoPassSettings(String playerId, Set<Phase> phases) {
_autoPassConfiguration.put(playerId, phases);
}
}

View File

@@ -23,18 +23,26 @@ public class PlayerPlaysPhaseActionsUntilPassesGameProcess implements GameProces
public void process(final LotroGame game) {
final List<Action> playableActions = game.getActionsEnvironment().getPhaseActions(_playerId);
game.getUserFeedback().sendAwaitingDecision(_playerId,
new CardActionSelectionDecision(game, 1, "Play " + game.getGameState().getCurrentPhase().getHumanReadable() + " action or Pass", playableActions) {
@Override
public void decisionMade(String result) throws DecisionResultInvalidException {
Action action = getSelectedAction(result);
if (action != null) {
_nextProcess = new PlayerPlaysPhaseActionsUntilPassesGameProcess(_playerId, _followingGameProcess);
game.getActionsEnvironment().addActionToStack(action);
} else
_nextProcess = _followingGameProcess;
}
});
if (playableActions.size() == 0 & game.shouldAutoPass(_playerId, game.getGameState().getCurrentPhase())) {
playerPassed();
} else {
game.getUserFeedback().sendAwaitingDecision(_playerId,
new CardActionSelectionDecision(game, 1, "Play " + game.getGameState().getCurrentPhase().getHumanReadable() + " action or Pass", playableActions) {
@Override
public void decisionMade(String result) throws DecisionResultInvalidException {
Action action = getSelectedAction(result);
if (action != null) {
_nextProcess = new PlayerPlaysPhaseActionsUntilPassesGameProcess(_playerId, _followingGameProcess);
game.getActionsEnvironment().addActionToStack(action);
} else
playerPassed();
}
});
}
}
private void playerPassed() {
_nextProcess = _followingGameProcess;
}
@Override

View File

@@ -42,27 +42,34 @@ public class PlayersPlayPhaseActionsInOrderGameProcess implements GameProcess {
}
final List<Action> playableActions = game.getActionsEnvironment().getPhaseActions(playerId);
game.getUserFeedback().sendAwaitingDecision(playerId,
new CardActionSelectionDecision(game, 1, "Play " + game.getGameState().getCurrentPhase().getHumanReadable() + " action or Pass", playableActions) {
@Override
public void decisionMade(String result) throws DecisionResultInvalidException {
Action action = getSelectedAction(result);
if (action != null) {
_nextProcess = new PlayersPlayPhaseActionsInOrderGameProcess(_playOrder, 0, _followingGameProcess);
game.getActionsEnvironment().addActionToStack(action);
} else {
_consecutivePasses++;
if (_consecutivePasses >= _playOrder.getPlayerCount())
_nextProcess = _followingGameProcess;
else
_nextProcess = new PlayersPlayPhaseActionsInOrderGameProcess(_playOrder, _consecutivePasses, _followingGameProcess);
if (playableActions.size() == 0 & game.shouldAutoPass(playerId, game.getGameState().getCurrentPhase())) {
playerPassed();
} else {
game.getUserFeedback().sendAwaitingDecision(playerId,
new CardActionSelectionDecision(game, 1, "Play " + game.getGameState().getCurrentPhase().getHumanReadable() + " action or Pass", playableActions) {
@Override
public void decisionMade(String result) throws DecisionResultInvalidException {
Action action = getSelectedAction(result);
if (action != null) {
_nextProcess = new PlayersPlayPhaseActionsInOrderGameProcess(_playOrder, 0, _followingGameProcess);
game.getActionsEnvironment().addActionToStack(action);
} else {
playerPassed();
}
}
}
});
});
}
}
}
private void playerPassed() {
_consecutivePasses++;
if (_consecutivePasses >= _playOrder.getPlayerCount())
_nextProcess = _followingGameProcess;
else
_nextProcess = new PlayersPlayPhaseActionsInOrderGameProcess(_playOrder, _consecutivePasses, _followingGameProcess);
}
@Override
public GameProcess getNextProcess() {
return _nextProcess;

View File

@@ -54,6 +54,12 @@ public class LotroGameMediator {
_userFeedback.setGame(_lotroGame);
}
public void setPlayerAutoPassSettings(String playerId, Set<Phase> phases) {
if (_playersPlaying.contains(playerId)) {
_lotroGame.setPlayerAutoPassSettings(playerId, phases);
}
}
public void sendMessageToPlayers(String message) {
_lotroGame.getGameState().sendMessage(message);
}

View File

@@ -1,5 +1,6 @@
package com.gempukku.lotro.server;
import com.gempukku.lotro.common.Phase;
import com.gempukku.lotro.game.LotroGameMediator;
import com.gempukku.lotro.game.LotroServer;
import com.gempukku.lotro.game.ParticipantCommunicationVisitor;
@@ -13,6 +14,7 @@ import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
@@ -22,7 +24,10 @@ import javax.ws.rs.core.Response;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
@Singleton
@Path("/game")
@@ -31,6 +36,18 @@ public class GameResource extends AbstractResource {
@Context
private LotroServer _lotroServer;
private static Set<Phase> _autoPassAll = new HashSet<Phase>();
static {
_autoPassAll.add(Phase.FELLOWSHIP);
_autoPassAll.add(Phase.SHADOW);
_autoPassAll.add(Phase.MANEUVER);
_autoPassAll.add(Phase.ARCHERY);
_autoPassAll.add(Phase.ASSIGNMENT);
_autoPassAll.add(Phase.SKIRMISH);
_autoPassAll.add(Phase.REGROUP);
}
@Path("/{gameId}")
@GET
@Produces(MediaType.APPLICATION_XML)
@@ -45,6 +62,8 @@ public class GameResource extends AbstractResource {
if (gameMediator == null)
sendError(Response.Status.NOT_FOUND);
gameMediator.setPlayerAutoPassSettings(resourceOwner.getName(), getAutoPassPhases(request));
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document doc = documentBuilder.newDocument();
@@ -56,6 +75,23 @@ public class GameResource extends AbstractResource {
return doc;
}
private Set<Phase> getAutoPassPhases(HttpServletRequest request) {
for (Cookie cookie : request.getCookies()) {
if (cookie.getName().equals("autoPassPhases")) {
final String[] phases = cookie.getValue().split(",");
Set<Phase> result = new HashSet<Phase>();
for (String phase : phases)
result.add(Phase.valueOf(phase));
return result;
}
}
for (Cookie cookie : request.getCookies()) {
if (cookie.getName().equals("autoPass") && cookie.getValue().equals("false"))
return Collections.emptySet();
}
return _autoPassAll;
}
@Path("/{gameId}/cardInfo")
@GET
@Produces("text/html")
@@ -106,6 +142,8 @@ public class GameResource extends AbstractResource {
if (gameMediator == null)
sendError(Response.Status.NOT_FOUND);
gameMediator.setPlayerAutoPassSettings(resourceOwner.getName(), getAutoPassPhases(request));
if (decisionId != null) {
try {
gameMediator.playerAnswered(resourceOwner, decisionId, decisionValue);