updated nameDuplicateChecker to remove $ and : and deal with empty names. Also renamed singularName() to checkName().

Some other minor adjustments
This commit is contained in:
Jonas 2022-04-07 13:13:09 +02:00
parent 2789779d62
commit 1b27a601f0
3 changed files with 12 additions and 12 deletions

View File

@ -38,7 +38,7 @@ public class ClientHandler implements Runnable {
this.socket = socket; this.socket = socket;
this.out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); this.out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
this.in = new BufferedReader(new InputStreamReader((socket.getInputStream()))); this.in = new BufferedReader(new InputStreamReader((socket.getInputStream())));
this.clientUserName = nameDuplicateChecker.singularName("U.N. Owen"); this.clientUserName = nameDuplicateChecker.checkName("U.N. Owen");
connectedClients.add(this); connectedClients.add(this);
serverPinger = new ServerPinger(socket, this); serverPinger = new ServerPinger(socket, this);
Thread sP = new Thread(serverPinger); Thread sP = new Thread(serverPinger);
@ -77,11 +77,6 @@ public class ClientHandler implements Runnable {
@Override @Override
/**
* The main logic of the client handler.
* Since every client is put on a string this is where
* most interactions between client and server are held
**/
public void run() { public void run() {
String msg; String msg;
while (socket.isConnected() && !socket.isClosed()) { while (socket.isConnected() && !socket.isClosed()) {
@ -109,7 +104,7 @@ public class ClientHandler implements Runnable {
*/ */
public void changeUsername(String newName) { public void changeUsername(String newName) {
String helper = this.getClientUserName(); String helper = this.getClientUserName();
this.clientUserName = nameDuplicateChecker.singularName(newName); this.clientUserName = nameDuplicateChecker.checkName(newName);
broadcastAnnouncement(helper + " has changed their nickname to " + clientUserName); broadcastAnnouncement(helper + " has changed their nickname to " + clientUserName);
} }
@ -121,7 +116,7 @@ public class ClientHandler implements Runnable {
* @param name The desired name. * @param name The desired name.
*/ */
public void setUsernameOnLogin(String name) { public void setUsernameOnLogin(String name) {
this.clientUserName = nameDuplicateChecker.singularName(name); this.clientUserName = nameDuplicateChecker.checkName(name);
broadcastAnnouncement( clientUserName + " has joined the Server"); broadcastAnnouncement( clientUserName + " has joined the Server");
} }

View File

@ -38,7 +38,7 @@ public class JServerProtocolParser {
try { try {
h.setUsernameOnLogin(msg.substring(6)); h.setUsernameOnLogin(msg.substring(6));
} catch (Exception e) { } catch (Exception e) {
h.setUsernameOnLogin("A Mysterious Passenger"); h.setUsernameOnLogin("U.N. Owen");
} }
break; break;
case "NAMEC": case "NAMEC":

View File

@ -48,12 +48,17 @@ public class nameDuplicateChecker {
} }
/** /**
* Returns the name as a String, if that name is already used by some other ClientHandler, * Adjusts the name to avoid conflicts and returns it as a String. Namely:
* it returns the name with some suffix. * If that name is already used by some other ClientHandler, it returns the name with some suffix.
* Also, any ":" or "$" are removed, so they can be used for whisper chat.
* Also, if the name is empty, it assigns a default value ("U.N. Owen").
*/ */
public static String singularName(String name) { public static String checkName(String name) {
String rtrn = name; //if this line is used, only duplicate names get a suffix. String rtrn = name; //if this line is used, only duplicate names get a suffix.
//String rtrn = extendName(name); //if this line is used, all clients get a suffix //String rtrn = extendName(name); //if this line is used, all clients get a suffix
rtrn = rtrn.replace("$","");
rtrn = rtrn.replace(":","");
if (rtrn.equalsIgnoreCase("")) {rtrn = "U.N. Owen";}
while (isTaken(rtrn)) { //todo: handle the (very unlikely) case that all names are taken. while (isTaken(rtrn)) { //todo: handle the (very unlikely) case that all names are taken.
rtrn = extendName(name); rtrn = extendName(name);
} }