Skip to content

Commit

Permalink
Release 1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Doc94 committed Jul 25, 2017
1 parent 734f26a commit 6eec69d
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 37 deletions.
25 changes: 21 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.mrdoc</groupId>
<artifactId>Twitch_TOPCheersTotal</artifactId>
<version>1.0.0</version>
<version>1.2.0</version>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
Expand Down Expand Up @@ -66,14 +66,25 @@
<include>**/config.yml</include>
</includes>
</resource>
<!-- <resource>
<directory>src/main/resources/examples</directory>
</resource> -->
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>test.App</mainClass>
<addDefaultImplementationEntries>
true
</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

Expand Down Expand Up @@ -131,6 +142,12 @@
<artifactId>JCDP</artifactId>
<version>2.0.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-io -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>

</project>
20 changes: 12 additions & 8 deletions src/main/java/me/mrdoc/twitchtopcheers/Core.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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<Integer,Cheering> cheersTOP = DBCheerManagent.getTOPCheers(ConfigManager.getConfig().getTop_size());
FileCheerManagent.setTOP(cheersTOP);
}

//testALL();
Expand All @@ -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<Cheering> cheers = DBCheerManagent.getTOPCheers(1);
HashMap<Integer,Cheering> 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));
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/me/mrdoc/twitchtopcheers/bot/TwitchBOT.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -74,14 +75,14 @@ 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
.setServerPassword(ConfigManager.getConfig().getChannel_config().get("bot_token")) //Your oauth password from http://twitchapps.com/tmi
.addAutoJoinChannel(ConfigManager.getConfig().getChannel_config().get("channel_name"))

.addListener(new ManageCheerListener())
.addListener(new GeneralListener())

;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.");
}



}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -33,9 +34,9 @@ private void onCheerEvent(MessageEvent event) {
DBCheerManagent.addCheer(event.getUser().getNick(),cheerInMsg);

//Recargamos TOP
ArrayList<Cheering> cheersTOP = DBCheerManagent.getTOPCheers(1);
HashMap<Integer,Cheering> cheersTOP = DBCheerManagent.getTOPCheers(ConfigManager.getConfig().getTop_size());

FileCheerManagent.setTOP(cheersTOP.get(0));
FileCheerManagent.setTOP(cheersTOP);
}

}
18 changes: 18 additions & 0 deletions src/main/java/me/mrdoc/twitchtopcheers/classes/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
public class Config {

private String out_message;
private String empty_message;
private Map<String, String> channel_config;
private int top_size;

public String getOut_message() {
return out_message;
Expand All @@ -27,4 +29,20 @@ public Map<String, String> getChannel_config() {
public void setChannel_config(Map<String, String> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -82,8 +81,8 @@ public static Cheering getCheer(String username) {
return null;
}

public static ArrayList<Cheering> getTOPCheers(int cant) {
ArrayList<Cheering> cheers = new ArrayList<>();
public static HashMap<Integer,Cheering> getTOPCheers(int cant) {
HashMap<Integer,Cheering> cheers = new HashMap<Integer,Cheering>();

String command = "SELECT * FROM cheerings ORDER BY cheermount DESC LIMIT ? ;";

Expand All @@ -95,19 +94,17 @@ public static ArrayList<Cheering> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -15,20 +17,29 @@
*/
public class FileCheerManagent {

private static File filetop;
private static HashMap<Integer,File> filetops = new HashMap<>();

public static void setTOP(HashMap<Integer,Cheering> 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();
Expand All @@ -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();
}
Expand Down
Loading

0 comments on commit 6eec69d

Please sign in to comment.