Don't allow players to have more than 5 open snapshots at a time

This commit is contained in:
Keir Nellyer 2016-11-01 17:21:56 +00:00
parent 828f39536b
commit aaa248833e
5 changed files with 63 additions and 16 deletions

View File

@ -20,6 +20,8 @@ import mineplex.core.report.data.Report;
*/
public class SnapshotManager
{
public static final int MAX_SNAPSHOTS = 5;
// There aren't any List or Set caching implementations
// For an easy work around, we store values as the Key
// For the value we just use some dummy object

View File

@ -6,15 +6,16 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;
import java.util.logging.Logger;
import mineplex.core.common.util.UtilTime;
import mineplex.core.report.data.Report;
import mineplex.serverdata.database.DBPool;
/**
@ -72,6 +73,7 @@ public class SnapshotRepository
private static final String GET_ID_FROM_TOKEN = "SELECT snapshots.id FROM snapshots WHERE snapshots.token = ?;";
private static final String GET_METADATA = "SELECT token, creatorId FROM snapshots WHERE id = ?;";
private static final String SET_TOKEN = "UPDATE snapshots SET token = ? WHERE id = ?;";
private static final String GET_USER_SNAPSHOTS = "SELECT snapshots.id FROM snapshots WHERE snapshots.creatorId = ?;";
private final String _serverName;
private final Logger _logger;
@ -208,6 +210,40 @@ public class SnapshotRepository
return future;
}
public CompletableFuture<List<Integer>> getUserSnapshots(int creatorId)
{
CompletableFuture<List<Integer>> future = CompletableFuture.supplyAsync(() ->
{
try (Connection connection = DBPool.getAccount().getConnection())
{
PreparedStatement preparedStatement = connection.prepareStatement(GET_USER_SNAPSHOTS);
preparedStatement.setInt(1, creatorId);
ResultSet resultSet = preparedStatement.executeQuery();
List<Integer> snapshotIds = new ArrayList<>();
while (resultSet.next())
{
snapshotIds.add(resultSet.getInt("id"));
}
return snapshotIds;
}
catch (SQLException e)
{
throw new RuntimeException(e);
}
});
future.exceptionally(throwable ->
{
_logger.log(Level.SEVERE, "Error getting snapshots for user " + creatorId + ".");
return new ArrayList<>();
});
return future;
}
private void setToken(Connection connection, int snapshotId, String token) throws SQLException
{
try (PreparedStatement setTokenStatement = connection.prepareStatement(SET_TOKEN))

View File

@ -33,23 +33,34 @@ public class ChatSnapCommand extends CommandBase<SnapshotPlugin>
{
SnapshotManager manager = Plugin.getSnapshotManager();
SnapshotRepository repository = manager.getRepository();
int accountId = _commandCenter.GetClientManager().getAccountId(player);
Set<SnapshotMessage> messages = manager.getMessagesInvolving(accountId);
repository.createSnapshot(accountId).thenAccept(snapshotMetadata ->
Plugin.getSnapshotManager().getRepository().getUserSnapshots(accountId).thenAccept(snapshotIds ->
{
String token = snapshotMetadata.getToken().orElseThrow(() ->
new IllegalStateException("Snapshot doesn't have a token."));
if (snapshotIds.size() < SnapshotManager.MAX_SNAPSHOTS)
{
Set<SnapshotMessage> messages = manager.getMessagesInvolving(accountId);
repository.insertMessages(snapshotMetadata.getId(), messages).join();
repository.createSnapshot(accountId).thenAccept(snapshotMetadata ->
{
String token = snapshotMetadata.getToken().orElseThrow(() ->
new IllegalStateException("Snapshot doesn't have a token."));
UtilPlayer.message(player, F.main(Plugin.getName(), "Snapshot successfully created."));
repository.insertMessages(snapshotMetadata.getId(), messages).join();
new JsonMessage(F.main(Plugin.getName(), "Your snapshot token is: "))
.extra(F.elem(token))
.click(ClickEvent.OPEN_URL, SnapshotRepository.getURL(token))
.sendToPlayer(player);
UtilPlayer.message(player, F.main(Plugin.getName(), "Snapshot successfully created."));
new JsonMessage(F.main(Plugin.getName(), "Your snapshot token is: "))
.extra(F.elem(token))
.click(ClickEvent.OPEN_URL, SnapshotRepository.getURL(token))
.sendToPlayer(player);
});
}
else
{
UtilPlayer.message(player, F.main(Plugin.getName(),
C.cRed + "Cannot create snapshot, you have reached the limit."));
}
});
}
else

View File

@ -92,7 +92,7 @@ public class ReportCommand extends CommandBase<ReportPlugin>
else
{
UtilPlayer.message(reporter, F.main(Plugin.getName(),
C.cRed + "Cannot create report, you currently have too many open reports."));
C.cRed + "Cannot create report, you have reached the limit."));
}
}
else

View File

@ -506,7 +506,7 @@ public class ReportRepository
public CompletableFuture<List<Long>> getOpenReports(int reporterId)
{
CompletableFuture<List<Long>> future = CompletableFuture.supplyAsync(() ->
return CompletableFuture.supplyAsync(() ->
{
try (Connection connection = DBPool.getAccount().getConnection())
{
@ -528,8 +528,6 @@ public class ReportRepository
throw new RuntimeException(e);
}
});
return future;
}
private Set<ReportMessage> getReportReasons(Connection connection, long reportId)