The server of the handler now handles sending periodic message, instead of it being done through Redis

This also fixes server restarts breaking handler messages.
This commit is contained in:
Keir Nellyer 2016-08-19 18:08:36 +01:00
parent d05a69ac69
commit bde51e6a54
4 changed files with 172 additions and 115 deletions

View File

@ -1,111 +0,0 @@
package mineplex.core.report;
import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Collectors;
import org.bukkit.scheduler.BukkitRunnable;
import mineplex.core.chatsnap.SnapshotRepository;
import mineplex.core.common.jsonchat.ChildJsonMessage;
import mineplex.core.common.jsonchat.ClickEvent;
import mineplex.core.common.jsonchat.HoverEvent;
import mineplex.core.common.jsonchat.JsonMessage;
import mineplex.core.common.util.C;
import mineplex.core.common.util.F;
import mineplex.core.report.redis.HandlerNotification;
import mineplex.core.report.data.Report;
import mineplex.core.report.data.ReportMessage;
/**
* Displays a message containing up-to-date details of a report to it's handler.
*/
public class ReportHandlerMessenger extends BukkitRunnable
{
private final ReportManager _reportManager;
private final Report _report;
public ReportHandlerMessenger(ReportManager reportManager, Report report)
{
_reportManager = reportManager;
_report = report;
}
@Override
public void run()
{
long reportId = _report.getId().orElse((long) -1);
if (_report.getHandlerId().isPresent())
{
_reportManager.isActiveReport(_report).thenAccept(isActive ->
{
if (isActive)
{
_reportManager.getReportRepository().getAccountName(_report.getSuspectId())
.thenAccept(suspectName ->
{
String prefix = F.main(ReportManager.getReportPrefix(reportId), "");
ChildJsonMessage jsonMessage = new JsonMessage("\n")
.extra(prefix + C.cAqua + "Report Overview")
.add("\n")
.add(prefix + C.cAqua + "Suspect - " + C.cGold + suspectName)
.add("\n")
.add(prefix + C.cAqua + "Type - " + C.cGold + _report.getCategory().getName())
.add("\n" + prefix + "\n")
.add(prefix + C.cGold + _report.getMessages().size() + C.cAqua + " total reports")
.add("\n")
.add(Arrays.stream(getReportReasons()).map(s -> prefix + s).collect(Collectors.joining("\n")))
.add("\n" + prefix + "\n");
if (_report.getCategory() == ReportCategory.CHAT_ABUSE)
{
jsonMessage = jsonMessage
.add(prefix + C.cAqua + "View chat log")
.hover(HoverEvent.SHOW_TEXT, C.cGray + "Opens the chat log in your default browser")
.click(ClickEvent.OPEN_URL, SnapshotRepository.getURL(reportId))
.add("\n");
}
jsonMessage = jsonMessage
.add(prefix + C.cAqua + "Close this report")
.hover(HoverEvent.SHOW_TEXT, C.cGray + "Usage: /reportclose <reason>")
.click(ClickEvent.SUGGEST_COMMAND, "/reportclose ")
.add("\n");
new HandlerNotification(_report, jsonMessage).publish();
});
}
else
{
// report has been closed, so this task should be cancelled
cancel();
}
});
}
else
{
// no handler (report perhaps aborted), so cancel task
cancel();
}
}
private String[] getReportReasons()
{
Collection<ReportMessage> reportMessages = _report.getMessages().values();
String[] output = new String[reportMessages.size()];
int count = 0;
for (ReportMessage reportMessage : reportMessages)
{
// this is blocking, but that's okay as it will only be called asynchronously
String reporterName = _reportManager.getReportRepository().getAccountName(reportMessage.getReporterId()).join();
// triple backslashes so this translates to valid JSON
output[count++] = String.format("%4$s(%d%4$s) %s%s%s - \\\"%s%s%4$s\\\"", count, C.cGold, reporterName, C.cGray, C.cPurple, reportMessage.getMessage());
}
return output;
}
}

View File

