Search in sources :

Example 36 with SOCResourceSet

use of soc.game.SOCResourceSet in project JSettlers2 by jdmonin.

the class SOCDisplaylessPlayerClient method handlePLAYERELEMENT_simple.

/**
 * Update game data for a simple player element or flag, for
 * {@link #handlePLAYERELEMENT(SOCGame, SOCPlayer, int, int, int, int)}.
 * Handles ASK_SPECIAL_BUILD, NUM_PICK_GOLD_HEX_RESOURCES, SCENARIO_CLOTH_COUNT, etc.
 *<P>
 * To avoid code duplication, also called from
 * {@link SOCPlayerClient.MessageTreater#handlePLAYERELEMENT(SOCPlayerElement)}
 * and {@link soc.robot.SOCRobotBrain#run()}.
 *<P>
 * Before v2.0.00 this method directly took a {@link SOCPlayerElement} instead of that message's
 * {@code action}, {@code etype}, and {@code val} fields.
 *
 * @param ga   Game to update
 * @param pl   Player to update; some elements take null. If null and {@code pn != -1}, will find {@code pl}
 *     using {@link SOCGame#getPlayer(int) ga.getPlayer(pn)}.
 * @param pn   Player number from message (sometimes -1 for none)
 * @param action   {@link SOCPlayerElement#SET}, {@link SOCPlayerElement#GAIN GAIN},
 *     or {@link SOCPlayerElement#LOSE LOSE}
 * @param etype  Element type, such as {@link SOCPlayerElement#SETTLEMENTS} or {@link SOCPlayerElement#NUMKNIGHTS}
 * @param val  The new value to set, or the delta to gain/lose
 * @param nickname  Our client player nickname/username, for the only element where that matters:
 *     {@link SOCPlayerElement#RESOURCE_COUNT}. Can be {@code null} otherwise.
 * @since 2.0.00
 */
public static void handlePLAYERELEMENT_simple(SOCGame ga, SOCPlayer pl, final int pn, final int action, final int etype, final int val, final String nickname) {
    if ((pl == null) && (pn != -1))
        pl = ga.getPlayer(pn);
    switch(etype) {
        case SOCPlayerElement.ASK_SPECIAL_BUILD:
            if (0 != val) {
                try {
                    // set per-player, per-game flags
                    ga.askSpecialBuild(pn, false);
                } catch (RuntimeException e) {
                }
            } else {
                pl.setAskedSpecialBuild(false);
            }
            break;
        case SOCPlayerElement.RESOURCE_COUNT:
            if (val != pl.getResources().getTotal()) {
                SOCResourceSet rsrcs = pl.getResources();
                if (D.ebugOn) {
                // pi.print(">>> RESOURCE COUNT ERROR: "+mes.getCount()+ " != "+rsrcs.getTotal());
                }
                // 
                if ((nickname != null) && !pl.getName().equals(nickname)) {
                    rsrcs.clear();
                    rsrcs.setAmount(val, SOCResourceConstants.UNKNOWN);
                }
            }
            break;
        case SOCPlayerElement.LAST_SETTLEMENT_NODE:
            pl.setLastSettlementCoord(val);
            break;
        case SOCPlayerElement.PLAYED_DEV_CARD_FLAG:
            {
                final boolean changeTo = (val != 0);
                if (pn != -1)
                    pl.setPlayedDevCard(changeTo);
                else
                    for (int p = 0; p < ga.maxPlayers; ++p) ga.getPlayer(p).setPlayedDevCard(changeTo);
            }
            break;
        case SOCPlayerElement.NUM_PICK_GOLD_HEX_RESOURCES:
            pl.setNeedToPickGoldHexResources(val);
            break;
        case SOCPlayerElement.SCENARIO_SVP:
            pl.setSpecialVP(val);
            break;
        case SOCPlayerElement.SCENARIO_PLAYEREVENTS_BITMASK:
            pl.setScenarioPlayerEvents(val);
            break;
        case SOCPlayerElement.SCENARIO_SVP_LANDAREAS_BITMASK:
            pl.setScenarioSVPLandAreas(val);
            break;
        case SOCPlayerElement.STARTING_LANDAREAS:
            pl.setStartingLandAreasEncoded(val);
            break;
        case SOCPlayerElement.SCENARIO_CLOTH_COUNT:
            if (pn != -1)
                pl.setCloth(val);
            else
                ((SOCBoardLarge) (ga.getBoard())).setCloth(val);
            break;
        case SOCPlayerElement.SCENARIO_WARSHIP_COUNT:
            switch(action) {
                case SOCPlayerElement.SET:
                    pl.setNumWarships(val);
                    break;
                case SOCPlayerElement.GAIN:
                    pl.setNumWarships(pl.getNumWarships() + val);
                    break;
            }
            break;
    }
}
Also used : SOCResourceSet(soc.game.SOCResourceSet)

Example 37 with SOCResourceSet

use of soc.game.SOCResourceSet in project JSettlers2 by jdmonin.

the class SOCGameMessageHandler method handleMAKEOFFER.

// / Player trades and bank trades ///
/**
 * handle "make offer" message.
 *
 * @param c  the connection that sent the message
 * @param mes  the message
 * @since 1.0.0
 */
private void handleMAKEOFFER(SOCGame ga, Connection c, final SOCMakeOffer mes) {
    final String gaName = ga.getName();
    if (ga.isGameOptionSet("NT")) {
        srv.messageToPlayer(c, gaName, "Trading is not allowed in this game.");
        // <---- Early return: No Trading ----
        return;
    }
    ga.takeMonitor();
    try {
        SOCTradeOffer offer = mes.getOffer();
        /**
         * remake the offer with data that we know is accurate,
         * namely the 'from' datum
         */
        SOCPlayer player = ga.getPlayer(c.getData());
        /**
         * announce the offer, including text message similar to bank/port trade.
         */
        if (player != null) {
            SOCTradeOffer remadeOffer;
            {
                SOCResourceSet offGive = offer.getGiveSet(), offGet = offer.getGetSet();
                remadeOffer = new SOCTradeOffer(gaName, player.getPlayerNumber(), offer.getTo(), offGive, offGet);
                player.setCurrentOffer(remadeOffer);
                // older clients need it sent from the server
                if (ga.clientVersionLowest < SOCStringManager.VERSION_FOR_I18N) {
                    // I18N OK: Pre-2.0.00 clients always use english
                    final String txt = SOCStringManager.getFallbackServerManagerForClient().formatSpecial(ga, "{0} offered to give {1,rsrcs} for {2,rsrcs}.", player.getName(), offGive, offGet);
                    srv.messageToGameForVersions(ga, 0, SOCStringManager.VERSION_FOR_I18N - 1, new SOCGameTextMsg(gaName, SOCServer.SERVERNAME, txt), true);
                }
            }
            SOCMakeOffer makeOfferMessage = new SOCMakeOffer(gaName, remadeOffer);
            srv.messageToGame(gaName, makeOfferMessage);
            srv.recordGameEvent(gaName, makeOfferMessage);
            /**
             * clear all the trade messages because a new offer has been made
             */
            srv.gameList.takeMonitorForGame(gaName);
            if (ga.clientVersionLowest >= SOCClearTradeMsg.VERSION_FOR_CLEAR_ALL) {
                srv.messageToGameWithMon(gaName, new SOCClearTradeMsg(gaName, -1));
            } else {
                for (int i = 0; i < ga.maxPlayers; i++) srv.messageToGameWithMon(gaName, new SOCClearTradeMsg(gaName, i));
            }
            srv.gameList.releaseMonitorForGame(gaName);
        }
    } catch (Exception e) {
        D.ebugPrintStackTrace(e, "Exception caught");
    }
    ga.releaseMonitor();
}
Also used : SOCTradeOffer(soc.game.SOCTradeOffer) SOCPlayer(soc.game.SOCPlayer) SOCResourceSet(soc.game.SOCResourceSet)

Example 38 with SOCResourceSet

use of soc.game.SOCResourceSet in project JSettlers2 by jdmonin.

the class TestResourceSet method addResourceSet_addsResourceSet.

@Test
public void addResourceSet_addsResourceSet() {
    SOCResourceSet rs1 = onePerType();
    SOCResourceSet rs2 = onePerType();
    rs1.add(rs2);
    assertEquals(2, rs1.getAmount(SOCResourceConstants.CLAY));
    assertEquals(2, rs1.getAmount(SOCResourceConstants.WHEAT));
    assertEquals(2, rs1.getAmount(SOCResourceConstants.ORE));
    assertEquals(2, rs1.getAmount(SOCResourceConstants.WOOD));
    assertEquals(2, rs1.getAmount(SOCResourceConstants.SHEEP));
    assertTrue(rs1.contains(SOCResourceConstants.WHEAT));
}
Also used : SOCResourceSet(soc.game.SOCResourceSet) Test(org.junit.Test)

Example 39 with SOCResourceSet

use of soc.game.SOCResourceSet in project JSettlers2 by jdmonin.

the class TestResourceSet method clone_isEqual.

@Test
public void clone_isEqual() {
    SOCResourceSet rs1 = onePerType();
    SOCResourceSet rs2 = onePerType();
    assertTrue(rs1.equals(rs2));
    assertTrue(rs2.equals(rs1));
}
Also used : SOCResourceSet(soc.game.SOCResourceSet) Test(org.junit.Test)

Example 40 with SOCResourceSet

use of soc.game.SOCResourceSet in project JSettlers2 by jdmonin.

the class TestResourceSet method removeAll_yieldsEmptyResourceSet2.

@Test
public void removeAll_yieldsEmptyResourceSet2() {
    SOCResourceSet rs1 = onePerType();
    SOCResourceSet rs2 = onePerType();
    rs1.subtract(rs2);
    assertEquals(0, rs1.getTotal());
}
Also used : SOCResourceSet(soc.game.SOCResourceSet) Test(org.junit.Test)

Aggregations

SOCResourceSet (soc.game.SOCResourceSet)54 Test (org.junit.Test)14 SOCTradeOffer (soc.game.SOCTradeOffer)10 SOCPlayer (soc.game.SOCPlayer)8 Stack (java.util.Stack)4 SOCShip (soc.game.SOCShip)3 ArrayList (java.util.ArrayList)2 StringTokenizer (java.util.StringTokenizer)2 SOCBoardLarge (soc.game.SOCBoardLarge)2 SOCGame (soc.game.SOCGame)2 SOCRoad (soc.game.SOCRoad)2 Queue (soc.util.Queue)2 Hashtable (java.util.Hashtable)1 Iterator (java.util.Iterator)1 List (java.util.List)1 MissingResourceException (java.util.MissingResourceException)1 Vector (java.util.Vector)1 SOCInventoryItem (soc.game.SOCInventoryItem)1 SOCLRPathData (soc.game.SOCLRPathData)1 SOCSpecialItem (soc.game.SOCSpecialItem)1