+ Added command functionality base and modified command stub

This commit is contained in:
Matthew Cech
2019-05-05 15:22:49 -07:00
parent 854da8d8cd
commit 645e632dcb
9 changed files with 218 additions and 16 deletions
+40 -10
View File
@@ -6,6 +6,7 @@ import java.util.List;
import java.util.concurrent.Semaphore;
import commands.*;
import core.benchmark.BenchmarkManager;
import core.lua.PluginManager;
import dataStructures.*;
import net.dv8tion.jda.core.entities.Emote;
@@ -42,6 +43,9 @@ public class ObjectBuilderFactory
// Plugin manager
private static PluginManager pluginManager;
// Userbenchmark csv manager instance
private static BenchmarkManager benchmarkManager;
// Localization classes - these are singletons, but should be initialized before almost all other
// things so their inclusion in the factory is to ensure they're started at the correct time.
@SuppressWarnings("unused") private static LocStrings locStrings;
@@ -93,6 +97,10 @@ public class ObjectBuilderFactory
}
}
////////////////////////
// Extraction Methods //
////////////////////////
// Explicitly locks: guildCache
public static KittyGuild ExtractGuild(GuildMessageReceivedEvent event)
{
@@ -311,16 +319,9 @@ public class ObjectBuilderFactory
user.avatarID = member.getUser().getAvatarUrl();
}
// Constructs a CommandEnabler if it doesn't exist, and gets the existing one if it does.
public static CommandEnabler ConstructCommandEnabler()
{
LazyInit();
if(commandEnabler == null)
commandEnabler = new CommandEnabler();
return commandEnabler;
}
//////////////////////////
// Construction Methods //
//////////////////////////
// Default construction of the command manager. In order to remotely resolve command enabling
// and disabling, what we do is construct the commands with a localized pair that is checked against
@@ -378,9 +379,22 @@ public class ObjectBuilderFactory
manager.Register(LocCommands.Stub("catch"), new CommandCatch(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.Stub("guildrolelist"), new CommandGuildRoleList(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.Stub("benchmark, bench"), new CommandBenchmark(KittyRole.General, KittyRating.Safe));
return manager;
}
// Constructs a CommandEnabler if it doesn't exist, and gets the existing one if it does.
public static CommandEnabler ConstructCommandEnabler()
{
LazyInit();
if(commandEnabler == null)
commandEnabler = new CommandEnabler();
return commandEnabler;
}
// Default database manager construction. It can be constructed
// in different ways, and so we construct it outside of the constructor for
// the factory since it doesn't have to be present / can be elsewhere.
@@ -425,6 +439,21 @@ public class ObjectBuilderFactory
return pluginManager;
}
public static BenchmarkManager ConstructBenchmarkManager()
{
LazyInit();
if(benchmarkManager == null)
benchmarkManager = new BenchmarkManager();
return benchmarkManager;
}
/////////////////////
// Utility Methods //
/////////////////////
// Returns number of cached guilds (does not equal total users in the database, only what's in memory)
public static Integer GetGuildCount()
{ synchronized(guildCache)
{
@@ -432,6 +461,7 @@ public class ObjectBuilderFactory
}
}
// Returns number of cached users (does not equal total users in the database, only what's in memory)
public static Integer GetUserCount()
{ synchronized(userCache)
{
+27
View File
@@ -0,0 +1,27 @@
package core.benchmark;
public class BenchmarkEntry
{
public final BenchmarkType type;
public final String partNumber;
public final String brand;
public final String model;
public final int rank;
public final float benchmark;
public final int samples;
public final String url;
BenchmarkEntry(String input)
{
String[] row = input.split(",");
type = BenchmarkType.valueOf(row[0]);
partNumber = row[1];
brand = row[2];
model = row[3];
rank = Integer.parseInt(row[4]);
benchmark = Float.parseFloat(row[5]);
samples = Integer.parseInt(row[6]);
url = row[7];
}
}
+11
View File
@@ -0,0 +1,11 @@
package core.benchmark;
import utils.GlobalLog;
public class BenchmarkLog
{
// Logging
public static void Log(String str) { GlobalLog.Log("[Benchmark] " + str); }
public static void Warn(String str) { GlobalLog.Warn("[Benchmark] " + str); }
public static void Error(String str) { GlobalLog.Error(" [Benchmark] " + str); }
}
+106
View File
@@ -0,0 +1,106 @@
package core.benchmark;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import utils.FileUtils;
import utils.directoryMonitor.DirectoryMonitor;
import utils.directoryMonitor.MonitoredFile;
// All things considered, this doesn't need to be particularly efficient since anything
// less than 10ms of search time won't be noticable to an end user really, not for
// a networked application that's not performance bound.
public class BenchmarkManager
{
// Variables
public final String directory = "assets/userbench/";
public final String extension = ".csv";
public final String lineDelimiter = "\n";
private List<BenchmarkEntry> raw;
private DirectoryMonitor directoryMonitor;
private boolean needsUpdate;
// Read in all extensions
public BenchmarkManager()
{
long start = Instant.now().toEpochMilli();
raw = new ArrayList<BenchmarkEntry>();
directoryMonitor = new DirectoryMonitor(directory);
needsUpdate = false;
RebuildLookup();
BenchmarkLog.Log("Took " + (Instant.now().toEpochMilli() - start) + " ms to load " + raw.size() + " entries from " + directoryMonitor.GetCurrentFiles().size() + " file" + (raw.size() > 1 ? "s" : ""));
}
// Rebuilds the files being monitored
public void RebuildLookup()
{
synchronized(raw)
{
raw.clear();
List<MonitoredFile> files;
synchronized(directoryMonitor)
{
files = directoryMonitor.GetCurrentFiles();
}
if(files != null)
{
for(MonitoredFile mf : files)
{
String contents = FileUtils.ReadContent(mf.path);
String[] lines = contents.split(lineDelimiter);
for(int i = 1; i < lines.length; ++i)
raw.add(new BenchmarkEntry(lines[i]));
}
}
else
{
BenchmarkLog.Warn("No " + extension + " files where found in " + directory);
}
}
}
// Search for a substring in the model name
public List<BenchmarkEntry> FindModel(String modelSubstr)
{
long start = Instant.now().toEpochMilli();
List<BenchmarkEntry> matching = new ArrayList<BenchmarkEntry>();
String searchSubstr = modelSubstr.toLowerCase();
for(BenchmarkEntry e : raw)
{
String model = e.model.toLowerCase().trim();
if(model.contains(searchSubstr))
matching.add(e);
}
BenchmarkLog.Log("Searched for '" + searchSubstr + "' for "+ (Instant.now().toEpochMilli() - start) + "ms and found " + matching.size() + " entries.");
return matching;
}
// Keeps tabs on any changes of the files.
public void Update()
{
needsUpdate = false;
directoryMonitor.Update(this::OnRescan, this::OnRescan, this::OnRescan);
if(needsUpdate)
RebuildLookup();
}
// When a file is changed, handle it.
private void OnRescan(MonitoredFile file)
{
// For now, all we need is to note that something was adjusted.
if(file.path.endsWith(extension))
needsUpdate = true;
}
}
+17
View File
@@ -0,0 +1,17 @@
package core.benchmark;
public enum BenchmarkType
{
CPU(0), GPU(1), SSD(2), HDD(4), RAM(8), USB(16);
private final int value;
private BenchmarkType(int value)
{
this.value = value;
}
public int getValue()
{
return value;
}
}