+ Added lua user and stats

This commit is contained in:
Matthew Cech
2019-04-21 01:29:23 -07:00
parent 6d1b4aa9f3
commit b01b4be00f
6 changed files with 52 additions and 22 deletions
+16 -12
View File
@@ -26,13 +26,17 @@ public class CommandStats extends Command
String out = "```\n"; String out = "```\n";
Stats stats = Stats.instance; Stats stats = Stats.instance;
long seen = stats.GetMessagesSeen();
long processed = stats.GetCommandsProcessed();
out += "----- [General] -----\n"; out += "----- [General] -----\n";
out += " Messages observed: " + stats.GetMessagesSeen() + "\n"; out += " Messages observed: " + seen + "\n";
out += "Commands processed: " + stats.GetCommandsProcessed() + "\n"; out += "Commands processed: " + processed + "\n";
out += " Command invoke %: " + ((int)((processed / (float)seen) * 1000)) / 10.0f + "%\n";
out += " KittyBot uptime: " + stats.GetFormattedUptime() + "\n"; out += " KittyBot uptime: " + stats.GetFormattedUptime() + "\n";
out += "\n"; out += "\n";
out += "----- [Health] -----\n"; out += "----- [Health] -----\n";
out += " SMT cores: " + stats.GetCPUAvailable() + "\n"; out += " SMT cores: " + stats.GetCPUAvailable() + "\n";
// If CPU load works on this OS, list it. // If CPU load works on this OS, list it.
double CPULoad = stats.GetSystemCPULoad(); double CPULoad = stats.GetSystemCPULoad();
@@ -46,20 +50,20 @@ public class CommandStats extends Command
Integer timed_waiting = data.states.get(Thread.State.TIMED_WAITING); Integer timed_waiting = data.states.get(Thread.State.TIMED_WAITING);
Integer waiting = data.states.get(Thread.State.WAITING); Integer waiting = data.states.get(Thread.State.WAITING);
out += " Child threads:" out += " Threads:"
+ " run: " + (runnable == null ? 0 : runnable) + " run-" + (runnable == null ? 0 : runnable)
+ " block: " + (blocked == null ? 0 : blocked) + " block-" + (blocked == null ? 0 : blocked)
+ " sleep: " + (timed_waiting == null ? 0 : timed_waiting) + " sleep-" + (timed_waiting == null ? 0 : timed_waiting)
+ " wait: " + (waiting == null ? 0 : waiting) + " wait-" + (waiting == null ? 0 : waiting)
+ " term: " + (terminated == null ? 0 : terminated) + " term-" + (terminated == null ? 0 : terminated)
+ "\n"; + "\n";
Integer guildCount = stats.GetGuildCount(); Integer guildCount = stats.GetGuildCount();
Integer userCount = stats.GetUserCount(); Integer userCount = stats.GetUserCount();
out += "\n----- [Counts] -----\n" out += "\n----- [Cache] -----\n"
+ "Guilds: " + guildCount + "\n" + "Cached Guilds: " + guildCount + "\n"
+ " Users: " + userCount; + " Cached Users: " + userCount;
out += "\n```"; out += "\n```";
res.Call(out); res.Call(out);
+3 -2
View File
@@ -8,6 +8,7 @@ import java.util.stream.Stream;
import org.luaj.vm2.Globals; import org.luaj.vm2.Globals;
import org.luaj.vm2.LuaValue; import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.jse.CoerceJavaToLua;
import org.luaj.vm2.lib.jse.JsePlatform; import org.luaj.vm2.lib.jse.JsePlatform;
// To promote flexibility, plugins are lua file with predefined callbacks. // To promote flexibility, plugins are lua file with predefined callbacks.
@@ -47,11 +48,11 @@ public class Plugin
} }
} }
public String Run(String args) public String Run(String args, PluginUser user)
{ {
try try
{ {
LuaValue res = func_plugin.call(LuaValue.valueOf(args)); LuaValue res = func_plugin.call(LuaValue.valueOf(args), user.AsLua());
if(!res.isnil()) if(!res.isnil())
return res.toString(); return res.toString();
+4 -2
View File
@@ -6,6 +6,8 @@ import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.stream.Stream; import java.util.stream.Stream;
import dataStructures.KittyUser;
// Reads in, handles, and manipulates plugins. Plugins are loaded in the order they appear in the folder. // Reads in, handles, and manipulates plugins. Plugins are loaded in the order they appear in the folder.
public class PluginManager public class PluginManager
{ {
@@ -37,12 +39,12 @@ public class PluginManager
// Runs all plugins, returning when it gets a non-nill result. // Runs all plugins, returning when it gets a non-nill result.
// Otherwise, returns null. // Otherwise, returns null.
public String RunAll(String input) public String RunAll(String input, KittyUser user)
{ {
for(int i = 0; i < plugins.size(); ++i) for(int i = 0; i < plugins.size(); ++i)
{ {
Plugin plugin = plugins.get(i); Plugin plugin = plugins.get(i);
String out = plugin.Run(input); String out = plugin.Run(input, new PluginUser(user));
if(out != null) if(out != null)
{ {
+12
View File
@@ -0,0 +1,12 @@
package core.lua;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.jse.CoerceJavaToLua;
public class PluginStructure
{
public LuaValue AsLua()
{
return CoerceJavaToLua.coerce(this);
}
}
+13
View File
@@ -0,0 +1,13 @@
package core.lua;
import dataStructures.KittyUser;
public class PluginUser extends PluginStructure
{
public String name;
public PluginUser(KittyUser user)
{
this.name = user.name;
}
}
+4 -6
View File
@@ -3,6 +3,7 @@ package main;
import javax.security.auth.login.LoginException; import javax.security.auth.login.LoginException;
import core.*; import core.*;
import core.lua.PluginManager; import core.lua.PluginManager;
import core.lua.PluginUser;
import dataStructures.KittyChannel; import dataStructures.KittyChannel;
import dataStructures.KittyGuild; import dataStructures.KittyGuild;
import dataStructures.KittyRole; import dataStructures.KittyRole;
@@ -52,7 +53,7 @@ public class Main extends ListenerAdapter
@Override @Override
public void onGuildMessageReceived(GuildMessageReceivedEvent event) public void onGuildMessageReceived(GuildMessageReceivedEvent event)
{ {
// Tweak the event as necessary // Tweak the event as necessary
if(!PreProcessSetup(event)) if(!PreProcessSetup(event))
return; return;
@@ -71,19 +72,16 @@ public class Main extends ListenerAdapter
return; return;
// Track beans! // Track beans!
user.ChangeBeans(1); user.ChangeBeans(1);
// RP logging system // RP logging system
RPManager.instance.addLine(channel, user, input); RPManager.instance.addLine(channel, user, input);
//
// Attempt to spin up a command. If the command doesn't exist. // Attempt to spin up a command. If the command doesn't exist.
// Run plugins right before invoking the commands but after all other setup. // Run plugins right before invoking the commands but after all other setup.
if(commandManager.InvokeOnNewThread(guild, channel, user, input, response) == false) if(commandManager.InvokeOnNewThread(guild, channel, user, input, response) == false)
{ {
String pluginOutput = pluginManager.RunAll(input.message); String pluginOutput = pluginManager.RunAll(input.message, user);
if(pluginOutput != null) if(pluginOutput != null)
response.Call(pluginOutput); response.Call(pluginOutput);