From cf7b7355ba25200f978e3b1b5203d14bc1abf960 Mon Sep 17 00:00:00 2001 From: beepbeat Date: Fri, 17 Nov 2017 22:17:51 +0100 Subject: [PATCH] Font is now configurable. Closes #13 Default config should be generated now (#14) updated error message to reflect, which file is missing fixes #15 --- src/ConfigHandler.kt | 27 +++++++++++++++++------- src/LogHandler.kt | 7 ++++++- src/Main.kt | 46 +++++++++++++++++++++++++++++++++++++---- src/WebSocketHandler.kt | 11 ++++++++++ 4 files changed, 79 insertions(+), 12 deletions(-) diff --git a/src/ConfigHandler.kt b/src/ConfigHandler.kt index f668c1d..f38a0d0 100644 --- a/src/ConfigHandler.kt +++ b/src/ConfigHandler.kt @@ -1,6 +1,7 @@ import org.apache.commons.io.IOUtils import java.io.File import java.io.PrintWriter +import java.lang.System import java.nio.charset.Charset import java.util.* @@ -14,11 +15,27 @@ public class Config{ } enum class entries{ - LogPathFr, ClientType, keyword, beep, LogPathRc, token + LogPathFr, ClientType, keyword, beep, LogPathRc, token, fontSize } -fun initConfig(){ - if (!configFile.exists()) configFile.createNewFile() +/** + * @return true if config already existed, false when it has been initialized with defaults + */ +fun initConfig() : Boolean{ + if (!configFile.exists()) { + configFile.createNewFile() + //DEFAULT CONFIG + config.varMap["${entries.LogPathFr}"] = "${System.getenv("appdata")}hexchat/logs/fuelrats/#fuelrats.log" + config.varMap["${entries.LogPathRc}"] = "${System.getenv("appdata")}hexchat/logs/fuelrats/#ratchat.log" + config.varMap["${entries.ClientType}"] = "hexchat" + config.varMap["${entries.keyword}"] = "ratsignal" + config.varMap["${entries.beep}"] = "true" + config.varMap["${entries.fontSize}"] = "20" + config.varMap["${entries.token}"] = "" + + + } + return true } fun readConfig(){ @@ -36,10 +53,6 @@ fun readConfig(){ println("token is: ${config.varMap["${entries.token}"]}") if ("${config.varMap["${entries.token}"]}" == "null"){config.varMap["${entries.token}"] = ""} - if (config.varMap["${entries.token}"] == ""){ - AuthHandler().authorize() - println(config.varMap["${entries.token}"]) - } } fun saveConfig(){ diff --git a/src/LogHandler.kt b/src/LogHandler.kt index 8684003..b904ed2 100644 --- a/src/LogHandler.kt +++ b/src/LogHandler.kt @@ -337,7 +337,7 @@ open class listener : TailerListenerAdapter(){ } override fun fileNotFound() { - toPrint.add("File Not Found") + toPrint.add("Path to the #Fuelrats log-file is incorrect. Please check your config, make sure you included the file name AND extension and restart") tailerfr.stop() } @@ -348,4 +348,9 @@ class rclistener : listener() { rctailerStopped = true ex!!.printStackTrace(stackFile) } + + override fun fileNotFound() { + toPrint.add("Path to the #Ratchat log-file is incorrect. Please check your config, make sure you included the file name AND extension and restart") + tailerrc.stop() + } } \ No newline at end of file diff --git a/src/Main.kt b/src/Main.kt index b8a53e7..b0103f2 100644 --- a/src/Main.kt +++ b/src/Main.kt @@ -4,12 +4,21 @@ import com.googlecode.lanterna.TextColor import com.googlecode.lanterna.input.KeyType import com.googlecode.lanterna.terminal.DefaultTerminalFactory import com.googlecode.lanterna.terminal.Terminal +import com.googlecode.lanterna.terminal.swing.AWTTerminalFontConfiguration +import com.googlecode.lanterna.terminal.swing.AWTTerminalFrame import kotlinx.coroutines.experimental.launch +import java.awt.Font import java.awt.event.KeyEvent import java.awt.event.KeyListener +import java.awt.event.WindowAdapter +import java.awt.event.WindowEvent +import java.io.BufferedOutputStream +import java.io.FileOutputStream +import java.io.PrintStream import java.util.* import kotlin.collections.ArrayList import kotlin.concurrent.fixedRateTimer +import kotlin.system.exitProcess class KeyProcessor : KeyListener{ @@ -25,17 +34,46 @@ class KeyProcessor : KeyListener{ } +class windowListen : WindowAdapter(){ + + override fun windowClosing(e: WindowEvent?) { + super.windowClosing(e) + } + + override fun windowClosed(e: WindowEvent?) { + super.windowClosed(e) + terminal!!.exitPrivateMode() + exitProcess(0) + } +} + fun main(args: Array) { - initConfig() - readConfig() + java.lang.System.setOut(PrintStream(BufferedOutputStream(FileOutputStream("output.txt")), true)) + + if (initConfig()){ + readConfig() + } + + if (config.varMap["${entries.token}"] == ""){ + AuthHandler().authorize() + println(config.varMap["${entries.token}"]) + } saveConfig() tailerfr.delay tailerrc.delay color = Pair(TextColor.ANSI.CYAN, color.second) - terminal = DefaultTerminalFactory().createTerminal() + terminal = DefaultTerminalFactory() + .setTerminalEmulatorTitle("JAFRDB: Just another FuelRats dispatch board") + .setForceAWTOverSwing(true) + .setTerminalEmulatorFontConfiguration(AWTTerminalFontConfiguration.newInstance(Font(Font.MONOSPACED, Font.PLAIN, config.varMap["${entries.fontSize}"]!!.toInt()))) + .createTerminal() + initTimers() + if (terminal is AWTTerminalFrame){ + (terminal as AWTTerminalFrame).addWindowListener(windowListen()) + } launch { while(true) { val keyStroke = terminal?.pollInput() ?: continue @@ -64,7 +102,7 @@ var tailerdog : Timer? = null var terminal: Terminal? = null fun initTimers(){ - screenupdate = fixedRateTimer("ScreenUpdater", true, 500, 1000, ::updateScreen) + screenupdate = fixedRateTimer("ScreenUpdater", true, 500, (1000.0 * (config.varMap["${entries.fontSize}"]!!.toLong() / 100.0 + 1.0).toLong()).toLong(), ::updateScreen) tailerdog = fixedRateTimer("TailerWatchdog", true, 500, 1000, ::checkTailer) } diff --git a/src/WebSocketHandler.kt b/src/WebSocketHandler.kt index 13724d2..0a51ad5 100644 --- a/src/WebSocketHandler.kt +++ b/src/WebSocketHandler.kt @@ -7,10 +7,12 @@ import com.google.gson.JsonObject import com.google.gson.JsonParser import com.google.gson.JsonPrimitive import kotlinx.coroutines.experimental.launch +import org.java_websocket.WebSocketImpl import org.java_websocket.handshake.ServerHandshake import java.lang.Exception import java.net.URI import java.util.* +import javax.net.ssl.SSLContext val parser = JsonParser() @@ -29,7 +31,14 @@ class WebSocket{ suspend fun init(){ + WebSocketImpl.DEBUG = true client = WebSocketClient(URI("wss://dev.api.fuelrats.com/?bearer=${config.varMap["${entries.token}"]}")) + println("TESTING REDIRECT, DEBUG MODE ACTIVATED") + + val sslcontext = SSLContext.getInstance("TLS") + sslcontext.init(null, null, null) + client.socket = sslcontext.socketFactory.createSocket() + var i = 1 var result = client.connectBlocking() while (!result){ @@ -54,6 +63,7 @@ class WebSocket{ meta.add("mydata", myData) root.add("meta", meta) client.send(root.toString()) + } @@ -62,6 +72,7 @@ class WebSocket{ class WebSocketClient(uri: URI) : org.java_websocket.client.WebSocketClient(uri){ override fun onOpen(handshakedata: ServerHandshake?) { //println("Opening Connection") + } override fun onClose(code: Int, reason: String?, remote: Boolean) {