It's a plugin for a Spigot Minecraft server to log the time players spend on the server and returns it to them.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

75 lines
2.3 KiB

import db.PlayerTimeDB
import java.time.Duration
import java.time.LocalDateTime
import java.util.UUID
import models.Player
/**
* A service to hold the all player that are currently playing
* It should only be used as Singleton @see Kotlin objects
*
* @property db the DB implementation which should be used to persist the state
*/
class SimplePlayerTime(private val db: PlayerTimeDB) : PlayerTime {
/**
* Adds the player to the playerMap.
* It creates an new player if it's no persisted and adds/refreshes the join time
*
* @param uuid the actual uuid of a player
* @param playerName for updating or to create a new player
*/
override fun playerJoin(uuid: UUID, playerName: String) {
val player: Player = if (db.existsById(uuid)) {
val dbPlayer = db.findById(uuid)
dbPlayer.joinTime = LocalDateTime.now()
dbPlayer.playerName = playerName
dbPlayer
} else {
val player = Player(uuid, playerName)
db.create(player)
player
}
db.save(player)
}
/**
* Updates the playtime and saves it
*
* @param uuid UUID
*/
override fun updatePlayTime(uuid: UUID) {
val player = db.findById(uuid)
val playTime = timePlayed(uuid)
player.playTime = playTime
db.save(player)
}
/**
* Returns the playtime including the old time
* if the stats got save it should use the save time instead of the join time
*
* @param uuid player uuid
*
* @return the total playtime
*/
override fun timePlayed(uuid: UUID): Duration {
val player = db.findById(uuid)
val playTime: Duration =
if ((player.lastSave != null) && Duration.between(player.lastSave, player.joinTime).isNegative) {
Duration.between(player.lastSave, LocalDateTime.now())
} else {
Duration.between(player.joinTime, LocalDateTime.now())
}
return Duration.from(player.playTime).plus(playTime)
}
/**
* Search for the players with the highest playtime
*
* @return List<Player> in a descending order by playtime
*/
override fun getTopPlayers(): List<Player> {
return db.findAll().sortedByDescending { player -> player.playTime }
}
}