@ -0,0 +1,141 @@
package mineplex.core.report;
import java.util.Arrays;
import java.util.Collection;
import java.util.Optional;
import java.util.stream.Collectors;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import mineplex.core.chatsnap.SnapshotRepository;
import mineplex.core.common.jsonchat.ChildJsonMessage;
import mineplex.core.common.jsonchat.ClickEvent;
import mineplex.core.common.jsonchat.HoverEvent;
import mineplex.core.common.jsonchat.JsonMessage;
import mineplex.core.common.util.C;
import mineplex.core.common.util.F;
import mineplex.core.report.data.Report;
import mineplex.core.report.data.ReportMessage;
/**
* Displays a message containing up-to-date details of a report to it's handler.
*/
public class ReportHandlerTask extends BukkitRunnable
{
private final ReportManager _reportManager;
private final Report _report;
public ReportHandlerTask(ReportManager reportManager, Report report)
{
_reportManager = reportManager;
_report = report;
}
public void start(JavaPlugin plugin)
{
runTaskTimer(plugin, 1L, 20L * 10);
_report.setHandlerTask(this);
}
@Override
public void run()
{
long reportId = _report.getId().orElse((long) -1);
_reportManager.isActiveReport(_report).thenAccept(isActive ->
{
if (isActive)
{
_reportManager.getReportRepository().getAccountName(_report.getSuspectId())
.thenAccept(suspectName ->
{
String prefix = F.main(ReportManager.getReportPrefix(reportId), "");
ChildJsonMessage jsonMessage = new JsonMessage("\n")
.extra(prefix + C.cAqua + "Report Overview")
.add("\n")
.add(prefix + C.cAqua + "Suspect - " + C.cGold + suspectName)
.add("\n")
.add(prefix + C.cAqua + "Type - " + C.cGold + _report.getCategory().getName())
.add("\n" + prefix + "\n")
.add(prefix + C.cGold + _report.getMessages().size() + C.cAqua + " total reports")
.add("\n")
.add(Arrays.stream(getReportReasons()).map(s -> prefix + s).collect(Collectors.joining("\n")))
.add("\n" + prefix + "\n");
if (_report.getCategory() == ReportCategory.CHAT_ABUSE)
{
jsonMessage = jsonMessage
.add(prefix + C.cAqua + "View chat log")
.hover(HoverEvent.SHOW_TEXT, C.cGray + "Opens the chat log in your default browser")
.click(ClickEvent.OPEN_URL, SnapshotRepository.getURL(reportId))
.add("\n");
}
jsonMessage = jsonMessage
.add(prefix + C.cAqua + "Close this report")
.hover(HoverEvent.SHOW_TEXT, C.cGray + "Usage: /reportclose <reason>")
.click(ClickEvent.SUGGEST_COMMAND, "/reportclose ")
.add("\n");
Optional<Integer> handlerIdOptional = _report.getHandlerId();
if (handlerIdOptional.isPresent())
{
int handlerId = handlerIdOptional.get();
JsonMessage finalJsonMessage = jsonMessage;
_reportManager.getReportRepository().getAccountUUID(handlerId).thenAccept(handlerUUID ->
{
if (handlerUUID != null)
{
Player handler = Bukkit.getPlayer(handlerUUID);
if (handler != null)
{
finalJsonMessage.sendToPlayer(handler);
}
else
{
// handler offline
cancel();
}
}
});
}
else
{
// no handler (report perhaps aborted), so cancel task
cancel();
}
});
}
else
{
// report has been closed, so this task should be cancelled
cancel();
}
});
}
private String[] getReportReasons()
{
Collection<ReportMessage> reportMessages = _report.getMessages().values();
String[] output = new String[reportMessages.size()];
int count = 0;
for (ReportMessage reportMessage : reportMessages)
{
// this is blocking, but that's okay as it will only be called asynchronously
String reporterName = _reportManager.getReportRepository().getAccountName(reportMessage.getReporterId()).join();
// triple backslashes so this translates to valid JSON
output[count++] = String.format("%4$s(%d%4$s) %s%s%s - \\\"%s%s%4$s\\\"", count, C.cGold, reporterName, C.cGray, C.cPurple, reportMessage.getMessage());
}
return output;
}
}

View File

@ -162,8 +162,8 @@ public class ReportManager
// will not function correctly
saveReport(report).thenAccept(reportId2 ->
{
// Show user details of the report every x seconds
new ReportHandlerMessenger(this, report).runTaskTimer(_plugin, 1L, 20L * 10);
// Regularly show user details of the report
new ReportHandlerTask(this, report).start(_plugin);
});
if (!_incognitoManager.Get(reportHandler).Status)
@ -550,9 +550,14 @@ public class ReportManager
{
if (report != null)
{
if (!report.getHandlerTask().isPresent())
{
new ReportHandlerTask(this, report).start(_plugin);
}
sendHandlerNotification(report,
F.main(getReportPrefix(report),
String.format("%s has re-joined the game.", player.getName())));
String.format("%s has joined %s.", player.getName(), F.elem(_serverName))));
}
}
);
@ -567,9 +572,18 @@ public class ReportManager
{
if (report != null)
{
Optional<ReportHandlerTask> handlerTaskOptional = report.getHandlerTask();
if (handlerTaskOptional.isPresent())
{
ReportHandlerTask handlerTask = handlerTaskOptional.get();
handlerTask.cancel();
report.setHandlerTask(null);
}
sendHandlerNotification(report,
F.main(getReportPrefix(report),
String.format("%s has left the game.", player.getName())));
String.format("%s has left %s.", player.getName(), F.elem(_serverName))));
}
}
);

View File

@ -6,6 +6,7 @@ import java.util.Optional;
import java.util.Set;
import mineplex.core.report.ReportCategory;
import mineplex.core.report.ReportHandlerTask;
import mineplex.core.report.ReportResult;
/**
@ -21,6 +22,8 @@ public class Report
private Integer _handlerId = null;
private ReportResult _reportResult = null;
private ReportHandlerTask _handlerTask = null;
public Report(int suspectId, ReportCategory category)
{
this(null, suspectId, category);
@ -107,4 +110,14 @@ public class Report
return latest;
}
public Optional<ReportHandlerTask> getHandlerTask()
{
return Optional.ofNullable(_handlerTask);
}
public void setHandlerTask(ReportHandlerTask handlerTask)
{
_handlerTask = handlerTask;
}
}