diff --git a/pom.xml b/pom.xml
index 81546bb..5daad84 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
me.mrdoc
Twitch_TOPCheersTotal
- 1.0.0
+ 1.2.0
1.8
@@ -66,14 +66,25 @@
**/config.yml
-
+
+ org.apache.maven.plugins
+ maven-jar-plugin
+
+
+
+ test.App
+
+ true
+
+
+
+
+
@@ -131,6 +142,12 @@
JCDP
2.0.3.1
+
+
+ org.apache.commons
+ commons-io
+ 1.3.2
+
\ No newline at end of file
diff --git a/src/main/java/me/mrdoc/twitchtopcheers/Core.java b/src/main/java/me/mrdoc/twitchtopcheers/Core.java
index d777a8d..169645b 100644
--- a/src/main/java/me/mrdoc/twitchtopcheers/Core.java
+++ b/src/main/java/me/mrdoc/twitchtopcheers/Core.java
@@ -9,7 +9,7 @@
import me.mrdoc.twitchtopcheers.utils.PrintConsole;
import java.io.IOException;
-import java.util.ArrayList;
+import java.util.HashMap;
import java.util.Scanner;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
@@ -27,7 +27,8 @@ public class Core {
public static final ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
public static void main(String[] args) throws IOException {
- PrintConsole.sendStatus("Iniciando sistema...");
+ PrintConsole.sendStatus("Iniciando sistema de TOP por session de cheers.");
+ PrintConsole.sendStatus("Version: " + Core.class.getPackage().getImplementationVersion());
loadConfig();
loadFileTop();
@@ -45,6 +46,12 @@ public static void main(String[] args) throws IOException {
if(response.equalsIgnoreCase("y")) {
PrintConsole.sendInfo("Borrando registros de TOP");
DBCheerManagent.purgeTable();
+ FileCheerManagent.purgeFiles();
+ loadFileTop(); //reloadFile again
+ } else {
+ //Como no se borran registros, actualizamos los archivos con TOP.
+ HashMap cheersTOP = DBCheerManagent.getTOPCheers(ConfigManager.getConfig().getTop_size());
+ FileCheerManagent.setTOP(cheersTOP);
}
//testALL();
@@ -58,19 +65,16 @@ public static void main(String[] args) throws IOException {
}
private static void testALL() {
+ PrintConsole.sendInfo("Corriendo test...");
DBCheerManagent.addCheer("test",200);
DBCheerManagent.addCheer("test2",800);
DBCheerManagent.addCheer("test3",100);
- ArrayList cheers = DBCheerManagent.getTOPCheers(1);
+ HashMap cheers = DBCheerManagent.getTOPCheers(ConfigManager.getConfig().getTop_size());
PrintConsole.sendInfo("CANTIDAD TOP: " + cheers.size());
- if(!cheers.isEmpty()) {
- PrintConsole.sendInfo(cheers.get(0).getUsername() + " " + cheers.get(0).getTotalCheers());
- }
-
- FileCheerManagent.setTOP(DBCheerManagent.getTOPCheers(1).get(0));
+ FileCheerManagent.setTOP(DBCheerManagent.getTOPCheers(ConfigManager.getConfig().getTop_size()));
//FileCheerManagent.setTOP(DBCheerManagent.getTOPCheers(1).get(0));
}
diff --git a/src/main/java/me/mrdoc/twitchtopcheers/bot/TwitchBOT.java b/src/main/java/me/mrdoc/twitchtopcheers/bot/TwitchBOT.java
index 0591b33..1204e9c 100644
--- a/src/main/java/me/mrdoc/twitchtopcheers/bot/TwitchBOT.java
+++ b/src/main/java/me/mrdoc/twitchtopcheers/bot/TwitchBOT.java
@@ -1,6 +1,7 @@
package me.mrdoc.twitchtopcheers.bot;
import me.mrdoc.twitchtopcheers.Core;
+import me.mrdoc.twitchtopcheers.bot.listener.GeneralListener;
import me.mrdoc.twitchtopcheers.bot.listener.ManageCheerListener;
import me.mrdoc.twitchtopcheers.managers.ConfigManager;
import me.mrdoc.twitchtopcheers.utils.PrintConsole;
@@ -74,7 +75,6 @@ private Configuration.Builder generateConfiguration() {
.addCapHandler(new EnableCapHandler("twitch.tv/tags"))
.addCapHandler(new EnableCapHandler("twitch.tv/commands"))
-
.addServer("irc.twitch.tv")
.setName(ConfigManager.getConfig().getChannel_config().get("bot_username")) //Your twitch.tv username
@@ -82,6 +82,7 @@ private Configuration.Builder generateConfiguration() {
.addAutoJoinChannel(ConfigManager.getConfig().getChannel_config().get("channel_name"))
.addListener(new ManageCheerListener())
+ .addListener(new GeneralListener())
;
}
diff --git a/src/main/java/me/mrdoc/twitchtopcheers/bot/listener/GeneralListener.java b/src/main/java/me/mrdoc/twitchtopcheers/bot/listener/GeneralListener.java
new file mode 100644
index 0000000..443f855
--- /dev/null
+++ b/src/main/java/me/mrdoc/twitchtopcheers/bot/listener/GeneralListener.java
@@ -0,0 +1,38 @@
+package me.mrdoc.twitchtopcheers.bot.listener;
+
+import me.mrdoc.twitchtopcheers.utils.PrintConsole;
+import org.pircbotx.hooks.ListenerAdapter;
+import org.pircbotx.hooks.events.ConnectAttemptFailedEvent;
+import org.pircbotx.hooks.events.ConnectEvent;
+import org.pircbotx.hooks.events.ServerResponseEvent;
+
+/**
+ * Created on 24-07-2017 for TwitchGOTH_TOPSumCheers.
+ *
+ * @author Doc
+ */
+public class GeneralListener extends ListenerAdapter {
+
+ @Override
+ public void onConnectAttemptFailed(ConnectAttemptFailedEvent event) {
+ PrintConsole.sendError(event.toString());
+ System.exit(0);
+ }
+
+ @Override
+ public void onServerResponse(ServerResponseEvent event) {
+ if(event.getCode() == 446 || event.getCode() == 339) {
+ PrintConsole.sendError("Error de conexion al canal IRC para trackear cheers. \nDetalles: " + event.getRawLine());
+ System.exit(0);
+ }
+ }
+
+ @Override
+ public void onConnect(ConnectEvent event) {
+ PrintConsole.sendInfo("Servidor conectado correctamente.");
+ event.respond("Conectado correctamente, trackeando envio de cheers.");
+ }
+
+
+
+}
diff --git a/src/main/java/me/mrdoc/twitchtopcheers/bot/listener/ManageCheerListener.java b/src/main/java/me/mrdoc/twitchtopcheers/bot/listener/ManageCheerListener.java
index f53e31b..1f31d4b 100644
--- a/src/main/java/me/mrdoc/twitchtopcheers/bot/listener/ManageCheerListener.java
+++ b/src/main/java/me/mrdoc/twitchtopcheers/bot/listener/ManageCheerListener.java
@@ -1,13 +1,14 @@
package me.mrdoc.twitchtopcheers.bot.listener;
import me.mrdoc.twitchtopcheers.classes.Cheering;
+import me.mrdoc.twitchtopcheers.managers.ConfigManager;
import me.mrdoc.twitchtopcheers.managers.DBCheerManagent;
import me.mrdoc.twitchtopcheers.managers.FileCheerManagent;
import me.mrdoc.twitchtopcheers.utils.PrintConsole;
import org.pircbotx.hooks.ListenerAdapter;
import org.pircbotx.hooks.events.MessageEvent;
-import java.util.ArrayList;
+import java.util.HashMap;
/**
* Created on 19-07-2017 for twitchtopcheers.
@@ -33,9 +34,9 @@ private void onCheerEvent(MessageEvent event) {
DBCheerManagent.addCheer(event.getUser().getNick(),cheerInMsg);
//Recargamos TOP
- ArrayList cheersTOP = DBCheerManagent.getTOPCheers(1);
+ HashMap cheersTOP = DBCheerManagent.getTOPCheers(ConfigManager.getConfig().getTop_size());
- FileCheerManagent.setTOP(cheersTOP.get(0));
+ FileCheerManagent.setTOP(cheersTOP);
}
}
diff --git a/src/main/java/me/mrdoc/twitchtopcheers/classes/Config.java b/src/main/java/me/mrdoc/twitchtopcheers/classes/Config.java
index e720274..2f0401e 100644
--- a/src/main/java/me/mrdoc/twitchtopcheers/classes/Config.java
+++ b/src/main/java/me/mrdoc/twitchtopcheers/classes/Config.java
@@ -10,7 +10,9 @@
public class Config {
private String out_message;
+ private String empty_message;
private Map channel_config;
+ private int top_size;
public String getOut_message() {
return out_message;
@@ -27,4 +29,20 @@ public Map getChannel_config() {
public void setChannel_config(Map channel_config) {
this.channel_config = channel_config;
}
+
+ public int getTop_size() {
+ return top_size;
+ }
+
+ public void setTop_size(int top_size) {
+ this.top_size = top_size;
+ }
+
+ public String getEmpty_message() {
+ return empty_message;
+ }
+
+ public void setEmpty_message(String empty_message) {
+ this.empty_message = empty_message;
+ }
}
diff --git a/src/main/java/me/mrdoc/twitchtopcheers/managers/ConfigManager.java b/src/main/java/me/mrdoc/twitchtopcheers/managers/ConfigManager.java
index 03b0fa1..5b1fcd7 100644
--- a/src/main/java/me/mrdoc/twitchtopcheers/managers/ConfigManager.java
+++ b/src/main/java/me/mrdoc/twitchtopcheers/managers/ConfigManager.java
@@ -3,6 +3,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import me.mrdoc.twitchtopcheers.classes.Config;
+import me.mrdoc.twitchtopcheers.utils.PrintConsole;
import java.io.File;
import java.io.IOException;
@@ -23,6 +24,10 @@ public static void loadConfig() {
try {
configData = mapper.readValue(new File(CONFIG_FILE), Config.class);
configData.getChannel_config().put("channel_name","#"+configData.getChannel_config().get("channel_name"));
+ if(!configData.getChannel_config().get("bot_token").contains("oauth:")) {
+ PrintConsole.sendError("El login del BOT no contiene oauth: por lo que la conexion no es posible");
+ System.exit(0);
+ }
} catch (IOException e) {
e.printStackTrace();
}
diff --git a/src/main/java/me/mrdoc/twitchtopcheers/managers/DBCheerManagent.java b/src/main/java/me/mrdoc/twitchtopcheers/managers/DBCheerManagent.java
index 1468b51..3cc3ee3 100644
--- a/src/main/java/me/mrdoc/twitchtopcheers/managers/DBCheerManagent.java
+++ b/src/main/java/me/mrdoc/twitchtopcheers/managers/DBCheerManagent.java
@@ -2,12 +2,11 @@
import me.mrdoc.twitchtopcheers.classes.Cheering;
import me.mrdoc.twitchtopcheers.db.SQLiteSystem;
-import me.mrdoc.twitchtopcheers.utils.PrintConsole;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
-import java.util.ArrayList;
+import java.util.HashMap;
/**
* Created on 20-07-2017 for TwitchGOTH_TOPSumCheers.
@@ -82,8 +81,8 @@ public static Cheering getCheer(String username) {
return null;
}
- public static ArrayList getTOPCheers(int cant) {
- ArrayList cheers = new ArrayList<>();
+ public static HashMap getTOPCheers(int cant) {
+ HashMap cheers = new HashMap();
String command = "SELECT * FROM cheerings ORDER BY cheermount DESC LIMIT ? ;";
@@ -95,19 +94,17 @@ public static ArrayList getTOPCheers(int cant) {
ResultSet rs = psGetCheer.executeQuery();
+ int pos = 1;
+
while(rs.next()) {
Cheering cheer = new Cheering(rs.getString("username"),rs.getInt("cheermount"));
- cheers.add(cheer);
+ cheers.put(pos,cheer);
+ pos = pos + 1;
}
} catch (SQLException e) {
e.printStackTrace();
}
- if(cheers.isEmpty()) {
- PrintConsole.sendError("No hay datos para cargar en el TOP, el sistema no puede continuar.");
- System.exit(0);
- }
-
return cheers;
}
diff --git a/src/main/java/me/mrdoc/twitchtopcheers/managers/FileCheerManagent.java b/src/main/java/me/mrdoc/twitchtopcheers/managers/FileCheerManagent.java
index 90217c3..df43261 100644
--- a/src/main/java/me/mrdoc/twitchtopcheers/managers/FileCheerManagent.java
+++ b/src/main/java/me/mrdoc/twitchtopcheers/managers/FileCheerManagent.java
@@ -2,11 +2,13 @@
import me.mrdoc.twitchtopcheers.classes.Cheering;
import me.mrdoc.twitchtopcheers.utils.PrintConsole;
+import org.apache.commons.io.FileUtils;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
+import java.util.HashMap;
/**
* Created on 20-07-2017 for TwitchGOTH_TOPSumCheers.
@@ -15,20 +17,29 @@
*/
public class FileCheerManagent {
- private static File filetop;
+ private static HashMap filetops = new HashMap<>();
+ public static void setTOP(HashMap top) {
+ if(top.isEmpty()) {
+ return;
+ }
+ for(int pos : top.keySet()) {
+ setTOP(pos,top.get(pos));
+ }
+ }
- public static void setTOP(Cheering cheer) {
+ public static void setTOP(int pos, Cheering cheer) {
try {
- FileWriter fw = new FileWriter(getFileTOP());
+ FileWriter fw = new FileWriter(filetops.get(pos));
BufferedWriter bw = new BufferedWriter(fw);
String message = ConfigManager.getConfig().getOut_message();
+ message = message.replace("{pos}",Integer.toString(pos));
message = message.replace("{username}",cheer.getUsername());
message = message.replace("{cheers}",cheer.getTotalCheers() + "");
- PrintConsole.sendInfo("Mensaje en top.txt fue reemplazado a: " + message);
+ PrintConsole.sendInfo("Mensaje en top_" + pos + ".txt fue reemplazado a: " + message);
bw.write(message);
bw.flush();
@@ -43,15 +54,41 @@ public static void loadFileCheerManagent() {
cheeckFile();
}
- private static File getFileTOP() {
- cheeckFile();
- return filetop;
+ private static File getFileTOP(int pos) {
+ if(filetops.containsKey(pos)) {
+ PrintConsole.sendError("Error al cargar archivo de TOP. La posicion es incorrecta.");
+ }
+ return filetops.get(pos);
+ }
+
+ public static void purgeFiles() {
+ PrintConsole.sendInfo("Eliminando archivos txt de TOP.");
+ try {
+ FileUtils.deleteDirectory(new File("cheers"));
+ } catch (IOException e) {
+ PrintConsole.sendWarning("Ocurrio un problema al momento de purgar la carpeta de cheers");
+ e.printStackTrace();
+ }
}
private static void cheeckFile() {
- filetop = new File("top.txt");
try {
- filetop.createNewFile();
+ filetops.clear(); //Para evitar problemas borramos todo lo registrado en caso de.
+
+ new File("cheers").mkdir(); //Creamos carpeta de txt
+
+ for(int pos = 1; pos <= ConfigManager.getConfig().getTop_size(); pos++) {
+ File filepos = new File("cheers/top_" + pos +".txt");
+ if(filepos.createNewFile()) {
+ FileWriter fw = new FileWriter(filepos);
+ BufferedWriter bw = new BufferedWriter(fw);
+
+ bw.write(ConfigManager.getConfig().getEmpty_message());
+ bw.flush();
+ bw.close();
+ }
+ filetops.put(pos,filepos);
+ }
} catch (IOException e) {
e.printStackTrace();
}
diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml
index 4585dc3..a9f1bae 100644
--- a/src/main/resources/config.yml
+++ b/src/main/resources/config.yml
@@ -1,7 +1,17 @@
-#Aqui va el mensaje de salida del sistema
-#Los parametros {username} y {cheers} seran reemplazados con el valor del sistema.
+#Configuracion
+#Asegurate de editar todo.
+
+#Los parametros {username} y {cheers} seran reemplazados con el valor del sistema en la config de messages.
+
+#Formato de mensaje cuando se asigna un top en un archivo
out_message: "{username} ha enviado en total {cheers} Cheers."
+#
+empty_message: "Sin TOP."
+
+#Cantidad de lugares del TOP (eso sera la cantidad de archivos para cada top)
+top_size: 2
+
channel_config:
#Ingrese el nombre de usuario del BOT
bot_username: ""