Advertisement
Guest User

Untitled

a guest
May 27th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.37 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package mpogassignment01;
  6.  
  7. import java.awt.Color;
  8. import java.awt.Graphics2D;
  9. import java.awt.Graphics2D;
  10. import java.awt.Image;
  11. import java.net.ServerSocket;
  12. import java.net.InetAddress;
  13. import java.io.InterruptedIOException;
  14. import java.io.BufferedReader;
  15. import java.io.IOException;
  16. import java.io.InputStreamReader;
  17. import java.io.OutputStreamWriter;
  18. import java.io.PrintWriter;
  19. import java.net.Socket;
  20. import java.util.ArrayList;
  21. import static java.awt.event.KeyEvent.*;
  22. import javax.swing.JOptionPane;
  23.  
  24. /**
  25. *
  26. * @author jiahui
  27. */
  28. public class Assignment01 extends GameEngine {
  29.  
  30. final static int PORT = 3000;
  31.  
  32. enum State {
  33.  
  34. MENU, CONNECTING, WAITING, CHATTING
  35. };
  36. State state;
  37. Socket sock;
  38. ServerSocket servSock;
  39. //String text;
  40. BufferedReader socketIn;
  41. PrintWriter socketOut;
  42. Graphics2D g2d;
  43. ArrayList<String> messages = new ArrayList<String>();
  44.  
  45. private Image mainMenuImage;
  46.  
  47. boolean checkChoice = false;
  48.  
  49. String userName = " ";
  50.  
  51. int checkNo = 0;
  52.  
  53. int HostHealth, ClientHealth = 100;
  54. int HostExp, ClientExp = 100;
  55.  
  56. // try {
  57. // mainMenuImage = Image.createImage("/Main.png");
  58. // } catch (IOException ioe) {
  59. // System.err.println("Failed to load Main Menu Image!");
  60. // } // Load Main Menu Image
  61.  
  62. void displayMessage(String s) {
  63. if (messages.size() > 10) // if array is too full:
  64. {
  65. messages.remove(0);
  66. }
  67. messages.add(s);
  68. }
  69.  
  70. String userInput = " ";
  71. boolean processUserInput() {
  72. char c =getLastKey();
  73. if (c=='\0')
  74. return false;
  75. if ((int)c==VK_ENTER)
  76. return true; // end of entry
  77. if((int)c==VK_BACK_SPACE || (int)c==VK_DELETE)
  78. {
  79. if(!userInput.isEmpty())
  80. {
  81. userInput=userInput.substring(0, userInput.length()-1);
  82. }
  83. return false; // not yet
  84. }
  85. userInput+=c;
  86. return false;
  87. }
  88.  
  89.  
  90.  
  91. public static void main(String[] args) {
  92. new Assignment01().go(1000, 700, false);
  93. }
  94.  
  95. public void startup() {
  96. state = State.MENU;
  97. String input = JOptionPane.showInputDialog("Please Enter a Nickname!", "New Player");
  98. userName = input;
  99. }
  100.  
  101. public void update() {
  102. if (keys[VK_ESCAPE]) {
  103. close(); // close me
  104. }
  105.  
  106. if (state == State.MENU) {
  107. updateMenu();
  108. } else if (state == State.WAITING) {
  109. updateWaiting();
  110. } else if (state == State.CONNECTING) {
  111. updateConnecting();
  112. } else {
  113. updateChatting();
  114.  
  115. }
  116.  
  117. }
  118.  
  119. void updateMenu() {
  120. if (keys[VK_C]) {
  121. state = State.CONNECTING;
  122. getLastKey();
  123. userInput="localhost";
  124. //userInput="172.17.66.58"; // jiahui
  125. //userInput="172.17.73.253"; //brenda
  126. }
  127. if (keys[VK_S]) {
  128. state = State.WAITING;
  129. setupServer();
  130. }
  131. }
  132.  
  133. void updateWaiting() {
  134. try {
  135. sock = servSock.accept();
  136. sock.setSoTimeout(1);
  137. displayMessage("We have a connection from " + sock.getLocalAddress().getHostAddress() + ":" + sock.getPort());
  138. socketIn = new BufferedReader(new InputStreamReader(sock.getInputStream()));
  139. socketOut = new PrintWriter(new OutputStreamWriter(sock.getOutputStream()));
  140. // we have a connection, to change state to Chatting
  141. state = State.CHATTING;
  142. getLastKey(); // will clear the last keypress
  143. } catch (InterruptedIOException e) {
  144. } catch (IOException e) {
  145. displayMessage("Error:" + e);
  146. }
  147. }
  148.  
  149. void updateConnecting() {
  150. if (processUserInput()) { //if user pressed return
  151. //connect to this FP:
  152. try {
  153. sock = new Socket(userInput, PORT);
  154. sock.setSoTimeout(1);
  155. //TODO: set up socketIn,socketOut
  156. socketIn = new BufferedReader(new InputStreamReader(sock.getInputStream()));
  157. socketOut = new PrintWriter(new OutputStreamWriter(sock.getOutputStream()));
  158. displayMessage("connected");
  159. // we have a connection, to change state to Chatting
  160. state = State.CHATTING;
  161. userInput = "";
  162. } catch (Exception e) {
  163. displayMessage("Error:" + e);
  164. }
  165.  
  166. }
  167. }
  168.  
  169. String recvSocket() {
  170. //receive the message
  171. try {
  172. String s = socketIn.readLine();
  173. return s;
  174. } catch (InterruptedIOException e) {
  175. } catch (IOException e) {
  176. displayMessage("Error: " + e);
  177. e.printStackTrace();
  178. }
  179. return null;
  180. }
  181.  
  182. void sendSocket(String s) {
  183.  
  184. socketOut.println(s);
  185. socketOut.flush();
  186.  
  187. }
  188.  
  189.  
  190. void updateChatting() {
  191. String s = recvSocket();
  192. if (s!=null){
  193. System.out.println(s);
  194. if(s.startsWith("CHAT ")){
  195. s=s.substring(5); // chop off "CHAT"
  196. displayMessage("I got a message:" + s);
  197. }
  198. if (s.startsWith("GAME ")){
  199. s=s.substring(5); // chop off "CHAT"
  200. //TODO: do something useful with the game
  201. displayMessage("PLAYER " + s);
  202. }
  203. }
  204.  
  205. if (keys[VK_LEFT] && checkNo==0){
  206. sendSocket("GAME ATTACK LEFT");
  207. //checkChoice = true;
  208. checkNo++;
  209. }
  210. //attack
  211. if (keys[VK_RIGHT] && checkNo==0){
  212. sendSocket("GAME ATTACK RIGHT");
  213. //checkChoice = true;
  214. checkNo++;
  215. }
  216. //idle
  217. if(keys[VK_UP] && checkNo==0){
  218. sendSocket("GAME IDLE");
  219. //checkChoice = true;
  220. checkNo++;
  221. }
  222.  
  223. if (processUserInput()) { // if user pressed return
  224. if (userInput.isEmpty() == false) { // if data was entered
  225. displayMessage("Sending:" + userInput);
  226. socketOut.println("CHAT " + userInput);
  227. socketOut.flush();
  228. userInput = ""; // clear user input
  229. }
  230. }
  231.  
  232. }
  233.  
  234. public void setupServer() {
  235. try {
  236. servSock = new ServerSocket(3000);
  237. servSock.setSoTimeout(1);
  238. InetAddress local = InetAddress.getLocalHost();
  239.  
  240. displayMessage("starting....");
  241. } catch (InterruptedIOException e) {
  242. } catch (IOException er) {
  243. displayMessage("Error Found: " + er);
  244. }
  245. }
  246.  
  247. public void draw(Graphics2D g2d) {
  248. if (keys[VK_ESCAPE]) {
  249. close(); // close me
  250. }
  251. if (state == State.MENU) {
  252. drawMenu(g2d);
  253. } else if (state == State.WAITING) {
  254. drawWaiting(g2d);
  255. } else if (state == State.CONNECTING) {
  256. drawConnecting(g2d);
  257. } else {
  258. drawChatting(g2d);
  259. }
  260. }
  261.  
  262. void drawMenu(Graphics2D g2d) {
  263. clear(g2d, Color.WHITE);
  264. g2d.setColor(Color.BLACK);
  265. g2d.drawString("Press 'C' for Client, 'S' for Server", 10, 50);
  266. }
  267.  
  268. void drawWaiting(Graphics2D g2d) {
  269. clear(g2d, Color.YELLOW);
  270. g2d.setColor(Color.RED);
  271. g2d.drawString("Waiting for connection", 10, 50);
  272. for (int i = 0; i < messages.size(); i++) {
  273. g2d.drawString(messages.get(i), 10, 60 + 10 * i);
  274. }
  275. }
  276.  
  277. void drawConnecting(Graphics2D g2d) {
  278. clear(g2d, Color.LIGHT_GRAY);
  279. g2d.setColor(Color.BLACK);
  280. g2d.drawString("Please enter server name", 10, 50);
  281. g2d.drawString(">" + userInput + ",", 10, 70);
  282.  
  283. }
  284.  
  285. void drawChatting(Graphics2D g2d) {
  286. clear(g2d, Color.CYAN);
  287. g2d.setColor(Color.BLACK);
  288. g2d.drawString(userName + " are connected", 40,530);
  289. for (int i = 0; i < messages.size(); i++) {
  290. g2d.drawString(messages.get(i), 40, 550 + 10 * i);
  291. g2d.drawString(userName+" > " + userInput + "|", 40, 670);
  292. }
  293. }
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement