PC-966 Add gui button for passing report to RC team

Misc. other changes
This commit is contained in:
Keir Nellyer 2016-09-13 18:28:09 +01:00
parent d115097eb9
commit 20c4488c5f
8 changed files with 147 additions and 63 deletions

View File

@ -25,9 +25,11 @@ import mineplex.core.chatsnap.command.PushSnapshotsHandler;
import mineplex.core.command.CommandCenter;
import mineplex.core.common.jsonchat.ChildJsonMessage;
import mineplex.core.common.jsonchat.JsonMessage;
import mineplex.core.common.util.BukkitFuture;
import mineplex.core.common.util.C;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilFuture;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.incognito.IncognitoManager;
import mineplex.core.portal.Portal;
import mineplex.core.report.redis.HandlerNotification;
@ -373,12 +375,28 @@ public class ReportManager
return future;
}
public CompletableFuture<Void> setHandlerAborted(Report report, boolean aborted)
public CompletableFuture<Void> abortReport(Report report)
{
long reportId = report.getId().orElseThrow(() -> new IllegalStateException("Report id must be present."));
int handlerId = report.getHandlerId().orElseThrow(() -> new IllegalStateException("Handler must be assigned to report."));
report.cancelHandlerTask();
report.setHandlerId(null);
return _reportRepository.setAborted(reportId, handlerId, aborted);
return _reportRepository.setAborted(reportId, handlerId, true);
}
public CompletableFuture<Void> assignTeam(Report report, Player player, ReportTeam team)
{
report.setAssignedTeam(team);
CompletableFuture<Long> future = abortReport(report).thenCompose(voidValue -> saveReport(report));
if (player != null)
{
future.thenCompose(BukkitFuture.accept(reportId ->
UtilPlayer.message(player,
F.main(getReportPrefix(reportId), "Report forwarded to " + F.elem(team.name()) + " team"))));
}
return future.thenApply(reportId -> null); // convert to Void
}
/**

View File

@ -36,7 +36,7 @@ public class ReportAbortCommand extends CommandBase<ReportPlugin>
{
Report report = reportOptional.get();
reportManager.setHandlerAborted(report, true).thenApply(BukkitFuture.accept(voidValue ->
reportManager.abortReport(report).thenApply(BukkitFuture.accept(voidValue ->
UtilPlayer.message(player, F.main(ReportManager.getReportPrefix(report),
"Report has been aborted and may be handled by another staff member."))));
}

View File

@ -42,11 +42,10 @@ public class ReportCloseCommand extends CommandBase<ReportPlugin>
if (reportOptional.isPresent())
{
Report report = reportOptional.get();
long reportId = report.getId().orElseThrow(() -> new IllegalStateException("Report id must be present."));
reportManager.getReportRepository().getAccountName(report.getSuspectId()).thenCompose(BukkitFuture.accept(suspectName ->
{
ReportResultPage reportResultPage = new ReportResultPage(Plugin, reportId, player, suspectName, reason);
ReportResultPage reportResultPage = new ReportResultPage(Plugin, report, player, suspectName, reason);
reportResultPage.openInventory(); // report is closed when player selects the result
}));
}

View File

@ -45,8 +45,8 @@ import org.apache.commons.lang3.StringUtils;
*/
public class ReportRepository
{
private static final String INSERT_REPORT = "INSERT INTO reports (suspectId, categoryId, snapshotId)\n" +
"VALUES (?, ?, ?);";
private static final String INSERT_REPORT = "INSERT INTO reports (suspectId, categoryId, snapshotId, assignedTeam)\n" +
"VALUES (?, ?, ?, ?);";
private static final String UPDATE_REPORT = "UPDATE reports SET snapshotId = ?, assignedTeam = ? WHERE id = ?;";
@ -517,18 +517,24 @@ public class ReportRepository
{
Optional<Long> reportIdOptional = report.getId();
Optional<Integer> snapshotIdOptional = report.getSnapshotId();
Optional<ReportTeam> teamOptional = report.getAssignedTeam();
long reportId;
if (reportIdOptional.isPresent())
{
reportId = reportIdOptional.get();
if (snapshotIdOptional.isPresent())
try (PreparedStatement updateReportStatement = connection.prepareStatement(UPDATE_REPORT))
{
PreparedStatement updateReportStatement = connection.prepareStatement(UPDATE_REPORT);
updateReportStatement.setInt(1, snapshotIdOptional.get());
if (snapshotIdOptional.isPresent())
{
updateReportStatement.setInt(1, snapshotIdOptional.get());
}
else
{
updateReportStatement.setNull(1, Types.INTEGER);
}
Optional<ReportTeam> teamOptional = report.getAssignedTeam();
if (teamOptional.isPresent())
{
updateReportStatement.setShort(2, teamOptional.get().getDatabaseId());
@ -544,30 +550,41 @@ public class ReportRepository
}
else
{
PreparedStatement insertReportStatement = connection.prepareStatement(INSERT_REPORT, Statement.RETURN_GENERATED_KEYS);
insertReportStatement.setInt(1, report.getSuspectId());
insertReportStatement.setInt(2, report.getCategory().getId());
try (PreparedStatement insertReportStatement = connection.prepareStatement(INSERT_REPORT, Statement.RETURN_GENERATED_KEYS))
{
insertReportStatement.setInt(1, report.getSuspectId());
insertReportStatement.setInt(2, report.getCategory().getId());
if (snapshotIdOptional.isPresent())
{
insertReportStatement.setInt(3, snapshotIdOptional.get());
}
else
{
insertReportStatement.setNull(3, Types.INTEGER);
}
if (snapshotIdOptional.isPresent())
{
insertReportStatement.setInt(3, snapshotIdOptional.get());
}
else
{
insertReportStatement.setNull(3, Types.INTEGER);
}
insertReportStatement.executeUpdate();
if (teamOptional.isPresent())
{
insertReportStatement.setInt(4, teamOptional.get().getDatabaseId());
}
else
{
insertReportStatement.setNull(4, Types.TINYINT);
}
ResultSet resultSet = insertReportStatement.getGeneratedKeys();
if (resultSet.next())
{
reportId = resultSet.getLong(1);
report._reportId = reportId;
}
else
{
throw new IllegalStateException("Query did not return a report id (we need one).");
insertReportStatement.executeUpdate();
ResultSet resultSet = insertReportStatement.getGeneratedKeys();
if (resultSet.next())
{
reportId = resultSet.getLong(1);
report._reportId = reportId;
}
else
{
throw new IllegalStateException("Query did not return a report id (we need one).");
}
}
}
@ -618,14 +635,8 @@ public class ReportRepository
future.exceptionally(throwable ->
{
Optional<Long> optional = report.getId();
_logger.log(Level.SEVERE,
"Error updating report" +
(optional.isPresent()
? " (#" + optional.get() + ")"
: ""
) + ".");
Optional<Long> idOptional = report.getId();
_logger.log(Level.SEVERE, "Error updating report" + idOptional.map(id -> "(#" + id + ")").orElse("") + ".", throwable);
return null;
});

View File

@ -27,11 +27,11 @@ import mineplex.serverdata.database.DBPool;
*/
public class ReportUserRepository
{
private static final String GET_TEAMS = "SELECT teamId FROM reportTeams WHERE accountId = ?;";
private static final String GET_TEAMS = "SELECT teamId FROM reportTeamMemberships WHERE accountId = ?;";
private static final String INSERT_TEAM = "INSERT IGNORE INTO reportTeams (accountId, teamId) VALUES (?, ?);";
private static final String INSERT_TEAM = "INSERT IGNORE INTO reportTeamMemberships (accountId, teamId) VALUES (?, ?);";
private static final String GET_TEAM_MEMBERS = "SELECT accountId FROM reportTeams WHERE teamId = ?;";
private static final String GET_TEAM_MEMBERS = "SELECT accountId FROM reportTeamMemberships WHERE teamId = ?;";
private static final String GRAB_RESULT_COUNT = "SELECT reports.categoryId, results.resultId, COUNT(*) AS count" +
" FROM reports, reportReasons reasons, reportResults results, reportResultTypes resultTypes" +

View File

@ -0,0 +1,48 @@
package mineplex.core.report.ui;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.Material;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
import mineplex.core.common.util.C;
import mineplex.core.gui.SimpleGuiItem;
import mineplex.core.itemstack.ItemBuilder;
import mineplex.core.report.ReportTeam;
/**
* A gui button which when clicked will forward a report onto the supplied team.
*/
public class ReportAssignTeamButton extends SimpleGuiItem
{
private static final Map<ReportTeam, ItemStack> DISPLAY_ITEMS = new HashMap<ReportTeam, ItemStack>(){{
ItemStack rcItem = new ItemBuilder(Material.PAPER)
.setTitle(C.cAqua + "Forward to RC")
.addLore("A member of the rules committee will review this report instead.")
.build();
put(ReportTeam.RC, rcItem);
}};
private final ReportResultPage _page;
private final ReportTeam _team;
public ReportAssignTeamButton(ReportResultPage page, ReportTeam team)
{
this(page, team, DISPLAY_ITEMS.get(team));
}
public ReportAssignTeamButton(ReportResultPage page, ReportTeam team, ItemStack displayItem)
{
super(displayItem);
_page = page;
_team = team;
}
@Override
public void click(ClickType clickType)
{
_page.assignTeam(_team);
}
}

View File

@ -45,19 +45,19 @@ public class ReportResultButton extends SimpleGuiItem
put(ReportResultType.ABUSIVE, itemAbuse);
}};
private ReportResultPage _reportResultPage;
private ReportResultType _result;
private ReportResultPage _page;
private ReportResultType _resultType;
public ReportResultButton(ReportResultPage reportResultPage, ReportResultType reportResultType)
public ReportResultButton(ReportResultPage page, ReportResultType resultType)
{
this(reportResultPage, reportResultType, ITEM_STACKS.get(reportResultType));
this(page, resultType, ITEM_STACKS.get(resultType));
}
public ReportResultButton(ReportResultPage reportResultPage, ReportResultType reportResultType, ItemStack displayItem)
public ReportResultButton(ReportResultPage page, ReportResultType resultType, ItemStack displayItem)
{
super(displayItem);
_reportResultPage = reportResultPage;
_result = reportResultType;
_page = page;
_resultType = resultType;
}
@Override
@ -69,7 +69,7 @@ public class ReportResultButton extends SimpleGuiItem
for (int i = 0; i < lore.size(); i++)
{
lore.set(i, lore.get(i).replace("%suspect%", _reportResultPage.getSuspectName()));
lore.set(i, lore.get(i).replace("%suspect%", _page.getSuspectName()));
}
itemMeta.setLore(lore);
@ -79,6 +79,6 @@ public class ReportResultButton extends SimpleGuiItem
@Override
public void click(ClickType clickType)
{
_reportResultPage.setResult(_result);
_page.closeReport(_resultType);
}
}

View File

@ -8,6 +8,8 @@ import mineplex.core.report.ReportManager;
import mineplex.core.report.ReportPlugin;
import mineplex.core.report.ReportResult;
import mineplex.core.report.ReportResultType;
import mineplex.core.report.ReportTeam;
import mineplex.core.report.data.Report;
/**
* User interface shown to a moderator when closing a report to determine the result of the report.
@ -15,17 +17,15 @@ import mineplex.core.report.ReportResultType;
public class ReportResultPage extends SimpleGui
{
private ReportManager _reportManager;
private long _reportId;
private Player _reportCloser;
private Report _report;
private String _suspectName;
private String _reason;
public ReportResultPage(ReportPlugin reportPlugin, long reportId, Player reportCloser, String suspectName, String reason)
public ReportResultPage(ReportPlugin reportPlugin, Report report, Player reportCloser, String suspectName, String reason)
{
super(reportPlugin.getPlugin(), reportCloser, "Report Result", 9 * 3);
super(reportPlugin.getPlugin(), reportCloser, "Close Report", 9 * 4);
_reportManager = reportPlugin.getReportManager();
_reportId = reportId;
_reportCloser = reportCloser;
_report = report;
_suspectName = suspectName;
_reason = reason;
@ -42,17 +42,25 @@ public class ReportResultPage extends SimpleGui
setItem(11, new ReportResultButton(this, ReportResultType.ACCEPTED));
setItem(13, new ReportResultButton(this, ReportResultType.DENIED));
setItem(15, new ReportResultButton(this, ReportResultType.ABUSIVE));
//setItem(34, );
setItem(35, new ReportAssignTeamButton(this, ReportTeam.RC));
}
public void setResult(ReportResultType result)
public void closeReport(ReportResultType result)
{
_reportCloser.closeInventory();
getPlayer().closeInventory();
unregisterListener();
ReportResult reportResult = new ReportResult(result, _reason);
_reportManager.getReportRepository().getReport(_reportId)
.thenCompose(report ->
_reportManager.closeReport(report, _reportCloser, reportResult));
_reportManager.closeReport(_report, getPlayer(), reportResult);
}
public void assignTeam(ReportTeam team)
{
getPlayer().closeInventory();
unregisterListener();
_reportManager.assignTeam(_report, getPlayer(), team);
}
public void unregisterListener()