Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.50 KB | None | 0 0
  1. package fi.utu.tech.distributed.gorilla.logic;
  2.  
  3. import java.net.InetAddress;
  4. import java.net.UnknownHostException;
  5.  
  6. import fi.utu.tech.distributed.gorilla.logic.mesh.InfoPacket;
  7. import fi.utu.tech.distributed.gorilla.logic.mesh.Mesh;
  8. import fi.utu.tech.distributed.gorilla.logic.mesh.MovementPacket;
  9. import fi.utu.tech.distributed.gorilla.logic.mesh.Packet;
  10. import fi.utu.tech.distributed.gorilla.views.MainCanvas;
  11. import javafx.application.Platform;
  12.  
  13. /**
  14. * Luokka, johon toteutetaan GorillaPelin moninplein logiikka.
  15. */
  16. public class GorillaLogicMultiplayer extends GorillaLogic {
  17.  
  18. // Moninpeliin vaadittavan Mesh-verkon luokka.
  19. private Mesh mesh;
  20.  
  21. // Grafiikoille tärkeä olio.
  22. public MainCanvas mainCanvas = new MainCanvas();
  23.  
  24. // Asetetaan peliin liittyvien pelaajien maksimilukumäärä.
  25. public final int maxPlayers = 6;
  26.  
  27. // Yhdistettävän koneen IP ja portti
  28. String friendIP;
  29. String friendPort;
  30.  
  31. boolean ipinputmode = false;
  32.  
  33. @Override
  34. protected void handleMultiplayer() {
  35. ipinputmode = true;
  36. }
  37.  
  38. protected void parseConnectionInfo(String cmd) throws IllegalArgumentException {
  39. String[] info = new String[2];
  40. int count = 0;
  41.  
  42.  
  43. for (int j = 0; j < cmd.length(); j++) {
  44. if (cmd.charAt(j) == ' ') {
  45. count++;
  46. }
  47. }
  48.  
  49. if (count != 1) {
  50. throw new IllegalArgumentException();
  51. }
  52.  
  53. for (int i = 0; i < cmd.length(); i++) {
  54. if (cmd.charAt(i) == ' ') {
  55. info[0] = cmd.substring(0, i);
  56. info[1] = cmd.substring(i, cmd.length());
  57. }
  58. }
  59. friendIP = info[0];
  60. friendPort = info[1];
  61.  
  62. inforead();
  63. }
  64.  
  65. @Override
  66. public void parseCommandLine(String cmd) {
  67. if (cmd.contains(" ")) {
  68. String rest = cmd.substring(cmd.split(" ")[0].length() + 1);
  69. switch (cmd.split(" ")[0]) {
  70. case "q":
  71. case "quit":
  72. case "exit":
  73. Platform.exit();
  74. break;
  75. case "name":
  76. handleNameChange(rest);
  77. break;
  78. case "s":
  79. case "chat":
  80. case "say":
  81. handleChatMessage(new ChatMessage(myName, "all", rest));
  82. break;
  83. case "a":
  84. case "k":
  85. case "angle":
  86. case "kulma":
  87. if (gameMode != GameMode.Game)
  88. return;
  89. try {
  90. double angle = Double.parseDouble(rest);
  91. MoveThrowBanana mtb = new MoveThrowBanana(angle, Double.NaN);
  92. handleThrowBanana(mtb);
  93. System.out.println("Asetettu kulma: " + angle);
  94. } catch (NumberFormatException e) {
  95. System.out.println("Virheellinen komento, oikea on: angle <liukuluku -45..225>");
  96. } catch (IllegalArgumentException e) {
  97. System.out.println(e.getMessage());
  98. }
  99. break;
  100. case "v":
  101. case "n":
  102. case "velocity":
  103. case "nopeus":
  104. if (gameMode != GameMode.Game)
  105. return;
  106. try {
  107. double velocity = Double.parseDouble(rest);
  108. MoveThrowBanana mtb = new MoveThrowBanana(Double.NaN, velocity);
  109. handleThrowBanana(mtb);
  110. System.out.println("Asetettu nopeus: " + velocity);
  111. } catch (NumberFormatException e) {
  112. System.out.println("Virheellinen komento, oikea on: velocity <liukuluku 0..150>");
  113. } catch (IllegalArgumentException e) {
  114. System.out.println(e.getMessage());
  115. }
  116. break;
  117. }
  118. }
  119.  
  120. if (Character.isDigit(cmd.charAt(0)) && ipinputmode == true) {
  121.  
  122. try{
  123. parseConnectionInfo(cmd);
  124. } catch (IllegalArgumentException e){
  125. System.out.println("Virheellinen IP ja portti.");
  126. }
  127.  
  128. //connectToServer(friendIP, friendPort); //kaverin kone
  129. try {
  130. connectToServer(InetAddress.getLocalHost().getHostAddress(), "5000"); //oma kone
  131. } catch (UnknownHostException e) {
  132. System.out.println("Ongelma switchissä!!!!!!");
  133. e.printStackTrace();
  134. }
  135. ipinputmode = false;
  136. }
  137.  
  138. else {
  139. switch (cmd) {
  140.  
  141. case "test":
  142. System.out.println("TOIMI NYT PERRRRRRRRRRRRRRRKELE!!!!");
  143. break;
  144.  
  145. case "inforead":
  146. inforead();
  147. break;
  148.  
  149. case "infoput":
  150. handleMultiplayer();
  151. break;
  152. }
  153. }
  154. }
  155.  
  156. public void inforead() {
  157. System.out.println("FriendIP on: " + friendIP);
  158. System.out.println("FriendPort on: " + friendPort);
  159. }
  160.  
  161. @Override
  162. public void startServer(String port) {
  163. try {
  164. this.mesh = new Mesh(5000, InetAddress.getLocalHost().getHostAddress());
  165. } catch (UnknownHostException e) {
  166. System.out.println("Ongelma startServerissä!!!");
  167. e.printStackTrace();
  168. }
  169. mesh.setLogic(this);
  170. mesh.setName("Mää");
  171. mesh.run();
  172. }
  173.  
  174. @Override
  175. public void connectToServer(String address, String port){
  176. mesh.connect(address, Integer.parseInt(port));
  177. }
  178.  
  179. @Override
  180. public void handleNameChange(String newName){
  181. myName = newName;
  182. mesh.setName(myName);
  183. }
  184.  
  185. @Override
  186. public void handleChatMessage(ChatMessage msg){
  187. System.out.printf("Sinä sanot: %s%n", msg.contents);
  188. mesh.broadcast((Packet) msg);
  189.  
  190. }
  191.  
  192. public void handlePacket(Packet p){
  193. if (p instanceof ChatMessage){
  194. ChatMessage msg = (ChatMessage) p;
  195. System.out.println(msg.getName() + " sanoo: " + msg.contents);
  196. }
  197. else if (p instanceof MovementPacket){}
  198.  
  199. else if (p instanceof InfoPacket){
  200. InfoPacket info = (InfoPacket) p;
  201.  
  202. GameState newGameState = info.getGameState();
  203. setMode(GameMode.Game);
  204.  
  205.  
  206.  
  207. }
  208. }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement