+ Highly experimental last message info now saved

This commit is contained in:
Matthew Cech
2019-09-14 00:56:45 -07:00
parent 9d0bbf0ed1
commit 9b5ae3371d
13 changed files with 252 additions and 114 deletions
+8 -2
View File
@@ -1,5 +1,6 @@
package commands.dew; package commands.dew;
import commands.dew.core.impl.DewCore;
import core.Command; import core.Command;
import core.LocStrings; import core.LocStrings;
import core.SubCommandFramework; import core.SubCommandFramework;
@@ -11,8 +12,10 @@ import dataStructures.KittyUser;
import dataStructures.Response; import dataStructures.Response;
import dataStructures.UserInput; import dataStructures.UserInput;
public class CommandDew extends Command { public class CommandDew extends Command
{
@SuppressWarnings("unused")
public static DewCore core;
SubCommandFramework framework = new SubCommandFramework(); SubCommandFramework framework = new SubCommandFramework();
@Override @Override
@@ -28,11 +31,14 @@ public class CommandDew extends Command {
{ {
super(level, rating); super(level, rating);
core = new DewCore(framework);
framework.addCommand("luatest", new CommandDewLuaTest(KittyRole.Dev, KittyRating.Safe)); framework.addCommand("luatest", new CommandDewLuaTest(KittyRole.Dev, KittyRating.Safe));
framework.addCommand("realm", new CommandDewRealm(KittyRole.Dev, KittyRating.Safe)); framework.addCommand("realm", new CommandDewRealm(KittyRole.Dev, KittyRating.Safe));
framework.addCommand("w", new CommandDewInput("w", KittyRole.Dev, KittyRating.Safe)); framework.addCommand("w", new CommandDewInput("w", KittyRole.Dev, KittyRating.Safe));
framework.addCommand("a", new CommandDewInput("a", KittyRole.Dev, KittyRating.Safe)); framework.addCommand("a", new CommandDewInput("a", KittyRole.Dev, KittyRating.Safe));
framework.addCommand("s", new CommandDewInput("s", KittyRole.Dev, KittyRating.Safe)); framework.addCommand("s", new CommandDewInput("s", KittyRole.Dev, KittyRating.Safe));
framework.addCommand("d", new CommandDewInput("d", KittyRole.Dev, KittyRating.Safe)); framework.addCommand("d", new CommandDewInput("d", KittyRole.Dev, KittyRating.Safe));
framework.addCommand("capture", new CommandDewCapture(KittyRole.Dev, KittyRating.Safe));
} }
} }
+28
View File
@@ -0,0 +1,28 @@
package commands.dew;
import commands.dew.adapter.KittyAdapterDewPlayer;
import commands.dew.core.impl.DewCore;
import core.SubCommand;
import core.SubCommandFormattable;
import dataStructures.KittyChannel;
import dataStructures.KittyGuild;
import dataStructures.KittyRating;
import dataStructures.KittyRole;
import dataStructures.KittyUser;
import dataStructures.UserInput;
public class CommandDewCapture extends SubCommand
{
public CommandDewCapture(KittyRole roleLevel, KittyRating contentRating)
{
super(roleLevel, contentRating);
}
@Override
public SubCommandFormattable OnRun(KittyGuild guild, KittyChannel channel, KittyUser user, UserInput input)
{
KittyAdapterDewPlayer player = DewCore.getOrCreate(user);
DewCore.playersCaptured.add(player);
return null;//new SubCommandFormattable("captured input for " + user.name);
}
}
+3 -2
View File
@@ -1,6 +1,7 @@
package commands.dew; package commands.dew;
import commands.dew.adapter.KittyAdapterDewPlayer; import commands.dew.adapter.KittyAdapterDewPlayer;
import commands.dew.core.impl.DewCore;
import core.SubCommand; import core.SubCommand;
import core.SubCommandFormattable; import core.SubCommandFormattable;
import dataStructures.KittyChannel; import dataStructures.KittyChannel;
@@ -22,7 +23,7 @@ public class CommandDewInput extends SubCommand
@Override @Override
public SubCommandFormattable OnRun(KittyGuild guild, KittyChannel channel, KittyUser user, UserInput input) public SubCommandFormattable OnRun(KittyGuild guild, KittyChannel channel, KittyUser user, UserInput input)
{ {
KittyAdapterDewPlayer player = CommandDewRealm.getOrCreate(user); KittyAdapterDewPlayer player = DewCore.getOrCreate(user);
int x = player.getRealmX(); int x = player.getRealmX();
int y = player.getRealmY(); int y = player.getRealmY();
@@ -35,6 +36,6 @@ public class CommandDewInput extends SubCommand
case "d": player.setRealmPos(x + 1, y); break; case "d": player.setRealmPos(x + 1, y); break;
} }
return new SubCommandFormattable(CommandDewRealm.drawWorld(player)); return new SubCommandFormattable(DewCore.drawWorld(player));
} }
} }
+3 -90
View File
@@ -1,11 +1,7 @@
package commands.dew; package commands.dew;
import java.util.ArrayList;
import java.util.List;
import commands.dew.adapter.KittyAdapterDewPlayer; import commands.dew.adapter.KittyAdapterDewPlayer;
import commands.dew.core.data.DewMapTile; import commands.dew.core.impl.DewCore;
import commands.dew.core.data.DewRealm;
import core.SubCommand; import core.SubCommand;
import core.SubCommandFormattable; import core.SubCommandFormattable;
import dataStructures.KittyChannel; import dataStructures.KittyChannel;
@@ -17,100 +13,17 @@ import dataStructures.UserInput;
public class CommandDewRealm extends SubCommand public class CommandDewRealm extends SubCommand
{ {
// TODO: CHANGE THIS! THIS IS TEMP STATIC, DO NOT CONTINUE TO DO THIS
private static DewRealm realm;
public static List<KittyAdapterDewPlayer> players;
public CommandDewRealm(KittyRole roleLevel, KittyRating contentRating) public CommandDewRealm(KittyRole roleLevel, KittyRating contentRating)
{ {
super(roleLevel, contentRating); super(roleLevel, contentRating);
realm = new DewRealm();
players = new ArrayList<KittyAdapterDewPlayer>();
for(int y = 0; y < realm.map.length; ++y)
{
for(int x = 0; x < realm.map[y].length; ++x)
{
realm.map[y][x] = DewMapTile.Grass.getValue();
}
}
}
public static KittyAdapterDewPlayer getOrCreate(KittyUser user)
{
for(KittyAdapterDewPlayer player : players)
{
if(player.getUniqueID() == user.uniqueID)
{
return player;
}
}
KittyAdapterDewPlayer player = new KittyAdapterDewPlayer(realm, user, 10, 10);
players.add(player);
return player;
}
public static String drawWorld(KittyAdapterDewPlayer player)
{
// Build empty world buffer
String[][] worldBuffer = new String[realm.map.length][realm.map[0].length];
// Build world
for(int y = 0; y < realm.map.length; ++y)
{
for(int x = 0; x < realm.map[y].length; ++x)
{
switch(DewMapTile.values()[realm.map[y][x]])
{
case Grass:
worldBuffer[y][x] = ",";
break;
default:
worldBuffer[y][x] = " ";
break;
}
}
}
// Build players
for(KittyAdapterDewPlayer p : players)
{
worldBuffer[p.getRealmY()][p.getRealmX()] = p.getVisual();
}
// Draw world
String out = "";
out += "**Realm ID - ** `" + realm.id + "`\n";
for(int y = 0; y < worldBuffer.length; ++y)
{
out += "`";
for(int x = 0; x < worldBuffer[y].length; ++x)
{
out += worldBuffer[y][x];
}
out += "`";
out += "\n";
}
out += "**Stats**\n";
out += "`Player Name` - `" + player.getName() + "`\n";
out += "`Player Icon` - `" + player.getVisual() + "`\n";
return out;
} }
@Override @Override
public SubCommandFormattable OnRun(KittyGuild guild, KittyChannel channel, KittyUser user, UserInput input) public SubCommandFormattable OnRun(KittyGuild guild, KittyChannel channel, KittyUser user, UserInput input)
{ {
KittyAdapterDewPlayer player = getOrCreate(user); KittyAdapterDewPlayer player = DewCore.getOrCreate(user);
String out = drawWorld(player); String out = DewCore.drawWorld(player);
return new SubCommandFormattable(out); return new SubCommandFormattable(out);
} }
@@ -1,7 +1,5 @@
package commands.dew.core.data; package commands.dew.core.data;
import commands.dew.core.api.IDewLuaSerializable;
public enum DewMapTile public enum DewMapTile
{ {
// Assign multiples of 2 for flag purposes // Assign multiples of 2 for flag purposes
+162 -3
View File
@@ -1,11 +1,170 @@
package commands.dew.core.impl; package commands.dew.core.impl;
import commands.dew.core.api.IDewPlayer; import java.util.ArrayList;
import java.util.List;
import commands.dew.adapter.KittyAdapterDewPlayer;
import commands.dew.core.data.DewMapTile;
import commands.dew.core.data.DewRealm;
import core.SubCommandFormattable;
import core.SubCommandFramework;
import dataStructures.KittyChannel;
import dataStructures.KittyGuild;
import dataStructures.KittyUser;
import dataStructures.Response;
import dataStructures.UserInput;
import utils.GlobalLog;
import utils.LogFilter;
public class DewCore public class DewCore
{ {
public DewCore(IDewPlayer player) // TODO: Make realm and players both per-realm
{ public static DewRealm realm;
public static List<KittyAdapterDewPlayer> players;
public static List<KittyAdapterDewPlayer> playersCaptured;
private static SubCommandFramework framework;
public DewCore(SubCommandFramework framework)
{
DewCore.framework = framework;
DewCore.realm = new DewRealm();
DewCore.players = new ArrayList<KittyAdapterDewPlayer>();
DewCore.playersCaptured = new ArrayList<KittyAdapterDewPlayer>();
for(int y = 0; y < realm.map.length; ++y)
{
for(int x = 0; x < realm.map[y].length; ++x)
{
realm.map[y][x] = DewMapTile.Grass.getValue();
}
}
}
// Returns false if not captured, true if captured.
public static boolean ProcessIfCaptured(KittyGuild guild, KittyChannel channel, KittyUser user, UserInput baseInput, Response res)
{
// Search for a captured player.
KittyAdapterDewPlayer player = null;
for(KittyAdapterDewPlayer p : playersCaptured)
{
if(p.getUniqueID() == user.uniqueID)
{
player = p;
break;
}
}
// If we didn't find that the person is captured, return false since we don't need to do anything.
if(player == null)
return false;
// See if we run anything - also, check to see if we need to exit.
boolean didRun = false;
try
{
UserInput toEdit = new UserInput(baseInput);
String key = toEdit.message.toLowerCase().trim();
toEdit.key = key;
toEdit.args = key;
switch(key)
{
case "cap":
case "capture":
case "uncapture":
case "stop":
case "quit":
case "exit":
playersCaptured.remove(player);
return false;
}
SubCommandFormattable f = framework.run(guild, channel, user, toEdit);
if(f != null)
{
Response.LastSendMessage.editMessage(f.resString).complete();
//f.Call(res);
didRun = true;
}
}
catch(Exception e)
{
GlobalLog.error(LogFilter.Command, e);
}
if(didRun)
{
baseInput.deleteOriginInput();
}
return didRun;
}
public static KittyAdapterDewPlayer getOrCreate(KittyUser user)
{
for(KittyAdapterDewPlayer player : players)
{
if(player.getUniqueID() == user.uniqueID)
{
return player;
}
}
KittyAdapterDewPlayer player = new KittyAdapterDewPlayer(realm, user, 10, 10);
players.add(player);
return player;
}
public static String drawWorld(KittyAdapterDewPlayer player)
{
// Build empty world buffer
String[][] worldBuffer = new String[realm.map.length][realm.map[0].length];
// Build world
for(int y = 0; y < realm.map.length; ++y)
{
for(int x = 0; x < realm.map[y].length; ++x)
{
switch(DewMapTile.values()[realm.map[y][x]])
{
case Grass:
worldBuffer[y][x] = ",";
break;
default:
worldBuffer[y][x] = " ";
break;
}
}
}
// Build players
for(KittyAdapterDewPlayer p : players)
{
worldBuffer[p.getRealmY()][p.getRealmX()] = p.getVisual();
}
// Draw world
String out = "";
out += "**Realm ID - ** `" + realm.id + "`\n";
for(int y = 0; y < worldBuffer.length; ++y)
{
out += "`";
for(int x = 0; x < worldBuffer[y].length; ++x)
{
out += worldBuffer[y][x];
}
out += "`";
out += "\n";
}
out += "**Stats**\n";
out += "`Player Name` - `" + player.getName() + "`\n";
out += "`Player Icon` - `" + player.getVisual() + "`\n";
return out;
} }
} }
@@ -1,7 +1,6 @@
package commands.dew.core.impl; package commands.dew.core.impl;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import org.luaj.vm2.Globals; import org.luaj.vm2.Globals;
import org.luaj.vm2.LuaValue; import org.luaj.vm2.LuaValue;
@@ -1,7 +1,5 @@
package commands.music; package commands.music;
import com.google.api.services.youtube.YouTube;
import core.SubCommand; import core.SubCommand;
import core.SubCommandFormattable; import core.SubCommandFormattable;
import dataStructures.KittyChannel; import dataStructures.KittyChannel;
@@ -13,13 +11,11 @@ import dataStructures.UserInput;
public class SubCommandAddTrack extends SubCommand public class SubCommandAddTrack extends SubCommand
{ {
public SubCommandAddTrack(KittyRole level, KittyRating rating) { super(level, rating); } public SubCommandAddTrack(KittyRole level, KittyRating rating) { super(level, rating); }
@Override @Override
public SubCommandFormattable OnRun(KittyGuild guild, KittyChannel channel, KittyUser user, UserInput input) public SubCommandFormattable OnRun(KittyGuild guild, KittyChannel channel, KittyUser user, UserInput input)
{ {
return null; return null;
} }
} }
+6 -1
View File
@@ -7,6 +7,7 @@ import dataStructures.KittyGuild;
import dataStructures.KittyUser; import dataStructures.KittyUser;
import dataStructures.UserInput; import dataStructures.UserInput;
import utils.GlobalLog; import utils.GlobalLog;
import utils.LogFilter;
public class SubCommandFramework public class SubCommandFramework
{ {
@@ -41,10 +42,14 @@ public class SubCommandFramework
} }
catch(Exception e) catch(Exception e)
{ {
GlobalLog.error(LogFilter.Command, e);
String help = Stats.instance.getHelpText(input.key); String help = Stats.instance.getHelpText(input.key);
if(help != null) if(help != null)
return new SubCommandFormattable(help); return new SubCommandFormattable(help);
return new SubCommandFormattable("Something has gone very wrong.");
return new SubCommandFormattable("Whoops! Internal subcommand error!");
} }
} }
} }
+14 -10
View File
@@ -5,6 +5,7 @@ import java.io.InputStream;
import net.dv8tion.jda.core.JDA; import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.MessageBuilder; import net.dv8tion.jda.core.MessageBuilder;
import net.dv8tion.jda.core.entities.Message;
import net.dv8tion.jda.core.entities.TextChannel; import net.dv8tion.jda.core.entities.TextChannel;
import net.dv8tion.jda.core.events.message.guild.*; import net.dv8tion.jda.core.events.message.guild.*;
import net.dv8tion.jda.core.EmbedBuilder; import net.dv8tion.jda.core.EmbedBuilder;
@@ -16,6 +17,9 @@ import utils.LogFilter;
// commands aren't exposed to the GuildMessageReceivedEvent then we should be fine. // commands aren't exposed to the GuildMessageReceivedEvent then we should be fine.
public class Response public class Response
{ {
// Last message
public static Message LastSendMessage = null;
// Variables // Variables
private GuildMessageReceivedEvent event; private GuildMessageReceivedEvent event;
private final int discordMessageMax = 2000; private final int discordMessageMax = 2000;
@@ -75,7 +79,7 @@ public class Response
File thumbImage = new File(thumbName); File thumbImage = new File(thumbName);
if(thumbImage.exists()) if(thumbImage.exists())
{ {
event.getChannel().sendFile(thumbImage, thumbName, message.build()).queue(); LastSendMessage = event.getChannel().sendFile(thumbImage, thumbName, message.build()).complete();
} }
} }
else else
@@ -83,7 +87,7 @@ public class Response
if(embedInfo.thumbnailURL != null) if(embedInfo.thumbnailURL != null)
embed.setThumbnail(embedInfo.thumbnailURL); embed.setThumbnail(embedInfo.thumbnailURL);
event.getChannel().sendMessage(embed.build()).queue(); LastSendMessage = event.getChannel().sendMessage(embed.build()).complete();
} }
} }
@@ -93,11 +97,11 @@ public class Response
GlobalLog.log(LogFilter.Response, "Sending response: " + toRespondWith); GlobalLog.log(LogFilter.Response, "Sending response: " + toRespondWith);
if(toRespondWith.length() > discordMessageMax) if(toRespondWith.length() > discordMessageMax)
{ {
event.getChannel().sendMessage(toRespondWith.substring(0, kittyMessageMax) + "\n\nI think that's enough!").queue(); LastSendMessage = event.getChannel().sendMessage(toRespondWith.substring(0, kittyMessageMax) + "\n\nI think that's enough!").complete();
} }
else else
{ {
event.getChannel().sendMessage(toRespondWith).queue(); LastSendMessage = event.getChannel().sendMessage(toRespondWith).complete();
} }
} }
@@ -108,11 +112,11 @@ public class Response
channel = kitty.getTextChannelById(Long.parseLong(channelID)); channel = kitty.getTextChannelById(Long.parseLong(channelID));
if(toRespondWith.length() > discordMessageMax) if(toRespondWith.length() > discordMessageMax)
{ {
channel.sendMessage(toRespondWith.substring(0, kittyMessageMax) + "\n\nI think that's enough!").queue(); LastSendMessage = channel.sendMessage(toRespondWith.substring(0, kittyMessageMax) + "\n\nI think that's enough!").complete();
} }
else else
{ {
channel.sendMessage(toRespondWith).queue(); LastSendMessage = channel.sendMessage(toRespondWith).complete();
} }
} }
@@ -122,11 +126,11 @@ public class Response
GlobalLog.log(LogFilter.Response, "Sending immediate response: " + toRespondWith); GlobalLog.log(LogFilter.Response, "Sending immediate response: " + toRespondWith);
if(toRespondWith.length() > discordMessageMax) if(toRespondWith.length() > discordMessageMax)
{ {
event.getChannel().sendMessage(toRespondWith.substring(0, kittyMessageMax) + "\n\nI think that's enough!"); LastSendMessage = event.getChannel().sendMessage(toRespondWith.substring(0, kittyMessageMax) + "\n\nI think that's enough!").complete();
} }
else else
{ {
event.getChannel().sendMessage(toRespondWith).complete(); LastSendMessage = event.getChannel().sendMessage(toRespondWith).complete();
} }
} }
@@ -134,13 +138,13 @@ public class Response
public void sendFile(File toRespondWith, String extension) public void sendFile(File toRespondWith, String extension)
{ {
GlobalLog.log(LogFilter.Response, "Sending file response"); GlobalLog.log(LogFilter.Response, "Sending file response");
event.getChannel().sendFile(toRespondWith, "return." + extension).queue(); LastSendMessage = event.getChannel().sendFile(toRespondWith, "return." + extension).complete();
} }
// Queues an input stream response to the channel that issued the command. // Queues an input stream response to the channel that issued the command.
public void sendFile(InputStream in, String extension) public void sendFile(InputStream in, String extension)
{ {
GlobalLog.log(LogFilter.Response, "Sending input stream response"); GlobalLog.log(LogFilter.Response, "Sending input stream response");
event.getChannel().sendFile(in, "return." + extension).queue(); LastSendMessage = event.getChannel().sendFile(in, "return." + extension).complete();
} }
} }
+21
View File
@@ -4,6 +4,7 @@ import java.util.List;
import core.ObjectBuilderFactory; import core.ObjectBuilderFactory;
import net.dv8tion.jda.core.entities.Member; import net.dv8tion.jda.core.entities.Member;
import net.dv8tion.jda.core.entities.Message;
import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent; import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent;
public class UserInput public class UserInput
@@ -14,6 +15,7 @@ public class UserInput
public KittyUser [] mentions; public KittyUser [] mentions;
private boolean isValid; private boolean isValid;
public String message; public String message;
private Message internalJDAMessage;
// NOTE(wisp): Designed to parse a string as it's constructed. // NOTE(wisp): Designed to parse a string as it's constructed.
// If a user enters the command "!ping some stuff here :D" // If a user enters the command "!ping some stuff here :D"
@@ -25,6 +27,7 @@ public class UserInput
// The CommandIndicator and spaces between the key and args are dropped. // The CommandIndicator and spaces between the key and args are dropped.
public UserInput(GuildMessageReceivedEvent event, KittyGuild guildContext) public UserInput(GuildMessageReceivedEvent event, KittyGuild guildContext)
{ {
this.internalJDAMessage = event.getMessage();
String toParse = event.getMessage().getContentRaw(); String toParse = event.getMessage().getContentRaw();
message = toParse; message = toParse;
@@ -61,6 +64,18 @@ public class UserInput
} }
} }
// Copy Constructor
public UserInput(UserInput other)
{
this.key = other.key;
this.args = other.args;
this.mentions = other.mentions;
this.isValid = other.isValid;
this.message = other.message;
this.internalJDAMessage = other.internalJDAMessage;
}
// Used for injecting direct commands from plugins // Used for injecting direct commands from plugins
public UserInput(String input, KittyGuild contextGuild) public UserInput(String input, KittyGuild contextGuild)
{ {
@@ -75,6 +90,12 @@ public class UserInput
return isValid; return isValid;
} }
// This deletes the input that was originally sent
public void deleteOriginInput()
{
internalJDAMessage.delete().queue();
}
// Finds first whitespace in the string // Finds first whitespace in the string
private int findFirstWhitespace(String str) private int findFirstWhitespace(String str)
{ {
+7
View File
@@ -4,6 +4,7 @@ import java.util.List;
import javax.security.auth.login.LoginException; import javax.security.auth.login.LoginException;
import commands.dew.core.impl.DewCore;
import core.CharacterManager; import core.CharacterManager;
import core.CommandEnabler; import core.CommandEnabler;
import core.CommandManager; import core.CommandManager;
@@ -89,6 +90,10 @@ public class Main extends ListenerAdapter
if(!Superintendent.perCommandUpkeepPre(pluginManager)) if(!Superintendent.perCommandUpkeepPre(pluginManager))
return; return;
// TODO: Condense outer if somehow...
if(!DewCore.ProcessIfCaptured(guild, channel, user, input, response))
{
// 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)
@@ -111,6 +116,8 @@ public class Main extends ListenerAdapter
} }
} }
}
// Run any upkeep in post we need to // Run any upkeep in post we need to
if(!Superintendent.perCommandUpkeepPost(kittyCore, databaseManager)) if(!Superintendent.perCommandUpkeepPost(kittyCore, databaseManager))
return; return;
+1
View File
@@ -2,6 +2,7 @@ package main;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import commands.dew.core.impl.DewCore;
import core.Config; import core.Config;
import core.DatabaseManager; import core.DatabaseManager;
import core.RPManager; import core.RPManager;