Skip to content

Commit

Permalink
Abstract reset method to work on MySQL too
Browse files Browse the repository at this point in the history
  • Loading branch information
darbyjack committed Sep 5, 2021
1 parent 524657c commit cff96c6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 32 deletions.
22 changes: 12 additions & 10 deletions src/main/kotlin/me/clip/voteparty/data/base/DatabaseVotePlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,27 @@ import java.util.UUID

internal interface DatabaseVotePlayer : Addon, State
{

override fun load()

override fun kill()


fun load(uuid: UUID): User?

fun save(data: User)


fun load(uuid: Collection<UUID>): Map<UUID, User?>
{
return uuid.associateWith(::load)
}

fun save(data: Collection<User>)
{
data.forEach(::save)
}

}

fun reset(data: User)

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ import java.util.logging.Level

internal class DatabaseVotePlayerGson(override val plugin: VotePartyPlugin) : DatabaseVotePlayer
{

private lateinit var gson: Gson


override fun load()
{
val builder = GsonBuilder().disableHtmlEscaping().enableComplexMapKeySerialization().setPrettyPrinting().serializeNulls()
gson = builder.create()

plugin.dataFolder.resolve("players").mkdirs()
}

override fun kill()
{

}


override fun load(uuid: UUID): User?
{
return try
Expand All @@ -40,7 +40,7 @@ internal class DatabaseVotePlayerGson(override val plugin: VotePartyPlugin) : Da
null
}
}

override fun save(data: User)
{
try
Expand All @@ -52,16 +52,21 @@ internal class DatabaseVotePlayerGson(override val plugin: VotePartyPlugin) : Da
logger.log(Level.SEVERE, "failed to save player:${data.uuid}", ex)
}
}


override fun reset(data: User)
{
data.reset()
}

override fun load(uuid: Collection<UUID>): Map<UUID, User?>
{
if (uuid.isNotEmpty())
{
return super.load(uuid)
}

val files = plugin.dataFolder.resolve("players").listFiles() ?: return emptyMap()

return files.mapNotNull()
{
try
Expand All @@ -74,5 +79,5 @@ internal class DatabaseVotePlayerGson(override val plugin: VotePartyPlugin) : Da
}
}.associateWith(::load)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ import java.sql.SQLException
import java.util.UUID
import java.util.logging.Level

internal class DatabaseVotePlayerMySQL(override val plugin: VotePartyPlugin) : DatabaseVotePlayer {
internal class DatabaseVotePlayerMySQL(override val plugin: VotePartyPlugin) : DatabaseVotePlayer
{

private lateinit var database: Database

override fun load() {
override fun load()
{
val sql = party.conf().getProperty(StorageSettings.SQL)

val options = DatabaseOptions.builder()
Expand All @@ -30,7 +32,8 @@ internal class DatabaseVotePlayerMySQL(override val plugin: VotePartyPlugin) : D

DB.setGlobalDatabase(database)

try {
try
{
database.executeUpdate(
"CREATE TABLE IF NOT EXISTS `voteparty_players` (" +
" `uuid` varchar(255) NOT NULL," +
Expand All @@ -48,7 +51,9 @@ internal class DatabaseVotePlayerMySQL(override val plugin: VotePartyPlugin) : D
") ENGINE=InnoDB DEFAULT CHARSET=utf8;"
)

} catch (ex: SQLException) {
}
catch (ex: SQLException)
{
throw RuntimeException("Error while creating database table:", ex)
}
}
Expand All @@ -58,8 +63,10 @@ internal class DatabaseVotePlayerMySQL(override val plugin: VotePartyPlugin) : D
DB.close()
}

override fun load(uuid: UUID): User? {
return try {
override fun load(uuid: UUID): User?
{
return try
{
val row = database.getFirstRow("SELECT name, claimable FROM voteparty_players WHERE uuid = ?", uuid.toString())
val user = User(uuid, row.getString("name"), mutableListOf(), row.getInt("claimable"))

Expand All @@ -76,7 +83,8 @@ internal class DatabaseVotePlayerMySQL(override val plugin: VotePartyPlugin) : D
}
}

override fun save(data: User) {
override fun save(data: User)
{
database.executeInsert("INSERT INTO voteparty_players (uuid, name, claimable) VALUES(?, ?, ?) ON DUPLICATE " +
"KEY UPDATE name = ?, claimable = ?", data.uuid.toString(), data.name, data.claimable, data.name, data.claimable)

Expand All @@ -85,6 +93,11 @@ internal class DatabaseVotePlayerMySQL(override val plugin: VotePartyPlugin) : D
}
}

override fun reset(data: User)
{
database.executeUpdate("DELETE FROM voteparty_votes WHERE UUID = ?", data.uuid.toString())
}

override fun load(uuid: Collection<UUID>): Map<UUID, User?>
{
if (uuid.isNotEmpty())
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/me/clip/voteparty/user/UsersHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class UsersHandler(override val plugin: VotePartyPlugin) : Addon, State, Listene

fun reset(player: OfflinePlayer)
{
get(player).reset()
database.reset(get(player))
}

fun saveAll()
Expand Down

0 comments on commit cff96c6

Please sign in to comment.