Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. package com.drpicox.fishingLagoon.phase2.presentation;
  2.  
  3. import com.drpicox.fishingLagoon.bots.BotId;
  4. import com.drpicox.fishingLagoon.client.ClientRound;
  5. import com.drpicox.fishingLagoon.client.FishingLagoonClient;
  6. import com.drpicox.fishingLagoon.strategy.Strategy;
  7.  
  8. import static java.util.Arrays.asList;
  9.  
  10. public class RemoteCombatController implements Runnable {
  11.  
  12. private static final long SECOND = 1000L;
  13.  
  14. private Strategy strategy;
  15. private FishingLagoonClient remoteServer;
  16. private BotId botId;
  17.  
  18. public RemoteCombatController(Strategy strategy, FishingLagoonClient remoteServer) {
  19. this.strategy = strategy;
  20. this.remoteServer = remoteServer;
  21. }
  22.  
  23. public void run() {
  24. botId = fetchBotIdFromRemoteServer();
  25. while (true) {
  26. try {
  27. makeStrategyCombatIntoRemoteRound();
  28. waitForBeforeNewRound();
  29. } catch (Exception e) {
  30. //e.printStackTrace();
  31. }
  32. }
  33. }
  34.  
  35. private void waitForBeforeNewRound() throws InterruptedException {
  36. Thread.sleep(1L * SECOND);
  37. }
  38.  
  39. private BotId fetchBotIdFromRemoteServer() {
  40.  
  41. return new BotId(remoteServer.getBot().getId());
  42.  
  43. }
  44.  
  45. private void makeStrategyCombatIntoRemoteRound() throws InterruptedException {
  46. var roundId = getActiveRoundOrCreate().getId();
  47.  
  48. Thread.sleep(SECOND/2); //Esperem una mica a sentar-nos perq tots els bots s'asseguin i nosaltres poguem triar on anar
  49. var roundValueSeated = seatStrategyIntoRemoteRound(roundId);
  50. waitForRemoteRoundSeatPhaseEnd(roundValueSeated);
  51.  
  52. var roundValueAfterCommand = sendStrategyCommandToRemoteRound(roundId);
  53. waitForRemoteRoundCommandPhaseEnd(roundValueAfterCommand);
  54.  
  55. var roundFinalValue = makeStrategyLearnFromRemoteRound(roundId);
  56. System.out.println(roundFinalValue);
  57. waitForRemoteRoundEnd(roundFinalValue);
  58. }
  59.  
  60. private ClientRound getActiveRoundOrCreate() {
  61. var activeRounds = remoteServer.listActiveRounds();
  62. if (!activeRounds.isEmpty()) {
  63. if (!activeRounds.get(0).getState().equalsIgnoreCase("SEATING")) {
  64. System.out.println("-Waiting for the Round to finish");
  65. var timing = activeRounds.get(0).getMillisecondsForEnd();
  66. try {
  67. System.out.print("-Thread goes sleeping for: " + timing / 1000 + " seconds\n");
  68. Thread.sleep(timing);
  69. } catch (InterruptedException e) {
  70. e.printStackTrace();
  71. }
  72.  
  73. } else {
  74. return activeRounds.get(0).withSelfId(botId.getValue());
  75. }
  76. }
  77.  
  78. return remoteServer.createRound(String.join("\n", "",
  79. "maxDensity=3.0",
  80. "weekCount=10",
  81. "lagoons=lagoonSmall,lagoonBig",
  82. "lagoonSmall.fishPopulation=100",
  83. "lagoonBig.fishPopulation=200"
  84. ));
  85. }
  86.  
  87. private ClientRound seatStrategyIntoRemoteRound(String roundId) throws InterruptedException {
  88.  
  89. System.out.println("SEATING");
  90. var roundValueWithSomeSeats = remoteServer.getRound(roundId);
  91. var lagoonIndex = strategy.seat(botId, roundValueWithSomeSeats.getLagoonRound());
  92. var roundValueSeated = remoteServer.seat(roundId, lagoonIndex);
  93.  
  94. return roundValueSeated;
  95. }
  96.  
  97. private ClientRound sendStrategyCommandToRemoteRound(String roundId) {
  98. System.out.println("COMMANDING");
  99. var roundValueWithAllSeats = remoteServer.getRound(roundId);
  100.  
  101. var actions = strategy.getOrders(botId, roundValueWithAllSeats.getLagoonRound());
  102. var roundValueAfterCommand = remoteServer.command(roundValueWithAllSeats.getId(), asList(actions));
  103.  
  104. return roundValueAfterCommand;
  105. }
  106.  
  107. private ClientRound makeStrategyLearnFromRemoteRound(String roundId) {
  108. System.out.println("SCORING");
  109. var roundFinalValue = remoteServer.getRound(roundId);
  110.  
  111. strategy.learnFromRound(botId, roundFinalValue.getLagoonRound());
  112.  
  113. return roundFinalValue;
  114. }
  115. private void waitForRemoteRoundSeatPhaseStart(ClientRound roundValue) throws InterruptedException {
  116. Thread.sleep(roundValue.getStartTs()-SECOND/2);
  117. }
  118.  
  119.  
  120. private void waitForRemoteRoundSeatPhaseEnd(ClientRound roundValue) throws InterruptedException {
  121. Thread.sleep(roundValue.getMillisecondsForEndSeat());
  122. }
  123.  
  124. private void waitForRemoteRoundCommandPhaseEnd(ClientRound roundValue) throws InterruptedException {
  125. Thread.sleep(roundValue.getMillisecondsForEndCommand());
  126. }
  127.  
  128. private void waitForRemoteRoundEnd(ClientRound roundValue) throws InterruptedException {
  129. Thread.sleep(roundValue.getMillisecondsForEnd());
  130. }
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement