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);
}
}
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);
}
Aggregations