Converting hard-coded usages of player roles to using the enum
This commit is contained in:
@@ -158,7 +158,7 @@ public class AdminRequestHandler extends LotroServerRequestHandler implements Ur
|
||||
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
||||
return "Banned until " + format.format(similarPlayer.getBannedUntil());
|
||||
}
|
||||
if (similarPlayer.getType().contains("n"))
|
||||
if (similarPlayer.hasType(Player.Type.UNBANNED))
|
||||
return "Unbanned";
|
||||
return "OK";
|
||||
}
|
||||
@@ -758,14 +758,14 @@ public class AdminRequestHandler extends LotroServerRequestHandler implements Ur
|
||||
private void validateAdmin(HttpRequest request) throws HttpProcessingException {
|
||||
Player player = getResourceOwnerSafely(request, null);
|
||||
|
||||
if (!player.getType().contains("a"))
|
||||
if (!player.hasType(Player.Type.ADMIN))
|
||||
throw new HttpProcessingException(403);
|
||||
}
|
||||
|
||||
private void validateLeagueAdmin(HttpRequest request) throws HttpProcessingException {
|
||||
Player player = getResourceOwnerSafely(request, null);
|
||||
|
||||
if (!player.getType().contains("l"))
|
||||
if (!player.hasType(Player.Type.LEAGUE_ADMIN))
|
||||
throw new HttpProcessingException(403);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,8 +92,8 @@ public class ChatRequestHandler extends LotroServerRequestHandler implements Uri
|
||||
throw new HttpProcessingException(404);
|
||||
|
||||
try {
|
||||
final boolean admin = resourceOwner.getType().contains("a");
|
||||
final boolean leagueAdmin = resourceOwner.getType().contains("l");
|
||||
final boolean admin = resourceOwner.hasType(Player.Type.ADMIN);
|
||||
final boolean leagueAdmin = resourceOwner.hasType(Player.Type.LEAGUE_ADMIN);
|
||||
if (message != null && message.trim().length() > 0) {
|
||||
String newMsg;
|
||||
newMsg = message.trim().replaceAll("\n\n\n+", "\n\n\n");
|
||||
@@ -243,7 +243,7 @@ public class ChatRequestHandler extends LotroServerRequestHandler implements Uri
|
||||
if (chatRoom == null)
|
||||
throw new HttpProcessingException(404);
|
||||
try {
|
||||
final boolean admin = resourceOwner.getType().contains("a");
|
||||
final boolean admin = resourceOwner.hasType(Player.Type.ADMIN);
|
||||
List<ChatMessage> chatMessages = chatRoom.joinUser(resourceOwner.getName(), admin);
|
||||
Collection<String> usersInRoom = chatRoom.getUsersInRoom(admin);
|
||||
|
||||
@@ -295,10 +295,9 @@ public class ChatRequestHandler extends LotroServerRequestHandler implements Uri
|
||||
private String formatPlayerNameForChatList(String userInRoom) {
|
||||
final Player player = _playerDao.getPlayer(userInRoom);
|
||||
if (player != null) {
|
||||
final String playerType = player.getType();
|
||||
if (playerType.contains("a"))
|
||||
if (player.hasType(Player.Type.ADMIN))
|
||||
return "* "+userInRoom;
|
||||
else if (playerType.contains("l"))
|
||||
else if (player.hasType(Player.Type.LEAGUE_ADMIN))
|
||||
return "+ "+userInRoom;
|
||||
}
|
||||
return userInRoom;
|
||||
|
||||
@@ -230,7 +230,7 @@ public class GameRequestHandler extends LotroServerRequestHandler implements Uri
|
||||
Element gameState = doc.createElement("gameState");
|
||||
|
||||
try {
|
||||
gameMediator.singupUserForGame(resourceOwner, new SerializationVisitor(doc, gameState));
|
||||
gameMediator.signupUserForGame(resourceOwner, new SerializationVisitor(doc, gameState));
|
||||
} catch (PrivateInformationException e) {
|
||||
throw new HttpProcessingException(403);
|
||||
}
|
||||
|
||||
@@ -103,7 +103,7 @@ public class LotroServerRequestHandler {
|
||||
if (resourceOwner == null)
|
||||
throw new HttpProcessingException(401);
|
||||
|
||||
if (resourceOwner.getType().contains("a") && participantId != null && !participantId.equals("null") && !participantId.equals("")) {
|
||||
if (resourceOwner.hasType(Player.Type.ADMIN) && participantId != null && !participantId.equals("null") && !participantId.equals("")) {
|
||||
resourceOwner = _playerDao.getPlayer(participantId);
|
||||
if (resourceOwner == null)
|
||||
throw new HttpProcessingException(401);
|
||||
|
||||
@@ -54,7 +54,7 @@ public class PlaytestRequestHandler extends LotroServerRequestHandler implements
|
||||
try {
|
||||
Player player = getResourceOwnerSafely(request, null);
|
||||
|
||||
_playerDAO.addPlayerFlag(player.getName(), "p");
|
||||
_playerDAO.addPlayerFlag(player.getName(), Player.Type.PLAY_TESTER);
|
||||
|
||||
responseWriter.writeHtmlResponse("OK");
|
||||
|
||||
@@ -68,7 +68,7 @@ public class PlaytestRequestHandler extends LotroServerRequestHandler implements
|
||||
try {
|
||||
Player player = getResourceOwnerSafely(request, null);
|
||||
|
||||
_playerDAO.removePlayerFlag(player.getName(), "p");
|
||||
_playerDAO.removePlayerFlag(player.getName(), Player.Type.PLAY_TESTER);
|
||||
|
||||
responseWriter.writeHtmlResponse("OK");
|
||||
|
||||
@@ -87,7 +87,7 @@ public class PlaytestRequestHandler extends LotroServerRequestHandler implements
|
||||
Document doc = documentBuilder.newDocument();
|
||||
Element hasTester = doc.createElement("hasTester");
|
||||
|
||||
hasTester.setAttribute("result", String.valueOf(player.getType().contains("p")));
|
||||
hasTester.setAttribute("result", String.valueOf(player.hasType(Player.Type.PLAY_TESTER)));
|
||||
|
||||
responseWriter.writeXmlResponse(doc);
|
||||
|
||||
|
||||
@@ -96,6 +96,10 @@ public class Player {
|
||||
return _type;
|
||||
}
|
||||
|
||||
public boolean hasType(Type type) {
|
||||
return Type.getTypes(_type).contains(type);
|
||||
}
|
||||
|
||||
public Integer getLastLoginReward() {
|
||||
return _lastLoginReward;
|
||||
}
|
||||
|
||||
@@ -63,7 +63,7 @@ public class CachedPlayerDAO implements PlayerDAO, Cached {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addPlayerFlag(String login, String flag) throws SQLException {
|
||||
public boolean addPlayerFlag(String login, Player.Type flag) throws SQLException {
|
||||
final boolean success = _delegate.addPlayerFlag(login, flag);
|
||||
if (success)
|
||||
clearCache();
|
||||
@@ -71,7 +71,7 @@ public class CachedPlayerDAO implements PlayerDAO, Cached {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removePlayerFlag(String login, String flag) throws SQLException {
|
||||
public boolean removePlayerFlag(String login, Player.Type flag) throws SQLException {
|
||||
final boolean success = _delegate.removePlayerFlag(login, flag);
|
||||
if (success)
|
||||
clearCache();
|
||||
|
||||
@@ -163,26 +163,52 @@ public class DbPlayerDAO implements PlayerDAO {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addPlayerFlag(String login, String flag) throws SQLException {
|
||||
try (Connection conn = _dbAccess.getDataSource().getConnection()) {
|
||||
try (PreparedStatement statement = conn.prepareStatement("UPDATE player SET type = CONCAT(type, ?) WHERE name=? AND type NOT LIKE CONCAT('%', ?, '%');")) {
|
||||
statement.setString(1, flag);
|
||||
statement.setString(2, login);
|
||||
statement.setString(3, flag);
|
||||
return statement.executeUpdate() == 1;
|
||||
public boolean addPlayerFlag(String login, Player.Type flag) throws SQLException {
|
||||
try {
|
||||
Sql2o db = new Sql2o(_dbAccess.getDataSource());
|
||||
|
||||
try (org.sql2o.Connection conn = db.beginTransaction()) {
|
||||
String sql = """
|
||||
UPDATE player
|
||||
SET type = CONCAT(type, :type)
|
||||
WHERE name= :login
|
||||
AND type NOT LIKE CONCAT('%', :type, '%');
|
||||
""";
|
||||
conn.createQuery(sql)
|
||||
.addParameter("login", login)
|
||||
.addParameter("type", flag.getValue())
|
||||
.executeUpdate();
|
||||
|
||||
conn.commit();
|
||||
return conn.getResult() == 1;
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
throw new RuntimeException("Unable to update player with playtester flag", ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removePlayerFlag(String login, String flag) throws SQLException {
|
||||
try (Connection conn = _dbAccess.getDataSource().getConnection()) {
|
||||
try (PreparedStatement statement = conn.prepareStatement("UPDATE player SET type = REPLACE(type, ?, '') WHERE name=? AND type LIKE CONCAT('%', ?, '%');")) {
|
||||
statement.setString(1, flag);
|
||||
statement.setString(2, login);
|
||||
statement.setString(3, flag);
|
||||
return statement.executeUpdate() == 1;
|
||||
public boolean removePlayerFlag(String login, Player.Type flag) throws SQLException {
|
||||
try {
|
||||
Sql2o db = new Sql2o(_dbAccess.getDataSource());
|
||||
|
||||
try (org.sql2o.Connection conn = db.beginTransaction()) {
|
||||
String sql = """
|
||||
UPDATE player
|
||||
SET type = REPLACE(type, :type, '')
|
||||
WHERE name= :login
|
||||
AND type LIKE CONCAT('%', :type, '%');
|
||||
""";
|
||||
conn.createQuery(sql)
|
||||
.addParameter("login", login)
|
||||
.addParameter("type", flag.getValue())
|
||||
.executeUpdate();
|
||||
|
||||
conn.commit();
|
||||
return conn.getResult() == 1;
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
throw new RuntimeException("Unable to update player to remove playtester flag", ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,8 +20,8 @@ public interface PlayerDAO {
|
||||
|
||||
public boolean unBanPlayer(String login) throws SQLException;
|
||||
|
||||
public boolean addPlayerFlag(String login, String flag) throws SQLException;
|
||||
public boolean removePlayerFlag(String login, String flag) throws SQLException;
|
||||
public boolean addPlayerFlag(String login, Player.Type flag) throws SQLException;
|
||||
public boolean removePlayerFlag(String login, Player.Type flag) throws SQLException;
|
||||
|
||||
public List<Player> findSimilarAccounts(String login) throws SQLException;
|
||||
public Set<String> getBannedUsernames() throws SQLException;
|
||||
|
||||
@@ -356,7 +356,7 @@ public class LotroGameMediator {
|
||||
|
||||
public GameCommunicationChannel getCommunicationChannel(Player player, int channelNumber) throws PrivateInformationException, SubscriptionConflictException, SubscriptionExpiredException {
|
||||
String playerName = player.getName();
|
||||
if (!player.getType().contains("a") && !_allowSpectators && !_playersPlaying.contains(playerName))
|
||||
if (!player.hasType(Player.Type.ADMIN) && !_allowSpectators && !_playersPlaying.contains(playerName))
|
||||
throw new PrivateInformationException();
|
||||
|
||||
_readLock.lock();
|
||||
@@ -394,9 +394,9 @@ public class LotroGameMediator {
|
||||
}
|
||||
}
|
||||
|
||||
public void singupUserForGame(Player player, ParticipantCommunicationVisitor visitor) throws PrivateInformationException {
|
||||
public void signupUserForGame(Player player, ParticipantCommunicationVisitor visitor) throws PrivateInformationException {
|
||||
String playerName = player.getName();
|
||||
if (!player.getType().contains("a") && !_allowSpectators && !_playersPlaying.contains(playerName))
|
||||
if (!player.hasType(Player.Type.ADMIN) && !_allowSpectators && !_playersPlaying.contains(playerName))
|
||||
throw new PrivateInformationException();
|
||||
|
||||
_readLock.lock();
|
||||
|
||||
Reference in New Issue
Block a user