Advertisement
Guest User

server main

a guest
May 27th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.63 KB | None | 0 0
  1. package ch.epfl.xblast.server;
  2.  
  3. import java.io.IOException;
  4. import java.net.InetSocketAddress;
  5. import java.net.SocketAddress;
  6. import java.net.StandardProtocolFamily;
  7. import java.nio.ByteBuffer;
  8. import java.nio.channels.*;
  9. import java.util.ArrayList;
  10. import java.util.HashMap;
  11. import java.util.HashSet;
  12. import java.util.Map;
  13. import java.util.Objects;
  14. import java.util.Optional;
  15. import java.util.Set;
  16.  
  17. import ch.epfl.xblast.server.GameStateSerializer;
  18. import ch.epfl.xblast.server.Level;
  19. import ch.epfl.xblast.server.graphics.BoardPainter;
  20.  
  21. import java.util.List;
  22.  
  23. import ch.epfl.xblast.Direction;
  24. import ch.epfl.xblast.PlayerAction;
  25. import ch.epfl.xblast.PlayerID;
  26. import ch.epfl.xblast.Time;
  27.  
  28. /**
  29. *
  30. * @author Amine Chaouachi (260709) / Alban Favre (260025)
  31. *
  32. */
  33. public class Main {
  34.  
  35. private static int NECES_PLAYERS = 4;
  36. private static int MAXIMUM_SERIALISED_BYTE = 420;
  37. static private Map<SocketAddress, PlayerID> addressID = new HashMap<>();
  38. static private BoardPainter bp = Level.DEFAULT_LEVEL.boardPainter();
  39. static private GameState gs = Level.DEFAULT_LEVEL.gameState();
  40.  
  41. /**
  42. * main method, useful for the program's correct functioning
  43. *
  44. * @param args
  45. * numbers of players
  46. * @throws InterruptedException
  47. * when interruptions occur
  48. */
  49. public static void main(String args[]) throws InterruptedException {
  50.  
  51. Map<PlayerID, Optional<Direction>> speedChangeEvents = new HashMap<>();
  52. Set<PlayerID> bombDropEvents = new HashSet<>();
  53.  
  54.  
  55.  
  56. // 1st Phase;
  57. Map<PlayerID, PlayerAction> whatToDo = new HashMap<>();
  58.  
  59. if (args.length != 0)
  60. NECES_PLAYERS = Integer.parseInt(args[0]);
  61.  
  62. try {
  63.  
  64. DatagramChannel channel = DatagramChannel
  65. .open(StandardProtocolFamily.INET);
  66. channel.bind(new InetSocketAddress(2016));
  67.  
  68. ByteBuffer buffer = ByteBuffer.allocate(1);
  69. SocketAddress senderAddress;
  70.  
  71. while (addressID.size() < NECES_PLAYERS) {
  72. System.out.println("while");
  73. buffer.clear();
  74. senderAddress = channel.receive(buffer);
  75. System.out.println("received");
  76. buffer.flip();
  77. if (!(addressID.containsKey(senderAddress))
  78. && buffer.get(0) == 0) {
  79.  
  80. System.out.println("if");
  81. addressID.put(senderAddress,
  82. PlayerID.values()[addressID.size()]);
  83.  
  84. }
  85. System.out.println("after if");
  86.  
  87. }
  88. System.out.println(addressID);
  89.  
  90. // phase 2 part giving gameState
  91. List<Byte> dfBytes = new ArrayList<>();
  92. dfBytes.addAll(GameStateSerializer.serialize(bp, gs));
  93.  
  94.  
  95. channel.configureBlocking(false);
  96. ByteBuffer b = ByteBuffer.allocate(MAXIMUM_SERIALISED_BYTE);
  97.  
  98. while (!gs.isGameOver()) {
  99.  
  100. System.out.println("game not over");
  101. for (SocketAddress s : addressID.keySet()) {
  102. b.clear();
  103.  
  104.  
  105. dfBytes.add(0,(byte)(addressID.get(s).ordinal()));
  106. System.out.println(dfBytes);
  107. /*
  108. * we add 1 to avoid having 0 in buffer.get(0)
  109. */
  110.  
  111. for (Byte e : dfBytes)
  112. b.put(e);
  113.  
  114. b.flip();
  115. channel.send(b, s);
  116. System.out.println("stuff sent");
  117. b.rewind();
  118. }
  119.  
  120. long beginning = System.nanoTime();
  121. long nextTick = beginning;
  122. // phase 2 part time
  123. nextTick += Ticks.TICK_NANOSECOND_DURATION;
  124.  
  125. if (nextTick > System.nanoTime())
  126. Thread.sleep((nextTick - System.nanoTime()) / Time.US_PER_S,
  127. (int) (nextTick - System.nanoTime())
  128. % Time.US_PER_S);
  129.  
  130. // phase 2 part receive infos
  131. SocketAddress s=null;
  132. while (Objects.nonNull(s=channel.receive(buffer))) {
  133.  
  134.  
  135.  
  136. PlayerAction a =PlayerAction.values()[buffer.get(0)];
  137. PlayerID pp=addressID.get(s);
  138. System.out.printf("playerID %s action %s\n",pp,a);
  139. whatToDo.put(pp,
  140. a);
  141. buffer.clear();
  142. }
  143.  
  144. // phase 2 part Compute nextGamestate
  145. for (PlayerID ID : whatToDo.keySet()) {
  146. if (whatToDo.get(ID).isMovementInput())
  147. speedChangeEvents.put(ID, Optional.ofNullable(
  148. whatToDo.get(ID).equivalentDirection()));
  149.  
  150. else if (whatToDo.get(ID).equals(PlayerAction.DROP_BOMB)) {
  151. bombDropEvents.add(ID);
  152. }
  153. //and else do nothing
  154. }
  155. gs = gs.next(speedChangeEvents, bombDropEvents);
  156. dfBytes = GameStateSerializer.serialize(bp, gs);
  157. whatToDo.clear();
  158. bombDropEvents.clear();
  159.  
  160. }
  161. System.out.println(gs.winner().isPresent()
  162. ? "winner is player: " + gs.winner() : "Draw");
  163.  
  164. } catch (IOException e) {
  165. e.printStackTrace();
  166. }
  167.  
  168. }
  169.  
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement