Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.drpicox.fishingLagoon.phase2.presentation;
- import com.drpicox.fishingLagoon.bots.BotId;
- import com.drpicox.fishingLagoon.client.ClientRound;
- import com.drpicox.fishingLagoon.client.FishingLagoonClient;
- import com.drpicox.fishingLagoon.strategy.Strategy;
- import static java.util.Arrays.asList;
- public class RemoteCombatController implements Runnable {
- private static final long SECOND = 1000L;
- private Strategy strategy;
- private FishingLagoonClient remoteServer;
- private BotId botId;
- public RemoteCombatController(Strategy strategy, FishingLagoonClient remoteServer) {
- this.strategy = strategy;
- this.remoteServer = remoteServer;
- }
- public void run() {
- botId = fetchBotIdFromRemoteServer();
- while (true) {
- try {
- makeStrategyCombatIntoRemoteRound();
- waitForBeforeNewRound();
- } catch (Exception e) {
- //e.printStackTrace();
- }
- }
- }
- private void waitForBeforeNewRound() throws InterruptedException {
- Thread.sleep(1L * SECOND);
- }
- private BotId fetchBotIdFromRemoteServer() {
- return new BotId(remoteServer.getBot().getId());
- }
- private void makeStrategyCombatIntoRemoteRound() throws InterruptedException {
- var roundId = getActiveRoundOrCreate().getId();
- Thread.sleep(SECOND/2); //Esperem una mica a sentar-nos perq tots els bots s'asseguin i nosaltres poguem triar on anar
- var roundValueSeated = seatStrategyIntoRemoteRound(roundId);
- waitForRemoteRoundSeatPhaseEnd(roundValueSeated);
- var roundValueAfterCommand = sendStrategyCommandToRemoteRound(roundId);
- waitForRemoteRoundCommandPhaseEnd(roundValueAfterCommand);
- var roundFinalValue = makeStrategyLearnFromRemoteRound(roundId);
- System.out.println(roundFinalValue);
- waitForRemoteRoundEnd(roundFinalValue);
- }
- private ClientRound getActiveRoundOrCreate() {
- var activeRounds = remoteServer.listActiveRounds();
- if (!activeRounds.isEmpty()) {
- if (!activeRounds.get(0).getState().equalsIgnoreCase("SEATING")) {
- System.out.println("-Waiting for the Round to finish");
- var timing = activeRounds.get(0).getMillisecondsForEnd();
- try {
- System.out.print("-Thread goes sleeping for: " + timing / 1000 + " seconds\n");
- Thread.sleep(timing);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- } else {
- return activeRounds.get(0).withSelfId(botId.getValue());
- }
- }
- return remoteServer.createRound(String.join("\n", "",
- "maxDensity=3.0",
- "weekCount=10",
- "lagoons=lagoonSmall,lagoonBig",
- "lagoonSmall.fishPopulation=100",
- "lagoonBig.fishPopulation=200"
- ));
- }
- private ClientRound seatStrategyIntoRemoteRound(String roundId) throws InterruptedException {
- System.out.println("SEATING");
- var roundValueWithSomeSeats = remoteServer.getRound(roundId);
- var lagoonIndex = strategy.seat(botId, roundValueWithSomeSeats.getLagoonRound());
- var roundValueSeated = remoteServer.seat(roundId, lagoonIndex);
- return roundValueSeated;
- }
- private ClientRound sendStrategyCommandToRemoteRound(String roundId) {
- System.out.println("COMMANDING");
- var roundValueWithAllSeats = remoteServer.getRound(roundId);
- var actions = strategy.getOrders(botId, roundValueWithAllSeats.getLagoonRound());
- var roundValueAfterCommand = remoteServer.command(roundValueWithAllSeats.getId(), asList(actions));
- return roundValueAfterCommand;
- }
- private ClientRound makeStrategyLearnFromRemoteRound(String roundId) {
- System.out.println("SCORING");
- var roundFinalValue = remoteServer.getRound(roundId);
- strategy.learnFromRound(botId, roundFinalValue.getLagoonRound());
- return roundFinalValue;
- }
- private void waitForRemoteRoundSeatPhaseStart(ClientRound roundValue) throws InterruptedException {
- Thread.sleep(roundValue.getStartTs()-SECOND/2);
- }
- private void waitForRemoteRoundSeatPhaseEnd(ClientRound roundValue) throws InterruptedException {
- Thread.sleep(roundValue.getMillisecondsForEndSeat());
- }
- private void waitForRemoteRoundCommandPhaseEnd(ClientRound roundValue) throws InterruptedException {
- Thread.sleep(roundValue.getMillisecondsForEndCommand());
- }
- private void waitForRemoteRoundEnd(ClientRound roundValue) throws InterruptedException {
- Thread.sleep(roundValue.getMillisecondsForEnd());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement