Simplified Login, updated name-duplicate-checking and added fun suffixes
This commit is contained in:
parent
c64c754d22
commit
d114d69595
@ -8,6 +8,7 @@ import java.net.Socket;
|
||||
import java.io.*;
|
||||
import java.net.SocketException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Objects;
|
||||
import java.util.Scanner;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
@ -150,7 +151,7 @@ public class Client {
|
||||
if (args.length < 1) {
|
||||
System.out.println("Enter the host's IP address (or type l for localhost)");
|
||||
hostname = sc.next();
|
||||
if (hostname == "l") {
|
||||
if (Objects.equals(hostname, "l")) {
|
||||
hostname = "localhost";
|
||||
}
|
||||
} else {
|
||||
|
||||
@ -1,29 +0,0 @@
|
||||
package ch.unibas.dmi.dbis.cs108.multiplayer.server;
|
||||
|
||||
import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
/**
|
||||
* This class is built to contain the usernames of all players in a single string. This allows a
|
||||
* duplicate check (ClientHandler) when a new player chooses a name: does the string with all
|
||||
* the previous names contain the new player's desired username? If yes, he is being assigned a
|
||||
* random name. If no, he can keep his desired name.
|
||||
*/
|
||||
|
||||
public class AllClientNames {
|
||||
public static final Logger LOGGER = LogManager.getLogger();
|
||||
public static final BudaLogConfig l = new BudaLogConfig(LOGGER);
|
||||
|
||||
static StringBuilder names = new StringBuilder();
|
||||
|
||||
/**
|
||||
* Saves a new name to the List of all Names
|
||||
*
|
||||
* @param currentName the new name to be added
|
||||
* @return All names adding the new currentName
|
||||
*/
|
||||
public static String allNames(String currentName) {
|
||||
return names.append(currentName).toString(); //todo: might use a String<> instead.
|
||||
}
|
||||
}
|
||||
@ -111,13 +111,9 @@ public class ClientHandler implements Runnable {
|
||||
* @param newName The desired new name to replace the old one with.
|
||||
*/
|
||||
public void changeUsername(String newName) {
|
||||
if (AllClientNames.allNames("").contains(newName)) { //todo: more elegant solution
|
||||
newName = NameGenerator.randomName(newName);
|
||||
}
|
||||
String h = this.clientUserName; //just a friendly little helper
|
||||
this.clientUserName = newName;
|
||||
AllClientNames.allNames(newName);
|
||||
broadcastAnnouncement(h + " has changed their nickname to " + clientUserName);
|
||||
String helper = this.getClientUserName();
|
||||
this.clientUserName = nameDuplicateChecker.singularName(newName);
|
||||
broadcastAnnouncement(helper + " has changed their nickname to " + clientUserName);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -128,10 +124,8 @@ public class ClientHandler implements Runnable {
|
||||
* @param name The desired name.
|
||||
*/
|
||||
public void setUsernameOnLogin(String name) {
|
||||
//todo: duplicate checking
|
||||
this.clientUserName = name;
|
||||
this.clientUserName = nameDuplicateChecker.singularName(name);
|
||||
broadcastAnnouncement( clientUserName + " has joined the Server");
|
||||
//todo: add this name to namelist
|
||||
}
|
||||
|
||||
/**
|
||||
@ -185,7 +179,6 @@ public class ClientHandler implements Runnable {
|
||||
|
||||
/**
|
||||
* Does exactly what it says on the tin, closes all connections of Client to Server.
|
||||
*
|
||||
*/
|
||||
public void disconnectClient() {
|
||||
sendMsgToClient("QUITC");
|
||||
|
||||
@ -1,37 +0,0 @@
|
||||
package ch.unibas.dmi.dbis.cs108.multiplayer.server;
|
||||
|
||||
import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
|
||||
import java.util.Random;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
|
||||
public class NameGenerator {
|
||||
public static final Logger LOGGER = LogManager.getLogger();
|
||||
public static final BudaLogConfig l = new BudaLogConfig(LOGGER);
|
||||
|
||||
/**
|
||||
* Creates a random alteration of a Name by adding 4 numbers at the end of the Name that shall be
|
||||
* altered
|
||||
*
|
||||
* @param username the to be altered username
|
||||
* @return username + four numbers
|
||||
*/
|
||||
static String randomName(String username) {
|
||||
StringBuilder name;
|
||||
while (true) {
|
||||
|
||||
name = new StringBuilder();
|
||||
Random r = new Random();
|
||||
for (int i = 0; i < 4; i++) {
|
||||
int c = r.nextInt(10);
|
||||
name.append(c);
|
||||
}
|
||||
if (!AllClientNames.allNames("").contains(username + name)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return username + name;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,63 @@
|
||||
package ch.unibas.dmi.dbis.cs108.multiplayer.server;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* This class is responsible for checking names for duplicates and assigning suffixes in case
|
||||
* of duplicate names.
|
||||
*/
|
||||
public class nameDuplicateChecker {
|
||||
static final String[] suffixes = new String[] {
|
||||
" from London",
|
||||
" of Prussia",
|
||||
" of Zagreb",
|
||||
" of Istanbul",
|
||||
" from Munich",
|
||||
", the Belgian traveller",
|
||||
", the wagon-lit conductor",
|
||||
", the American",
|
||||
" the 3rd",
|
||||
", Heir to the Throne of Liechtenstein",
|
||||
", the private investigator",
|
||||
", the butler",
|
||||
", the mysterious stranger",
|
||||
", the Bulgarian novelist",
|
||||
", the French delegate",
|
||||
", young and sweet, only 17",
|
||||
", definitely not a ghost"
|
||||
};
|
||||
|
||||
/**
|
||||
* Adds a randomly chosen suffix to the name.
|
||||
*/
|
||||
static String extendName(String name) {
|
||||
Random r = new Random();
|
||||
return (name + suffixes[r.nextInt(suffixes.length)]);
|
||||
}
|
||||
|
||||
/**
|
||||
* returns true if this name is already taken by some clientHandler.
|
||||
*/
|
||||
static boolean isTaken(String name) {
|
||||
for (ClientHandler client : ClientHandler.getConnectedClients()) {
|
||||
if (client.getClientUserName().equalsIgnoreCase(name)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name as a String, if that name is already used by some other ClientHandler,
|
||||
* it returns the name with some suffix.
|
||||
*/
|
||||
public static String singularName(String name) {
|
||||
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
|
||||
while (isTaken(rtrn)) {
|
||||
rtrn = extendName(name);
|
||||
}
|
||||
return rtrn;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user