Guest User

Untitled

a guest
Jul 19th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.53 KB | None | 0 0
  1. import processing.net.*;
  2.  
  3. Server s;
  4. Client c;
  5. boolean Stopped; // Has the server been stopped yet?
  6. boolean HostJY,TwoJY,ThreeJY,FourJY; // Has the Host Joined Yet?
  7.  
  8. // Inputs
  9. String input;
  10. int[] data = new int[8]; // 8 For 9 different pieces of info.
  11. String[] pname;
  12.  
  13. //Minimap Vars
  14. int HostX,HostY,TwoX,TwoY,ThreeX,ThreeY,FourX,FourY;
  15. PImage m;
  16.  
  17. //Disconnecting Lost Clients
  18. int HostTime,TwoTime,ThreeTime,FourTime,Time;
  19.  
  20. // Scorekeeping
  21. int Team1Score,Team2Score;
  22.  
  23. void setup() {
  24. size(500, 250);
  25. background(204);
  26. stroke(0);
  27. // frameRate(5); // Slow it down a little
  28. s = new Server(this, 8998); // Start a simple server on a port
  29. Stopped = false;
  30. HostJY = false;
  31. TwoJY = false;
  32. ThreeJY = false;
  33. FourJY = false;
  34. HostX = 250;
  35. HostY = 125;
  36. TwoX = 250;
  37. TwoY = 125;
  38. ThreeX = 250;
  39. ThreeY = 125;
  40. FourX = 250;
  41. FourY = 150;
  42. smooth();
  43. m = loadImage("Online2.bmp", "bmp");
  44. image(m,0,0);
  45. }
  46.  
  47. void draw() {
  48. Time++;
  49. // print(Time + "\n");
  50. if ((mousePressed == true) && (Stopped == false)) {
  51. s.stop(); // If Mouse clicks, Server Stops Serving.
  52. print("Stopped.");
  53. Stopped = true;
  54. }
  55. image(m,0,0);
  56. if (HostJY == true) {
  57. person(HostX, HostY);
  58. }
  59. if (TwoJY == true) {
  60. person(TwoX, TwoY);
  61. }
  62. if (ThreeJY == true) {
  63. person(ThreeX, ThreeY);
  64. }
  65. if (FourJY == true) {
  66. person(FourX, FourY);
  67. }
  68.  
  69. // Receive data from client
  70. if (Stopped == false) { // If server hasn't stopped yet,
  71. c = s.available(); // Then c is the next Client
  72.  
  73. if (c != null) { // If c exists, go forward.
  74. input = c.readString(); // Read what c has to say.
  75. if (input.indexOf("\n") > 0) {
  76. input = input.substring(0, input.indexOf("\n")); // Read only up to the newline.
  77. data = int(split(input, ' ')); // Split c's values into an array by the spaces.
  78. pname = split(input, ' ');
  79. for (int i=0; i<=data.length; i++) { // Take however many things c said and shove them into the table "data".
  80. }
  81. if (data[0] == 33) { // If client throws a 33 (request for Hostship) at me
  82. if (HostJY == false) { // and the Host hasn't joined yet
  83. s.write("Host \n"); // I'll send back a "Host" to say he's the Host.
  84. HostJY = true; // And I'll tell myself the host has joined.
  85. HostTime = Time;
  86. print("Host Joined \n");
  87. }
  88. else if (TwoJY == false) { // otherwise
  89. s.write(2+"\n");
  90. TwoJY = true;
  91. TwoTime = Time;
  92. print("Player " + 2 + " Joined. \n");
  93. }
  94. else if (ThreeJY == false) {
  95. s.write(3+"\n");
  96. ThreeJY = true;
  97. ThreeTime = Time;
  98. print("Player " + 3 + " Joined. \n");
  99. }
  100. else if (FourJY == false) {
  101. s.write(4+"\n");
  102. FourJY = true;
  103. FourTime = Time;
  104. print("Player " + 4 + " Joined. \n");
  105. }
  106. else {
  107. s.write("Server Full \n");
  108. // s.disconnect(c);
  109. }
  110. }
  111. if ((data[0] == 50) && (data.length == 2)) {
  112. if ((data[1] == 1) || (data[1] == 3)) {
  113. Team1Score++;
  114. s.write(42 + "," + Team1Score + "\n");
  115. // print(Team1Score);
  116. } else if ((data[1] == 2) || (data[1] == 4)) {
  117. Team2Score++;
  118. s.write(43 + "," + Team2Score + "\n");
  119. // print(Team2Score);
  120. }
  121. }
  122. if ((data[0] == 60) && (data.length == 2)) {
  123. s.write((data[1] + 100) + "\n");
  124. print("BALLS \n");
  125. }
  126. if ((data[0] == 40) && (data.length == 2)) { // If client throws a 33 (request for Hostship) at me
  127. // print("Someone Left");
  128. if ((HostJY == true) && (data[1] == 1)) { //and the Host hasn't joined yet
  129. // s.write("Host \n"); // I'll send back a "Host" to say he's the Host.
  130. HostJY = false; // And I'll tell myself the host has joined.
  131. print("Host Left. \n");
  132. s.disconnect(c);
  133. s.write(5+"\n");
  134. }
  135. else if ((TwoJY == true) && (data[1] == 2)) { // otherwise
  136. // s.write(2+"\n");
  137. TwoJY = false;
  138. print("Player " + 2 + " Left. \n");
  139. s.disconnect(c);
  140. s.write(6+"\n");
  141. }
  142. else if ((ThreeJY == true) && (data[1] == 3)) {
  143. // s.write(3+"\n");
  144. ThreeJY = false;
  145. print("Player " + 3 + " Left. \n");
  146. s.disconnect(c);
  147. s.write(7+"\n");
  148. }
  149. else if ((FourJY == true) && (data[1] == 4)) {
  150. // s.write(4+"\n");
  151. FourJY = false;
  152. print("Player " + 4 + " Left. \n");
  153. s.disconnect(c);
  154. s.write(8+"\n");
  155. // print("Player Joined");
  156. }
  157. }
  158. if ((HostJY == true) && (Time - HostTime >= 500)) { //and the Host hasn't joined yet
  159. // s.write("Host \n"); // I'll send back a "Host" to say he's the Host.
  160. HostJY = false; // And I'll tell myself the host has joined.
  161. print("Host Timed Out. \n");
  162. // s.disconnect(c);
  163. s.write(5+"\n");
  164. }
  165. else if ((TwoJY == true) && (Time - TwoTime >= 500)) { // otherwise
  166. // s.write(2+"\n");
  167. TwoJY = false;
  168. print("Player " + 2 + " Timed Out. \n");
  169. // s.disconnect(c);
  170. s.write(6+"\n");
  171. }
  172. else if ((ThreeJY == true) && (Time - ThreeTime >= 500)) {
  173. // s.write(3+"\n");
  174. ThreeJY = false;
  175. print("Player " + 3 + " Timed Out. \n");
  176. // s.disconnect(c);
  177. s.write(7+"\n");
  178. }
  179. else if ((FourJY == true) && (Time - FourTime >= 500)) {
  180. // s.write(4+"\n");
  181. FourJY = false;
  182. print("Player " + 4 + " Timed out. \n");
  183. // s.disconnect(c);
  184. s.write(8+"\n");
  185. // print("Player Joined");
  186. }
  187.  
  188. if ((data[0] > 0) && (data[0] <= 4) && (data.length == 10)) {
  189. // 0 Is the Player ID, 1 is the X Position, 2 is the Y Position, 3 is the Velocity X, 4 is the Velocity Y, 5 is the HFlipped, 6 is ViewAngle, 7 is the Health, 8 is whether or not he's firing, 9 is their name.
  190. s.write(data[0] + "," + data[1] + "," + data[2] + "," + data[3] + "," + data[4] + "," + data[5] + "," + data[6] + "," + data[7] + "," + data[8] + "," + pname[9] + "\n");
  191. if (data[0] == 1) { //and the Host hasn't joined yet
  192. HostX = ceil(data[1]/4);
  193. HostY = ceil(data[2]/4);
  194. HostTime = Time;
  195. }
  196. else if (data[0] == 2) { // otherwise
  197. TwoX = ceil(data[1]/4);
  198. TwoY = ceil(data[2]/4);
  199. TwoTime = Time;
  200. }
  201. else if (data[0] == 3) {
  202. ThreeX = ceil(data[1]/4);
  203. ThreeY = ceil(data[2]/4);
  204. ThreeTime = Time;
  205. }
  206. else if (data[0] == 4) {
  207. FourX = ceil(data[1]/4);
  208. FourY = ceil(data[2]/4);
  209. FourTime = Time;
  210. }
  211. }
  212. }
  213. }
  214. }
  215. }
  216.  
  217. void person(float x, float y) {
  218. ellipse(x,y,10,10);
  219. }
Add Comment
Please, Sign In to add comment