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

This commit is contained in:
Keir Nellyer 2016-10-31 15:49:15 +00:00
parent b5ef642f39
commit 828f39536b
3 changed files with 89 additions and 21 deletions

View File

@ -54,6 +54,7 @@ public class ReportManager
private static final String NAME = "Report";
private static final int INITIAL_PRIORITY = 15;
private static final int ABUSE_BAN_THRESHOLD = 3;
public static final int MAXIMUM_REPORTS = 5;
private final JavaPlugin _plugin;
private final SnapshotManager _snapshotManager;
@ -594,6 +595,16 @@ public class ReportManager
return future;
}
public CompletableFuture<List<Report>> getOpenReports(int reporterId)
{
return _reportRepository.getOpenReports(reporterId)
.thenApply(reportIds ->
reportIds.stream().map(_reportRepository::getReport).collect(Collectors.toList()))
.thenCompose(UtilFuture::sequence)
.thenApply(UtilCollections::unboxPresent)
.thenCompose(reports -> UtilFuture.filter(reports, this::isActiveReport));
}
/**
* Calculates the priority of a report.
* This takes many parameters into account including:

View File

@ -1,5 +1,7 @@
package mineplex.core.report.command;
import java.util.logging.Level;
import org.bukkit.entity.Player;
import mineplex.core.account.CoreClient;
@ -51,34 +53,55 @@ public class ReportCommand extends CommandBase<ReportPlugin>
Player suspect = UtilPlayer.searchOnline(reporter, playerName, false);
String reason = F.combine(args, 1, null, false);
if (suspect != null)
reportManager.getOpenReports(reporterId).whenComplete((reports, throwable) ->
{
// allow developer (iKeirNez) to report himself (for easy testing reasons)
if (suspect == reporter && !reportManager.isDevMode(reporter.getUniqueId()))
if (throwable == null)
{
UtilPlayer.message(reporter, F.main(Plugin.getName(), C.cRed + "You cannot report yourself."));
}
else
{
CoreClient suspectClient = clientManager.Get(suspect);
new ReportCreatePage(Plugin, reporter, reporterId, suspectClient, reason).openInventory();
}
}
else
{
clientManager.loadClientByName(playerName, suspectClient ->
{
if (suspectClient != null)
if (reports.size() < ReportManager.MAXIMUM_REPORTS)
{
new ReportCreatePage(Plugin, reporter, reporterId, suspectClient, reason).openInventory();
if (suspect != null)
{
// allow developer (iKeirNez) to report himself (for easy testing reasons)
if (suspect == reporter && !reportManager.isDevMode(reporter.getUniqueId()))
{
UtilPlayer.message(reporter, F.main(Plugin.getName(),
C.cRed + "You cannot report yourself."));
}
else
{
CoreClient suspectClient = clientManager.Get(suspect);
new ReportCreatePage(Plugin, reporter, reporterId, suspectClient, reason).openInventory();
}
}
else
{
clientManager.loadClientByName(playerName, suspectClient ->
{
if (suspectClient != null)
{
new ReportCreatePage(Plugin, reporter, reporterId, suspectClient, reason).openInventory();
}
else
{
UtilPlayer.message(reporter, F.main(Plugin.getName(),
C.cRed + "Unable to find player '" + playerName + "'!"));
}
});
}
}
else
{
UtilPlayer.message(reporter, F.main(Plugin.getName(), C.cRed + "Unable to find player '"
+ playerName + "'!"));
UtilPlayer.message(reporter, F.main(Plugin.getName(),
C.cRed + "Cannot create report, you currently have too many open reports."));
}
});
}
}
else
{
UtilPlayer.message(reporter, F.main(Plugin.getName(),
C.cRed + "An error occurred, please try again."));
Plugin.getPlugin().getLogger().log(Level.SEVERE, "Error whilst fetching open reports.", throwable);
}
});
}
}
else

View File

@ -115,6 +115,12 @@ public class ReportRepository
" AND reportHandlers.aborted IS FALSE\n" +
" AND (reports.region IS NULL OR reports.region = ?);";
private static final String GET_USER_OPEN_REPORTS = "SELECT reports.id FROM reports\n" +
" INNER JOIN reportReasons ON reports.id = reportReasons.reportId\n" +
" LEFT JOIN reportResults ON reports.id = reportResults.reportId\n" +
"WHERE reportResults.reportId IS NULL\n" +
" AND reportReasons.reporterId = ?;";
private static final String GET_USER_RESULT_COUNT = "SELECT COUNT(reports.id) AS resultCount" +
" FROM reports, reportReasons, reportResults" +
" WHERE reports.id = reportReasons.reportId" +
@ -498,6 +504,34 @@ public class ReportRepository
return future;
}
public CompletableFuture<List<Long>> getOpenReports(int reporterId)
{
CompletableFuture<List<Long>> future = CompletableFuture.supplyAsync(() ->
{
try (Connection connection = DBPool.getAccount().getConnection())
{
PreparedStatement preparedStatement = connection.prepareStatement(GET_USER_OPEN_REPORTS);
preparedStatement.setInt(1, reporterId);
ResultSet resultSet = preparedStatement.executeQuery();
List<Long> reports = new ArrayList<>();
while (resultSet.next())
{
reports.add(resultSet.getLong("id"));
}
return reports;
}
catch (SQLException e)
{
throw new RuntimeException(e);
}
});
return future;
}
private Set<ReportMessage> getReportReasons(Connection connection, long reportId)
{
Set<ReportMessage> reportMessages = new HashSet<>();