From d5aa62e917bbab07db5225cc53d13a245d7939b1 Mon Sep 17 00:00:00 2001 From: Obique Date: Sun, 15 Dec 2019 20:29:47 -0500 Subject: [PATCH] Added mariadb compatibility for user database --- build.gradle | 8 +- .../data/server_info/database/Database.kt | 79 ++++++++++++++++ .../database/DatabaseConfiguration.kt | 53 +++++++++++ .../DatabaseTable.kt} | 49 +++------- .../database/PswgConfigDatabase.kt | 82 +++++++++++++++++ .../database/PswgObjectDatabase.kt | 90 +++++++++++++++++++ .../database/PswgResourceDatabase.kt | 73 +++++++++++++++ .../server_info/database/PswgUserDatabase.kt | 81 +++++++++++++++++ .../mariadb/PswgUserDatabaseMaria.kt | 77 ++++++++++++++++ ...Database.kt => PswgConfigDatabaseMongo.kt} | 41 ++++----- .../data/server_info/mongodb/PswgDatabase.kt | 86 ++++++------------ ...Database.kt => PswgObjectDatabaseMongo.kt} | 44 ++++----- ...tabase.kt => PswgResourceDatabaseMongo.kt} | 28 +++--- .../mongodb/PswgUserDatabaseMongo.kt | 67 ++++++++++++++ .../objects/swg/creature/CreatureObject.java | 3 +- .../support/global/zone/LoginService.java | 12 +-- 16 files changed, 700 insertions(+), 173 deletions(-) create mode 100644 src/main/java/com/projectswg/holocore/resources/support/data/server_info/database/Database.kt create mode 100644 src/main/java/com/projectswg/holocore/resources/support/data/server_info/database/DatabaseConfiguration.kt rename src/main/java/com/projectswg/holocore/resources/support/data/server_info/{mongodb/PswgUserDatabase.kt => database/DatabaseTable.kt} (58%) create mode 100644 src/main/java/com/projectswg/holocore/resources/support/data/server_info/database/PswgConfigDatabase.kt create mode 100644 src/main/java/com/projectswg/holocore/resources/support/data/server_info/database/PswgObjectDatabase.kt create mode 100644 src/main/java/com/projectswg/holocore/resources/support/data/server_info/database/PswgResourceDatabase.kt create mode 100644 src/main/java/com/projectswg/holocore/resources/support/data/server_info/database/PswgUserDatabase.kt create mode 100644 src/main/java/com/projectswg/holocore/resources/support/data/server_info/mariadb/PswgUserDatabaseMaria.kt rename src/main/java/com/projectswg/holocore/resources/support/data/server_info/mongodb/{PswgConfigDatabase.kt => PswgConfigDatabaseMongo.kt} (72%) rename src/main/java/com/projectswg/holocore/resources/support/data/server_info/mongodb/{PswgObjectDatabase.kt => PswgObjectDatabaseMongo.kt} (68%) rename src/main/java/com/projectswg/holocore/resources/support/data/server_info/mongodb/{PswgResourceDatabase.kt => PswgResourceDatabaseMongo.kt} (66%) create mode 100644 src/main/java/com/projectswg/holocore/resources/support/data/server_info/mongodb/PswgUserDatabaseMongo.kt diff --git a/build.gradle b/build.gradle index d2ced2a50..b2b09536e 100644 --- a/build.gradle +++ b/build.gradle @@ -5,13 +5,12 @@ plugins { id 'java' id 'idea' id "com.github.johnrengelman.shadow" version "5.1.0" - id "org.javamodularity.moduleplugin" version "1.5.0" + id "org.javamodularity.moduleplugin" version "1.6.0" id 'org.jetbrains.kotlin.jvm' version '1.3.50' - id "org.beryx.jlink" version "2.15.1" + id "org.beryx.jlink" version "2.16.2" } -mainClassName = 'com.projectswg.holocore.ProjectSWG' -sourceCompatibility = 12 +mainClassName = 'holocore/com.projectswg.holocore.ProjectSWG' sourceSets { main @@ -61,6 +60,7 @@ dependencies { compile group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib', version: '1.3.50' compile group: 'org.jetbrains.kotlinx', name: 'kotlinx-coroutines-core', version: '1.3.0' compile group: 'org.xerial', name: 'sqlite-jdbc', version: '3.28.0' + compile group: 'org.mariadb.jdbc', name: 'mariadb-java-client', version: '2.5.2' compile group: 'org.mongodb', name: 'mongodb-driver-sync', version: '3.11.0' compile group: 'me.joshlarson', name: "fast-json", version: '3.0.0' compile group: 'me.joshlarson', name: 'jlcommon-network', version: '1.0.0' diff --git a/src/main/java/com/projectswg/holocore/resources/support/data/server_info/database/Database.kt b/src/main/java/com/projectswg/holocore/resources/support/data/server_info/database/Database.kt new file mode 100644 index 000000000..ad0217b8b --- /dev/null +++ b/src/main/java/com/projectswg/holocore/resources/support/data/server_info/database/Database.kt @@ -0,0 +1,79 @@ +/*********************************************************************************** + * Copyright (c) 2019 /// Project SWG /// www.projectswg.com * + * * + * ProjectSWG is the first NGE emulator for Star Wars Galaxies founded on * + * July 7th, 2011 after SOE announced the official shutdown of Star Wars Galaxies. * + * Our goal is to create an emulator which will provide a server for players to * + * continue playing a game similar to the one they used to play. We are basing * + * it on the final publish of the game prior to end-game events. * + * * + * This file is part of Holocore. * + * * + * --------------------------------------------------------------------------------* + * * + * Holocore is free software: you can redistribute it and/or modify * + * it under the terms of the GNU Affero General Public License as * + * published by the Free Software Foundation, either version 3 of the * + * License, or (at your option) any later version. * + * * + * Holocore is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Affero General Public License for more details. * + * * + * You should have received a copy of the GNU Affero General Public License * + * along with Holocore. If not, see . * + ***********************************************************************************/ + +package com.projectswg.holocore.resources.support.data.server_info.database + +import com.mongodb.client.MongoDatabase +import com.mongodb.client.model.Filters +import me.joshlarson.jlcommon.log.Log +import java.sql.DriverManager +import java.util.logging.Handler +import java.util.logging.Level +import java.util.logging.LogRecord +import java.util.logging.Logger + +class Database(mongo: MongoDatabase) { + + val config = DatabaseTable(mongo.getCollection("config"), null, null, null) + private val configuration = config.mongo.find(Filters.exists("connector")).map { DatabaseConfiguration(it) }.first() + private val connection = if (configuration == null || configuration.connector != "mariadb") null + else DriverManager.getConnection("jdbc:mariadb://${configuration.host}:${configuration.port}/${configuration.database}?autoReconnect=true&user=${configuration.user}&password=${configuration.pass}") + val users = DatabaseTable(mongo.getCollection("users"), configuration, connection, configuration?.tables?.get("users")) + val objects = DatabaseTable(mongo.getCollection("objects"), configuration, connection, configuration?.tables?.get("objects")) + val resources = DatabaseTable(mongo.getCollection("resources"), configuration, connection, configuration?.tables?.get("resources")) + + init { + setupMongoLogging() + } + + private fun setupMongoLogging() { + var mongoLogger: Logger? = Logger.getLogger("com.mongodb") + while (mongoLogger != null) { + for (handler in mongoLogger.handlers) { + mongoLogger.removeHandler(handler) + } + if (mongoLogger.parent != null) + mongoLogger = mongoLogger.parent + else + break + } + mongoLogger?.addHandler(object : Handler() { + override fun publish(record: LogRecord) { + when (record.level) { + Level.INFO -> Log.i("MongoDB: %s", record.message) + Level.WARNING -> Log.w("MongoDB: %s", record.message) + Level.SEVERE -> Log.e("MongoDB: %s", record.message) + else -> Log.t("MongoDB: %s", record.message) + } + } + + override fun flush() {} + override fun close() {} + }) + } + +} diff --git a/src/main/java/com/projectswg/holocore/resources/support/data/server_info/database/DatabaseConfiguration.kt b/src/main/java/com/projectswg/holocore/resources/support/data/server_info/database/DatabaseConfiguration.kt new file mode 100644 index 000000000..5e961de66 --- /dev/null +++ b/src/main/java/com/projectswg/holocore/resources/support/data/server_info/database/DatabaseConfiguration.kt @@ -0,0 +1,53 @@ +/*********************************************************************************** + * Copyright (c) 2019 /// Project SWG /// www.projectswg.com * + * * + * ProjectSWG is the first NGE emulator for Star Wars Galaxies founded on * + * July 7th, 2011 after SOE announced the official shutdown of Star Wars Galaxies. * + * Our goal is to create an emulator which will provide a server for players to * + * continue playing a game similar to the one they used to play. We are basing * + * it on the final publish of the game prior to end-game events. * + * * + * This file is part of Holocore. * + * * + * --------------------------------------------------------------------------------* + * * + * Holocore is free software: you can redistribute it and/or modify * + * it under the terms of the GNU Affero General Public License as * + * published by the Free Software Foundation, either version 3 of the * + * License, or (at your option) any later version. * + * * + * Holocore is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Affero General Public License for more details. * + * * + * You should have received a copy of the GNU Affero General Public License * + * along with Holocore. If not, see . * + ***********************************************************************************/ + +package com.projectswg.holocore.resources.support.data.server_info.database + +import org.bson.Document + + +class DatabaseConfiguration(doc: Document) { + + val connector: String = doc.getString("connector") ?: "mongodb" + val host: String = doc.getString("host") ?: "" + val port: Int = doc.getInteger("port", 0) + val database: String = doc.getString("database") ?: "" + val user: String = doc.getString("user") ?: "" + val pass: String = doc.getString("pass") ?: "" + val accessLevels = doc.getList("accessLevels", Document::class.java, ArrayList()) + .map { ConfigurationAccessLevels(it) } + .filter { it.user.isNotEmpty() } + .groupBy { it.user } + .mapValues { it.value.last().accessLevel } + val tables: Map = doc.get("tables", Document()).toMap().filter { it.key is String && it.value is String }.mapValues { it.value as String } + + class ConfigurationAccessLevels(doc: Document) { + val user: String = doc.getString("user") ?: "" + val accessLevel: String = doc.getString("accessLevel") ?: "player" + } + +} diff --git a/src/main/java/com/projectswg/holocore/resources/support/data/server_info/mongodb/PswgUserDatabase.kt b/src/main/java/com/projectswg/holocore/resources/support/data/server_info/database/DatabaseTable.kt similarity index 58% rename from src/main/java/com/projectswg/holocore/resources/support/data/server_info/mongodb/PswgUserDatabase.kt rename to src/main/java/com/projectswg/holocore/resources/support/data/server_info/database/DatabaseTable.kt index d3e5b2dc9..0edd975cc 100644 --- a/src/main/java/com/projectswg/holocore/resources/support/data/server_info/mongodb/PswgUserDatabase.kt +++ b/src/main/java/com/projectswg/holocore/resources/support/data/server_info/database/DatabaseTable.kt @@ -1,63 +1,38 @@ /*********************************************************************************** * Copyright (c) 2019 /// Project SWG /// www.projectswg.com * - * * + * * * ProjectSWG is the first NGE emulator for Star Wars Galaxies founded on * * July 7th, 2011 after SOE announced the official shutdown of Star Wars Galaxies. * * Our goal is to create an emulator which will provide a server for players to * * continue playing a game similar to the one they used to play. We are basing * * it on the final publish of the game prior to end-game events. * - * * + * * * This file is part of Holocore. * - * * + * * * --------------------------------------------------------------------------------* - * * + * * * Holocore is free software: you can redistribute it and/or modify * * it under the terms of the GNU Affero General Public License as * * published by the Free Software Foundation, either version 3 of the * * License, or (at your option) any later version. * - * * + * * * Holocore is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU Affero General Public License for more details. * - * * + * * * You should have received a copy of the GNU Affero General Public License * - * along with Holocore. If not, see //www.gnu.org/licenses/>. * - */ + * along with Holocore. If not, see . * + ***********************************************************************************/ -package com.projectswg.holocore.resources.support.data.server_info.mongodb +package com.projectswg.holocore.resources.support.data.server_info.database import com.mongodb.client.MongoCollection -import com.mongodb.client.model.Filters -import com.mongodb.client.model.IndexOptions -import com.mongodb.client.model.Indexes import org.bson.Document +import java.sql.Connection -class PswgUserDatabase(private val collection: MongoCollection?) { +class DatabaseTable(val mongo: MongoCollection, val configuration: DatabaseConfiguration?, val mariaConnection: Connection?, val mariaTable: String?) { - init { - collection?.createIndex(Indexes.ascending("username"), IndexOptions().unique(true)) - } - - fun getUser(username: String): UserMetadata? { - return collection - ?.find(Filters.eq("username", username)) - ?.map { UserMetadata(it) } - ?.first() - } - -} - -class UserMetadata(doc: Document) { - - val accountId: String = doc.getObjectId("_id").toHexString() - val username: String = doc.getString("username") - val password: String = doc.getString("password") - val accessLevel: String = doc.getString("accessLevel") - val isBanned: Boolean = doc.getBoolean("banned") - - override fun toString(): String = "UserMetadata[username=$username accessLevel=$accessLevel banned=$isBanned]" - override fun equals(other: Any?): Boolean = if (other is UserMetadata) accountId == other.accountId else false - override fun hashCode(): Int = accountId.hashCode() + fun isMariaDefined() = mariaConnection != null && mariaTable != null } diff --git a/src/main/java/com/projectswg/holocore/resources/support/data/server_info/database/PswgConfigDatabase.kt b/src/main/java/com/projectswg/holocore/resources/support/data/server_info/database/PswgConfigDatabase.kt new file mode 100644 index 000000000..6d172c0d4 --- /dev/null +++ b/src/main/java/com/projectswg/holocore/resources/support/data/server_info/database/PswgConfigDatabase.kt @@ -0,0 +1,82 @@ +/*********************************************************************************** + * Copyright (c) 2019 /// Project SWG /// www.projectswg.com * + * * + * ProjectSWG is the first NGE emulator for Star Wars Galaxies founded on * + * July 7th, 2011 after SOE announced the official shutdown of Star Wars Galaxies. * + * Our goal is to create an emulator which will provide a server for players to * + * continue playing a game similar to the one they used to play. We are basing * + * it on the final publish of the game prior to end-game events. * + * * + * This file is part of Holocore. * + * * + * --------------------------------------------------------------------------------* + * * + * Holocore is free software: you can redistribute it and/or modify * + * it under the terms of the GNU Affero General Public License as * + * published by the Free Software Foundation, either version 3 of the * + * License, or (at your option) any later version. * + * * + * Holocore is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Affero General Public License for more details. * + * * + * You should have received a copy of the GNU Affero General Public License * + * along with Holocore. If not, see . * + ***********************************************************************************/ + +/*********************************************************************************** + * Copyright (c) 2019 /// Project SWG /// www.projectswg.com * + * * + * ProjectSWG is the first NGE emulator for Star Wars Galaxies founded on * + * July 7th, 2011 after SOE announced the official shutdown of Star Wars Galaxies. * + * Our goal is to create an emulator which will provide a server for players to * + * continue playing a game similar to the one they used to play. We are basing * + * it on the final publish of the game prior to end-game events. * + * * + * This file is part of Holocore. * + * * + * --------------------------------------------------------------------------------* + * * + * Holocore is free software: you can redistribute it and/or modify * + * it under the terms of the GNU Affero General Public License as * + * published by the Free Software Foundation, either version 3 of the * + * License, or (at your option) any later version. * + * * + * Holocore is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Affero General Public License for more details. * + * * + * You should have received a copy of the GNU Affero General Public License * + * along with Holocore. If not, see //www.gnu.org/licenses/>. * + */ + +package com.projectswg.holocore.resources.support.data.server_info.database + +/** + * Empty config database that only returns the defaults + */ +interface PswgConfigDatabase { + + fun getString(o: Any, key: String, def: String): String + fun getBoolean(o: Any, key: String, def: Boolean): Boolean + fun getInt(o: Any, key: String, def: Int): Int + fun getDouble(o: Any, key: String, def: Double): Double + fun getLong(o: Any, key: String, def: Long): Long + + companion object { + + fun createDefault(): PswgConfigDatabase { + return object : PswgConfigDatabase { + override fun getString(o: Any, key: String, def: String): String = def + override fun getBoolean(o: Any, key: String, def: Boolean): Boolean = def + override fun getInt(o: Any, key: String, def: Int): Int = def + override fun getDouble(o: Any, key: String, def: Double): Double = def + override fun getLong(o: Any, key: String, def: Long): Long = def + } + } + + } + +} diff --git a/src/main/java/com/projectswg/holocore/resources/support/data/server_info/database/PswgObjectDatabase.kt b/src/main/java/com/projectswg/holocore/resources/support/data/server_info/database/PswgObjectDatabase.kt new file mode 100644 index 000000000..1156fcf81 --- /dev/null +++ b/src/main/java/com/projectswg/holocore/resources/support/data/server_info/database/PswgObjectDatabase.kt @@ -0,0 +1,90 @@ +/*********************************************************************************** + * Copyright (c) 2019 /// Project SWG /// www.projectswg.com * + * * + * ProjectSWG is the first NGE emulator for Star Wars Galaxies founded on * + * July 7th, 2011 after SOE announced the official shutdown of Star Wars Galaxies. * + * Our goal is to create an emulator which will provide a server for players to * + * continue playing a game similar to the one they used to play. We are basing * + * it on the final publish of the game prior to end-game events. * + * * + * This file is part of Holocore. * + * * + * --------------------------------------------------------------------------------* + * * + * Holocore is free software: you can redistribute it and/or modify * + * it under the terms of the GNU Affero General Public License as * + * published by the Free Software Foundation, either version 3 of the * + * License, or (at your option) any later version. * + * * + * Holocore is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Affero General Public License for more details. * + * * + * You should have received a copy of the GNU Affero General Public License * + * along with Holocore. If not, see . * + ***********************************************************************************/ + +/*********************************************************************************** + * Copyright (c) 2019 /// Project SWG /// www.projectswg.com * + * * + * ProjectSWG is the first NGE emulator for Star Wars Galaxies founded on * + * July 7th, 2011 after SOE announced the official shutdown of Star Wars Galaxies. * + * Our goal is to create an emulator which will provide a server for players to * + * continue playing a game similar to the one they used to play. We are basing * + * it on the final publish of the game prior to end-game events. * + * * + * This file is part of Holocore. * + * * + * --------------------------------------------------------------------------------* + * * + * Holocore is free software: you can redistribute it and/or modify * + * it under the terms of the GNU Affero General Public License as * + * published by the Free Software Foundation, either version 3 of the * + * License, or (at your option) any later version. * + * * + * Holocore is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Affero General Public License for more details. * + * * + * You should have received a copy of the GNU Affero General Public License * + * along with Holocore. If not, see //www.gnu.org/licenses/>. * + */ + +package com.projectswg.holocore.resources.support.data.server_info.database + +import com.projectswg.common.data.encodables.mongo.MongoData +import com.projectswg.holocore.resources.support.objects.swg.SWGObject + +interface PswgObjectDatabase { + + val objects: List + + fun addObject(obj: SWGObject) + fun addObjects(objects: Collection) + + fun removeObject(id: Long): Boolean + + fun getCharacterCount(account: String): Int + fun isCharacter(firstName: String): Boolean + + fun clearObjects(): Long + + companion object { + + fun createDefault(): PswgObjectDatabase { + return object : PswgObjectDatabase { + override val objects: List = ArrayList() + override fun addObject(obj: SWGObject) {} + override fun addObjects(objects: Collection) {} + override fun removeObject(id: Long): Boolean = true + override fun getCharacterCount(account: String): Int = 0 + override fun isCharacter(firstName: String): Boolean = false + override fun clearObjects(): Long = 0 + } + } + + } + +} diff --git a/src/main/java/com/projectswg/holocore/resources/support/data/server_info/database/PswgResourceDatabase.kt b/src/main/java/com/projectswg/holocore/resources/support/data/server_info/database/PswgResourceDatabase.kt new file mode 100644 index 000000000..3a8662543 --- /dev/null +++ b/src/main/java/com/projectswg/holocore/resources/support/data/server_info/database/PswgResourceDatabase.kt @@ -0,0 +1,73 @@ +/*********************************************************************************** + * Copyright (c) 2019 /// Project SWG /// www.projectswg.com * + * * + * ProjectSWG is the first NGE emulator for Star Wars Galaxies founded on * + * July 7th, 2011 after SOE announced the official shutdown of Star Wars Galaxies. * + * Our goal is to create an emulator which will provide a server for players to * + * continue playing a game similar to the one they used to play. We are basing * + * it on the final publish of the game prior to end-game events. * + * * + * This file is part of Holocore. * + * * + * --------------------------------------------------------------------------------* + * * + * Holocore is free software: you can redistribute it and/or modify * + * it under the terms of the GNU Affero General Public License as * + * published by the Free Software Foundation, either version 3 of the * + * License, or (at your option) any later version. * + * * + * Holocore is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Affero General Public License for more details. * + * * + * You should have received a copy of the GNU Affero General Public License * + * along with Holocore. If not, see . * + ***********************************************************************************/ + +/*********************************************************************************** + * Copyright (c) 2019 /// Project SWG /// www.projectswg.com * + * * + * ProjectSWG is the first NGE emulator for Star Wars Galaxies founded on * + * July 7th, 2011 after SOE announced the official shutdown of Star Wars Galaxies. * + * Our goal is to create an emulator which will provide a server for players to * + * continue playing a game similar to the one they used to play. We are basing * + * it on the final publish of the game prior to end-game events. * + * * + * This file is part of Holocore. * + * * + * --------------------------------------------------------------------------------* + * * + * Holocore is free software: you can redistribute it and/or modify * + * it under the terms of the GNU Affero General Public License as * + * published by the Free Software Foundation, either version 3 of the * + * License, or (at your option) any later version. * + * * + * Holocore is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Affero General Public License for more details. * + * * + * You should have received a copy of the GNU Affero General Public License * + * along with Holocore. If not, see //www.gnu.org/licenses/>. * + */ + +package com.projectswg.holocore.resources.support.data.server_info.database + +import com.projectswg.holocore.resources.gameplay.crafting.resource.galactic.GalacticResource + +interface PswgResourceDatabase { + + var resources: List + + companion object { + + fun createDefault(): PswgResourceDatabase { + return object : PswgResourceDatabase { + override var resources: List = ArrayList() + } + } + + } + +} diff --git a/src/main/java/com/projectswg/holocore/resources/support/data/server_info/database/PswgUserDatabase.kt b/src/main/java/com/projectswg/holocore/resources/support/data/server_info/database/PswgUserDatabase.kt new file mode 100644 index 000000000..2caa56c08 --- /dev/null +++ b/src/main/java/com/projectswg/holocore/resources/support/data/server_info/database/PswgUserDatabase.kt @@ -0,0 +1,81 @@ +/*********************************************************************************** + * Copyright (c) 2019 /// Project SWG /// www.projectswg.com * + * * + * ProjectSWG is the first NGE emulator for Star Wars Galaxies founded on * + * July 7th, 2011 after SOE announced the official shutdown of Star Wars Galaxies. * + * Our goal is to create an emulator which will provide a server for players to * + * continue playing a game similar to the one they used to play. We are basing * + * it on the final publish of the game prior to end-game events. * + * * + * This file is part of Holocore. * + * * + * --------------------------------------------------------------------------------* + * * + * Holocore is free software: you can redistribute it and/or modify * + * it under the terms of the GNU Affero General Public License as * + * published by the Free Software Foundation, either version 3 of the * + * License, or (at your option) any later version. * + * * + * Holocore is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Affero General Public License for more details. * + * * + * You should have received a copy of the GNU Affero General Public License * + * along with Holocore. If not, see . * + ***********************************************************************************/ + +/*********************************************************************************** + * Copyright (c) 2019 /// Project SWG /// www.projectswg.com * + * * + * ProjectSWG is the first NGE emulator for Star Wars Galaxies founded on * + * July 7th, 2011 after SOE announced the official shutdown of Star Wars Galaxies. * + * Our goal is to create an emulator which will provide a server for players to * + * continue playing a game similar to the one they used to play. We are basing * + * it on the final publish of the game prior to end-game events. * + * * + * This file is part of Holocore. * + * * + * --------------------------------------------------------------------------------* + * * + * Holocore is free software: you can redistribute it and/or modify * + * it under the terms of the GNU Affero General Public License as * + * published by the Free Software Foundation, either version 3 of the * + * License, or (at your option) any later version. * + * * + * Holocore is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Affero General Public License for more details. * + * * + * You should have received a copy of the GNU Affero General Public License * + * along with Holocore. If not, see //www.gnu.org/licenses/>. * + */ + +package com.projectswg.holocore.resources.support.data.server_info.database + +import com.projectswg.holocore.resources.support.global.player.AccessLevel + +interface PswgUserDatabase { + + fun getUser(username: String): UserMetadata? + + companion object { + + fun createDefault(): PswgUserDatabase { + return object : PswgUserDatabase { + override fun getUser(username: String): UserMetadata? = null + } + } + + } + +} + +class UserMetadata(val accountId: String, val username: String, val password: String, val accessLevel: AccessLevel, val isBanned: Boolean) { + + override fun toString(): String = "UserMetadata[username=$username accessLevel=$accessLevel banned=$isBanned]" + override fun equals(other: Any?): Boolean = if (other is UserMetadata) accountId == other.accountId else false + override fun hashCode(): Int = accountId.hashCode() + +} diff --git a/src/main/java/com/projectswg/holocore/resources/support/data/server_info/mariadb/PswgUserDatabaseMaria.kt b/src/main/java/com/projectswg/holocore/resources/support/data/server_info/mariadb/PswgUserDatabaseMaria.kt new file mode 100644 index 000000000..5e93d6977 --- /dev/null +++ b/src/main/java/com/projectswg/holocore/resources/support/data/server_info/mariadb/PswgUserDatabaseMaria.kt @@ -0,0 +1,77 @@ +/*********************************************************************************** + * Copyright (c) 2019 /// Project SWG /// www.projectswg.com * + * * + * ProjectSWG is the first NGE emulator for Star Wars Galaxies founded on * + * July 7th, 2011 after SOE announced the official shutdown of Star Wars Galaxies. * + * Our goal is to create an emulator which will provide a server for players to * + * continue playing a game similar to the one they used to play. We are basing * + * it on the final publish of the game prior to end-game events. * + * * + * This file is part of Holocore. * + * * + * --------------------------------------------------------------------------------* + * * + * Holocore is free software: you can redistribute it and/or modify * + * it under the terms of the GNU Affero General Public License as * + * published by the Free Software Foundation, either version 3 of the * + * License, or (at your option) any later version. * + * * + * Holocore is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Affero General Public License for more details. * + * * + * You should have received a copy of the GNU Affero General Public License * + * along with Holocore. If not, see . * + ***********************************************************************************/ + +package com.projectswg.holocore.resources.support.data.server_info.mariadb + +import com.projectswg.holocore.resources.support.data.server_info.database.DatabaseTable +import com.projectswg.holocore.resources.support.data.server_info.database.PswgUserDatabase +import com.projectswg.holocore.resources.support.data.server_info.database.UserMetadata +import com.projectswg.holocore.resources.support.global.player.AccessLevel +import me.joshlarson.jlcommon.log.Log +import java.sql.ResultSet +import java.sql.SQLException +import java.util.* + +class PswgUserDatabaseMaria(private val database: DatabaseTable): PswgUserDatabase { + + private val getUserStatement = ThreadLocal.withInitial { database.mariaConnection!!.prepareStatement("SELECT userID, password, banned FROM ${database.mariaTable!!} WHERE username = $1") } + + override fun getUser(username: String): UserMetadata? { + try { + val accessLevel = when (database.configuration?.accessLevels?.get(username)) { + "warden" -> AccessLevel.WARDEN + "csr" -> AccessLevel.CSR + "qa" -> AccessLevel.QA + "dev" -> AccessLevel.DEV + else -> AccessLevel.PLAYER + } + val statement = getUserStatement.get() + statement.setString(1, username) + statement.executeQuery().use { set -> + return UserMetadata(accountId = set.getInt("userId").toString(16).toUpperCase(Locale.US), + username = username, + password = set.getString("password") ?: return null, + accessLevel = accessLevel, + isBanned = set.getInt("banned") != 0) + } + } catch (e: SQLException) { + Log.w("SQLException when looking up user: $username") + Log.w(e) + } + return null + } + + private inline fun (ResultSet).use(op: (ResultSet) -> Unit) { + @Suppress("ConvertTryFinallyToUseCall") + try { + op(this) + } finally { + close() + } + } + +} diff --git a/src/main/java/com/projectswg/holocore/resources/support/data/server_info/mongodb/PswgConfigDatabase.kt b/src/main/java/com/projectswg/holocore/resources/support/data/server_info/mongodb/PswgConfigDatabaseMongo.kt similarity index 72% rename from src/main/java/com/projectswg/holocore/resources/support/data/server_info/mongodb/PswgConfigDatabase.kt rename to src/main/java/com/projectswg/holocore/resources/support/data/server_info/mongodb/PswgConfigDatabaseMongo.kt index c3223e5c9..533223ab9 100644 --- a/src/main/java/com/projectswg/holocore/resources/support/data/server_info/mongodb/PswgConfigDatabase.kt +++ b/src/main/java/com/projectswg/holocore/resources/support/data/server_info/mongodb/PswgConfigDatabaseMongo.kt @@ -1,29 +1,29 @@ /*********************************************************************************** * Copyright (c) 2019 /// Project SWG /// www.projectswg.com * - * * + * * * ProjectSWG is the first NGE emulator for Star Wars Galaxies founded on * * July 7th, 2011 after SOE announced the official shutdown of Star Wars Galaxies. * * Our goal is to create an emulator which will provide a server for players to * * continue playing a game similar to the one they used to play. We are basing * * it on the final publish of the game prior to end-game events. * - * * + * * * This file is part of Holocore. * - * * + * * * --------------------------------------------------------------------------------* - * * + * * * Holocore is free software: you can redistribute it and/or modify * * it under the terms of the GNU Affero General Public License as * * published by the Free Software Foundation, either version 3 of the * * License, or (at your option) any later version. * - * * + * * * Holocore is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU Affero General Public License for more details. * - * * + * * * You should have received a copy of the GNU Affero General Public License * - * along with Holocore. If not, see //www.gnu.org/licenses/>. * - */ + * along with Holocore. If not, see . * + ***********************************************************************************/ package com.projectswg.holocore.resources.support.data.server_info.mongodb @@ -31,20 +31,16 @@ import com.mongodb.client.MongoCollection import com.mongodb.client.model.Filters import com.mongodb.client.model.IndexOptions import com.mongodb.client.model.Indexes +import com.projectswg.holocore.resources.support.data.server_info.database.PswgConfigDatabase import org.bson.Document -import java.util.* -/** - * Empty config database that only returns the defaults - */ -class PswgConfigDatabase(private val collection: MongoCollection?) { +class PswgConfigDatabaseMongo(private val collection: MongoCollection) : PswgConfigDatabase { init { - collection?.createIndex(Indexes.ascending("package"), IndexOptions().unique(true)) + collection.createIndex(Indexes.ascending("package"), IndexOptions().unique(true)) } - fun getString(o: Any, key: String, def: String): String { - collection ?: return def + override fun getString(o: Any, key: String, def: String): String { for (config in getConfigurations(o)) { if (config.containsKey(key)) return config.getString(key) @@ -52,8 +48,7 @@ class PswgConfigDatabase(private val collection: MongoCollection?) { return def } - fun getBoolean(o: Any, key: String, def: Boolean): Boolean { - collection ?: return def + override fun getBoolean(o: Any, key: String, def: Boolean): Boolean { for (config in getConfigurations(o)) { if (config.containsKey(key)) return config.getBoolean(key)!! @@ -61,8 +56,7 @@ class PswgConfigDatabase(private val collection: MongoCollection?) { return def } - fun getInt(o: Any, key: String, def: Int): Int { - collection ?: return def + override fun getInt(o: Any, key: String, def: Int): Int { for (config in getConfigurations(o)) { if (config.containsKey(key)) return config.getInteger(key)!! @@ -70,8 +64,7 @@ class PswgConfigDatabase(private val collection: MongoCollection?) { return def } - fun getDouble(o: Any, key: String, def: Double): Double { - collection ?: return def + override fun getDouble(o: Any, key: String, def: Double): Double { for (config in getConfigurations(o)) { if (config.containsKey(key)) return config.getDouble(key)!! @@ -79,8 +72,7 @@ class PswgConfigDatabase(private val collection: MongoCollection?) { return def } - fun getLong(o: Any, key: String, def: Long): Long { - collection ?: return def + override fun getLong(o: Any, key: String, def: Long): Long { for (config in getConfigurations(o)) { if (config.containsKey(key)) return config.getLong(key)!! @@ -89,7 +81,6 @@ class PswgConfigDatabase(private val collection: MongoCollection?) { } private fun getConfigurations(o: Any): List { - collection!! // Should be verified in calling functions var packageKey = if (o is Class<*>) o.packageName else o.javaClass.packageName require(packageKey.startsWith("com.projectswg.holocore")) { "packageKey must be a part of holocore, was: $packageKey" } diff --git a/src/main/java/com/projectswg/holocore/resources/support/data/server_info/mongodb/PswgDatabase.kt b/src/main/java/com/projectswg/holocore/resources/support/data/server_info/mongodb/PswgDatabase.kt index 6a4c8a71a..729cce00e 100644 --- a/src/main/java/com/projectswg/holocore/resources/support/data/server_info/mongodb/PswgDatabase.kt +++ b/src/main/java/com/projectswg/holocore/resources/support/data/server_info/mongodb/PswgDatabase.kt @@ -1,75 +1,47 @@ package com.projectswg.holocore.resources.support.data.server_info.mongodb -import com.mongodb.WriteConcern import com.mongodb.client.MongoClients import com.mongodb.client.MongoCollection -import com.mongodb.client.MongoDatabase -import me.joshlarson.jlcommon.log.Log +import com.projectswg.holocore.resources.support.data.server_info.database.* +import com.projectswg.holocore.resources.support.data.server_info.mariadb.PswgUserDatabaseMaria import org.bson.Document -import java.util.logging.Handler -import java.util.logging.Level -import java.util.logging.LogRecord -import java.util.logging.Logger -import kotlin.reflect.KProperty object PswgDatabase { - private val database = ObjectRef(null) - val config by DatabaseDelegate(database, "config") { PswgConfigDatabase(it?.withWriteConcern(WriteConcern.ACKNOWLEDGED)) } - val users by DatabaseDelegate(database, "users") { PswgUserDatabase(it?.withWriteConcern(WriteConcern.ACKNOWLEDGED)) } - val objects by DatabaseDelegate(database, "objects") { PswgObjectDatabase(it?.withWriteConcern(WriteConcern.JOURNALED)) } - val resources by DatabaseDelegate(database, "resources") { PswgResourceDatabase(it?.withWriteConcern(WriteConcern.ACKNOWLEDGED)) } + private var configImpl = PswgConfigDatabase.createDefault() + private var usersImpl = PswgUserDatabase.createDefault() + private var objectsImpl = PswgObjectDatabase.createDefault() + private var resourcesImpl = PswgResourceDatabase.createDefault() + + val config: PswgConfigDatabase + get() = configImpl + val users: PswgUserDatabase + get() = usersImpl + val objects: PswgObjectDatabase + get() = objectsImpl + val resources: PswgResourceDatabase + get() = resourcesImpl fun initialize(connectionString: String, databaseName: String) { - setupMongoLogging() val client = MongoClients.create(connectionString) val database = client.getDatabase(databaseName) + val databaseConfig = Database(database) - this.database.element = database + val config = PswgConfigDatabaseMongo(databaseConfig.config.mongo) + val users = initTable(databaseConfig.users, defaultCreator = {PswgUserDatabase.createDefault()}, mariaInitializer = ::PswgUserDatabaseMaria, mongoInitializer = ::PswgUserDatabaseMongo) + val objects = initTable(databaseConfig.objects, defaultCreator = {PswgObjectDatabase.createDefault()}, mongoInitializer = ::PswgObjectDatabaseMongo) + val resources = initTable(databaseConfig.resources, defaultCreator = {PswgResourceDatabase.createDefault()}, mongoInitializer = ::PswgResourceDatabaseMongo) + + this.configImpl = config + this.usersImpl = users + this.objectsImpl = objects + this.resourcesImpl = resources } - private fun setupMongoLogging() { - var mongoLogger: Logger? = Logger.getLogger("com.mongodb") - while (mongoLogger != null) { - for (handler in mongoLogger.handlers) { - mongoLogger.removeHandler(handler) - } - if (mongoLogger.parent != null) - mongoLogger = mongoLogger.parent - else - break - } - mongoLogger?.addHandler(object : Handler() { - override fun publish(record: LogRecord) { - when (record.level) { - Level.INFO -> Log.i("MongoDB: %s", record.message) - Level.WARNING -> Log.w("MongoDB: %s", record.message) - Level.SEVERE -> Log.e("MongoDB: %s", record.message) - else -> Log.t("MongoDB: %s", record.message) - } - } - - override fun flush() {} - override fun close() {} - }) + private fun initTable(table: DatabaseTable, defaultCreator: () -> T, mariaInitializer: (DatabaseTable) -> T = {defaultCreator()}, mongoInitializer: (MongoCollection) -> T = {defaultCreator()}): T { + if (table.isMariaDefined()) + return mariaInitializer(table) + return mongoInitializer(table.mongo) } - private class DatabaseDelegate(private val database: ObjectRef, private val collectionName: String, private var databaseSupplier: (MongoCollection?) -> T) { - - private var prevDatabase: MongoDatabase? = null - private var prevReturn: T = databaseSupplier(null) - - operator fun getValue(thisRef: Any?, property: KProperty<*>): T { - val database = this.database.element - if (prevDatabase != database) { - this.prevDatabase = database - this.prevReturn = databaseSupplier(database?.getCollection(collectionName)) - } - return prevReturn - } - - } - - private class ObjectRef(var element: T) - } diff --git a/src/main/java/com/projectswg/holocore/resources/support/data/server_info/mongodb/PswgObjectDatabase.kt b/src/main/java/com/projectswg/holocore/resources/support/data/server_info/mongodb/PswgObjectDatabaseMongo.kt similarity index 68% rename from src/main/java/com/projectswg/holocore/resources/support/data/server_info/mongodb/PswgObjectDatabase.kt rename to src/main/java/com/projectswg/holocore/resources/support/data/server_info/mongodb/PswgObjectDatabaseMongo.kt index 222a284ba..e2346bd6c 100644 --- a/src/main/java/com/projectswg/holocore/resources/support/data/server_info/mongodb/PswgObjectDatabase.kt +++ b/src/main/java/com/projectswg/holocore/resources/support/data/server_info/mongodb/PswgObjectDatabaseMongo.kt @@ -1,29 +1,29 @@ /*********************************************************************************** * Copyright (c) 2019 /// Project SWG /// www.projectswg.com * - * * + * * * ProjectSWG is the first NGE emulator for Star Wars Galaxies founded on * * July 7th, 2011 after SOE announced the official shutdown of Star Wars Galaxies. * * Our goal is to create an emulator which will provide a server for players to * * continue playing a game similar to the one they used to play. We are basing * * it on the final publish of the game prior to end-game events. * - * * + * * * This file is part of Holocore. * - * * + * * * --------------------------------------------------------------------------------* - * * + * * * Holocore is free software: you can redistribute it and/or modify * * it under the terms of the GNU Affero General Public License as * * published by the Free Software Foundation, either version 3 of the * * License, or (at your option) any later version. * - * * + * * * Holocore is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU Affero General Public License for more details. * - * * + * * * You should have received a copy of the GNU Affero General Public License * - * along with Holocore. If not, see //www.gnu.org/licenses/>. * - */ + * along with Holocore. If not, see . * + ***********************************************************************************/ package com.projectswg.holocore.resources.support.data.server_info.mongodb @@ -31,28 +31,26 @@ import com.mongodb.client.MongoCollection import com.mongodb.client.model.* import com.projectswg.common.data.encodables.mongo.MongoData import com.projectswg.holocore.resources.support.data.persistable.SWGObjectFactory +import com.projectswg.holocore.resources.support.data.server_info.database.PswgObjectDatabase import com.projectswg.holocore.resources.support.objects.swg.SWGObject import org.bson.Document -import java.util.* import java.util.regex.Pattern import java.util.stream.Collectors.toList -class PswgObjectDatabase(private val collection: MongoCollection?) { +class PswgObjectDatabaseMongo(private val collection: MongoCollection) : PswgObjectDatabase { - val objects: List - get() = collection?.find()?.map { MongoData(it) }?.into(ArrayList()) ?: ArrayList() + override val objects: List + get() = collection.find().map { MongoData(it) }.into(ArrayList()) ?: ArrayList() init { - collection?.createIndex(Indexes.ascending("id"), IndexOptions().unique(true)) + collection.createIndex(Indexes.ascending("id"), IndexOptions().unique(true)) } - fun addObject(obj: SWGObject) { - collection ?: return + override fun addObject(obj: SWGObject) { collection.replaceOne(Filters.eq("id", obj.objectId), SWGObjectFactory.save(obj, MongoData()).toDocument(), ReplaceOptions().upsert(true)) } - fun addObjects(objects: Collection) { - collection ?: return + override fun addObjects(objects: Collection) { if (objects.isEmpty()) return collection.bulkWrite(objects.stream() @@ -67,26 +65,22 @@ class PswgObjectDatabase(private val collection: MongoCollection?) { BulkWriteOptions().ordered(false)) } - fun removeObject(id: Long): Boolean { - collection ?: return true + override fun removeObject(id: Long): Boolean { return collection.deleteOne(Filters.eq("id", id)).deletedCount > 0 } - fun getCharacterCount(account: String): Int { - collection ?: return 0 + override fun getCharacterCount(account: String): Int { return collection.countDocuments(Filters.eq("account", account)).toInt() } - fun isCharacter(firstName: String): Boolean { - collection ?: return false + override fun isCharacter(firstName: String): Boolean { return collection.countDocuments(Filters.and( Filters.regex("template", "object/creature/player/shared_.+\\.iff"), Filters.regex("base3.objectName", Pattern.compile(Pattern.quote(firstName) + "( .+|$)", Pattern.CASE_INSENSITIVE)) )) > 0 } - fun clearObjects(): Long { - collection ?: return 0 + override fun clearObjects(): Long { return collection.deleteMany(Filters.exists("_id")).deletedCount } diff --git a/src/main/java/com/projectswg/holocore/resources/support/data/server_info/mongodb/PswgResourceDatabase.kt b/src/main/java/com/projectswg/holocore/resources/support/data/server_info/mongodb/PswgResourceDatabaseMongo.kt similarity index 66% rename from src/main/java/com/projectswg/holocore/resources/support/data/server_info/mongodb/PswgResourceDatabase.kt rename to src/main/java/com/projectswg/holocore/resources/support/data/server_info/mongodb/PswgResourceDatabaseMongo.kt index 878541f02..da8d23f7d 100644 --- a/src/main/java/com/projectswg/holocore/resources/support/data/server_info/mongodb/PswgResourceDatabase.kt +++ b/src/main/java/com/projectswg/holocore/resources/support/data/server_info/mongodb/PswgResourceDatabaseMongo.kt @@ -1,29 +1,29 @@ /*********************************************************************************** * Copyright (c) 2019 /// Project SWG /// www.projectswg.com * - * * + * * * ProjectSWG is the first NGE emulator for Star Wars Galaxies founded on * * July 7th, 2011 after SOE announced the official shutdown of Star Wars Galaxies. * * Our goal is to create an emulator which will provide a server for players to * * continue playing a game similar to the one they used to play. We are basing * * it on the final publish of the game prior to end-game events. * - * * + * * * This file is part of Holocore. * - * * + * * * --------------------------------------------------------------------------------* - * * + * * * Holocore is free software: you can redistribute it and/or modify * * it under the terms of the GNU Affero General Public License as * * published by the Free Software Foundation, either version 3 of the * * License, or (at your option) any later version. * - * * + * * * Holocore is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU Affero General Public License for more details. * - * * + * * * You should have received a copy of the GNU Affero General Public License * - * along with Holocore. If not, see //www.gnu.org/licenses/>. * - */ + * along with Holocore. If not, see . * + ***********************************************************************************/ package com.projectswg.holocore.resources.support.data.server_info.mongodb @@ -31,16 +31,16 @@ import com.mongodb.client.MongoCollection import com.mongodb.client.model.* import com.projectswg.common.data.encodables.mongo.MongoData import com.projectswg.holocore.resources.gameplay.crafting.resource.galactic.GalacticResource +import com.projectswg.holocore.resources.support.data.server_info.database.PswgResourceDatabase import org.bson.Document -import java.util.* import java.util.stream.Collectors.toList -class PswgResourceDatabase(private val collection: MongoCollection?) { +class PswgResourceDatabaseMongo(private val collection: MongoCollection) : PswgResourceDatabase { - var resources: List - get() = collection?.find()?.map { MongoData.create(it) { GalacticResource() } }?.into(ArrayList()) ?: ArrayList() + override var resources: List + get() = collection.find().map { MongoData.create(it) { GalacticResource() } }.into(ArrayList()) ?: ArrayList() set(value) { - collection?.bulkWrite(value.stream() + collection.bulkWrite(value.stream() .map { resource -> ReplaceOneModel( Filters.eq("id", resource.id), // match the resource id @@ -53,7 +53,7 @@ class PswgResourceDatabase(private val collection: MongoCollection?) { } init { - collection?.createIndex(Indexes.ascending("id"), IndexOptions().unique(true)) + collection.createIndex(Indexes.ascending("id"), IndexOptions().unique(true)) } } diff --git a/src/main/java/com/projectswg/holocore/resources/support/data/server_info/mongodb/PswgUserDatabaseMongo.kt b/src/main/java/com/projectswg/holocore/resources/support/data/server_info/mongodb/PswgUserDatabaseMongo.kt new file mode 100644 index 000000000..1d90f377f --- /dev/null +++ b/src/main/java/com/projectswg/holocore/resources/support/data/server_info/mongodb/PswgUserDatabaseMongo.kt @@ -0,0 +1,67 @@ +/*********************************************************************************** + * Copyright (c) 2019 /// Project SWG /// www.projectswg.com * + * * + * ProjectSWG is the first NGE emulator for Star Wars Galaxies founded on * + * July 7th, 2011 after SOE announced the official shutdown of Star Wars Galaxies. * + * Our goal is to create an emulator which will provide a server for players to * + * continue playing a game similar to the one they used to play. We are basing * + * it on the final publish of the game prior to end-game events. * + * * + * This file is part of Holocore. * + * * + * --------------------------------------------------------------------------------* + * * + * Holocore is free software: you can redistribute it and/or modify * + * it under the terms of the GNU Affero General Public License as * + * published by the Free Software Foundation, either version 3 of the * + * License, or (at your option) any later version. * + * * + * Holocore is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Affero General Public License for more details. * + * * + * You should have received a copy of the GNU Affero General Public License * + * along with Holocore. If not, see . * + ***********************************************************************************/ + +package com.projectswg.holocore.resources.support.data.server_info.mongodb + +import com.mongodb.client.MongoCollection +import com.mongodb.client.model.Filters +import com.mongodb.client.model.IndexOptions +import com.mongodb.client.model.Indexes +import com.projectswg.holocore.resources.support.data.server_info.database.PswgUserDatabase +import com.projectswg.holocore.resources.support.data.server_info.database.UserMetadata +import com.projectswg.holocore.resources.support.global.player.AccessLevel +import org.bson.Document + +class PswgUserDatabaseMongo(private val mongoCollection: MongoCollection) : PswgUserDatabase { + + init { + mongoCollection.createIndex(Indexes.ascending("username"), IndexOptions().unique(true)) + } + + override fun getUser(username: String): UserMetadata? { + return mongoCollection + .find(Filters.eq("username", username)) + .map { createUserMetadata(it) } + .first() + } + + private fun createUserMetadata(doc: Document): UserMetadata { + val accessLevel = when (doc.getString("accessLevel")) { + "warden" -> AccessLevel.WARDEN + "csr" -> AccessLevel.CSR + "qa" -> AccessLevel.QA + "dev" -> AccessLevel.DEV + else -> AccessLevel.PLAYER + } + return UserMetadata(accountId = doc.getObjectId("_id").toHexString(), + username = doc.getString("username"), + password = doc.getString("password"), + accessLevel = accessLevel, + isBanned = doc.getBoolean("banned")) + } + +} diff --git a/src/main/java/com/projectswg/holocore/resources/support/objects/swg/creature/CreatureObject.java b/src/main/java/com/projectswg/holocore/resources/support/objects/swg/creature/CreatureObject.java index 95bab925d..086254a4c 100644 --- a/src/main/java/com/projectswg/holocore/resources/support/objects/swg/creature/CreatureObject.java +++ b/src/main/java/com/projectswg/holocore/resources/support/objects/swg/creature/CreatureObject.java @@ -63,7 +63,8 @@ import java.util.concurrent.atomic.AtomicReference; import java.util.function.Predicate; import java.util.stream.Stream; -public class CreatureObject extends TangibleObject { +public class +CreatureObject extends TangibleObject { private transient long lastReserveOperation = 0; diff --git a/src/main/java/com/projectswg/holocore/services/support/global/zone/LoginService.java b/src/main/java/com/projectswg/holocore/services/support/global/zone/LoginService.java index c0679b97e..d67137fe4 100644 --- a/src/main/java/com/projectswg/holocore/services/support/global/zone/LoginService.java +++ b/src/main/java/com/projectswg/holocore/services/support/global/zone/LoginService.java @@ -48,10 +48,9 @@ import com.projectswg.holocore.intents.support.global.zone.creation.DeleteCharac import com.projectswg.holocore.intents.support.objects.swg.DestroyObjectIntent; import com.projectswg.holocore.intents.support.objects.swg.ObjectCreatedIntent; import com.projectswg.holocore.resources.support.data.server_info.StandardLog; +import com.projectswg.holocore.resources.support.data.server_info.database.UserMetadata; import com.projectswg.holocore.resources.support.data.server_info.mongodb.PswgDatabase; -import com.projectswg.holocore.resources.support.data.server_info.mongodb.UserMetadata; import com.projectswg.holocore.resources.support.global.network.DisconnectReason; -import com.projectswg.holocore.resources.support.global.player.AccessLevel; import com.projectswg.holocore.resources.support.global.player.Player; import com.projectswg.holocore.resources.support.global.player.Player.PlayerServer; import com.projectswg.holocore.resources.support.global.player.PlayerState; @@ -229,14 +228,7 @@ public class LoginService extends Service { } private void onSuccessfulLogin(UserMetadata user, Player player) { - switch(user.getAccessLevel()) { - default: - case "player": player.setAccessLevel(AccessLevel.PLAYER); break; - case "warden": player.setAccessLevel(AccessLevel.WARDEN); break; - case "csr": player.setAccessLevel(AccessLevel.CSR); break; - case "qa": player.setAccessLevel(AccessLevel.QA); break; - case "dev": player.setAccessLevel(AccessLevel.DEV); break; - } + player.setAccessLevel(user.getAccessLevel()); player.setAccountId(user.getUsername()); player.setPlayerState(PlayerState.LOGGED_IN); new LoginEventIntent(player.getNetworkId(), LoginEvent.LOGIN_SUCCESS).broadcast();