From d13e8cde771f70a1f99a8312855a9aaefd3f60e8 Mon Sep 17 00:00:00 2001 From: jenzur Date: Tue, 5 May 2020 23:52:01 +0200 Subject: [PATCH] adding ingame chat support from sourcemod through java to python --- .../java/DataLayer/DBCPDataSourceAutismo.java | 42 +++++++++++++++++++ .../src/main/java/DataLayer/DataMapper.java | 28 +++++++++++++ .../main/java/FunctionLayer/Datahandler.java | 14 ++++++- .../PresentationLayer/DiscordHandler.java | 10 +++++ 4 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 ArtificialAutism/src/main/java/DataLayer/DBCPDataSourceAutismo.java diff --git a/ArtificialAutism/src/main/java/DataLayer/DBCPDataSourceAutismo.java b/ArtificialAutism/src/main/java/DataLayer/DBCPDataSourceAutismo.java new file mode 100644 index 00000000..df599f68 --- /dev/null +++ b/ArtificialAutism/src/main/java/DataLayer/DBCPDataSourceAutismo.java @@ -0,0 +1,42 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package DataLayer; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.apache.commons.dbcp2.BasicDataSource; +import DataLayer.settings; + +/** + * + * @author install1 + */ +public class DBCPDataSourceAutismo { + private static BasicDataSource ds = new BasicDataSource(); + static { + try { + ds.setDriver(new com.mysql.cj.jdbc.Driver()); + ds.setUrl(settings.autismo_url); + ds.setUsername(settings.autismo_username); + ds.setPassword(settings.autismo_password); + ds.setMaxTotal(-1); + ds.setMinIdle(5); + ds.setMaxIdle(-1); + ds.setMaxOpenPreparedStatements(100); + } catch (SQLException ex) { + Logger.getLogger(DBCPDataSourceAutismo.class.getName()).log(Level.SEVERE, null, ex); + } + } + + public static Connection getConnection() throws SQLException { + return ds.getConnection(); + } + + private DBCPDataSourceAutismo() { + } +} diff --git a/ArtificialAutism/src/main/java/DataLayer/DataMapper.java b/ArtificialAutism/src/main/java/DataLayer/DataMapper.java index b19734f4..ecf22d42 100644 --- a/ArtificialAutism/src/main/java/DataLayer/DataMapper.java +++ b/ArtificialAutism/src/main/java/DataLayer/DataMapper.java @@ -105,6 +105,34 @@ public class DataMapper { return hlStatsMessages; } + public static String check_autismo_mysql() { + String target = ""; + try (Connection l_cCon = DBCPDataSourceAutismo.getConnection()) { + String l_sSQL = "SELECT chatmessage FROM unloze_css_autism_bot.chatting c WHERE c.responsemessage = '' LIMIT 1"; + try (PreparedStatement l_pStatement = l_cCon.prepareStatement(l_sSQL)) { + try (ResultSet l_rsSearch = l_pStatement.executeQuery()) { + while (l_rsSearch.next()) { + target = l_rsSearch.getString(1); + } + } + } + } catch (SQLException ex) { + Logger.getLogger(DataMapper.class.getName()).log(Level.SEVERE, null, ex); + } + return target; + } + + public static void update_autismo_mysql(String responseMsg, String update_string) { + try (Connection l_cCon = DBCPDataSourceAutismo.getConnection()) { + String l_sSQL = "UPDATE unloze_css_autism_bot.`chatting` SET `responsemessage` = '" + responseMsg + "' WHERE `chatmessage` = '" + update_string + "'"; + try (PreparedStatement l_pStatement = l_cCon.prepareStatement(l_sSQL)) { + l_pStatement.execute(); + } + } catch (SQLException ex) { + Logger.getLogger(DataMapper.class.getName()).log(Level.SEVERE, null, ex); + } + } + public static void CloseConnections(PreparedStatement ps, ResultSet rs, Connection con) { if (rs != null) { try { diff --git a/ArtificialAutism/src/main/java/FunctionLayer/Datahandler.java b/ArtificialAutism/src/main/java/FunctionLayer/Datahandler.java index 429c68e3..84a8fc04 100644 --- a/ArtificialAutism/src/main/java/FunctionLayer/Datahandler.java +++ b/ArtificialAutism/src/main/java/FunctionLayer/Datahandler.java @@ -220,7 +220,7 @@ public class Datahandler { hlStatsMessages.put(str, hlStatsMessages.size()); } } - int capacity = 600; + int capacity = 50; hlStatsMessages.keySet().forEach(str -> { if (!str.startsWith("!") && MessageResponseHandler.getStr().values().size() < capacity) { String orElse = strCacheLocal.values().parallelStream().filter(e -> e.equals(str)).findAny().orElse(null); @@ -790,6 +790,18 @@ public class Datahandler { return stringCache.values().size() - (stringCache.values().size() / 10); } + public void update_autismo_mysql() { + String update_string = DataMapper.check_autismo_mysql(); + if (!update_string.isEmpty()) { + try { + String getResponseMsg = getResponseMsg(update_string); + DataMapper.update_autismo_mysql(getResponseMsg, update_string); + } catch (CustomError ex) { + Logger.getLogger(Datahandler.class.getName()).log(Level.SEVERE, null, ex); + } + } + } + private static class AnnotationCollector implements Consumer { private static int i = 0; diff --git a/ArtificialAutism/src/main/java/PresentationLayer/DiscordHandler.java b/ArtificialAutism/src/main/java/PresentationLayer/DiscordHandler.java index 5f69859f..87dd4079 100644 --- a/ArtificialAutism/src/main/java/PresentationLayer/DiscordHandler.java +++ b/ArtificialAutism/src/main/java/PresentationLayer/DiscordHandler.java @@ -19,6 +19,8 @@ import FunctionLayer.DoStuff; import FunctionLayer.PipelineJMWESingleton; import java.io.IOException; import java.sql.SQLException; +import java.util.Timer; +import java.util.TimerTask; import java.util.logging.Level; import java.util.logging.Logger; import org.javacord.api.DiscordApi; @@ -29,6 +31,12 @@ import org.javacord.api.DiscordApiBuilder; * @author install1 */ public class DiscordHandler { + public static class update_autismo_ingame_msgs extends TimerTask { + @Override + public void run() { + Datahandler.instance.update_autismo_mysql(); + } + } public static void main(String[] args) { System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", "15"); @@ -48,6 +56,8 @@ public class DiscordHandler { Datahandler.instance.updateStringCache(); String token = "NTI5NzAxNTk5NjAyMjc4NDAx.Dw0vDg.7-aMjVWdQMYPl8qVNyvTCPS5F_A"; DiscordApi api = new DiscordApiBuilder().setToken(token).login().join(); + Timer timer = new Timer(); + timer.scheduleAtFixedRate(new update_autismo_ingame_msgs(), 700, 700); api.addMessageCreateListener(event -> { if (!FunctionLayer.DoStuff.isOccupied()) { FunctionLayer.DoStuff.doStuff(event, api);