Advertisement
Guest User

Untitled

a guest
Mar 24th, 2013
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.76 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.io.PrintWriter;
  5. import java.net.ServerSocket;
  6. import java.net.Socket;
  7. import java.net.SocketTimeoutException;
  8. import java.util.ArrayList;
  9. import java.util.Random;
  10. import java.util.regex.Pattern;
  11.  
  12. public class Server extends Thread {
  13.  
  14. protected static boolean serverContinue = true;
  15. protected static int clientAmount = 0;
  16. protected static ArrayList<Server> servers = new ArrayList<Server>(24);
  17. protected Socket clientSocket;
  18. protected PrintWriter out;
  19. protected BufferedReader in;
  20. protected int ID = 0, points = 0, plays = 0, streak = 0;
  21.  
  22. public static void main(String[] args) throws IOException {
  23. ServerSocket serverSocket = null;
  24.  
  25. try {
  26. serverSocket = new ServerSocket(10008);
  27. System.out.println("Connection Socket Created");
  28. try {
  29. while (serverContinue) {
  30. serverSocket.setSoTimeout(10000);
  31. System.out.println("Waiting for Connection");
  32. try {
  33. Server serv = new Server(serverSocket.accept());
  34. servers.add(serv);
  35. castServerMessage("User #" + clientAmount
  36. + " has joined the session");
  37. } catch (SocketTimeoutException ste) {
  38. System.out.println("Timeout Occurred");
  39. }
  40. }
  41. } catch (IOException e) {
  42. System.err.println("Accept failed.");
  43. System.exit(1);
  44. }
  45. } catch (IOException e) {
  46. System.err.println("Could not listen on port: 10008.");
  47. System.exit(1);
  48. } finally {
  49. try {
  50. System.out.println("Closing Server Connection Socket");
  51. serverSocket.close();
  52. } catch (IOException e) {
  53. System.err.println("Could not close port: 10008.");
  54. System.exit(1);
  55. }
  56. }
  57. }
  58.  
  59. public static void castUserMessage(String message, int ID) {
  60. castMessage("User #" + ID, message);
  61. }
  62.  
  63. public static void castServerMessage(String message) {
  64. castMessage("Server", message);
  65. }
  66.  
  67. public static void castAdminMessage(String message) {
  68. castMessage("Admin", message);
  69. }
  70.  
  71. public static void castMessage(String sender, String message) {
  72. for (Server serv : servers) {
  73. if (serv.out != null)
  74. serv.out.println("@" + sender + ": " + message);
  75. }
  76. }
  77.  
  78. private Server(Socket clientSoc) {
  79. clientSocket = clientSoc;
  80. start();
  81. }
  82.  
  83. public void run() {
  84. System.out.println("New Communication Thread Started");
  85.  
  86. ID = clientAmount++;
  87.  
  88. try {
  89. out = new PrintWriter(clientSocket.getOutputStream(), true);
  90. in = new BufferedReader(new InputStreamReader(
  91. clientSocket.getInputStream()));
  92.  
  93. String inputLine;
  94.  
  95. int r = 0;
  96. boolean hasRequested = false;
  97.  
  98. while ((inputLine = in.readLine()) != null) {
  99. if (inputLine.equals("quit")) {
  100. out.println("@Server: Bye!");
  101. break;
  102. }
  103.  
  104. if (inputLine.equals("?")) {
  105. out.println("\"quit\" ends connection");
  106. out.println("\"request\" requests equation");
  107. out.println("\"points\" shows points");
  108. out.println("\"users\" shows people playing");
  109. out.println("\"msg [message]\" casts a message to all players");
  110. out.println("You simply type the result of the equation you've been given to answer it");
  111. out.println("Answering correctly will result in streaks which will give you bonus points");
  112. continue;
  113. }
  114.  
  115. if (inputLine.startsWith("msg ")) {
  116. if (inputLine.length() > 5) {
  117. castUserMessage(
  118. inputLine.substring(4, inputLine.length()), ID);
  119. } else {
  120. out.println("@Syntax: msg [message]");
  121. }
  122. continue;
  123. }
  124.  
  125. if (inputLine.startsWith("adm ")) {
  126. if (inputLine.length() > 5) {
  127. castAdminMessage(inputLine.substring(4,
  128. inputLine.length()));
  129. } else {
  130. out.println("@Syntax: adm [message]");
  131. }
  132. continue;
  133. }
  134.  
  135. if (inputLine.equals("points")) {
  136. out.println("@Server: You have " + points + " points and "
  137. + plays + " requests!");
  138. continue;
  139. }
  140.  
  141. if (inputLine.equals("users")) {
  142. if (clientAmount == 1) {
  143. out.println("@Server: You are alone here!");
  144. } else {
  145. out.println("@Server: There are " + clientAmount
  146. + " people playing!");
  147. }
  148. int count = 0;
  149. for (Server serv : servers) {
  150. count++;
  151. out.println("@Server: User #" + serv.ID + " with "
  152. + serv.points + " points and " + serv.plays
  153. + " requests");
  154. }
  155. continue;
  156. }
  157.  
  158. if (inputLine.equals("request")) {
  159. if (hasRequested) {
  160. out.println("@Server: You already have a request!");
  161. } else {
  162. Random rng = new Random();
  163. int a = 50 - rng.nextInt(101), b = 50 - rng
  164. .nextInt(101);
  165. r = a + b;
  166. hasRequested = true;
  167. plays++;
  168. out.println("@Server: " + a + " + " + b + " = ?");
  169. }
  170. continue;
  171. }
  172.  
  173. if (inputLine.equals("" + r)) {
  174. if (hasRequested) {
  175. hasRequested = false;
  176. points += 2 + streak;
  177. out.println("@Server: Correct! +2 points"
  178. + (streak > 0 ? " and +" + streak
  179. + " more for being on a streak!" : ""));
  180. streak++;
  181. } else {
  182. out.println("@Server: That's the answer before!");
  183. }
  184. continue;
  185. } else if (!inputLine.equals("" + r)
  186. && Pattern.matches("-?[0-9]+", inputLine)
  187. && hasRequested) {
  188. points--;
  189. hasRequested = false;
  190. out.println("@Server: Wrong answer! -1 points"
  191. + (streak > 0 ? ", you also aren't on a streak anymore"
  192. : ""));
  193. streak = 0;
  194. continue;
  195. }
  196. }
  197.  
  198. clientAmount--;
  199.  
  200. servers.remove(this);
  201.  
  202. out.close();
  203. in.close();
  204. clientSocket.close();
  205.  
  206. castServerMessage("User #" + ID + " has quit the session");
  207. } catch (IOException e) {
  208. System.err.println("Problem with Communication Server");
  209. System.exit(1);
  210. }
  211. }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement