Browse Source

Implements the getTopPlayer method to get all player sorted by there playtime

merge-requests/15/merge
Augusto Dwenger 2 years ago
parent
commit
0895514469
  1. 3
      build.gradle.kts
  2. 14
      src/main/kotlin/PlayerTime.kt
  3. 9
      src/main/kotlin/SimplePlayerTime.kt
  4. 22
      src/main/kotlin/db/FilePlayerTimeDB.kt
  5. 7
      src/main/kotlin/db/PlayerFinder.kt
  6. 5
      src/test/kotlin/FakePlayerTime.kt
  7. 36
      src/test/kotlin/db/FilePlayerTimeDBTest.kt
  8. 40
      src/test/kotlin/db/SimplePlayerTimeTest.kt

3
build.gradle.kts

@ -45,7 +45,8 @@ tasks {
test {
useJUnitPlatform()
testLogging {
events("passed", "skipped", "failed")
events("skipped", "failed")
// events("passed", "skipped", "failed")
}
}
dokka {

14
src/main/kotlin/PlayerTime.kt

@ -1,5 +1,6 @@
import java.time.Duration
import java.util.UUID
import models.Player
interface PlayerTime {
/**
@ -28,6 +29,19 @@ interface PlayerTime {
*/
fun timePlayed(uuid: UUID): Duration
/**
* Search for the players with the highest playtime
*
* @return List<Player>
*/
fun getTopPlayers(): List<Player>
/**
* Formats the duration to a simple String
*
* @param duration Duration
* @return String (DD:HH:MM)
*/
fun timeToString(duration: Duration): String {
val days: String =
if (duration.toDaysPart() < 10) """0${duration.toDaysPart()}""" else """${duration.toDaysPart()}"""

9
src/main/kotlin/SimplePlayerTime.kt

@ -63,4 +63,13 @@ class SimplePlayerTime(private val db: PlayerTimeDB) : PlayerTime {
}
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 }
}
}

22
src/main/kotlin/db/FilePlayerTimeDB.kt

@ -1,10 +1,12 @@
package db
import com.google.gson.Gson
import com.google.gson.JsonSyntaxException
import java.io.File
import java.time.LocalDateTime
import java.util.UUID
import models.Player
import mu.KotlinLogging
/**
* FileDB manages the file based PlayerTime DB and provides basic functionality to interact with it
@ -14,6 +16,8 @@ import models.Player
*/
class FilePlayerTimeDB(private val path: File = File("./plugins/PlayTime/")) : PlayerTimeDB {
val logger = KotlinLogging.logger {}
private val gson = Gson()
/**
@ -100,6 +104,24 @@ class FilePlayerTimeDB(private val path: File = File("./plugins/PlayTime/")) : P
throw PlayerNotFoundException("""The player with the name $name was not found""")
}
/**
* Should read all player and return theme
*
* @return List<Player>
*/
override fun findAll(): List<Player> {
val playerList = mutableListOf<Player>()
val fileList = path.listFiles() ?: return emptyList()
fileList.forEach { file: File ->
try {
playerList.add(gson.fromJson(file.readText(), Player::class.java))
} catch (e: JsonSyntaxException) {
logger.error { "The player with the ID ${{ file.name }} could not be read out of the files!" }
}
}
return playerList.toList()
}
/**
* Writs a player into an existing file
* To create a new player file @see createPlayer(player: Player)

7
src/main/kotlin/db/PlayerFinder.kt

@ -45,4 +45,11 @@ interface PlayerFinder {
* @throws PlayerNotFoundException
*/
fun findByName(name: String): Player
/**
* Should read all player and return theme
*
* @return List<Player>
*/
fun findAll(): List<Player>
}

5
src/test/kotlin/FakePlayerTime.kt

@ -1,6 +1,7 @@
import java.time.Duration
import java.util.UUID
import models.Player
class FakePlayerTime : PlayerTime {
@ -15,4 +16,8 @@ class FakePlayerTime : PlayerTime {
override fun timePlayed(uuid: UUID): Duration {
TODO("not implemented") // To change body of created functions use File | Settings | File Templates.
}
override fun getTopPlayers(): List<Player> {
TODO("not implemented") // To change body of created functions use File | Settings | File Templates.
}
}

36
src/test/kotlin/db/FilePlayerTimeDBTest.kt

@ -156,4 +156,40 @@ class FilePlayerTimeDBTest {
assertFalse(fileDB.existsByName("foo"))
}
}
@Test
fun `find all players`() {
val playerName = "playerName"
val fileDB = FilePlayerTimeDB(dbDir)
val uuid = UUID.randomUUID()
val uuid2 = UUID.randomUUID()
val player = Player(uuid, playerName)
val player2 = Player(uuid2, playerName)
File(dbDir, uuid.toString()).writeText(Gson().toJson(player))
File(dbDir, uuid2.toString()).writeText(Gson().toJson(player2))
val playerList = fileDB.findAll()
assertFalse(playerList.isEmpty())
assertEquals(2, playerList.size)
assertTrue(playerList.contains(player))
assertTrue(playerList.contains(player2))
}
@Test
fun `find all players if there is no one`() {
val fileDB = FilePlayerTimeDB(dbDir)
assertTrue(fileDB.findAll().isEmpty())
}
@Test
fun `error by deserialization`() {
val fileDB = FilePlayerTimeDB(dbDir)
val uuid = UUID.randomUUID()
File(dbDir, uuid.toString()).writeText("Foo")
assertTrue(fileDB.findAll().isEmpty())
}
}

40
src/test/kotlin/db/SimplePlayerTimeTest.kt

@ -11,13 +11,13 @@ import java.time.LocalDateTime
import java.util.UUID
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
import kotlin.test.Test
import models.Player
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertNotEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
class SimplePlayerTimeTest {
@ -184,4 +184,42 @@ class SimplePlayerTimeTest {
println(playTime.toMinutes())
}
}
@Nested
inner class TopPlayer {
@Test
fun `get list with players`() {
val player1 = Player(UUID.randomUUID(), "player1", playTime = Duration.ZERO)
val player2 = Player(UUID.randomUUID(), "player2", playTime = Duration.ofMinutes(10))
val mockPlayerTimeDB = mockk<PlayerTimeDB>()
every { mockPlayerTimeDB.findAll() } returns listOf(player1, player2)
val playerService = SimplePlayerTime(mockPlayerTimeDB)
val topPlayer = playerService.getTopPlayers()
assertFalse(topPlayer.isEmpty())
assertEquals(2, topPlayer.size)
}
@Test
fun `get list with players sorted by most time`() {
val player1 = Player(UUID.randomUUID(), "player1", playTime = Duration.ZERO)
val player2 = Player(UUID.randomUUID(), "player2", playTime = Duration.ofMinutes(10))
val player3 = Player(UUID.randomUUID(), "player3", playTime = Duration.ofHours(2))
val mockPlayerTimeDB = mockk<PlayerTimeDB>()
every { mockPlayerTimeDB.findAll() } returns listOf(player1, player2, player3)
val playerService = SimplePlayerTime(mockPlayerTimeDB)
val topPlayer = playerService.getTopPlayers()
assertEquals(player3, topPlayer[0])
assertEquals(player2, topPlayer[1])
assertEquals(player1, topPlayer[2])
}
}
}

Loading…
Cancel
Save