Skip to content

Commit

Permalink
First implementation of a GUI for the Client.
Browse files Browse the repository at this point in the history
For saving the data, a JSON file will be used. User Inputs also get validated before accepting them and the Client will receive a Message with what has gone wrong. Also updated the ReadMe file to give more information including some pictures of the GUI.
  • Loading branch information
DManstrator committed Sep 12, 2018
1 parent c55317b commit 3cce1cf
Show file tree
Hide file tree
Showing 11 changed files with 605 additions and 95 deletions.
1 change: 1 addition & 0 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="lib/json-20180130.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,25 @@
# Hangman-Multiplayer
A networking Version of Hangman.

## How to use
1. Go to the [Releases-Tab](../../releases) and download at least the Client (**hangman_client.jar**). When you want **to host**, you should also get the **hangman_server.jar**. Put the file(s) into a folder wherever you want but **don't let them in the Downloads Folder** since the Client will create a configuration file in that folder.
2. For the Host: Double-click the hangman_server.jar. It won't open anything (as of now) and if you want to close it, just go to the Task Manager -> Processes and look for "javaw.exe". When you scroll to the right, you should see "hangman_server.jar" in the Command Line Column.<br />Make sure to tell you friend your IP address so that he can join your server. To get your IP: Just search for your IP address online ([this site](http://www.myipaddress.com/show-my-ip-address/) is one of the sites which will give you your IP address). You will also need to open a port in your router, else nobody can connect to your PC from outside (which is also good but one uncommon port shouldn't be dangerous). Just google for your Wi-Fi Router and how to open Ports.

3. For the Client: Just double click the hangman_client.jar, it will open a little fancy GUI for you. For the first use, you must set a few things like your nickname. Let me explain the values:<br />- "Name" is a Nickname you want to use, feel free to be as creative as you want but a few names are locked for obvious reasons.<br />- "Server IP" is the IP-Address of the Server, you can't choose that and you need to get it from your friend or a public host service.<br />- "Server Port" is a number which will be used that nobody can join a Server by accident.<br />If you want to know more about Ports, read the FAQ or google for it, it's really interesting!<br /><br />When you are done, click "Save & Return" and now you can join the Server.

## Pictures
Here some pictures that you know how the GUI looks like:<br />
![Main Menu on start](img/main-menu_start.png)<br />
![Setting your User Profile](img/user-profile.png)<br />
![Inputs will be checked](img/input-validator.png)<br />
![Game is ready to go](img/main-menu_startable.png)

## FAQ
- Q: How to play?<br />
A: Read the text above. ;)

- Q: What is a port and why is it dangerous when I open it?<br />
A: Well, it's hard to explain without getting to deep. A port is basically a number for a service / protocol, a Webserver has the Port 80, SMTP has 25 and many more. Also when you open / forward a port to the Internet, everybody in the world who has you IP can scan for open ports and send data over this port to your computer which could be dangerous (which is kind of unlikely to be honest). When you take an uncommon Port, the chances are even smaller that this open port will be recognized by popular services.

- Q: Which names are restricted?<br />
A: Find it out. ;)
Binary file added img/input-validator.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/main-menu_start.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/main-menu_startable.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/user-profile.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lib/json-20180130.jar
Binary file not shown.
125 changes: 125 additions & 0 deletions src/tk/dmanstrator/hangman/client/ClientConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package tk.dmanstrator.hangman.client;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Collectors;

import org.json.JSONObject;

