Advertisement
Guest User

Untitled

a guest
May 27th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.63 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 Health = 100;
  54. int Exp = 50;
  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. void reset()
  71. {
  72. checkNo = 0;
  73. }
  74.  
  75. String userInput = " ";
  76. boolean processUserInput() {
  77. char c =getLastKey();
  78. if (c=='\0')
  79. return false;
  80. if ((int)c==VK_ENTER)
  81. return true; // end of entry
  82. if((int)c==VK_BACK_SPACE || (int)c==VK_DELETE)
  83. {
  84. if(!userInput.isEmpty())
  85. {
  86. userInput=userInput.substring(0, userInput.length()-1);
  87. }
  88. return false; // not yet
  89. }
  90. userInput+=c;
  91. return false;
  92. }
  93.  
  94.  
  95.  
  96. public static void main(String[] args) {
  97. new Assignment01().go(1000, 700, false);
  98. }
  99.  
  100. public void startup() {
  101. state = State.MENU;
  102. String input = JOptionPane.showInputDialog("Please Enter a Nickname!", "New Player");
  103. userName = input;
  104. }
  105.  
  106. public void update() {
  107. if (keys[VK_ESCAPE]) {
  108. close(); // close me
  109. }
  110.  
  111. if (state == State.MENU) {
  112. updateMenu();
  113. } else if (state == State.WAITING) {
  114. updateWaiting();
  115. } else if (state == State.CONNECTING) {
  116. updateConnecting();
  117. } else {
  118. updateChatting();
  119.  
  120. }
  121.  
  122. }
  123.  
  124. void updateMenu() {
  125. if (keys[VK_C]) {
  126. state = State.CONNECTING;
  127. getLastKey();
  128. userInput="localhost";
  129. //userInput="172.17.66.58"; // jiahui
  130. //userInput="172.17.73.253"; //brenda
  131. }
  132. if (keys[VK_S]) {
  133. state = State.WAITING;
  134. setupServer();
  135. }
  136. }
  137.  
  138. void updateWaiting() {
  139. try {
  140. sock = servSock.accept();
  141. sock.setSoTimeout(1);
  142. displayMessage("We have a connection from " + sock.getLocalAddress().getHostAddress() + ":" + sock.getPort());
  143. socketIn = new BufferedReader(new InputStreamReader(sock.getInputStream()));
  144. socketOut = new PrintWriter(new OutputStreamWriter(sock.getOutputStream()));
  145. // we have a connection, to change state to Chatting
  146. state = State.CHATTING;
  147. getLastKey(); // will clear the last keypress
  148. } catch (InterruptedIOException e) {
  149. } catch (IOException e) {
  150. displayMessage("Error:" + e);
  151. }
  152. }
  153.  
  154. void updateConnecting() {
  155. if (processUserInput()) { //if user pressed return
  156. //connect to this FP:
  157. try {
  158. sock = new Socket(userInput, PORT);
  159. sock.setSoTimeout(1);
  160. //TODO: set up socketIn,socketOut
  161. socketIn = new BufferedReader(new InputStreamReader(sock.getInputStream()));
  162. socketOut = new PrintWriter(new OutputStreamWriter(sock.getOutputStream()));
  163. displayMessage("connected");
  164. // we have a connection, to change state to Chatting
  165. state = State.CHATTING;
  166. userInput = "";
  167. } catch (Exception e) {
  168. displayMessage("Error:" + e);
  169. }
  170.  
  171. }
  172. }
  173.  
  174. String recvSocket() {
  175. //receive the message
  176. try {
  177. String s = socketIn.readLine();
  178. return s;
  179. } catch (InterruptedIOException e) {
  180. } catch (IOException e) {
  181. displayMessage("Error: " + e);
  182. e.printStackTrace();
  183. }
  184. return null;
  185. }
  186.  
  187. void sendSocket(String s) {
  188. socketOut.println(s);
  189. socketOut.flush();
  190. }
  191.  
  192.  
  193. void updateChatting() {
  194. String s = recvSocket();
  195. if (s!=null){
  196. System.out.println(s);
  197. if(s.startsWith("CHAT ")){
  198. s=s.substring(5); // chop off "CHAT"
  199. displayMessage("I got a message:" + s);
  200. }
  201. if (s.startsWith("GAME ")){
  202. s=s.substring(5); // chop off "CHAT"
  203. //TODO: do something useful with the game
  204. displayMessage("PLAYER " + s);
  205. }
  206. }
  207.  
  208. if (keys[VK_LEFT] && checkNo==0){
  209. sendSocket("GAME ATTACK");
  210. //checkChoice = true;
  211. Exp = Exp - 10;
  212. checkNo++;
  213. }
  214. //attack
  215. if (keys[VK_RIGHT] && checkNo==0){
  216. sendSocket("GAME DEFEND");
  217. //checkChoice = true;
  218. Exp = Exp - 5;
  219. checkNo++;
  220. }
  221. //idle
  222. if(keys[VK_UP] && checkNo==0){
  223. sendSocket("GAME IDLE");
  224. //checkChoice = true;
  225. checkNo++;
  226. if (Exp == 50)
  227. Exp = 50;
  228. else
  229. Exp = Exp + 10;
  230. }
  231.  
  232. if (processUserInput()) { // if user pressed return
  233. if (userInput.isEmpty() == false) { // if data was entered
  234. displayMessage("Sending:" + userInput);
  235. socketOut.println("CHAT " + userInput);
  236. socketOut.flush();
  237. userInput = ""; // clear user input
  238. }
  239. }
  240. }
  241.  
  242. public void setupServer() {
  243. try {
  244. servSock = new ServerSocket(3000);
  245. servSock.setSoTimeout(1);
  246. InetAddress local = InetAddress.getLocalHost();
  247.  
  248. displayMessage("starting....");
  249. } catch (InterruptedIOException e) {
  250. } catch (IOException er) {
  251. displayMessage("Error Found: " + er);
  252. }
  253. }
  254.  
  255. public void draw(Graphics2D g2d) {
  256. if (keys[VK_ESCAPE]) {
  257. close(); // close me
  258. }
  259. if (state == State.MENU) {
  260. drawMenu(g2d);
  261. } else if (state == State.WAITING) {
  262. drawWaiting(g2d);
  263. } else if (state == State.CONNECTING) {
  264. drawConnecting(g2d);
  265. } else {
  266. drawChatting(g2d);
  267. }
  268. }
  269.  
  270. void drawMenu(Graphics2D g2d) {
  271. clear(g2d, Color.WHITE);
  272. g2d.setColor(Color.BLACK);
  273. g2d.drawString("Press 'C' for Client, 'S' for Server", 10, 50);
  274. }
  275.  
  276. void drawWaiting(Graphics2D g2d) {
  277. clear(g2d, Color.YELLOW);
  278. g2d.setColor(Color.RED);
  279. g2d.drawString("Waiting for connection", 10, 50);
  280. for (int i = 0; i < messages.size(); i++) {
  281. g2d.drawString(messages.get(i), 10, 60 + 10 * i);
  282. }
  283. }
  284.  
  285. void drawConnecting(Graphics2D g2d) {
  286. clear(g2d, Color.LIGHT_GRAY);
  287. g2d.setColor(Color.BLACK);
  288. g2d.drawString("Please enter server name", 10, 50);
  289. g2d.drawString(">" + userInput + ",", 10, 70);
  290.  
  291. }
  292.  
  293. void drawChatting(Graphics2D g2d) {
  294. clear(g2d, Color.CYAN);
  295. g2d.setColor(Color.BLACK);
  296. g2d.drawString(userName + " are connected", 40,530);
  297. g2d.drawString("my health: " + Health, 40,50);
  298. g2d.drawString("my exp: " + Exp, 240,50);
  299.  
  300. for (int i = 0; i < messages.size(); i++) {
  301. g2d.drawString(messages.get(i), 40, 550 + 10 * i);
  302. g2d.drawString(userName+" > " + userInput + "|", 40, 670);
  303. }
  304. }
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement