Search in sources :

Example 1 with TechResults

use of games.strategy.triplea.delegate.dataObjects.TechResults in project triplea by triplea-game.

the class TripleAPlayer method tech.

private void tech() {
    if (getPlayerBridge().isGameOver()) {
        return;
    }
    final ITechDelegate techDelegate;
    try {
        techDelegate = (ITechDelegate) getPlayerBridge().getRemoteDelegate();
    } catch (final ClassCastException e) {
        final String errorContext = "PlayerBridge step name: " + getPlayerBridge().getStepName() + ", Remote class name: " + getPlayerBridge().getRemoteDelegate().getClass();
        // for some reason the client is not seeing or getting these errors, so print to err too
        System.err.println(errorContext);
        ClientLogger.logQuietly(errorContext, e);
        throw new IllegalStateException(errorContext, e);
    }
    final PlayerID id = getPlayerId();
    if (!soundPlayedAlreadyTechnology) {
        ClipPlayer.play(SoundPath.CLIP_PHASE_TECHNOLOGY, id);
        soundPlayedAlreadyTechnology = true;
    }
    final TechRoll techRoll = ui.getTechRolls(id);
    if (techRoll != null) {
        final TechResults techResults = techDelegate.rollTech(techRoll.getRolls(), techRoll.getTech(), techRoll.getNewTokens(), techRoll.getWhoPaysHowMuch());
        if (techResults.isError()) {
            ui.notifyError(techResults.getErrorString());
            tech();
        } else {
            ui.notifyTechResults(techResults);
        }
    }
}
Also used : ITechDelegate(games.strategy.triplea.delegate.remote.ITechDelegate) PlayerID(games.strategy.engine.data.PlayerID) TechResults(games.strategy.triplea.delegate.dataObjects.TechResults) TechRoll(games.strategy.triplea.delegate.dataObjects.TechRoll)

Example 2 with TechResults

use of games.strategy.triplea.delegate.dataObjects.TechResults in project triplea by triplea-game.

the class WW2V3Year41Test method testTechTokens.

@Test
public void testTechTokens() {
    // Set up the test
    final PlayerID germans = GameDataTestUtil.germans(gameData);
    final ITestDelegateBridge delegateBridge = getDelegateBridge(germans);
    delegateBridge.setStepName("germanTech");
    final TechnologyDelegate techDelegate = techDelegate(gameData);
    techDelegate.setDelegateBridgeAndPlayer(delegateBridge);
    techDelegate.start();
    final TechnologyFrontier mech = new TechnologyFrontier("", gameData);
    mech.addAdvance(TechAdvance.findAdvance(TechAdvance.TECH_PROPERTY_MECHANIZED_INFANTRY, gameData, null));
    // Add tech token
    gameData.performChange(ChangeFactory.changeResourcesChange(germans, gameData.getResourceList().getResource(Constants.TECH_TOKENS), 1));
    // Check to make sure it was successful
    final int initTokens = germans.getResources().getQuantity("techTokens");
    assertEquals(1, initTokens);
    // Fail the roll
    delegateBridge.setRandomSource(new ScriptedRandomSource(new int[] { 3 }));
    final TechResults roll = techDelegate.rollTech(1, mech, 0, null);
    // Check to make sure it failed
    assertEquals(0, roll.getHits());
    final int midTokens = germans.getResources().getQuantity("techTokens");
    assertEquals(1, midTokens);
    // Make a Successful roll
    delegateBridge.setRandomSource(new ScriptedRandomSource(new int[] { 5 }));
    final TechResults roll2 = techDelegate.rollTech(1, mech, 0, null);
    // Check to make sure it succeeded and all tokens were removed
    assertEquals(1, roll2.getHits());
    final int finalTokens = germans.getResources().getQuantity("techTokens");
    assertEquals(0, finalTokens);
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) TechResults(games.strategy.triplea.delegate.dataObjects.TechResults) ITestDelegateBridge(games.strategy.engine.data.ITestDelegateBridge) TechnologyFrontier(games.strategy.engine.data.TechnologyFrontier) ScriptedRandomSource(games.strategy.engine.random.ScriptedRandomSource) Test(org.junit.jupiter.api.Test)

Example 3 with TechResults

use of games.strategy.triplea.delegate.dataObjects.TechResults in project triplea by triplea-game.

the class TechnologyDelegate method rollTech.

@Override
public TechResults rollTech(final int techRolls, final TechnologyFrontier techToRollFor, final int newTokens, final IntegerMap<PlayerID> whoPaysHowMuch) {
    int rollCount = techRolls;
    if (isWW2V3TechModel()) {
        rollCount = newTokens;
    }
    final boolean canPay = checkEnoughMoney(rollCount, whoPaysHowMuch);
    if (!canPay) {
        return new TechResults("Not enough money to pay for that many tech rolls.");
    }
    chargeForTechRolls(rollCount, whoPaysHowMuch);
    int currTokens = 0;
    if (isWW2V3TechModel()) {
        currTokens = player.getResources().getQuantity(Constants.TECH_TOKENS);
    }
    final GameData data = getData();
    if (getAvailableTechs(player, data).isEmpty()) {
        if (isWW2V3TechModel()) {
            final Resource techTokens = data.getResourceList().getResource(Constants.TECH_TOKENS);
            final String transcriptText = player.getName() + " No more available tech advances.";
            bridge.getHistoryWriter().startEvent(transcriptText);
            final Change removeTokens = ChangeFactory.changeResourcesChange(bridge.getPlayerId(), techTokens, -currTokens);
            bridge.addChange(removeTokens);
        }
        return new TechResults("No more available tech advances.");
    }
    final String annotation = player.getName() + " rolling for tech.";
    final int[] random;
    int techHits;
    int remainder = 0;
    final int diceSides = data.getDiceSides();
    if (BaseEditDelegate.getEditMode(data)) {
        final ITripleAPlayer tripleaPlayer = getRemotePlayer();
        random = tripleaPlayer.selectFixedDice(techRolls, diceSides, true, annotation, diceSides);
        techHits = getTechHits(random);
    } else if (isLowLuckTechOnly()) {
        techHits = techRolls / diceSides;
        remainder = techRolls % diceSides;
        if (remainder > 0) {
            random = bridge.getRandom(diceSides, 1, player, DiceType.TECH, annotation);
            if (random[0] + 1 <= remainder) {
                techHits++;
            }
        } else {
            random = bridge.getRandom(diceSides, 1, player, DiceType.TECH, annotation);
            remainder = diceSides;
        }
    } else {
        random = bridge.getRandom(diceSides, techRolls, player, DiceType.TECH, annotation);
        techHits = getTechHits(random);
    }
    final boolean isRevisedModel = isWW2V2() || (isSelectableTechRoll() && !isWW2V3TechModel());
    final String directedTechInfo = isRevisedModel ? " for " + techToRollFor.getTechs().get(0) : "";
    final DiceRoll renderDice = (isLowLuckTechOnly() ? new DiceRoll(random, techHits, remainder, false) : new DiceRoll(random, techHits, diceSides - 1, true));
    bridge.getHistoryWriter().startEvent(player.getName() + (random.length > 1 ? " roll " : " rolls : ") + MyFormatter.asDice(random) + directedTechInfo + " and gets " + techHits + " " + MyFormatter.pluralize("hit", techHits), renderDice);
    if (isWW2V3TechModel() && (techHits > 0 || Properties.getRemoveAllTechTokensAtEndOfTurn(data))) {
        techCategory = techToRollFor;
        // remove all the tokens
        final Resource techTokens = data.getResourceList().getResource(Constants.TECH_TOKENS);
        final String transcriptText = player.getName() + " removing all Technology Tokens after " + (techHits > 0 ? "successful" : "unsuccessful") + " research.";
        bridge.getHistoryWriter().startEvent(transcriptText);
        final Change removeTokens = ChangeFactory.changeResourcesChange(bridge.getPlayerId(), techTokens, -currTokens);
        bridge.addChange(removeTokens);
    }
    final Collection<TechAdvance> advances;
    if (isRevisedModel) {
        if (techHits > 0) {
            advances = Collections.singletonList(techToRollFor.getTechs().get(0));
        } else {
            advances = Collections.emptyList();
        }
    } else {
        advances = getTechAdvances(techHits);
    }
    // Put in techs so they can be activated later.
    techs.put(player, advances);
    final List<String> advancesAsString = new ArrayList<>();
    int count = advances.size();
    final StringBuilder text = new StringBuilder();
    for (final TechAdvance advance : advances) {
        text.append(advance.getName());
        count--;
        advancesAsString.add(advance.getName());
        if (count > 1) {
            text.append(", ");
        }
        if (count == 1) {
            text.append(" and ");
        }
    }
    final String transcriptText = player.getName() + " discover " + text;
    if (advances.size() > 0) {
        bridge.getHistoryWriter().startEvent(transcriptText);
        // play a sound
        getSoundChannel().playSoundForAll(SoundPath.CLIP_TECHNOLOGY_SUCCESSFUL, player);
    } else {
        getSoundChannel().playSoundForAll(SoundPath.CLIP_TECHNOLOGY_FAILURE, player);
    }
    return new TechResults(random, remainder, techHits, advancesAsString, player);
}
Also used : GameData(games.strategy.engine.data.GameData) Resource(games.strategy.engine.data.Resource) ArrayList(java.util.ArrayList) Change(games.strategy.engine.data.Change) ITripleAPlayer(games.strategy.triplea.player.ITripleAPlayer) TechResults(games.strategy.triplea.delegate.dataObjects.TechResults)

