Skip to content

Commit

Permalink
fix: abc
Browse files Browse the repository at this point in the history
  • Loading branch information
w305jack committed Nov 5, 2023
1 parent 00a2859 commit 58f7633
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 129 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package tw.waterballsa.utopia.utopiagamification

import net.dv8tion.jda.api.entities.Guild
import net.dv8tion.jda.api.interactions.commands.Command
import net.dv8tion.jda.api.interactions.commands.OptionType
import net.dv8tion.jda.api.interactions.commands.OptionType.STRING
import net.dv8tion.jda.api.interactions.commands.build.CommandData
import net.dv8tion.jda.api.interactions.commands.build.Commands
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData
Expand Down Expand Up @@ -34,7 +34,7 @@ class RegisterGamificationCommand(
// 排行榜
SubcommandData(LEADERBOARD_COMMAND_NAME, "leaderboard")
.addOptionalOption(
OptionType.STRING,
STRING,
OPTION_COMMAND_NAME,
LEADERBOARD_OPTION_MY_RANK,
Command.Choice(LEADERBOARD_OPTION_MY_RANK, LEADERBOARD_OPTION_MY_RANK)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package tw.waterballsa.utopia.utopiagamification.leaderboard

import dev.minn.jda.ktx.messages.Embed
import mu.KotlinLogging
import net.dv8tion.jda.api.entities.Guild
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent
import net.dv8tion.jda.api.interactions.components.buttons.Button
import org.springframework.stereotype.Component
import tw.waterballsa.utopia.gamification.leaderboard.domain.LeaderBoardItem
import tw.waterballsa.utopia.gamification.repositories.LeaderBoardRepository
import tw.waterballsa.utopia.utopiagamification.quest.listeners.UtopiaGamificationListener
import tw.waterballsa.utopia.utopiagamification.repositories.PlayerRepository
import tw.waterballsa.utopia.jda.UtopiaListener
import tw.waterballsa.utopia.utopiagamification.leaderboard.repository.LeaderBoardRepository
import tw.waterballsa.utopia.utopiagamification.repositories.query.Page
import tw.waterballsa.utopia.utopiagamification.repositories.query.PageRequest
import tw.waterballsa.utopia.utopiagamification.repositories.query.Pageable
Expand All @@ -21,14 +18,11 @@ private const val LEADERBOARD_NEXT_BUTTON = "utopia-leaderboard-next"
private const val LEADERBOARD_OPTION = "options"
private const val LEADERBOARD_SUBCOMMAND_NAME = "leaderboard"
private const val LEADERBOARD_MY_RANK = "my-rank"
private val log = KotlinLogging.logger {}

@Component
class LeaderBoardListener(
guild: Guild,
playerRepository: PlayerRepository,
private val leaderBoardRepository: LeaderBoardRepository,
) : UtopiaGamificationListener(guild, playerRepository) {
) : UtopiaListener() {

override fun onSlashCommandInteraction(event: SlashCommandInteractionEvent) {

Expand All @@ -37,21 +31,21 @@ class LeaderBoardListener(
return
}

//1. 使用 Discord Embedded Message:
// - 印出多列:`<rank> <@userId> Lv.<等級> Exp: <經驗值> $<賞金>` ,每一列代表一個排名。
//2. Discord Embedded Message 下方有兩個按鈕,”Previous Page” 和 “Next Page”。
val isLeaderboardQuery = options.isEmpty()
if (isLeaderboardQuery) {
//1. 使用 Discord Embedded Message:
// - 印出多列:`<rank> <@userId> Lv.<等級> Exp: <經驗值> $<賞金>` ,每一列代表一個排名。
//2. Discord Embedded Message 下方有兩個按鈕,”Previous Page” 和 “Next Page”。
queryLeaderboard()
}

// 假設是 leaderboard my-rank 指令的話,
// 會去 query 指定的 player
// 然後 output「妳的排名為第 N 名」到 discord channel
val leaderboardOption = getOption(LEADERBOARD_OPTION)?.asString ?: return

val isSelfRankQuery = leaderboardOption == LEADERBOARD_MY_RANK
if (isSelfRankQuery) {
// 假設是 leaderboard my-rank 指令的話,
// 會去 query 指定的 player
// 然後 output「妳的排名為第 N 名」到 discord channel
querySelfRank()
}
}
Expand Down Expand Up @@ -79,7 +73,9 @@ class LeaderBoardListener(

override fun onButtonInteraction(event: ButtonInteractionEvent) {
with(event) {
if (!button.isLeaderBoardButton()) return
if (!button.isLeaderBoardButton()) {
return
}

deferEdit().queue()

Expand Down Expand Up @@ -145,13 +141,17 @@ class LeaderBoardListener(
}
if (page.hasNext()) {
buttons.add(
Button.success(makeButtonId(LEADERBOARD_PREVIOUS_BUTTON, page.nextPageable()), "下一頁")
.asEnabled()
Button.success(
makeButtonId(LEADERBOARD_PREVIOUS_BUTTON, page.nextPageable()),
"下一頁"
).asEnabled()
)
} else {
buttons.add(
Button.success(makeButtonId(LEADERBOARD_PREVIOUS_BUTTON, page.getPageable()), "下一頁")
.asDisabled()
Button.success(
makeButtonId(LEADERBOARD_PREVIOUS_BUTTON, page.getPageable()),
"下一頁"
).asDisabled()
)
}
return buttons
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tw.waterballsa.utopia.gamification.repositories
package tw.waterballsa.utopia.utopiagamification.leaderboard.repository

import tw.waterballsa.utopia.gamification.leaderboard.domain.LeaderBoardItem
import tw.waterballsa.utopia.utopiagamification.repositories.PageableRepository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,28 @@ package tw.waterballsa.utopia.gamification.repositories.mongodb.repositoryimpl

import org.springframework.stereotype.Component
import tw.waterballsa.utopia.gamification.leaderboard.domain.LeaderBoardItem
import tw.waterballsa.utopia.gamification.repositories.LeaderBoardRepository
import tw.waterballsa.utopia.utopiagamification.leaderboard.repository.LeaderBoardRepository
import tw.waterballsa.utopia.utopiagamification.quest.domain.Player
import tw.waterballsa.utopia.utopiagamification.repositories.PlayerRepository
import tw.waterballsa.utopia.utopiagamification.repositories.page
import tw.waterballsa.utopia.utopiagamification.repositories.query.Page
import tw.waterballsa.utopia.utopiagamification.repositories.query.Pageable

@Component
class MongodbLeaderBoardRepository(
private val playerRepository: PlayerRepository
) : LeaderBoardRepository {

override fun findAll(pageable: Pageable): Page<LeaderBoardItem> = playerRepository.findAllPlayers()
override fun findAll(pageable: Pageable): Page<LeaderBoardItem> = playerRepository.findAll()
.rank()
.page(pageable)

override fun queryPlayerRank(playerId: String): LeaderBoardItem? = playerRepository.findAllPlayers()
override fun queryPlayerRank(playerId: String): LeaderBoardItem? = playerRepository.findAll()
.rank()
.find { it.playerId == playerId }

}

@Component
class InMemoryLeaderBoardRepository : LeaderBoardRepository {

private val players = listOf(
Player("1", "Jack", 5600u),
Player("2", "張無忌", 4500u),
Player("3", "Mily", 2912u),
Player("4", "咪五", 2200u),
Player("5", "M萬", 2000u),
Player("6", "M兔", 1888u),
Player("7", "", 1870u),
Player("8", "咪四", 1200u),
Player("9", "Wally", 1100u),
Player("10", "infinite", 1000u),
Player("11", "阿瓜", 675u),
Player("12", "咪六", 675u),
Player("369063122033573910", "Anri", 900u),
Player("245902943679807490", "Jack", 900u)
)

override fun findAll(pageable: Pageable): Page<LeaderBoardItem> = players.rank().page(pageable)

override fun queryPlayerRank(playerId: String): LeaderBoardItem? =
players.rank().find { it.playerId == playerId }

}

private fun Collection<Player>.rank(): List<LeaderBoardItem> =
sortedWith(rankOrder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import tw.waterballsa.utopia.utopiagamification.quest.domain.Player
interface PlayerRepository {
fun findPlayerById(id: String): Player?
fun savePlayer(player: Player): Player
fun findAllPlayers(): List<Player>
fun findAll(): List<Player>
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class MongodbPlayerRepository(

override fun savePlayer(player: Player): Player = playerRepository.save(player.toDocument()).toDomain()

override fun findAllPlayers(): List<Player> = playerRepository.findAll().map { it.toDomain() }
override fun findAll(): List<Player> = playerRepository.findAll().map { it.toDomain() }

private fun PlayerDocument.toDomain(): Player = Player(
id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import tw.waterballsa.utopia.utopiagamification.repositories.query.Pageable.Comp
import kotlin.math.ceil
import kotlin.reflect.safeCast

class PageImpl<T> constructor(private val content: List<T>, private val pageable: Pageable, private var total: Long): Page<T> {
class PageImpl<T> constructor(
private val content: List<T>,
private val pageable: Pageable,
private var total: Long
): Page<T> {

constructor(content: List<T>?) : this(content?: emptyList(), unpaged(), content?.size?.toLong()?: 0)

// constructor(content: List<T>?, pageable: Pageable) : this(content?: emptyList(), pageable, content?.size?.toLong()?: 0)

init {
if (content.isNotEmpty()) {
val offset = pageable.getOffset()
Expand Down Expand Up @@ -67,8 +69,6 @@ class PageImpl<T> constructor(private val content: List<T>, private val pageable

override fun toString(): String {
val contentType = "UNKNOWN"
val content = getContent()
val type = content.firstOrNull().let { Any::class.safeCast(it)} ?: throw IllegalArgumentException()
return "Page ${getNumber() + 1} of ${getTotalPages()} containing $contentType instances"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,14 @@ abstract class AbstractPageRequest(private val page: Int, private val size: Int)
return result
}

override fun equals(obj: Any?): Boolean {
if (this === obj) {
override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}
val other = AbstractPageRequest::class.safeCast(obj)?: return false
if (other?.javaClass != this.javaClass) {
return false
}
val other = AbstractPageRequest::class.safeCast(other)?: return false
return this.page == other.page && this.size == other.size
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@ import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Primary
import tw.waterballsa.utopia.gamification.leaderboard.domain.LeaderBoardItem
import tw.waterballsa.utopia.gamification.repositories.LeaderBoardRepository
import tw.waterballsa.utopia.gamification.repositories.mongodb.repositoryimpl.MongodbLeaderBoardRepository
import tw.waterballsa.utopia.utopiagamification.leaderboard.repository.LeaderBoardRepository
import tw.waterballsa.utopia.utopiagamification.quest.domain.Player
import tw.waterballsa.utopia.utopiagamification.repositories.PlayerRepository
import tw.waterballsa.utopia.utopiagamification.repositories.query.PageRequest
Expand All @@ -28,9 +24,10 @@ class LeaderBoardIntegrationTest @Autowired constructor(
"""
test player rank query
Given:
- setup 3 Players data, Jack is test target
| Name | Lv | Exp | Bounty |
| ----- | -- | ---- | ------ |
| self | 6 | 1870 | 0 |
| Jack | 6 | 1870 | 0 |
| Wally | 4 | 1100 | 0 |
| Mily | 11 | 2912 | 0 |
When:
Expand All @@ -40,13 +37,15 @@ class LeaderBoardIntegrationTest @Autowired constructor(
"""
)
fun `test player rank query`() {
// when
// Given
val jack = playerRepository.savePlayer(Player(id = "1", name = "Jack", level = 6u, exp = 1870u))
playerRepository.savePlayer(Player(id = "2", name = "Wally", level = 4u, exp = 1100u))
playerRepository.savePlayer(Player(id = "3", name = "Mily", level = 11u, exp = 2912u))

// then
// When
val jackLeaderBoardItem = leaderBoardRepository.queryPlayerRank(jack.id)

// Then
assertThat(jackLeaderBoardItem).isNotNull
assertThat(jackLeaderBoardItem!!.name).isEqualTo(jack.name)
assertThat(jackLeaderBoardItem.exp).isEqualTo(jack.exp)
Expand All @@ -59,6 +58,7 @@ class LeaderBoardIntegrationTest @Autowired constructor(
"""
test leaderboard query
Given:
- setup 12 players data
| Name | Lv | Exp | Bounty |
| ----- | -- | ---- | ------ |
| Jack | 11 | 5600 | 0 |
Expand All @@ -80,9 +80,11 @@ class LeaderBoardIntegrationTest @Autowired constructor(
"""
)
fun `test leaderboard query`() {
// Given
prepareLeaderBoard()

val firstPage = PageRequest.of(0, 10)

// When & Then
assertLeaderBoardItems(firstPage,
LeaderBoardItem("1", "Jack", 5600u, 11u, 0u, 1),
LeaderBoardItem("2", "張無忌", 4500u, 10u, 0u, 2),
Expand All @@ -96,12 +98,14 @@ class LeaderBoardIntegrationTest @Autowired constructor(
LeaderBoardItem("10", "fin", 1000u, 5u, 0u, 10)
)

// When & Then
val secondPage = firstPage.next()
assertLeaderBoardItems(secondPage,
LeaderBoardItem("11", "阿瓜", 675u, 4u, 0u, 11),
LeaderBoardItem("12", "咪六", 675u, 4u, 0u, 12)
)

// When & Then
val thirdPage = secondPage.next()
assertLeaderBoardItems(thirdPage)
}
Expand Down Expand Up @@ -130,13 +134,3 @@ class LeaderBoardIntegrationTest @Autowired constructor(
assertThat(actualLeaderBoardItems).isEqualTo(expectedLeaderBoardItems.toList())
}
}

@Configuration
open class MongoConfig {

@Bean
@Primary
open fun mongodbLeaderBoardRepository(playerRepository: PlayerRepository): LeaderBoardRepository =
MongodbLeaderBoardRepository(playerRepository)

}
Loading

0 comments on commit 58f7633

Please sign in to comment.