Merge branch 'develop' into feature/format_refactor

This commit is contained in:
Matthew Cech
2019-06-18 01:05:22 -07:00
40 changed files with 978 additions and 113 deletions
+94
View File
@@ -0,0 +1,94 @@
package core;
import java.util.ArrayList;
import java.util.Vector;
import dataStructures.KittyCharacter;
import dataStructures.KittyTrackedLong;
import dataStructures.KittyUser;
import me.xdrop.fuzzywuzzy.FuzzySearch;
import utils.GlobalLog;
import utils.LogFilter;
public class CharacterManager
{
public static CharacterManager instance;
KittyTrackedLong uniqueID = new KittyTrackedLong("CharacterIDCounter", "");
private Vector <KittyCharacter> characters = new Vector<KittyCharacter>();
public CharacterManager()
{
if(instance == null)
{
instance = this;
}
else
{
GlobalLog.Error(LogFilter.Core, "Attempted to create a second CharacterManager singleton!");
return;
}
}
public ArrayList<KittyCharacter> searchCharacter(String query)
{
ArrayList<KittyCharacter> chars = new ArrayList<KittyCharacter>();
try
{
long UID = Long.parseLong(query);
for(KittyCharacter character:characters)
{
if(character.getUID() == UID)
{
chars.add(character);
break;
}
}
}catch(Exception e)
{
for(KittyCharacter character:characters)
{
if(FuzzySearch.ratio(query.toLowerCase(), character.getName().toLowerCase()) > 60)
{
chars.add(character);
}
}
}
return chars;
}
public boolean addCharacter(KittyUser user, String name, String bio, String refImage)
{
for(KittyCharacter character:characters)
{
if(character.getName().equals(name) && character.getOwner().discordID.equals(user.discordID))
{
return false;
}
}
characters.add(new KittyCharacter(user, name, bio, refImage, uniqueID.Get()));
uniqueID.Add(1);
return true;
}
public void removeCharacter(KittyCharacter character, String name)
{
characters.remove(character);
}
public void editRefImage(KittyCharacter character, String refImage)
{
character.editRefImage(refImage);
}
public void editBio(KittyCharacter character, String bio)
{
character.editBio(bio);
}
public void editName(KittyCharacter character, String name)
{
character.editName(name);
}
}
+3 -3
View File
@@ -111,12 +111,12 @@ public class CommandManager
}
// Looks up command by name. If it exists, dumps help text, otherwise returns null.
public String GetCommandHelpText(String lookup)
public String GetCommandHelpText(String key)
{
Command command = commands.get(lookup);
Command command = commands.get(key);
if(command != null)
return command.getHelpText();
return "`" + key + "`: " + command.getHelpText();
return null;
}
+8
View File
@@ -0,0 +1,8 @@
package core;
import java.awt.Color;
public final class Config
{
public static final Color ColorDefault = new Color(7*16, 8*16, 9*16); // A slate-grey
}
+27 -1
View File
@@ -2,6 +2,8 @@ package core;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import network.JDBCDriver;
import network.JDBCDriverSQLite;
@@ -72,7 +74,7 @@ public class DatabaseDriverKeyValue
// Creates a key. The key will be created if it doesn't exist, and the default value returned.
public String CreateGetKey(String key)
{
GlobalLog.Log(LogFilter.Database, "CreateGeyKey: key-" + key);
GlobalLog.Log(LogFilter.Database, "CreateGetKey: " + key);
if(HasKey(key))
{
@@ -119,6 +121,30 @@ public class DatabaseDriverKeyValue
GlobalLog.Log(LogFilter.Database, "CreateKey status: " + status);
}
// Get all keys containing a substring
public List<String> GetKeysWith(String keySubstring)
{
String command = "SELECT * FROM " + tableName + " WHERE " + keyColumnName + " like ?";
ResultSet result = driver.ExecuteReturningStatement(JDBCStatementType.Select, command, new String[] { "%" + keySubstring + "%" });
List<String> keys = new ArrayList<String>();
try
{
while(result.next())
{
String key = (String) result.getObject(1);
keys.add(key);
}
}
catch (SQLException e)
{
e.printStackTrace();
}
return keys;
}
// Transforms a result into a string if possible.
private String ResultAsString(ResultSet rs, String key)
{
+9 -2
View File
@@ -1,6 +1,7 @@
package core;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import utils.GlobalLog;
import utils.LogFilter;
@@ -128,6 +129,10 @@ public class DatabaseManager
}
}
public List<String> scrapeGlobalForString(String substring)
{
return globalDataDriver.GetKeysWith(substring);
}
/////////////////
// Global Data //
@@ -141,7 +146,8 @@ public class DatabaseManager
}
}
private String globalGetRemoteValue(String key)
// You can get values, but not modify them.
public String globalGetRemoteValue(String key)
{
synchronized(globalDataDriver)
{
@@ -170,7 +176,8 @@ public class DatabaseManager
}
}
private String characterGetRemoteValue(String key)
// You can get values, but not modify them.
public String characterGetRemoteValue(String key)
{
synchronized(characterDataDriver)
{
+78 -35
View File
@@ -4,12 +4,23 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Semaphore;
import javax.security.auth.login.LoginException;
import commands.*;
import core.lua.PluginManager;
import dataStructures.*;
import main.Main;
import net.dv8tion.jda.core.AccountType;
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.JDABuilder;
import net.dv8tion.jda.core.entities.Emote;
import net.dv8tion.jda.core.entities.Game;
import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.entities.Member;
import net.dv8tion.jda.core.entities.User;
import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent;
import offline.Ref;
import utils.AdminControl;
import utils.GlobalLog;
import utils.LogFilter;
@@ -53,6 +64,7 @@ public class ObjectBuilderFactory
// TODO: Investigate using 'synchronized' instead potentially
private static boolean hasInitialized;
private static Semaphore initMutex = new Semaphore(1);
private static JDA kitty;
// This is it, this is how the lazy init starts!
private static void LazyInit()
@@ -265,6 +277,7 @@ public class ObjectBuilderFactory
return user;
}
// TODO: Clean up
public static KittyUser extractUserByJDAUser(String guildID, String name, String userID, String avatarID, String discordID)
{
LazyInit();
@@ -295,7 +308,9 @@ public class ObjectBuilderFactory
return user;
}
public static KittyUser getCachedUser(String guildID, String userID)
// There is some redundant lookup occurring. If the user isn't cached yet, then we construct them and cache them.
// The assumption is made that the user does, in fact, exist.
public static KittyUser getKittyUser(String guildID, String userID)
{
String uid = guildID + userID;
KittyUser user = null;
@@ -304,6 +319,16 @@ public class ObjectBuilderFactory
user = userCache.get(uid);
}
if(user == null)
{
Guild jdaGuild = kitty.getGuildById(guildID);
Member jdaMember = jdaGuild.getMemberById(userID);
User jdaUser = jdaMember.getUser();
user = ExtractUserByJDAUser(guildID, jdaMember.getNickname(), jdaUser.getId(), jdaUser.getAvatarUrl(), jdaUser.getId());
updateUser(user, jdaMember);
}
return user;
}
@@ -326,6 +351,17 @@ public class ObjectBuilderFactory
// Construction Methods //
//////////////////////////
public static KittyCore ConstructKittyCore() throws LoginException, InterruptedException
{
LazyInit();
kitty = new JDABuilder(AccountType.BOT).setToken(Ref.TestToken).buildBlocking();
kitty.getPresence().setGame(Game.playing("with digital yarn"));
kitty.addEventListener(new Main());
return new KittyCore(kitty);
}
// 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
// the CommandEnabler object passed in. In theory, we could have multiple CommandManagers, tho we can
@@ -361,39 +397,46 @@ public class ObjectBuilderFactory
manager.Register(LocCommands.stub("raffleend"), new CommandRaffleEnd(KittyRole.Mod, KittyRating.Safe));
// General
manager.Register(LocCommands.stub("fetch"), new CommandFetch(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.stub("guildroleadd"), new CommandGuildRoleAdd(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.stub("guildroleremove"), new CommandGuildRoleRemove(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.stub("teey"), new CommandTeey(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.stub("perish, thenperish"), new CommandPerish(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.stub("yeet"), new CommandYeet(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.stub("ping"), new CommandPing(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.stub("boop"), new CommandBoop(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.stub("roll"), new CommandRoll(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.stub("choose"), new CommandChoose(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.stub("help"), new CommandHelp(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.stub("info, about"), new CommandInfo(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.stub("vote"), new CommandPollVote(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.stub("results"), new CommandPollResults(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.stub("showpoll"), new CommandPollShow(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.stub("wolfram"), new CommandWolfram(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.stub("c++, g++, cplus, cpp"), new CommandColiru(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.stub("java, jdoodle"), new CommandJDoodle(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.stub("beans"), new CommandBeansShow(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.stub("role"), new CommandRole(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.stub("bet"), new CommandBetBeans(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.stub("map"), new CommandMap(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.stub("rpstart"), new CommandRPStart(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.stub("rpend"), new CommandRPEnd(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.stub("tony, stark, dontfeelgood, dontfeelsogood"), new CommandStark(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.stub("blur"), new CommandBlurry(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.stub("eightball, 8ball"), new CommandEightBall(KittyRole.General, KittyRating.Safe));
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("bethistory"), new CommandBetHistory(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.stub("crouton"), new CommandCrouton(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.stub("benchmark, bench"), new CommandBenchmark(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.stub("rafflejoin"), new CommandRaffleJoin(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.Stub("fetch"), new CommandFetch(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.Stub("guildroleadd"), new CommandGuildRoleAdd(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.Stub("guildroleremove"), new CommandGuildRoleRemove(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.Stub("teey"), new CommandTeey(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.Stub("perish, thenperish"), new CommandPerish(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.Stub("yeet"), new CommandYeet(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.Stub("ping"), new CommandPing(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.Stub("boop"), new CommandBoop(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.Stub("roll"), new CommandRoll(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.Stub("choose"), new CommandChoose(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.Stub("help"), new CommandHelp(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.Stub("info, about"), new CommandInfo(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.Stub("vote"), new CommandPollVote(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.Stub("results"), new CommandPollResults(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.Stub("showpoll"), new CommandPollShow(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.Stub("wolfram"), new CommandWolfram(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.Stub("c++, g++, cplus, cpp"), new CommandColiru(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.Stub("java, jdoodle"), new CommandJDoodle(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.Stub("beans"), new CommandBeansShow(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.Stub("role"), new CommandRole(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.Stub("bet"), new CommandBetBeans(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.Stub("map"), new CommandMap(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.Stub("rpstart"), new CommandRPStart(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.Stub("rpend"), new CommandRPEnd(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.Stub("tony, stark, dontfeelgood, dontfeelsogood"), new CommandStark(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.Stub("blur"), new CommandBlurry(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.Stub("eightball, 8ball"), new CommandEightBall(KittyRole.General, KittyRating.Safe));
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("bethistory"), new CommandBetHistory(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.Stub("crouton"), new CommandCrouton(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.Stub("benchmark, bench"), new CommandBenchmark(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.Stub("rafflejoin"), new CommandRaffleJoin(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.Stub("charactercreate"), new CommandCharacterCreate(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.Stub("searchcharacter"), new CommandCharacterSearch(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.Stub("charactereditbio"), new CommandCharacterEditBio(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.Stub("charactereditname"), new CommandCharacterEditName(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.Stub("charactereditURL"), new CommandCharacterEditURL(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.Stub("leaderboard"), new CommandLeaderboard(KittyRole.General, KittyRating.Safe));
manager.Register(LocCommands.Stub("color, colour"), new CommandColor(KittyRole.General, KittyRating.Safe));
return manager;
}
@@ -473,4 +516,4 @@ public class ObjectBuilderFactory
return userCache.size();
}
}
}
}
+1 -3
View File
@@ -6,9 +6,7 @@ import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
import dataStructures.*;
import net.dv8tion.jda.core.JDA;
import utils.GlobalLog;
import utils.LogFilter;
@@ -57,7 +55,7 @@ public class RPManager
return log;
}
public static void Upkeep(JDA kitty)
public static void Upkeep(KittyCore kitty)
{
Response res = new Response(null, kitty);
String reminder = "";