Example 4 with TechResults

use of games.strategy.triplea.delegate.dataObjects.TechResults in project triplea by triplea-game.

the class RevisedTest method testTechRolls.

@Test
public void testTechRolls() {
    // Set up the test
    final PlayerID germans = GameDataTestUtil.germans(gameData);
    final ITestDelegateBridge delegateBridge = getDelegateBridge(germans);
    delegateBridge.setStepName("germanTech");
    final TechnologyDelegate techDelegate = techDelegate(gameData);
    techDelegate.setDelegateBridgeAndPlayer(delegateBridge);
    techDelegate.start();
    final TechAttachment ta = TechAttachment.get(germans);
    // PlayerAttachment pa = PlayerAttachment.get(germans);
    final TechnologyFrontier rockets = new TechnologyFrontier("", gameData);
    rockets.addAdvance(TechAdvance.findAdvance(TechAdvance.TECH_PROPERTY_ROCKETS, gameData, null));
    final TechnologyFrontier jet = new TechnologyFrontier("", gameData);
    jet.addAdvance(TechAdvance.findAdvance(TechAdvance.TECH_PROPERTY_JET_POWER, gameData, null));
    // Check to make sure it was successful
    final int initPUs = germans.getResources().getQuantity("PUs");
    // Fail the roll
    delegateBridge.setRandomSource(new ScriptedRandomSource(new int[] { 3, 4 }));
    final TechResults roll = techDelegate.rollTech(2, rockets, 0, null);
    // Check to make sure it failed
    assertEquals(0, roll.getHits());
    final int midPUs = germans.getResources().getQuantity("PUs");
    assertEquals(initPUs - 10, midPUs);
    // Make a Successful roll
    delegateBridge.setRandomSource(new ScriptedRandomSource(new int[] { 5 }));
    final TechResults roll2 = techDelegate.rollTech(1, rockets, 0, null);
    // Check to make sure it succeeded
    assertEquals(1, roll2.getHits());
    final int finalPUs = germans.getResources().getQuantity("PUs");
    assertEquals(midPUs - 5, finalPUs);
    // Test the variable tech cost
    // Make a Successful roll
    ta.setTechCost("6");
    delegateBridge.setRandomSource(new ScriptedRandomSource(new int[] { 5 }));
    final TechResults roll3 = techDelegate.rollTech(1, jet, 0, null);
    // Check to make sure it succeeded
    assertEquals(1, roll3.getHits());
    final int variablePus = germans.getResources().getQuantity("PUs");
    assertEquals(finalPUs - 6, variablePus);
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) TechAttachment(games.strategy.triplea.attachments.TechAttachment) TechResults(games.strategy.triplea.delegate.dataObjects.TechResults) ITestDelegateBridge(games.strategy.engine.data.ITestDelegateBridge) TechnologyFrontier(games.strategy.engine.data.TechnologyFrontier) ScriptedRandomSource(games.strategy.engine.random.ScriptedRandomSource) Test(org.junit.jupiter.api.Test)

Aggregations

TechResults (games.strategy.triplea.delegate.dataObjects.TechResults)4 PlayerID (games.strategy.engine.data.PlayerID)3 ITestDelegateBridge (games.strategy.engine.data.ITestDelegateBridge)2 TechnologyFrontier (games.strategy.engine.data.TechnologyFrontier)2 ScriptedRandomSource (games.strategy.engine.random.ScriptedRandomSource)2 Test (org.junit.jupiter.api.Test)2 Change (games.strategy.engine.data.Change)1 GameData (games.strategy.engine.data.GameData)1 Resource (games.strategy.engine.data.Resource)1 TechAttachment (games.strategy.triplea.attachments.TechAttachment)1 TechRoll (games.strategy.triplea.delegate.dataObjects.TechRoll)1 ITechDelegate (games.strategy.triplea.delegate.remote.ITechDelegate)1 ITripleAPlayer (games.strategy.triplea.player.ITripleAPlayer)1 ArrayList (java.util.ArrayList)1