/* * To start the Client in console mode use one of the following command * > java Client * > java Client username * > java Client username portNumber * > java Client username portNumber serverAddress * at the console prompt * If the portNumber is not specified 1500 is used * If the serverAddress is not specified "localHost" is used * If the username is not specified "Anonymous" is used * > java Client * is equivalent to * > java Client Anonymous 1500 localhost * are equivalent * * In console mode, if an error occurs the program simply stops * when a GUI id used, the GUI is informed of the disconnection */ public static void main(String[] args) { int portNumber = 1500; String serverAddress = "localhost"; String userName = "******"; switch (args.length) { // > javac Client username portNumber serverAddr case 3: serverAddress = args[2]; // > javac Client username portNumber case 2: try { portNumber = Integer.parseInt(args[1]); } catch (Exception e) { System.out.println("Invalid port number."); System.out.println("Usage is: > java Client [username] [portNumber] [serverAddress]"); return; } // > javac Client username case 1: userName = args[0]; // > java Client case 0: break; // invalid number of arguments default: System.out.println("Usage is: > java Client [username] [portNumber] {serverAddress]"); return; } Client client = new Client(serverAddress, portNumber, userName); if (!client.start()) { return; } Scanner scan = new Scanner(System.in); while (true) { System.out.print("> "); String msg = scan.nextLine(); if (msg.equalsIgnoreCase("LOGOUT")) { client.sendMessage(new ChatMessage(ChatMessage.LOGOUT, "")); break; } else if (msg.equalsIgnoreCase("WHOISIN")) { client.sendMessage(new ChatMessage(ChatMessage.WHOISIN, "")); } else { client.sendMessage(new ChatMessage(ChatMessage.MESSAGE, msg)); } } client.disconnect(); }
public void actionPerformed(ActionEvent e) { Object o = e.getSource(); for (JRadioButton u : ub) { if (o == u) { mapTM.setUnitType(u.getText()); System.out.println("Set unit type - " + u.getText()); return; } } if (o == timer) { /* mmLabel.setText("Main Menu ("+(cdTime--)+")"); if(cdTime == 0){ timer.stop(); gsButton.setText("Game Start"); mmLabel.setText("Main Menu"); ArrayList<Building> bArr = new ArrayList<Building>(); String temp = "Elixir Collector-24,8-960|Elixir Collector-31,8-960|Gold Mine-17,10-960|Elixir Collector-25,21-960|Elixir Collector-11,22-960"; String[] bs = temp.split("\\|"); for(String b : bs){ String[] bParts = b.split("-"); String[] cParts = bParts[1].split(","); int x = Integer.parseInt(cParts[0].trim()); int y = Integer.parseInt(cParts[1].trim()); Building tb = new Building(bParts[0], new Coordinate(x,y)); tb.setHp(Integer.parseInt(bParts[2].trim())); bArr.add(tb); } mapTM.setBuildings(bArr); switchCards("TM"); } */ return; } if (o == gsButton) { if (timer.isRunning()) { // timer.stop(); gsButton.setText("Game Start"); mmLabel.setText("Main Menu"); // sends the leave command client.sendUDP("leave~" + unameUDP); return; } // sends the joinlobby command client.sendUDP("joinlobby~" + unameUDP); // gsButton.setText("Game Stop"); String serverResp = client.receiveUDP(); if (serverResp.trim().equals("false")) { // place false handler here } else { String[] enemies = serverResp.trim().split(","); ArrayList<Building> bArr = new ArrayList<Building>(); String mapConfig = getBaseConfig(enemies[0]); String[] bs = mapConfig.split("\\|"); for (String b : bs) { String[] bParts = b.split("-"); String[] cParts = bParts[1].split(","); int x = Integer.parseInt(cParts[0].trim()); int y = Integer.parseInt(cParts[1].trim()); Building tb = new Building(bParts[0], new Coordinate(x, y)); tb.setHp(Integer.parseInt(bParts[2].trim())); bArr.add(tb); } mapTM.setBuildings(bArr); switchCards("TM"); } // System.out.println(serverResp); // cdTime = 10; // timer.start(); return; } if (o == logout) { client.sendMessage(new ChatMessage(ChatMessage.LOGOUT, "")); chatArea.setText(""); switchCards("Login"); return; } if (o == cmButton) { String baseConfig = getBaseConfig(); System.out.println("base config: " + baseConfig); for (int i = 0; i < mapSize; i++) { for (int j = 0; j < mapSize; j++) { tiles[i][j].setValue(""); } } if (!baseConfig.equals("")) { String[] bs = baseConfig.split("\\|"); for (String b : bs) { String[] bParts = b.split("-"); String[] cParts = bParts[1].split(","); int x = Integer.parseInt(cParts[0].trim()); int y = Integer.parseInt(cParts[1].trim()); int index = 0; for (int i = 0; i < bb.size(); i++) { if (bb.get(i).getText().split("-")[0].trim().equals(bParts[0])) { index = i; break; } } insertBuilding(y, x, index); } } switchCards("CM"); return; } if (o == tmButton) { ArrayList<Building> bArr = new ArrayList<Building>(); for (int i = 0; i < 40; i++) { for (int j = 0; j < 40; j++) { if (tiles[i][j].getValue().equals("") || tiles[i][j].getValue().contains("-")) { continue; } // weird part here bArr.add(new Building(tiles[i][j].getValue(), new Coordinate(j, i))); } } mapTM.setBuildings(bArr); switchCards("TM"); return; } // if it the who is in button if (o == whoIsIn) { client.sendMessage(new ChatMessage(ChatMessage.WHOISIN, "")); return; } if (o == cmBack) { ArrayList<Building> bArr = new ArrayList<Building>(); for (int i = 0; i < 40; i++) { for (int j = 0; j < 40; j++) { if (tiles[i][j].getValue().equals("") || tiles[i][j].getValue().contains("-")) { continue; } // weird part here bArr.add(new Building(tiles[i][j].getValue(), new Coordinate(j, i))); } } String temp = "mapdata~" + unameUDP + "~"; int tileCount = 40; int dim = 600; int tileDim = (int) (dim / tileCount); int counter = 0; for (Building b : bArr) { int x, y, hp; x = b.getPos().getX() / tileDim; y = b.getPos().getY() / tileDim; hp = b.getHp(); temp += b.getName() + "-" + x + "," + y + "-" + hp + "|"; counter += 1; } if (counter > 0) { temp = temp.substring(0, temp.length() - 1); // removes the last '|' } else { temp += "none"; } client.sendUDP(temp); // allows saving of the current state of the map into the user's account switchCards("Menu"); return; } if (o == tmBack) { switchCards("Menu"); return; } for (int i = 0; i < 40; i++) { for (int j = 0; j < 40; j++) { if (o == tiles[i][j]) { for (int k = 0; k < bb.size(); k++) { if (bb.get(k).isSelected()) { if (bb.get(k).getText().equals("Remove Building")) { removeBuilding(i, j); return; } insertBuilding(i, j, k); // JOptionPane.showMessageDialog(null, bb.get(k).getText()); return; } } // JOptionPane.showMessageDialog(null, "i-"+i+" j-"+j); return; } } } // ok it is coming from the JTextField if (connected) { // just have to send the message client.sendMessage(new ChatMessage(ChatMessage.MESSAGE, chatField.getText())); chatField.setText(""); return; } if (o == login) { // ok it is a connection request String username = usernameField.getText().trim(); String password = passwordField.getText().trim(); // empty username ignore it if (username.length() == 0) return; // empty serverAddress ignore it String server = tfServer.getText().trim(); if (server.length() == 0) return; // empty or invalid port numer, ignore it String portNumber = tfPort.getText().trim(); if (portNumber.length() == 0) return; int port = 0; try { port = Integer.parseInt(portNumber); } catch (Exception en) { return; // nothing I can do if port number is not valid } // try creating a new Client with GUI client = new Client(server, port, username, password, this); // test if we can start the Client if (!client.start()) return; unameUDP = username; switchCards("Menu"); // fetching of the base_config string from the database chatField.setText(""); chatArea.setText(""); } }