First commit
This commit is contained in:
@@ -0,0 +1,200 @@
|
||||
package club.mcscrims.speedhg.database
|
||||
|
||||
import club.mcscrims.core.database.mongodb.MongoConnection
|
||||
import club.mcscrims.core.database.mongodb.MongoRepository
|
||||
import com.mongodb.client.model.Aggregates
|
||||
import com.mongodb.client.model.Filters
|
||||
import com.mongodb.client.model.Sorts
|
||||
import com.mongodb.client.model.Updates
|
||||
import kotlinx.coroutines.flow.toList
|
||||
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 java.util.UUID
|
||||
|
||||
/**
|
||||
* MongoDB-Entity für die Stats von einem Spieler
|
||||
*/
|
||||
data class PlayerStats(
|
||||
@BsonId
|
||||
val id: ObjectId? = null,
|
||||
|
||||
@BsonProperty("uuid")
|
||||
val uuid: String,
|
||||
|
||||
@BsonProperty("kills")
|
||||
val kills: Int,
|
||||
|
||||
@BsonProperty("deaths")
|
||||
val deaths: Int,
|
||||
|
||||
@BsonProperty("wins")
|
||||
val wins: Int,
|
||||
|
||||
@BsonProperty("gamesPlayed")
|
||||
val gamesPlayed: Int,
|
||||
|
||||
@BsonProperty("unathleticIndex")
|
||||
val unathleticIndex: Double,
|
||||
|
||||
@BsonProperty("ironFarmed")
|
||||
val ironFarmed: Double
|
||||
)
|
||||
|
||||
/**
|
||||
* MongoDB-Repository für Stats-Daten
|
||||
*/
|
||||
class StatsRepository(
|
||||
connection: MongoConnection
|
||||
): MongoRepository<PlayerStats>( connection, "player_stats", PlayerStats::class.java ) {
|
||||
|
||||
override fun getId(
|
||||
entity: PlayerStats
|
||||
): ObjectId?
|
||||
{
|
||||
return entity.id
|
||||
}
|
||||
|
||||
override fun setId(
|
||||
entity: PlayerStats,
|
||||
id: ObjectId
|
||||
): PlayerStats
|
||||
{
|
||||
return entity.copy( id = id )
|
||||
}
|
||||
|
||||
override fun entityToUpdateDocument(
|
||||
entity: PlayerStats
|
||||
): Bson = Updates.combine(
|
||||
Updates.set( "kills", entity.kills ),
|
||||
Updates.set( "deaths", entity.deaths ),
|
||||
Updates.set( "wins", entity.wins ),
|
||||
Updates.set( "gamesPlayed", entity.gamesPlayed ),
|
||||
Updates.set( "unathleticIndex", entity.unathleticIndex ),
|
||||
Updates.set( "ironFarmed", entity.ironFarmed )
|
||||
)
|
||||
|
||||
/**
|
||||
* Findet einen Spieler anhand seiner UUID
|
||||
*/
|
||||
suspend fun findByUuid(
|
||||
uuid: UUID
|
||||
): PlayerStats?
|
||||
{
|
||||
val filter = Filters.eq( "uuid", uuid.toString() )
|
||||
return findFirst( filter )
|
||||
}
|
||||
|
||||
/**
|
||||
* Findet Top-Spieler nach Kills
|
||||
*/
|
||||
suspend fun findTopPlayersByKills(
|
||||
limit: Int = 10
|
||||
): List<PlayerStats>
|
||||
{
|
||||
return connection.getCollection( collectionName ).aggregate(listOf(
|
||||
Aggregates.sort(Sorts.descending( "kills" )),
|
||||
Aggregates.limit( limit )
|
||||
), PlayerStats::class.java ).toList()
|
||||
}
|
||||
|
||||
/**
|
||||
* Findet Top-Spieler nach Deaths
|
||||
*/
|
||||
suspend fun findTopPlayersByDeaths(
|
||||
limit: Int = 10
|
||||
): List<PlayerStats>
|
||||
{
|
||||
return connection.getCollection( collectionName ).aggregate(listOf(
|
||||
Aggregates.sort(Sorts.ascending( "deaths" )),
|
||||
Aggregates.limit( limit )
|
||||
), PlayerStats::class.java ).toList()
|
||||
}
|
||||
|
||||
/**
|
||||
* Findet Top-Spieler nach Wins
|
||||
*/
|
||||
suspend fun findTopPlayersByWins(
|
||||
limit: Int = 10
|
||||
): List<PlayerStats>
|
||||
{
|
||||
return connection.getCollection( collectionName ).aggregate(listOf(
|
||||
Aggregates.sort(Sorts.descending( "wins" )),
|
||||
Aggregates.limit( limit )
|
||||
), PlayerStats::class.java ).toList()
|
||||
}
|
||||
|
||||
/**
|
||||
* Fügt Kills zu einem Spieler hinzu
|
||||
*/
|
||||
suspend fun addKills(
|
||||
uuid: UUID,
|
||||
kills: Int
|
||||
) {
|
||||
val filter = Filters.eq( "uuid", uuid.toString() )
|
||||
val update = Updates.inc( "kills", kills )
|
||||
connection.updateOne( collectionName, filter, update )
|
||||
}
|
||||
|
||||
/**
|
||||
* Fügt Deaths zu einem Spieler hinzu
|
||||
*/
|
||||
suspend fun addDeaths(
|
||||
uuid: UUID,
|
||||
deaths: Int
|
||||
) {
|
||||
val filter = Filters.eq( "uuid", uuid.toString() )
|
||||
val update = Updates.inc( "deaths", deaths )
|
||||
connection.updateOne( collectionName, filter, update )
|
||||
}
|
||||
|
||||
/**
|
||||
* Fügt Wins zu einem Spieler hinzu
|
||||
*/
|
||||
suspend fun addWins(
|
||||
uuid: UUID,
|
||||
wins: Int
|
||||
) {
|
||||
val filter = Filters.eq( "uuid", uuid.toString() )
|
||||
val update = Updates.inc( "wins", wins )
|
||||
connection.updateOne( collectionName, filter, update )
|
||||
}
|
||||
|
||||
/**
|
||||
* Fügt Spiele zu einem Spieler hinzu
|
||||
*/
|
||||
suspend fun addGames(
|
||||
uuid: UUID,
|
||||
gamesPlayed: Int
|
||||
) {
|
||||
val filter = Filters.eq( "uuid", uuid.toString() )
|
||||
val update = Updates.inc( "gamesPlayed", gamesPlayed )
|
||||
connection.updateOne( collectionName, filter, update )
|
||||
}
|
||||
|
||||
/**
|
||||
* Fügt Unathletic-Index zu einem Spieler hinzu
|
||||
*/
|
||||
suspend fun addUnathleticIndex(
|
||||
uuid: UUID,
|
||||
unathleticIndex: Double
|
||||
) {
|
||||
val filter = Filters.eq( "uuid", uuid.toString() )
|
||||
val update = Updates.inc( "unathleticIndex", unathleticIndex )
|
||||
connection.updateOne( collectionName, filter, update )
|
||||
}
|
||||
|
||||
/**
|
||||
* Fügt Eisen zu einem Spieler hinzu
|
||||
*/
|
||||
suspend fun addIronFarmed(
|
||||
uuid: UUID,
|
||||
ironFarmed: Double
|
||||
) {
|
||||
val filter = Filters.eq( "uuid", uuid.toString() )
|
||||
val update = Updates.inc( "ironFarmed", ironFarmed )
|
||||
connection.updateOne( collectionName, filter, update )
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user