Converted some of the management server to kotlin
Added json configs for management server Cleaned up some files
This commit is contained in:
@@ -1,13 +1,9 @@
|
||||
package ms.system;
|
||||
package ms.system
|
||||
|
||||
/**
|
||||
* The operating systems
|
||||
* @author Clayton Williams
|
||||
*
|
||||
*/
|
||||
public enum OperatingSystem {
|
||||
|
||||
UNIX,
|
||||
WINDOWS
|
||||
|
||||
}
|
||||
enum class OperatingSystem {
|
||||
UNIX, WINDOWS
|
||||
}
|
||||
@@ -476,11 +476,6 @@ public final class CommunicationInfo {
|
||||
* @return The rank.
|
||||
*/
|
||||
public ClanRank getRank(String contact) {
|
||||
for (String name : ServerConstants.ADMINISTRATORS) {
|
||||
if (contact.equals(name)) {
|
||||
return ClanRank.ADMINISTRATOR;
|
||||
}
|
||||
}
|
||||
return contacts.get(contact);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,100 +0,0 @@
|
||||
package ms.system.mysql;
|
||||
|
||||
import ms.ServerConstants;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* Manages the sql connections.
|
||||
* @author Vexia
|
||||
*
|
||||
*/
|
||||
public final class SQLManager {
|
||||
|
||||
/**
|
||||
* If the sql manager is locally hosted.
|
||||
*/
|
||||
public static final boolean LOCAL = true;
|
||||
|
||||
/**
|
||||
* The database URL.
|
||||
*/
|
||||
public static final String DATABASE_URL = (LOCAL ? "127.0.0.1" : "keldagrim.org") + ":3306/" + (SQLManager.LOCAL ? "global" : ServerConstants.DATABASE_NAMES[1]);
|
||||
|
||||
/**
|
||||
* The username of the user.
|
||||
*/
|
||||
private static final String USERNAME = (LOCAL ? "root" : "keldagr1_user");
|
||||
|
||||
/**
|
||||
* The password of the user.
|
||||
*/
|
||||
private static final String PASSWORD = (LOCAL ? "" : "2jf4wkz$");
|
||||
|
||||
|
||||
/**
|
||||
* IF the sql manager is initialized.
|
||||
*/
|
||||
private static boolean initialized;
|
||||
|
||||
/**
|
||||
* Constructs a new {@code SQLManager} {@code Object}
|
||||
*/
|
||||
public SQLManager() {
|
||||
/**
|
||||
* empty.
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the sql manager.
|
||||
*/
|
||||
public static void init() {
|
||||
initialized = true;
|
||||
WorldListSQLHandler.clearWorldList();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a connection from the pool.
|
||||
* @return The connection.
|
||||
*/
|
||||
public static Connection getConnection() {
|
||||
try {
|
||||
return DriverManager.getConnection("jdbc:mysql://" + DATABASE_URL + "?useTimezone=true&serverTimezone=UTC", USERNAME, PASSWORD);
|
||||
} catch (SQLException e) {
|
||||
System.out.println("Error: Mysql error message=" + e.getMessage() + ".");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Releases the connection so it's available for usage.
|
||||
* @param connection The connection.
|
||||
*/
|
||||
public static void close(Connection connection) {
|
||||
try {
|
||||
connection.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the initialized.
|
||||
* @return the initialized
|
||||
*/
|
||||
public static boolean isInitialized() {
|
||||
return initialized;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the bainitialized.
|
||||
* @param initialized the initialized to set.
|
||||
*/
|
||||
public static void setInitialized(boolean initialized) {
|
||||
SQLManager.initialized = initialized;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package ms.system.mysql
|
||||
|
||||
import ms.system.util.ManagementConstants
|
||||
import ms.system.util.ManagementConstants.Companion.DATABASE_HOST_ADDRESS
|
||||
import ms.system.util.ManagementConstants.Companion.DATABASE_NAME
|
||||
import ms.system.util.ManagementConstants.Companion.DATABASE_PASSWORD
|
||||
import ms.system.util.ManagementConstants.Companion.DATABASE_PORT
|
||||
import ms.system.util.ManagementConstants.Companion.DATABASE_USERNAME
|
||||
import java.sql.Connection
|
||||
import java.sql.DriverManager
|
||||
import java.sql.SQLException
|
||||
|
||||
/**
|
||||
* Manages the sql connections.
|
||||
* @author Vexia
|
||||
*/
|
||||
object SQLManager {
|
||||
|
||||
/**
|
||||
* IF the sql manager is initialized.
|
||||
*/
|
||||
var isInitialized = false
|
||||
|
||||
/**
|
||||
* Initializes the sql manager.
|
||||
*/
|
||||
@JvmStatic
|
||||
fun init() {
|
||||
isInitialized = true
|
||||
WorldListSQLHandler.clearWorldList()
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a connection from the pool.
|
||||
* @return The connection.
|
||||
*/
|
||||
@JvmStatic
|
||||
val connection: Connection?
|
||||
get() {
|
||||
try {
|
||||
return DriverManager.getConnection(
|
||||
"jdbc:mysql://$DATABASE_HOST_ADDRESS:$DATABASE_PORT/$DATABASE_NAME?useTimezone=true&serverTimezone=UTC",
|
||||
DATABASE_USERNAME,
|
||||
DATABASE_PASSWORD
|
||||
)
|
||||
} catch (e: SQLException) {
|
||||
println("Error: Mysql error message=" + e.message + ".")
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Releases the connection so it's available for usage.
|
||||
* @param connection The connection.
|
||||
*/
|
||||
@JvmStatic
|
||||
fun close(connection: Connection) {
|
||||
try {
|
||||
connection.close()
|
||||
} catch (e: SQLException) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package ms.system.util
|
||||
|
||||
import org.json.simple.JSONObject
|
||||
import org.json.simple.parser.JSONParser
|
||||
import java.io.File
|
||||
import java.io.FileReader
|
||||
import kotlin.system.exitProcess
|
||||
|
||||
/**
|
||||
* Class for parsing the server config, I.E default.json
|
||||
* @param path the path to the JSON file to parse.
|
||||
* @author Ceikry
|
||||
*/
|
||||
class ManagementConfigParser(path: String) {
|
||||
val pathTo = parsePath(path)
|
||||
val confFile = File(pathTo)
|
||||
val parser = JSONParser()
|
||||
var reader: FileReader? = null
|
||||
var data: JSONObject? = null
|
||||
|
||||
init {
|
||||
if(!confFile.canonicalFile.exists()){
|
||||
println("Could not find ${confFile.canonicalFile} - Double check your working directory!")
|
||||
exitProcess(0)
|
||||
} else if(!pathTo.contains(".json")) {
|
||||
println("Config file MUST be a JSON file!!")
|
||||
println("(Got $pathTo)")
|
||||
} else {
|
||||
reader = FileReader(pathTo)
|
||||
data = parser.parse(reader) as JSONObject
|
||||
parseDatabaseInformation()
|
||||
parseWorldTechnicalSettings()
|
||||
}
|
||||
}
|
||||
|
||||
private fun parseDatabaseInformation(){
|
||||
data ?: return
|
||||
val dbData = data!!["DatabaseInformation"] as JSONObject
|
||||
ManagementConstants().parseDBProp(dbData)
|
||||
}
|
||||
|
||||
private fun parseWorldTechnicalSettings(){
|
||||
data ?: return
|
||||
val wtiData = data!!["WorldTechnicalInformation"] as JSONObject
|
||||
ManagementConstants().parseWTIProp(wtiData)
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a path string
|
||||
* @author Ceikry
|
||||
* @param pathString The string to parse
|
||||
* @return a String with the proper file separators for the current OS.
|
||||
*/
|
||||
fun parsePath(pathString: String): String {
|
||||
var pathTokens: List<String>? = null
|
||||
if(pathString.contains("/"))
|
||||
pathTokens = pathString.split("/")
|
||||
else if(pathString.contains("\\"))
|
||||
pathTokens = pathString.split("\\")
|
||||
|
||||
pathTokens ?: return pathString //return the initial pathString if path does not contain file separators.
|
||||
var pathProduct = ""
|
||||
for(token in pathTokens){
|
||||
if(token != "")
|
||||
pathProduct += "$token${File.separator}"
|
||||
}
|
||||
|
||||
return pathProduct
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package ms.system.util
|
||||
|
||||
import org.json.simple.JSONObject
|
||||
|
||||
class ManagementConstants {
|
||||
|
||||
companion object {
|
||||
|
||||
//MySQL main database name
|
||||
var DATABASE_NAME: String = "global"
|
||||
|
||||
//MySQL database username
|
||||
var DATABASE_USERNAME: String = "root"
|
||||
|
||||
//MySQL database password
|
||||
var DATABASE_PASSWORD: String = ""
|
||||
|
||||
//MySQL host
|
||||
var DATABASE_HOST_ADDRESS: String = "127.0.0.1"
|
||||
|
||||
//MySQL port
|
||||
var DATABASE_PORT: Int = 3306
|
||||
|
||||
//Max amount of worlds supported on the world list
|
||||
var MAX_WORLD_AMOUNT: Int = 10
|
||||
|
||||
//User world hop delay in seconds
|
||||
var WORLD_HOP_DELAY: Long = 20_000L
|
||||
|
||||
}
|
||||
|
||||
fun parseDBProp(data: JSONObject) {
|
||||
DATABASE_NAME = data["database_name"].toString()
|
||||
DATABASE_USERNAME = data["database_username"].toString()
|
||||
DATABASE_PASSWORD = data["database_password"].toString()
|
||||
DATABASE_HOST_ADDRESS = data["database_host"].toString()
|
||||
DATABASE_PORT = data["database_port"].toString().toInt()
|
||||
}
|
||||
|
||||
fun parseWTIProp(data: JSONObject) {
|
||||
MAX_WORLD_AMOUNT = data["world_limit"].toString().toInt()
|
||||
WORLD_HOP_DELAY = data["worldhop_delay"].toString().toLong()
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user