Add player repository
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
package club.mcscrims.speedhg.database
|
||||
|
||||
import club.mcscrims.core.database.mongodb.MongoConnection
|
||||
import club.mcscrims.core.database.mongodb.MongoRepository
|
||||
import com.mongodb.client.model.Filters
|
||||
import com.mongodb.client.model.Updates
|
||||
import org.bson.codecs.pojo.annotations.BsonId
|
||||
import org.bson.codecs.pojo.annotations.BsonProperty
|
||||
import org.bson.conversions.Bson
|
||||
import org.bson.types.ObjectId
|
||||
import org.bukkit.Bukkit
|
||||
import java.util.UUID
|
||||
|
||||
/**
|
||||
* MongoDB-Entity für einen Spieler
|
||||
*/
|
||||
data class KitPlayer(
|
||||
@BsonId
|
||||
val id: ObjectId = ObjectId(),
|
||||
|
||||
@BsonProperty
|
||||
val uuid: String,
|
||||
|
||||
@BsonProperty
|
||||
val isAlive: Boolean,
|
||||
|
||||
@BsonProperty
|
||||
val unlockedKits: List<String>,
|
||||
|
||||
@BsonProperty
|
||||
val server: String
|
||||
)
|
||||
|
||||
/**
|
||||
* MongoDB-Repository für Spieler-Daten
|
||||
*/
|
||||
class PlayerRepository(
|
||||
connection: MongoConnection
|
||||
): MongoRepository<KitPlayer>( connection, "kit_players", KitPlayer::class.java ) {
|
||||
|
||||
override fun getId(
|
||||
entity: KitPlayer
|
||||
): ObjectId
|
||||
{
|
||||
return entity.id
|
||||
}
|
||||
|
||||
override fun setId(
|
||||
entity: KitPlayer,
|
||||
id: ObjectId
|
||||
): KitPlayer
|
||||
{
|
||||
return entity.copy( id = id )
|
||||
}
|
||||
|
||||
override fun entityToUpdateDocument(
|
||||
entity: KitPlayer
|
||||
): Bson = Updates.combine(
|
||||
Updates.set("isAlive", entity.isAlive),
|
||||
Updates.set("unlockedKits", entity.unlockedKits),
|
||||
Updates.set("server", entity.server)
|
||||
)
|
||||
|
||||
/**
|
||||
* Findet einen Spieler anhand seiner UUID
|
||||
*/
|
||||
suspend fun findByUuid(
|
||||
uuid: UUID
|
||||
): KitPlayer?
|
||||
{
|
||||
val filter = Filters.eq("uuid", uuid.toString())
|
||||
return findFirst(filter)
|
||||
}
|
||||
|
||||
/**
|
||||
* Findet alle lebenden Spieler für einen Server
|
||||
*/
|
||||
suspend fun findAlivePlayers(
|
||||
server: String
|
||||
): List<KitPlayer>
|
||||
{
|
||||
val filter = Filters.and(
|
||||
Filters.eq("server", server),
|
||||
Filters.eq("isAlive", true)
|
||||
)
|
||||
return find(filter, Bukkit.getMaxPlayers())
|
||||
}
|
||||
|
||||
/**
|
||||
* Aktualisiert den Alive-Status eines Spielers
|
||||
*/
|
||||
suspend fun updateAliveStatus(
|
||||
uuid: UUID,
|
||||
isAlive: Boolean
|
||||
) {
|
||||
val filter = Filters.eq("uuid", uuid.toString())
|
||||
val update = Updates.set("isAlive", isAlive)
|
||||
connection.updateOne(collectionName, filter, update)
|
||||
}
|
||||
|
||||
/**
|
||||
* Aktualisiert den Server eines Spielers
|
||||
*/
|
||||
suspend fun updateServer(
|
||||
uuid: UUID,
|
||||
server: String
|
||||
) {
|
||||
val filter = Filters.eq("uuid", uuid.toString())
|
||||
val update = Updates.set("server", server)
|
||||
connection.updateOne(collectionName, filter, update)
|
||||
}
|
||||
|
||||
/**
|
||||
* Aktualisiert die freigeschalteten Kits eines Spielers
|
||||
*/
|
||||
suspend fun updateUnlockedKits(
|
||||
uuid: UUID,
|
||||
unlockedKits: List<String>
|
||||
) {
|
||||
val filter = Filters.eq("uuid", uuid.toString())
|
||||
val update = Updates.set("unlockedKits", unlockedKits)
|
||||
connection.updateOne(collectionName, filter, update)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user