/**
* Class represents a Client Configuration for the Hangman Game.
* @author DManstrator
*
*/
public class ClientConfiguration {

/**
* (Nick)name of the Client.
*/
private final String name;

/**
* IP Address of the Server it should connect to.
*/
private final String ipAddr;

/**
* Port of the Server it should connect to.
*/
private final int port;

/**
* Marks the Instance as valid or invalid.
*/
private final boolean valid;

/**
* Default Constructor.
* Checks if the configuration file is available and creates a valid {@link ClientConfiguration} in that case. Else, an invalid {@link ClientConfiguration} will be created.
* If a {@link ClientConfiguration} is valid can be checked with the {@link ClientConfiguration#isValid()} method.
*/
public ClientConfiguration() {
String fileContent = "";
try {
fileContent = Files.readAllLines(Paths.get("config.json")).stream().collect(Collectors.joining(System.lineSeparator()));
} catch (IOException e) {
}
if (!fileContent.isEmpty()) {
JSONObject jsonContent = new JSONObject(fileContent);
name = jsonContent.getString("name");
ipAddr = jsonContent.getString("server_ip");
port = jsonContent.getInt("server_port");
valid = true;
} else {
valid = false;
name = null;
ipAddr = null;
port = -1;
}
}

/**
* Custom Constructor. Creates a valid {@link ClientConfiguration} with the given data and will write it into the configuration file.
* @param name Name of Client
* @param ipAddr IP Address of the Server it should connect to
* @param port Port of the Server it should connect to
*/
public ClientConfiguration(String name, String ipAddr, int port) {
this.name = name;
this.ipAddr = ipAddr;
this.port = port;
valid = true;

JSONObject config = new JSONObject();
config.put("name", name);
config.put("server_ip", ipAddr);
config.put("server_port", port);

File file = new File("config.json");
try {
file.createNewFile();
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(config.toString());
fileWriter.flush();
fileWriter.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}

/**
* Gets the Name of the Client.
* @return the name of the Client
*/
public String getName() {
return name;
}

/**
* Gets the IP-Address the Client wants to connect to.
* @return the IP-Address the Client wants to connect to
*/
public String getIpAddr() {
return ipAddr;
}

/**
* Gets the Port the Client wants to connect to.
* @return the Port the Client wants to connect to
*/
public int getPort() {
return port;
}

/**
* Checks if an Instance of this Object is valid or invalid.
* @return <code>true</code> if instance is valid, else <code>false</code>
*/
public boolean isValid() {
return valid;
}

}
134 changes: 134 additions & 0 deletions src/tk/dmanstrator/hangman/client/ClientStart.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package tk.dmanstrator.hangman.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;

/**
* One of the two Main-Programs of the Hangman Game, starts a new Client.
* Class represents the Client for the Hangman. With initializing, it builds a Connection to the Server.
* @author DManstrator
*
*/
public class ClientStart implements Runnable {

/**
* Name of the player.
*/
private final String name;

/**
* Socket of Player.
*/
private final Socket socket;

/**
* GUI of Client.
*/
private HangmanClient hangmanClient;

/**
* Custom-Constructor of Client.
* @param hangmanClient GUI of Client
* @param config Configuration to apply
*/
public ClientStart(HangmanClient hangmanClient, ClientConfiguration config) {
String name = config.getName();
String ipAddr = config.getIpAddr();
int port = config.getPort();
Socket socket = null;
System.out.println(String.format("%s wants to connect to %s:%d", name, ipAddr, port));

boolean worked = false; // lazy workaround
try {
socket = new Socket();
socket.connect(new InetSocketAddress(ipAddr, port), 2500);
worked = true;
} catch (SocketTimeoutException ex) {
hangmanClient.cancelGui();

} catch (IOException ex) {
hangmanClient.cancelGui();
}

this.socket = socket;
this.name = name;
this.hangmanClient = hangmanClient;
if (worked) {
new Thread(this).start();
hangmanClient.acceptGui();
}
// TODO send the name to the Server
}

/**
* Getter for the name of the Client.
* @return Name of Client.
*/
public String getPlayerName() {
return name;
}

/**
* Returns this client.
* @return this client
*/
public Socket getSocketOfClient() {
return socket;
}

/**
* Returns the GUI of the Client.
* @return the GUI of the Client
*/
public HangmanClient getGui() {
return hangmanClient;
}

/**
* run-Method.
*/
@Override
public void run() {
while (true) {
try {
receive();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

/**
* Receives a message from the Client Connection.
* @throws IOException in case something breaks.
*/
private void receive() throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String answer = reader.readLine(); // gets the response of the client
System.out.println(answer);
// TODO do something with it
}

/**
* Writes a message to the Client Connection.
* @param message Message to write.
*/
public void send(String message) {
try {
PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
writer.println(message);
writer.flush();
} catch (IOException e) {
// TODO
e.printStackTrace();
}
}

}
Loading

0 comments on commit 3cce1cf

Please sign in to comment.