Merge remote-tracking branch 'origin/develop'
This commit is contained in:
+43
-42
@@ -1,47 +1,48 @@
|
|||||||
indicator=1
|
[CommandEnabler]
|
||||||
boop=1
|
|
||||||
tony, stark, dontfeelgood, dontfeelsogood=1
|
|
||||||
yeet=1
|
|
||||||
role=1
|
|
||||||
ping=1
|
|
||||||
rating=1
|
|
||||||
roll=1
|
|
||||||
blur=1
|
|
||||||
choose=1
|
|
||||||
poll=1
|
|
||||||
rpstart=1
|
|
||||||
bet=1
|
|
||||||
perish, thenperish=1
|
|
||||||
teey=1
|
|
||||||
stats=1
|
|
||||||
beans=1
|
beans=1
|
||||||
eightball, 8ball=1
|
benchmark, bench=1
|
||||||
vote=1
|
bet=1
|
||||||
results=1
|
bethistory=1
|
||||||
wolfram=1
|
blur=1
|
||||||
map=1
|
boop=1
|
||||||
info, about=1
|
buildhelp=1
|
||||||
givebeans=1
|
|
||||||
c++, g++, cplus, cpp=1
|
c++, g++, cplus, cpp=1
|
||||||
work=1
|
catch=1
|
||||||
showpoll=1
|
choose=1
|
||||||
rpend=1
|
crouton=1
|
||||||
rpg=1
|
dbflush=1
|
||||||
tweet=1
|
dbstats=1
|
||||||
java, jdoodle=1
|
eightball, 8ball=1
|
||||||
help=1
|
fetch=1
|
||||||
buildHelp=1
|
givebeans=1
|
||||||
invite=1
|
|
||||||
shutdown=1
|
|
||||||
addguildrole=1
|
|
||||||
allowedguildrole=1
|
|
||||||
guildroleremove=1
|
|
||||||
guildroleadd=1
|
guildroleadd=1
|
||||||
guildroleallowed=1
|
guildroleallowed=1
|
||||||
fetch=1
|
|
||||||
guildrolenotallowed=1
|
|
||||||
catch=1
|
|
||||||
guildrolelist=1
|
guildrolelist=1
|
||||||
benchmark, bench=1
|
guildrolenotallowed=1
|
||||||
bethistory=1
|
guildroleremove=1
|
||||||
crouton=1
|
help=1
|
||||||
|
indicator=1
|
||||||
|
info, about=1
|
||||||
|
invite=1
|
||||||
|
java, jdoodle=1
|
||||||
|
map=1
|
||||||
|
perish, thenperish=1
|
||||||
|
ping=1
|
||||||
|
poll=1
|
||||||
|
rating=1
|
||||||
|
results=1
|
||||||
|
role=1
|
||||||
|
roll=1
|
||||||
|
rpend=1
|
||||||
|
rpg=1
|
||||||
|
rpstart=1
|
||||||
|
showpoll=1
|
||||||
|
shutdown=1
|
||||||
|
stats=1
|
||||||
|
teey=1
|
||||||
|
tony, stark, dontfeelgood, dontfeelsogood=1
|
||||||
|
tweet=1
|
||||||
|
vote=1
|
||||||
|
wolfram=1
|
||||||
|
work=1
|
||||||
|
yeet=1
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ perish, thenperish=
|
|||||||
teey=
|
teey=
|
||||||
guildrolelist=
|
guildrolelist=
|
||||||
stats=
|
stats=
|
||||||
|
dbstats=
|
||||||
beans=
|
beans=
|
||||||
eightball, 8ball=
|
eightball, 8ball=
|
||||||
catch=
|
catch=
|
||||||
@@ -43,6 +44,7 @@ buildHelp=
|
|||||||
fetch=
|
fetch=
|
||||||
crouton=
|
crouton=
|
||||||
invite=
|
invite=
|
||||||
|
dbflush=
|
||||||
shutdown=
|
shutdown=
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -138,6 +138,9 @@ RatingWarning=Warning: NSFW may slip through, images are only based on tags on t
|
|||||||
RatingChanged=Kittybot content set to
|
RatingChanged=Kittybot content set to
|
||||||
RatingInvalid=Invalid content rating
|
RatingInvalid=Invalid content rating
|
||||||
|
|
||||||
|
[CommandDBStats]
|
||||||
|
DBStatsInfo=
|
||||||
|
|
||||||
[CommandGiveBeans]
|
[CommandGiveBeans]
|
||||||
GiveBeansNoneMentioned=You didn't mention anyone!
|
GiveBeansNoneMentioned=You didn't mention anyone!
|
||||||
GiveBeansSuccess=Gave %s %s beans!
|
GiveBeansSuccess=Gave %s %s beans!
|
||||||
@@ -229,6 +232,9 @@ WolframError=Something went wrong!
|
|||||||
WolframInfo=Will query wolframalpha with your question and give a full image output of the answer
|
WolframInfo=Will query wolframalpha with your question and give a full image output of the answer
|
||||||
WolframNoArgs=You need to provide some arguments!
|
WolframNoArgs=You need to provide some arguments!
|
||||||
|
|
||||||
|
[CommandDBFlush]
|
||||||
|
DBFlushInfo=
|
||||||
|
|
||||||
[CommandMap]
|
[CommandMap]
|
||||||
MapInfo=Generates a map! You can pass additional information if you want with the flags `-s<seed>-w<width>-h<height>`. If one of the fields isn't provided, its default will be used. Note that adjusting the width and height impacts the map outcomes.\n\nDefault seed: Random,\nDefault Width: 35(max %s),\nDefault Height: 25(max %s)
|
MapInfo=Generates a map! You can pass additional information if you want with the flags `-s<seed>-w<width>-h<height>`. If one of the fields isn't provided, its default will be used. Note that adjusting the width and height impacts the map outcomes.\n\nDefault seed: Random,\nDefault Width: 35(max %s),\nDefault Height: 25(max %s)
|
||||||
MapSeed=Seed
|
MapSeed=Seed
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package commands;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
|
||||||
|
import core.Command;
|
||||||
|
import core.DatabaseManager;
|
||||||
|
import core.LocStrings;
|
||||||
|
import dataStructures.KittyChannel;
|
||||||
|
import dataStructures.KittyEmbed;
|
||||||
|
import dataStructures.KittyGuild;
|
||||||
|
import dataStructures.KittyRating;
|
||||||
|
import dataStructures.KittyRole;
|
||||||
|
import dataStructures.KittyUser;
|
||||||
|
import dataStructures.Response;
|
||||||
|
import dataStructures.UserInput;
|
||||||
|
|
||||||
|
public class CommandDBFlush extends Command
|
||||||
|
{
|
||||||
|
public CommandDBFlush(KittyRole level, KittyRating rating) { super(level, rating); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String HelpText() { return LocStrings.Stub("DBFlushInfo"); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void OnRun(KittyGuild guild, KittyChannel channel, KittyUser user, UserInput input, Response res)
|
||||||
|
{
|
||||||
|
int numUpdated = DatabaseManager.instance.Upkeep();
|
||||||
|
|
||||||
|
KittyEmbed embed = new KittyEmbed();
|
||||||
|
embed.title = "Database queue flushed";
|
||||||
|
embed.descriptionText = "**Dirty:** " + numUpdated;
|
||||||
|
embed.color = new Color(7*16, 8*16, 9*16);
|
||||||
|
|
||||||
|
res.CallEmbed(embed);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package commands;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.text.DateFormat;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
|
||||||
|
import core.Command;
|
||||||
|
import core.DatabaseManager;
|
||||||
|
import core.LocStrings;
|
||||||
|
import dataStructures.KittyChannel;
|
||||||
|
import dataStructures.KittyEmbed;
|
||||||
|
import dataStructures.KittyGuild;
|
||||||
|
import dataStructures.KittyRating;
|
||||||
|
import dataStructures.KittyRole;
|
||||||
|
import dataStructures.KittyUser;
|
||||||
|
import dataStructures.Response;
|
||||||
|
import dataStructures.UserInput;
|
||||||
|
|
||||||
|
public class CommandDBStats extends Command
|
||||||
|
{
|
||||||
|
DateFormat dateFormat;
|
||||||
|
public CommandDBStats(KittyRole level, KittyRating rating)
|
||||||
|
{
|
||||||
|
super(level, rating);
|
||||||
|
dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String HelpText() { return LocStrings.Stub("DBStatsInfo"); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void OnRun(KittyGuild guild, KittyChannel channel, KittyUser user, UserInput input, Response res)
|
||||||
|
{
|
||||||
|
KittyEmbed embed = new KittyEmbed();
|
||||||
|
embed.title = "Database Info";
|
||||||
|
embed.descriptionText = "**Tracked Items:** " + DatabaseManager.instance.GetTrackedObjectsSize();
|
||||||
|
embed.descriptionText += "\n";
|
||||||
|
embed.descriptionText += "**Last Upkeep:** " + dateFormat.format(DatabaseManager.instance.GetLastUpkeep()) + " UTC-7";
|
||||||
|
embed.color = new Color(7*16, 8*16, 9*16);
|
||||||
|
|
||||||
|
res.CallEmbed(embed);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,90 @@
|
|||||||
|
package core;
|
||||||
|
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.ListIterator;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
import dataStructures.Pair;
|
||||||
|
import utils.GlobalLog;
|
||||||
|
import utils.LogFilter;
|
||||||
|
import utils.io.FileUtils;
|
||||||
|
|
||||||
|
public class BaseKeyValueFile
|
||||||
|
{
|
||||||
|
// Variables
|
||||||
|
public static final String headerStart = "[";
|
||||||
|
public static final String headerEnd = "]";
|
||||||
|
public static final String pairSplit = "=";
|
||||||
|
public static final char pairSeparator = '\n';
|
||||||
|
|
||||||
|
protected final String filename;
|
||||||
|
protected final String header;
|
||||||
|
|
||||||
|
|
||||||
|
// Constructor
|
||||||
|
public BaseKeyValueFile(String filename)
|
||||||
|
{
|
||||||
|
this.filename = filename;
|
||||||
|
this.header = headerStart + this.getClass().getSimpleName() + headerEnd;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reads in and calls the specifid function for each keyvalue pair we find
|
||||||
|
protected void Parse(Consumer<? super Pair<String, String>> keyValueCallback)
|
||||||
|
{
|
||||||
|
File f = new File(filename);
|
||||||
|
if(f.isFile() && f.canRead())
|
||||||
|
{
|
||||||
|
String content = FileUtils.ReadContent(f).trim();
|
||||||
|
String[] lines = content.split("" + pairSeparator);
|
||||||
|
|
||||||
|
for(int i = 0; i < lines.length; ++i)
|
||||||
|
{
|
||||||
|
if(lines[i].contains(header))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
String[] pair = lines[i].split(pairSplit);
|
||||||
|
|
||||||
|
if(pair.length < 2)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
String key = pair[0].trim().toLowerCase();
|
||||||
|
String value = pair[1].trim().toLowerCase();
|
||||||
|
|
||||||
|
keyValueCallback.accept(new Pair<String, String>(key, value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Writes out a set of keyvalue pairs
|
||||||
|
protected void Write(List<Pair<String, String>> toWrite)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ListIterator<Pair<String, String>> iter = toWrite.listIterator();
|
||||||
|
|
||||||
|
String outString = "";
|
||||||
|
outString += header + pairSeparator;
|
||||||
|
|
||||||
|
while(iter.hasNext())
|
||||||
|
{
|
||||||
|
Pair<String, String> pair = iter.next();
|
||||||
|
String key = pair.First.toLowerCase();
|
||||||
|
String value = pair.Second.toLowerCase();
|
||||||
|
|
||||||
|
outString += key + pairSplit + value + pairSeparator;
|
||||||
|
}
|
||||||
|
|
||||||
|
BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
|
||||||
|
writer.write(outString);
|
||||||
|
writer.close();
|
||||||
|
}
|
||||||
|
catch (IOException e)
|
||||||
|
{
|
||||||
|
GlobalLog.Error(LogFilter.Core, "Issue writing file " + filename + ": " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,15 +9,15 @@ import java.nio.file.Path;
|
|||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import dataStructures.TaggedPairStore;
|
import dataStructures.TaggedPairStore;
|
||||||
import utils.FileUtils;
|
|
||||||
import utils.GlobalLog;
|
import utils.GlobalLog;
|
||||||
import utils.LogFilter;
|
import utils.LogFilter;
|
||||||
import utils.io.FileMonitor;
|
import utils.io.FileMonitor;
|
||||||
|
import utils.io.FileUtils;
|
||||||
import utils.io.MonitoredFile;
|
import utils.io.MonitoredFile;
|
||||||
|
|
||||||
// A quick-and-dirty localization tool that scrapes the project for calls to itself, then
|
// A quick-and-dirty localization tool that scrapes the project for calls to itself, then
|
||||||
// generates/updates a file externally with all the stub values as keys that are localized.
|
// generates/updates a file externally with all the stub values as keys that are localized.
|
||||||
public abstract class LocBase
|
public abstract class BaseLocFile
|
||||||
{
|
{
|
||||||
// Pre-defined values
|
// Pre-defined values
|
||||||
public static final String KittySourceDirectory = "./src";
|
public static final String KittySourceDirectory = "./src";
|
||||||
@@ -37,8 +37,8 @@ public abstract class LocBase
|
|||||||
// File monitoring
|
// File monitoring
|
||||||
protected FileMonitor fileMonitor;
|
protected FileMonitor fileMonitor;
|
||||||
|
|
||||||
// Ok... so this is an array because if it's not an array, the parser will parse the string
|
// Constructor
|
||||||
public LocBase(String filename, String functionName)
|
public BaseLocFile(String filename, String functionName)
|
||||||
{
|
{
|
||||||
this.filename = filename;
|
this.filename = filename;
|
||||||
this.functionName = functionName;
|
this.functionName = functionName;
|
||||||
@@ -1,13 +1,11 @@
|
|||||||
package core;
|
package core;
|
||||||
|
|
||||||
import java.io.BufferedWriter;
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileWriter;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import utils.FileUtils;
|
import java.util.Vector;
|
||||||
|
import dataStructures.Pair;
|
||||||
import utils.GlobalLog;
|
import utils.GlobalLog;
|
||||||
import utils.LogFilter;
|
import utils.LogFilter;
|
||||||
|
|
||||||
@@ -15,12 +13,9 @@ import utils.LogFilter;
|
|||||||
// commands that are being looked up will behave slightly differently so trimming
|
// commands that are being looked up will behave slightly differently so trimming
|
||||||
// rules for this file are different than the localization ones - this is more
|
// rules for this file are different than the localization ones - this is more
|
||||||
// aggresive with whitespace removal.
|
// aggresive with whitespace removal.
|
||||||
public class CommandEnabler
|
public class CommandEnabler extends BaseKeyValueFile
|
||||||
{
|
{
|
||||||
// Config/const variables
|
// Config/const variables
|
||||||
public static final String filename = "commands.config";
|
|
||||||
public static final String pairSplit = "=";
|
|
||||||
public static final char pairSeparator = '\n';
|
|
||||||
public static final String enabled = "1";
|
public static final String enabled = "1";
|
||||||
public static final String disabled = "0";
|
public static final String disabled = "0";
|
||||||
public static final boolean defaultEnabledState = true;
|
public static final boolean defaultEnabledState = true;
|
||||||
@@ -28,13 +23,18 @@ public class CommandEnabler
|
|||||||
// Local variables
|
// Local variables
|
||||||
private HashMap<String, Boolean> enabledMap; // Quick lookup
|
private HashMap<String, Boolean> enabledMap; // Quick lookup
|
||||||
private ArrayList<String> keyList; // Tracking ordering for later
|
private ArrayList<String> keyList; // Tracking ordering for later
|
||||||
|
private final static String name = "commands.config";
|
||||||
|
|
||||||
public CommandEnabler()
|
public CommandEnabler()
|
||||||
{
|
{
|
||||||
|
super(name);
|
||||||
|
|
||||||
|
// Create/Init variables
|
||||||
GlobalLog.Log(LogFilter.Core, "Initializing " + this.getClass().getSimpleName());
|
GlobalLog.Log(LogFilter.Core, "Initializing " + this.getClass().getSimpleName());
|
||||||
enabledMap = new HashMap<>();
|
enabledMap = new HashMap<>();
|
||||||
keyList = new ArrayList<>();
|
keyList = new ArrayList<>();
|
||||||
|
|
||||||
|
// Startup
|
||||||
ReadIn();
|
ReadIn();
|
||||||
GetTrackedCommands();
|
GetTrackedCommands();
|
||||||
WriteOut();
|
WriteOut();
|
||||||
@@ -43,21 +43,9 @@ public class CommandEnabler
|
|||||||
// Reads in the config file and parses it, keeping tabs on the order it read things
|
// Reads in the config file and parses it, keeping tabs on the order it read things
|
||||||
private void ReadIn()
|
private void ReadIn()
|
||||||
{
|
{
|
||||||
File f = new File(filename);
|
Parse((pair) ->{
|
||||||
if(f.isFile() && f.canRead())
|
String key = pair.First;
|
||||||
{
|
String value = pair.Second;
|
||||||
String content = FileUtils.ReadContent(f).trim();
|
|
||||||
String[] lines = content.split("" + pairSeparator);
|
|
||||||
|
|
||||||
for(int i = 0; i < lines.length; ++i)
|
|
||||||
{
|
|
||||||
String[] pair = lines[i].split(pairSplit);
|
|
||||||
|
|
||||||
if(pair.length < 2)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
String key = pair[0].trim();
|
|
||||||
String value = pair[1].trim().toLowerCase();
|
|
||||||
|
|
||||||
keyList.add(key);
|
keyList.add(key);
|
||||||
|
|
||||||
@@ -65,8 +53,7 @@ public class CommandEnabler
|
|||||||
enabledMap.putIfAbsent(key, true);
|
enabledMap.putIfAbsent(key, true);
|
||||||
else
|
else
|
||||||
enabledMap.putIfAbsent(key, false);
|
enabledMap.putIfAbsent(key, false);
|
||||||
}
|
});
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Look up the already scraped values from the localizer and store them if they
|
// Look up the already scraped values from the localizer and store them if they
|
||||||
@@ -77,7 +64,8 @@ public class CommandEnabler
|
|||||||
|
|
||||||
for(int i = 0; i < unloc.size(); ++i)
|
for(int i = 0; i < unloc.size(); ++i)
|
||||||
{
|
{
|
||||||
String command = unloc.get(i);
|
String command = unloc.get(i).toLowerCase();
|
||||||
|
|
||||||
if(enabledMap.putIfAbsent(command, defaultEnabledState) == null)
|
if(enabledMap.putIfAbsent(command, defaultEnabledState) == null)
|
||||||
{
|
{
|
||||||
GlobalLog.Log(LogFilter.Strings, "Identified new toggleable raw command: " + command);
|
GlobalLog.Log(LogFilter.Strings, "Identified new toggleable raw command: " + command);
|
||||||
@@ -89,35 +77,31 @@ public class CommandEnabler
|
|||||||
// Write out enabled/disabled file info.
|
// Write out enabled/disabled file info.
|
||||||
private void WriteOut()
|
private void WriteOut()
|
||||||
{
|
{
|
||||||
try
|
List<Pair<String, String>> list = new Vector<Pair<String, String>>();
|
||||||
{
|
|
||||||
String outString = "";
|
|
||||||
for(int i = 0; i < keyList.size(); ++i)
|
for(int i = 0; i < keyList.size(); ++i)
|
||||||
{
|
{
|
||||||
String key = keyList.get(i);
|
String key = keyList.get(i).toLowerCase();
|
||||||
String value = enabled;
|
String value = enabled.toLowerCase();
|
||||||
|
|
||||||
if(enabledMap.get(key) == false)
|
if(enabledMap.get(key) == false)
|
||||||
value = disabled;
|
value = disabled.toLowerCase();
|
||||||
|
|
||||||
outString += key + pairSplit + value + pairSeparator;
|
list.add(new Pair<String, String>(key, value));
|
||||||
}
|
}
|
||||||
|
|
||||||
BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
|
Collections.sort(list, (c1, c2) -> { return c1.First.compareTo(c2.First); });
|
||||||
writer.write(outString);
|
|
||||||
writer.close();
|
Write(list);
|
||||||
}
|
|
||||||
catch (IOException e)
|
|
||||||
{
|
|
||||||
GlobalLog.Error(LogFilter.Core, "Command enabler issue writing file! " + e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Looks up a key to see if it's enabled or not
|
// Looks up a key to see if it's enabled or not
|
||||||
public boolean IsEnabled(String key)
|
public boolean IsEnabled(String key)
|
||||||
{
|
{
|
||||||
if(enabledMap.containsKey(key))
|
String toCheck = key.toLowerCase();
|
||||||
return enabledMap.get(key);
|
|
||||||
|
if(enabledMap.containsKey(toCheck))
|
||||||
|
return enabledMap.get(toCheck);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import java.sql.SQLException;
|
|||||||
|
|
||||||
import network.JDBCDriver;
|
import network.JDBCDriver;
|
||||||
import network.JDBCDriverSQLite;
|
import network.JDBCDriverSQLite;
|
||||||
|
import network.JDBCStatementType;
|
||||||
import utils.GlobalLog;
|
import utils.GlobalLog;
|
||||||
import utils.LogFilter;
|
import utils.LogFilter;
|
||||||
|
|
||||||
@@ -28,14 +29,25 @@ public class DatabaseDriver
|
|||||||
driver = null;
|
driver = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Makes sure a table exists with the specified name.
|
||||||
|
// The keyName specifies the key column label, and the valueName specifies the value column label.
|
||||||
|
public void EnsureTableExists(String tableName, String keyName, String valueName)
|
||||||
|
{
|
||||||
|
// Require a global table if it doesn't exist already
|
||||||
|
driver.ExecuteStatement(JDBCStatementType.Create, "CREATE TABLE IF NOT EXISTS " + tableName + " (" + keyName + " text PRIMARY KEY, " + valueName + " text)", null);
|
||||||
|
}
|
||||||
|
|
||||||
// Set up and create a table in the database
|
// Set up and create a table in the database
|
||||||
public boolean Connect()
|
public boolean Connect()
|
||||||
{
|
{
|
||||||
driver = new JDBCDriverSQLite();
|
driver = new JDBCDriverSQLite();
|
||||||
driver.Connect();
|
if(driver.Connect() == false)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// Require a global table if it doesn't exist already
|
// Verify tables we want to use exist
|
||||||
driver.ExecuteStatement("CREATE TABLE IF NOT EXISTS " + globalTableName + " (" + globalKeyName + " text PRIMARY KEY, " + globalValueName + " text);", null);
|
EnsureTableExists(globalTableName, globalKeyName, globalValueName); // General table. Do not remove.
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -75,15 +87,16 @@ public class DatabaseDriver
|
|||||||
// Prototype formatting for key updating
|
// Prototype formatting for key updating
|
||||||
private void UpdateKey(String key, String value)
|
private void UpdateKey(String key, String value)
|
||||||
{
|
{
|
||||||
String command = "UPDATE " + globalTableName + " SET " + globalValueName + " = ? WHERE " + globalKeyName + " = ?;";
|
String command = "UPDATE " + globalTableName + " SET " + globalValueName + " = ? WHERE " + globalKeyName + " = ?";
|
||||||
driver.ExecuteStatement(command, new String[] { key, value });
|
boolean status = driver.ExecuteStatement(JDBCStatementType.Update, command, new String[] { value, key });
|
||||||
|
GlobalLog.Log(LogFilter.Database, "UpdateKey status: " + status);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Protoype updating for seeing if a key exists
|
// Protoype updating for seeing if a key exists
|
||||||
private boolean HasKey(String key)
|
private boolean HasKey(String key)
|
||||||
{
|
{
|
||||||
String command = "SELECT COUNT(1) as count FROM " + globalTableName + " WHERE " + globalKeyName + " = ?;";
|
String command = "SELECT COUNT(1) as count FROM " + globalTableName + " WHERE " + globalKeyName + " = ?";
|
||||||
ResultSet set = driver.ExecuteReturningStatement(command, new String[] { key });
|
ResultSet set = driver.ExecuteReturningStatement(JDBCStatementType.Select, command, new String[] { key });
|
||||||
String out = ResultAsString(set, "count");
|
String out = ResultAsString(set, "count");
|
||||||
return out.charAt(0) == '1';
|
return out.charAt(0) == '1';
|
||||||
}
|
}
|
||||||
@@ -91,16 +104,17 @@ public class DatabaseDriver
|
|||||||
// Prototype for getting a key
|
// Prototype for getting a key
|
||||||
private String GetKey(String key)
|
private String GetKey(String key)
|
||||||
{
|
{
|
||||||
String command = "SELECT " + globalValueName + " as searchedKey FROM " + globalTableName +" WHERE " + globalKeyName + " = ?;";
|
String command = "SELECT " + globalValueName + " as searchedKey FROM " + globalTableName +" WHERE " + globalKeyName + " = ?";
|
||||||
ResultSet set = driver.ExecuteReturningStatement(command, new String[] { key });
|
ResultSet set = driver.ExecuteReturningStatement(JDBCStatementType.Select, command, new String[] { key });
|
||||||
return ResultAsString(set, "searchedKey");
|
return ResultAsString(set, "searchedKey");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prototype for creating a key
|
// Prototype for creating a key
|
||||||
private void CreateKey(String key, String value)
|
private void CreateKey(String key, String value)
|
||||||
{
|
{
|
||||||
String command = "INSERT INTO " + globalTableName + " (GlobalKey, GlobalValue) VALUES (?, ?);";
|
String command = "INSERT INTO " + globalTableName + " (GlobalKey, GlobalValue) VALUES (?, ?)";
|
||||||
driver.ExecuteStatement(command, new String[] { key, value });
|
boolean status = driver.ExecuteStatement(JDBCStatementType.Insert, command, new String[] { key, value });
|
||||||
|
GlobalLog.Log(LogFilter.Database, "CreateKey status: " + status);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Transforms a result into a string if possible.
|
// Transforms a result into a string if possible.
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package core;
|
package core;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.Date;
|
||||||
|
import java.util.Vector;
|
||||||
import utils.GlobalLog;
|
import utils.GlobalLog;
|
||||||
import utils.LogFilter;
|
import utils.LogFilter;
|
||||||
|
|
||||||
@@ -11,11 +11,14 @@ public class DatabaseManager
|
|||||||
public static DatabaseManager instance = null;
|
public static DatabaseManager instance = null;
|
||||||
|
|
||||||
// Private internal variables
|
// Private internal variables
|
||||||
private ArrayList<DatabaseTrackedObject> trackedObjects;
|
private Vector<DatabaseTrackedObject> trackedObjects;
|
||||||
private DatabaseDriver driver;
|
private DatabaseDriver driver;
|
||||||
|
private Date lastUpkeep;
|
||||||
|
|
||||||
public DatabaseManager()
|
public DatabaseManager()
|
||||||
{
|
{
|
||||||
|
GlobalLog.Log(LogFilter.Database, "Creating database manager");
|
||||||
|
|
||||||
if(instance == null)
|
if(instance == null)
|
||||||
{
|
{
|
||||||
instance = this;
|
instance = this;
|
||||||
@@ -26,15 +29,25 @@ public class DatabaseManager
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
trackedObjects = new ArrayList<DatabaseTrackedObject>();
|
lastUpkeep = new Date();
|
||||||
|
trackedObjects = new Vector<DatabaseTrackedObject>();
|
||||||
driver = new DatabaseDriver();
|
driver = new DatabaseDriver();
|
||||||
driver.Connect();
|
|
||||||
|
if(driver.Connect() == false)
|
||||||
|
{
|
||||||
|
GlobalLog.Error("Database failed to connect. Currently, without the DB, this bot can not run.");
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Thumbs through registered objects and syncs them with the database.
|
// Thumbs through registered objects and syncs them with the database.
|
||||||
// Consider moving this operation to a separate thread.
|
// Consider moving this operation to a separate thread.
|
||||||
public void Upkeep()
|
public int Upkeep()
|
||||||
{
|
{
|
||||||
|
synchronized(trackedObjects)
|
||||||
|
{
|
||||||
|
int numUpdated = 0;
|
||||||
|
|
||||||
for(int i = 0 ; i < trackedObjects.size(); ++i)
|
for(int i = 0 ; i < trackedObjects.size(); ++i)
|
||||||
{
|
{
|
||||||
DatabaseTrackedObject dto = trackedObjects.get(i);
|
DatabaseTrackedObject dto = trackedObjects.get(i);
|
||||||
@@ -43,23 +56,48 @@ public class DatabaseManager
|
|||||||
{
|
{
|
||||||
SetRemoteValue(dto.identifier, dto.Serialize());
|
SetRemoteValue(dto.identifier, dto.Serialize());
|
||||||
dto.Resolve();
|
dto.Resolve();
|
||||||
|
++numUpdated;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lastUpkeep = new Date();
|
||||||
|
|
||||||
|
return numUpdated;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Register(DatabaseTrackedObject tracked)
|
public void Register(DatabaseTrackedObject tracked)
|
||||||
|
{
|
||||||
|
synchronized(trackedObjects)
|
||||||
{
|
{
|
||||||
trackedObjects.add(tracked);
|
trackedObjects.add(tracked);
|
||||||
tracked.DeSerialzie(GetRemoteValue(tracked.identifier));
|
tracked.DeSerialzie(GetRemoteValue(tracked.identifier));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public String GetRemoteValue(String key)
|
public String GetRemoteValue(String key)
|
||||||
|
{
|
||||||
|
synchronized(driver)
|
||||||
{
|
{
|
||||||
return driver.CreateGetKey(key);
|
return driver.CreateGetKey(key);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void SetRemoteValue(String key, String value)
|
public void SetRemoteValue(String key, String value)
|
||||||
|
{
|
||||||
|
synchronized(driver)
|
||||||
{
|
{
|
||||||
driver.CreateSetKey(key, value);
|
driver.CreateSetKey(key, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Date GetLastUpkeep()
|
||||||
|
{
|
||||||
|
return lastUpkeep;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int GetTrackedObjectsSize()
|
||||||
|
{
|
||||||
|
return trackedObjects.size();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import dataStructures.Pair;
|
|||||||
|
|
||||||
// Performs the same localization for the strings associated with command names as
|
// Performs the same localization for the strings associated with command names as
|
||||||
// is performed with general strings in the application
|
// is performed with general strings in the application
|
||||||
public class LocCommands extends LocBase
|
public class LocCommands extends BaseLocFile
|
||||||
{
|
{
|
||||||
public static final String fileName = "locCommands.config";
|
public static final String fileName = "locCommands.config";
|
||||||
public static final String function = "LocCommands.Stub";
|
public static final String function = "LocCommands.Stub";
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import utils.io.FileMonitor;
|
|||||||
// A quick-and-dirty localization tool that scrapes the project for calls to itself, then
|
// A quick-and-dirty localization tool that scrapes the project for calls to itself, then
|
||||||
// generates/updates a file externally (phrases.config) with all the stub values as keys that
|
// generates/updates a file externally (phrases.config) with all the stub values as keys that
|
||||||
// can then be localized.
|
// can then be localized.
|
||||||
public class LocStrings extends LocBase
|
public class LocStrings extends BaseLocFile
|
||||||
{
|
{
|
||||||
public static final String fileName = "locStrings.config";
|
public static final String fileName = "locStrings.config";
|
||||||
public static final String function = "LocStrings.Stub";
|
public static final String function = "LocStrings.Stub";
|
||||||
|
|||||||
@@ -328,22 +328,28 @@ public class ObjectBuilderFactory
|
|||||||
|
|
||||||
CommandManager manager = new CommandManager(commandEnabler);
|
CommandManager manager = new CommandManager(commandEnabler);
|
||||||
|
|
||||||
|
// Dev
|
||||||
manager.Register(LocCommands.Stub("work"), new CommandDoWork(KittyRole.Dev, KittyRating.Safe));
|
manager.Register(LocCommands.Stub("work"), new CommandDoWork(KittyRole.Dev, KittyRating.Safe));
|
||||||
manager.Register(LocCommands.Stub("shutdown"), new CommandShutdown(KittyRole.Dev, KittyRating.Safe));
|
manager.Register(LocCommands.Stub("shutdown"), new CommandShutdown(KittyRole.Dev, KittyRating.Safe));
|
||||||
manager.Register(LocCommands.Stub("stats"), new CommandStats(KittyRole.Dev, KittyRating.Safe));
|
manager.Register(LocCommands.Stub("stats"), new CommandStats(KittyRole.Dev, KittyRating.Safe));
|
||||||
manager.Register(LocCommands.Stub("invite"), new CommandInvite(KittyRole.Dev, KittyRating.Safe));
|
manager.Register(LocCommands.Stub("invite"), new CommandInvite(KittyRole.Dev, KittyRating.Safe));
|
||||||
manager.Register(LocCommands.Stub("buildHelp"), new CommandHelpBuilder(KittyRole.Dev, KittyRating.Safe));
|
manager.Register(LocCommands.Stub("buildHelp"), new CommandHelpBuilder(KittyRole.Dev, KittyRating.Safe));
|
||||||
manager.Register(LocCommands.Stub("tweet"), new CommandTweet(KittyRole.Dev, KittyRating.Safe));
|
manager.Register(LocCommands.Stub("tweet"), new CommandTweet(KittyRole.Dev, KittyRating.Safe));
|
||||||
|
manager.Register(LocCommands.Stub("dbflush"), new CommandDBFlush(KittyRole.Dev, KittyRating.Safe));
|
||||||
|
manager.Register(LocCommands.Stub("dbstats"), new CommandDBStats(KittyRole.Dev, KittyRating.Safe));
|
||||||
|
|
||||||
|
// Admin
|
||||||
manager.Register(LocCommands.Stub("rating"), new CommandRating(KittyRole.Admin, KittyRating.Safe));
|
manager.Register(LocCommands.Stub("rating"), new CommandRating(KittyRole.Admin, KittyRating.Safe));
|
||||||
manager.Register(LocCommands.Stub("indicator"), new CommandChangeIndicator(KittyRole.Admin, KittyRating.Safe));
|
manager.Register(LocCommands.Stub("indicator"), new CommandChangeIndicator(KittyRole.Admin, KittyRating.Safe));
|
||||||
manager.Register(LocCommands.Stub("guildroleallowed"), new CommandGuildRoleAllowed(KittyRole.Admin, KittyRating.Safe));
|
manager.Register(LocCommands.Stub("guildroleallowed"), new CommandGuildRoleAllowed(KittyRole.Admin, KittyRating.Safe));
|
||||||
manager.Register(LocCommands.Stub("guildrolenotallowed"), new CommandGuildRoleNotAllowed(KittyRole.Admin, KittyRating.Safe));
|
manager.Register(LocCommands.Stub("guildrolenotallowed"), new CommandGuildRoleNotAllowed(KittyRole.Admin, KittyRating.Safe));
|
||||||
|
|
||||||
|
// Mod
|
||||||
manager.Register(LocCommands.Stub("poll"), new CommandPollManage(KittyRole.Mod, KittyRating.Safe));
|
manager.Register(LocCommands.Stub("poll"), new CommandPollManage(KittyRole.Mod, KittyRating.Safe));
|
||||||
manager.Register(LocCommands.Stub("givebeans"), new CommandGiveBeans(KittyRole.Mod, KittyRating.Safe));
|
manager.Register(LocCommands.Stub("givebeans"), new CommandGiveBeans(KittyRole.Mod, KittyRating.Safe));
|
||||||
manager.Register(LocCommands.Stub("rpg"), new CommandRPG(KittyRole.Mod, KittyRating.Safe));
|
manager.Register(LocCommands.Stub("rpg"), new CommandRPG(KittyRole.Mod, KittyRating.Safe));
|
||||||
|
|
||||||
|
// General
|
||||||
manager.Register(LocCommands.Stub("fetch"), new CommandFetch(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("guildroleadd"), new CommandGuildRoleAdd(KittyRole.General, KittyRating.Safe));
|
||||||
manager.Register(LocCommands.Stub("guildroleremove"), new CommandGuildRoleRemove(KittyRole.General, KittyRating.Safe));
|
manager.Register(LocCommands.Stub("guildroleremove"), new CommandGuildRoleRemove(KittyRole.General, KittyRating.Safe));
|
||||||
@@ -375,7 +381,6 @@ public class ObjectBuilderFactory
|
|||||||
manager.Register(LocCommands.Stub("guildrolelist"), new CommandGuildRoleList(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("bethistory"), new CommandBetHistory(KittyRole.General, KittyRating.Safe));
|
||||||
manager.Register(LocCommands.Stub("crouton"), new CommandCrouton(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("benchmark, bench"), new CommandBenchmark(KittyRole.General, KittyRating.Safe));
|
||||||
|
|
||||||
return manager;
|
return manager;
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package core;
|
||||||
|
|
||||||
|
public class Settings {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -6,8 +6,8 @@ import java.util.Collections;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import dataStructures.Pair;
|
import dataStructures.Pair;
|
||||||
import utils.FileUtils;
|
|
||||||
import utils.io.DirectoryMonitor;
|
import utils.io.DirectoryMonitor;
|
||||||
|
import utils.io.FileUtils;
|
||||||
import utils.io.MonitoredFile;
|
import utils.io.MonitoredFile;
|
||||||
|
|
||||||
// All things considered, this doesn't need to be particularly efficient since anything
|
// All things considered, this doesn't need to be particularly efficient since anything
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package main;
|
package main;
|
||||||
|
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
import core.DatabaseManager;
|
import core.DatabaseManager;
|
||||||
import core.LocCommands;
|
import core.LocCommands;
|
||||||
import core.LocStrings;
|
import core.LocStrings;
|
||||||
@@ -68,20 +70,6 @@ public class Superintendent
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// This is for stuff that we need to do on a regular basis, but don't
|
|
||||||
// necessarily want running at all points in time.
|
|
||||||
// Happens just after the command / plugin runs.
|
|
||||||
public static boolean PerCommandUpkeepPost(JDA bot, DatabaseManager databaseManager)
|
|
||||||
{
|
|
||||||
// Upkeep database
|
|
||||||
databaseManager.Upkeep();
|
|
||||||
|
|
||||||
// Update command-specific
|
|
||||||
RPManager.Upkeep(bot);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Only called once per command. Good for lazily updating.
|
// Only called once per command. Good for lazily updating.
|
||||||
// Happens just before the command / plugin runs.
|
// Happens just before the command / plugin runs.
|
||||||
public static boolean PerCommandUpkeepPre()
|
public static boolean PerCommandUpkeepPre()
|
||||||
@@ -92,4 +80,25 @@ public class Superintendent
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static final Integer delayTimerReset = 5;
|
||||||
|
private static AtomicInteger delayTimerCurrent = new AtomicInteger(delayTimerReset);
|
||||||
|
|
||||||
|
// This is for stuff that we need to do on a regular basis, but don't
|
||||||
|
// necessarily want running at all points in time.
|
||||||
|
// Happens just after the command / plugin runs.
|
||||||
|
public static boolean PerCommandUpkeepPost(JDA bot, DatabaseManager databaseManager)
|
||||||
|
{
|
||||||
|
// Upkeep database lazily on occasion
|
||||||
|
if(delayTimerCurrent.decrementAndGet() < 0)
|
||||||
|
{
|
||||||
|
databaseManager.Upkeep();
|
||||||
|
delayTimerCurrent.set(delayTimerReset);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update command-specific
|
||||||
|
RPManager.Upkeep(bot);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,6 +15,6 @@ public abstract class JDBCDriver
|
|||||||
// Executes a SQL command with the database. Returns if it was executed successfully, or in the
|
// Executes a SQL command with the database. Returns if it was executed successfully, or in the
|
||||||
// case of the returning statement, returns the ResultSet. Args are placed into the prepared statement
|
// case of the returning statement, returns the ResultSet. Args are placed into the prepared statement
|
||||||
// in place of each '?' places into it.
|
// in place of each '?' places into it.
|
||||||
public abstract boolean ExecuteStatement(String command, String[] args);
|
public abstract boolean ExecuteStatement(JDBCStatementType type, String command, String[] args);
|
||||||
public abstract ResultSet ExecuteReturningStatement(String command, String[] args);
|
public abstract ResultSet ExecuteReturningStatement(JDBCStatementType type, String command, String[] args);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,13 +17,13 @@ public class JDBCDriverMySQL extends JDBCDriver
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean ExecuteStatement(String statement, String[] args) {
|
public boolean ExecuteStatement(JDBCStatementType type, String statement, String[] args) {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ResultSet ExecuteReturningStatement(String statement, String[] args) {
|
public ResultSet ExecuteReturningStatement(JDBCStatementType type, String statement, String[] args) {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,13 +17,13 @@ public class JDBCDriverPostgreSQL extends JDBCDriver
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean ExecuteStatement(String statement, String[] args) {
|
public boolean ExecuteStatement(JDBCStatementType type, String statement, String[] args) {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ResultSet ExecuteReturningStatement(String statement, String[] args) {
|
public ResultSet ExecuteReturningStatement(JDBCStatementType type, String statement, String[] args) {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ public class JDBCDriverSQLite extends JDBCDriver
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ResultSet ExecuteReturningStatement(String command, String[] args)
|
public ResultSet ExecuteReturningStatement(JDBCStatementType type, String command, String[] args)
|
||||||
{
|
{
|
||||||
if(connection == null)
|
if(connection == null)
|
||||||
return null;
|
return null;
|
||||||
@@ -77,11 +77,21 @@ public class JDBCDriverSQLite extends JDBCDriver
|
|||||||
if(args != null && args.length > 0)
|
if(args != null && args.length > 0)
|
||||||
{
|
{
|
||||||
PreparedStatement statement = connection.prepareStatement(command);
|
PreparedStatement statement = connection.prepareStatement(command);
|
||||||
|
ResultSet set = null;
|
||||||
|
|
||||||
for(int i = 0; i < args.length; ++i)
|
for(int i = 0; i < args.length; ++i)
|
||||||
statement.setString(i + 1, args[i]);
|
statement.setString(i + 1, args[i]);
|
||||||
|
|
||||||
ResultSet set = statement.executeQuery();
|
switch(type)
|
||||||
|
{
|
||||||
|
case Select:
|
||||||
|
set = statement.executeQuery();
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new Exception("Unsupported statement type for this function!");
|
||||||
|
}
|
||||||
|
|
||||||
return set;
|
return set;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -91,7 +101,7 @@ public class JDBCDriverSQLite extends JDBCDriver
|
|||||||
return set;
|
return set;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (SQLException e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -106,7 +116,7 @@ public class JDBCDriverSQLite extends JDBCDriver
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean ExecuteStatement(String command, String[] args)
|
public boolean ExecuteStatement(JDBCStatementType type, String command, String[] args)
|
||||||
{
|
{
|
||||||
if(connection == null)
|
if(connection == null)
|
||||||
return false;
|
return false;
|
||||||
@@ -118,12 +128,34 @@ public class JDBCDriverSQLite extends JDBCDriver
|
|||||||
{
|
{
|
||||||
if(args != null && args.length > 0)
|
if(args != null && args.length > 0)
|
||||||
{
|
{
|
||||||
|
boolean executed = false;
|
||||||
PreparedStatement statement = connection.prepareStatement(command);
|
PreparedStatement statement = connection.prepareStatement(command);
|
||||||
|
//GlobalLog.Log("COMMAND IS -- " + command);
|
||||||
|
|
||||||
for(int i = 0; i < args.length; ++i)
|
for(int i = 0; i < args.length; ++i)
|
||||||
|
{
|
||||||
|
//GlobalLog.Log("Args: " + args[i]);
|
||||||
statement.setString(i + 1, args[i]);
|
statement.setString(i + 1, args[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
switch(type)
|
||||||
|
{
|
||||||
|
case Create:
|
||||||
|
executed = statement.executeUpdate() == 1;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Update:
|
||||||
|
executed = statement.executeUpdate() == 1;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Insert:
|
||||||
|
executed = statement.executeUpdate() == 1;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new Exception("Unsupported statement type for this function!");
|
||||||
|
}
|
||||||
|
|
||||||
boolean executed = statement.execute();
|
|
||||||
return executed;
|
return executed;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -133,7 +165,7 @@ public class JDBCDriverSQLite extends JDBCDriver
|
|||||||
return executed;
|
return executed;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (SQLException e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package network;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public enum JDBCStatementType
|
||||||
|
{
|
||||||
|
Insert (0), Update(1), Select(2), Create(4);
|
||||||
|
|
||||||
|
private final int value;
|
||||||
|
private JDBCStatementType(int value)
|
||||||
|
{
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getValue()
|
||||||
|
{
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Optional<JDBCStatementType> valueOf(int value)
|
||||||
|
{
|
||||||
|
return Arrays.stream(values()).filter(role -> role.value == value).findFirst();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,8 +7,11 @@ import java.text.DateFormat;
|
|||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
|
import utils.io.FileUtils;
|
||||||
|
|
||||||
public class GlobalLog
|
public class GlobalLog
|
||||||
{
|
{
|
||||||
|
private static final String directory = "logs/";
|
||||||
private static final String log = "Log";
|
private static final String log = "Log";
|
||||||
private static final String warn = "Warning";
|
private static final String warn = "Warning";
|
||||||
private static final String error = "ERROR";
|
private static final String error = "ERROR";
|
||||||
@@ -19,7 +22,10 @@ public class GlobalLog
|
|||||||
{
|
{
|
||||||
DateFormat dF = new SimpleDateFormat("yyyy_MM_dd_HH-mm");
|
DateFormat dF = new SimpleDateFormat("yyyy_MM_dd_HH-mm");
|
||||||
Date today = new Date();
|
Date today = new Date();
|
||||||
outputLog = new PrintWriter((dF.format(today) + ".log"), "UTF-8");
|
|
||||||
|
FileUtils.CreateDirectoryIfDoesntExist(directory);
|
||||||
|
outputLog = new PrintWriter(directory + (dF.format(today) + ".log"), "UTF-8");
|
||||||
|
GlobalLog.Log(LogFilter.Util, "Finished initializing logging system");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void Write(String status, LogFilter filter, String body)
|
private static void Write(String status, LogFilter filter, String body)
|
||||||
|
|||||||
@@ -9,8 +9,6 @@ import java.util.List;
|
|||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import utils.FileUtils;
|
|
||||||
|
|
||||||
// NOTE: Consider shifting internal behavior to https://docs.oracle.com/javase/tutorial/essential/io/notification.html
|
// NOTE: Consider shifting internal behavior to https://docs.oracle.com/javase/tutorial/essential/io/notification.html
|
||||||
// for external stability and support
|
// for external stability and support
|
||||||
public class DirectoryMonitor
|
public class DirectoryMonitor
|
||||||
|
|||||||
@@ -3,8 +3,6 @@ package utils.io;
|
|||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
import utils.FileUtils;
|
|
||||||
|
|
||||||
// Monitors a single file for changes - the file must exist and is expected to continue to exist.
|
// Monitors a single file for changes - the file must exist and is expected to continue to exist.
|
||||||
public class FileMonitor
|
public class FileMonitor
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package utils;
|
package utils.io;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -9,8 +9,20 @@ import java.nio.file.Paths;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import utils.GlobalLog;
|
||||||
|
import utils.LogFilter;
|
||||||
|
|
||||||
public class FileUtils
|
public class FileUtils
|
||||||
{
|
{
|
||||||
|
public static void CreateDirectoryIfDoesntExist(String directoryName)
|
||||||
|
{
|
||||||
|
File directory = new File(directoryName);
|
||||||
|
|
||||||
|
if (! directory.exists()){
|
||||||
|
directory.mkdir();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Reads all lines from a file as a string
|
// Reads all lines from a file as a string
|
||||||
public static String ReadContent(File file) { return ReadContent(file.toPath()); }
|
public static String ReadContent(File file) { return ReadContent(file.toPath()); }
|
||||||
public static String ReadContent(Path filePath)
|
public static String ReadContent(Path filePath)
|
||||||
Reference in New Issue
Block a user