Search in sources :

Example 11 with SOCPlayerElement

use of soc.message.SOCPlayerElement in project JSettlers2 by jdmonin.

the class SOCGameHandler method forceGamePlayerDiscardOrGain.

/**
 * Force this player (not current player) to discard, or gain random resources from a gold hex,
 * and report resources to all players. Does not send gameState, which may have changed when
 * this method called {@link SOCGame#playerDiscardOrGainRandom(int, boolean)}.
 *<P>
 * Discards if {@link SOCGame#getGameState() cg.getGameState()} == {@link SOCGame#WAITING_FOR_DISCARDS},
 * otherwise picks enough random resources for {@link SOCPlayer#getNeedToPickGoldHexResources()}.
 *<P>
 * Assumes, as {@link #endGameTurn(SOCGame, SOCPlayer, boolean)} does:
 * <UL>
 * <LI> ga.takeMonitor already called (not the same as {@link SOCGameList#takeMonitorForGame(String)})
 * <LI> gamelist.takeMonitorForGame is NOT called, we do NOT have that monitor
 * </UL>
 *
 * @param cg  Game object
 * @param cpn Game's current player number
 * @param c   Connection of discarding/gaining player
 * @param plName Discarding/gaining player {@code pn}'s name, for GameTextMsg
 * @param pn  Player number who must discard/gain resources
 * @throws IllegalStateException if {@code pn} is current player, or if incorrect game state or incorrect
 *     player status; see {@link SOCGame#playerDiscardOrGainRandom(int, boolean)} for details
 */
private final void forceGamePlayerDiscardOrGain(final SOCGame cg, final int cpn, final Connection c, final String plName, final int pn) throws IllegalStateException {
    final boolean isDiscard = (cg.getGameState() == SOCGame.WAITING_FOR_DISCARDS);
    final SOCResourceSet rset = cg.playerDiscardOrGainRandom(pn, isDiscard);
    // Report resources lost or gained; see also forceEndGameTurn for same reporting code.
    final String gaName = cg.getName();
    final int totalRes = rset.getTotal();
    if (isDiscard) {
        if ((c != null) && c.isConnected())
            reportRsrcGainLoss(gaName, rset, true, true, pn, -1, null, c);
        srv.messageToGameExcept(gaName, c, new SOCPlayerElement(gaName, pn, SOCPlayerElement.LOSE, SOCPlayerElement.UNKNOWN, totalRes, true), true);
        // "{0} discarded {1} resources."
        srv.messageToGameKeyed(cg, true, "action.discarded", plName, totalRes);
        System.err.println("Forced discard: " + totalRes + " from " + plName + " in game " + gaName);
    } else {
        // Send SOCPlayerElement messages, "gains" text
        reportRsrcGainGold(cg, cg.getPlayer(pn), pn, rset, true, false);
        System.err.println("Forced gold picks: " + totalRes + " to " + plName + " in game " + gaName);
    }
}
Also used : SOCPlayerElement(soc.message.SOCPlayerElement)

Example 12 with SOCPlayerElement

use of soc.message.SOCPlayerElement in project JSettlers2 by jdmonin.

the class SOCGameHandler method debugGiveResources.

/**
 * this is a debugging command that gives resources to a player.
 * Format: rsrcs: #cl #or #sh #wh #wo playername
 */
private final void debugGiveResources(Connection c, String mes, SOCGame game) {
    StringTokenizer st = new StringTokenizer(mes.substring(6));
    int[] resources = new int[SOCResourceConstants.WOOD + 1];
    int resourceType = SOCResourceConstants.CLAY;
    String name = "";
    boolean parseError = false;
    while (st.hasMoreTokens()) {
        if (resourceType <= SOCResourceConstants.WOOD) {
            String token = st.nextToken();
            try {
                resources[resourceType] = Integer.parseInt(token);
                resourceType++;
            } catch (NumberFormatException e) {
                parseError = true;
                break;
            }
        } else {
            // get all the of the line, in case there's a space in the player name ("robot 7"),
            // by choosing an unlikely separator character
            name = st.nextToken(Character.toString((char) 1)).trim();
            break;
        }
    }
    SOCPlayer pl = null;
    if (!parseError) {
        pl = debug_getPlayer(c, game, name);
        if (pl == null)
            parseError = true;
    }
    if (parseError) {
        srv.messageToPlayer(c, game.getName(), "### Usage: " + DEBUG_COMMANDS_HELP_RSRCS);
        srv.messageToPlayer(c, game.getName(), DEBUG_COMMANDS_HELP_PLAYER);
        // <--- early return ---
        return;
    }
    SOCResourceSet rset = pl.getResources();
    int pnum = pl.getPlayerNumber();
    String outMes = "### " + pl.getName() + " gets";
    for (resourceType = SOCResourceConstants.CLAY; resourceType <= SOCResourceConstants.WOOD; resourceType++) {
        rset.add(resources[resourceType], resourceType);
        outMes += (" " + resources[resourceType]);
        // SOCResourceConstants.CLAY == SOCPlayerElement.CLAY
        srv.messageToGame(game.getName(), new SOCPlayerElement(game.getName(), pnum, SOCPlayerElement.GAIN, resourceType, resources[resourceType]));
    }
    srv.messageToGame(game.getName(), outMes);
}
Also used : StringTokenizer(java.util.StringTokenizer) SOCPlayerElement(soc.message.SOCPlayerElement)

Aggregations

SOCPlayerElement (soc.message.SOCPlayerElement)12 Connection (soc.server.genericServer.Connection)5 ArrayList (java.util.ArrayList)3 SOCDevCardAction (soc.message.SOCDevCardAction)3 SOCGameElements (soc.message.SOCGameElements)3 SOCPlayerElements (soc.message.SOCPlayerElements)3 HashSet (java.util.HashSet)2 SOCInventoryItemAction (soc.message.SOCInventoryItemAction)2 SOCMessage (soc.message.SOCMessage)2 SOCPieceValue (soc.message.SOCPieceValue)2 SOCRollDicePrompt (soc.message.SOCRollDicePrompt)2 SOCSetPlayedDevCard (soc.message.SOCSetPlayedDevCard)2 SOCSimpleRequest (soc.message.SOCSimpleRequest)2 SOCTurn (soc.message.SOCTurn)2 Date (java.util.Date)1 NoSuchElementException (java.util.NoSuchElementException)1 StringTokenizer (java.util.StringTokenizer)1 SOCChangeFace (soc.message.SOCChangeFace)1 SOCClearOffer (soc.message.SOCClearOffer)1 SOCDevCardCount (soc.message.SOCDevCardCount)1