added name validation
This commit is contained in:
@@ -1,12 +1,10 @@
|
||||
package eu.jonahbauer.wizard.server;
|
||||
|
||||
import eu.jonahbauer.wizard.common.messages.server.ServerMessage;
|
||||
import eu.jonahbauer.wizard.common.messages.server.SessionCreatedMessage;
|
||||
import eu.jonahbauer.wizard.common.messages.server.SessionListMessage;
|
||||
import eu.jonahbauer.wizard.common.messages.server.SessionRemovedMessage;
|
||||
import eu.jonahbauer.wizard.common.messages.server.*;
|
||||
import eu.jonahbauer.wizard.common.model.Configuration;
|
||||
import eu.jonahbauer.wizard.server.debug.DebugSession;
|
||||
import eu.jonahbauer.wizard.server.machine.Player;
|
||||
import org.intellij.lang.annotations.Language;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.*;
|
||||
@@ -14,6 +12,8 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
|
||||
public class Lobby {
|
||||
@Language("RegExp")
|
||||
private static final String SESSION_NAME_PATTERN = "[a-zA-Z0-9_ ]{1,20}";
|
||||
private static final Lobby INSTANCE = new Lobby();
|
||||
public static Lobby getInstance() {
|
||||
return INSTANCE;
|
||||
@@ -26,8 +26,16 @@ public class Lobby {
|
||||
private Lobby() {}
|
||||
|
||||
public Session createSession(@NotNull String name, long timeout, @NotNull Configuration configuration) {
|
||||
if (!name.matches(SESSION_NAME_PATTERN)) {
|
||||
throw new NackException(NackMessage.SESSION_NAME_NOT_ALLOWED, "Session name is too short, too long or contains illegal characters.");
|
||||
}
|
||||
|
||||
lock.readLock().lock();
|
||||
try {
|
||||
if (sessions.values().stream().anyMatch(s -> s.getName().equalsIgnoreCase(name))) {
|
||||
throw new NackException(NackMessage.SESSION_NAME_TAKEN, "Session name is already taken.");
|
||||
}
|
||||
|
||||
Session session;
|
||||
do {
|
||||
session = new Session(UUID.randomUUID(), name, timeout, configuration);
|
||||
|
@@ -16,6 +16,7 @@ import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.intellij.lang.annotations.Language;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
@@ -26,6 +27,8 @@ import java.util.concurrent.ThreadLocalRandom;
|
||||
@Log4j2
|
||||
@EqualsAndHashCode(of = "uuid")
|
||||
public class Session implements Observer {
|
||||
@Language("RegExp")
|
||||
private static final String PLAYER_NAME_PATTERN = "[a-zA-Z0-9_ ]{1,20}";
|
||||
protected static final int MIN_PLAYERS = 3;
|
||||
protected static final int MAX_PLAYERS = 6;
|
||||
|
||||
@@ -63,7 +66,9 @@ public class Session implements Observer {
|
||||
} else if (players.size() == MAX_PLAYERS) {
|
||||
throw new NackException(NackMessage.SESSION_FULL, "Session is full.");
|
||||
} else if (players.values().stream().anyMatch(p -> p.getName().equalsIgnoreCase(name))) {
|
||||
throw new NackException(NackMessage.NAME_TAKEN, "Name is already taken.");
|
||||
throw new NackException(NackMessage.PLAYER_NAME_TAKEN, "Player name is already taken.");
|
||||
} else if (!name.matches(PLAYER_NAME_PATTERN)) {
|
||||
throw new NackException(NackMessage.PLAYER_NAME_NOT_ALLOWED, "Player name is too short, too long or contains illegal characters.");
|
||||
}
|
||||
|
||||
SessionPlayer sessionPlayer;
|
||||
|
@@ -41,7 +41,7 @@ public class DebugSession extends Session {
|
||||
} else if (getPlayers().size() == MAX_PLAYERS) {
|
||||
throw new NackException(NackMessage.SESSION_FULL, "Session is full.");
|
||||
} else if (getPlayers().values().stream().anyMatch(p -> p.getName().equalsIgnoreCase(name))) {
|
||||
throw new NackException(NackMessage.NAME_TAKEN, "Name is already taken.");
|
||||
throw new NackException(NackMessage.PLAYER_NAME_TAKEN, "Player name is already taken.");
|
||||
}
|
||||
|
||||
SessionPlayer sessionPlayer;
|
||||
|
Reference in New Issue
Block a user