Skip to content

Commit

Permalink
refactor assignPlayerQuestUsecase and add test (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
wow890209 authored Nov 15, 2023
1 parent 515607d commit 0643327
Show file tree
Hide file tree
Showing 24 changed files with 307 additions and 157 deletions.
5 changes: 0 additions & 5 deletions utopia-gamification/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,5 @@
<artifactId>utopia-test-kit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>tw.waterballsa.utopia</groupId>
<artifactId>utopia-test-kit</artifactId>
<version>${revision}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ abstract class Action(
abstract class Criteria {

abstract fun meet(action: Action): Boolean

open val link: String = ""
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Quest(
val periodType: PeriodType = PeriodType.NONE,
val criteria: Action.Criteria,
val reward: Reward,
val link: String = "",
val nextQuestId: Int? = null,
val postMessage: String = completeMessage
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,4 @@ class MessageReactionCriteria(
action.messageId == messageId && action.emoji == emoji

override fun toString(): String = "點選 $emoji 表情。"

override val link: String
get() = channelIdRule.toString()
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ class MessageSentCriteria(
${numberOfVoiceChannelMembersRule.toString("")}
""".trimIndent()

override val link: String
get() = channelIdRule.toString()
}

class ChannelIdRule(private val channelId: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,4 @@ class PostCriteria(
private fun meetCriteria(action: PostAction): Boolean = channelIdRule.meet(action.channelId)

override fun toString(): String = "發一則貼文"

override val link: String
get() = channelIdRule.toString()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package tw.waterballsa.utopia.utopiagamification.quest.domain.exception

class AssignedQuestException(playerId: String, questId: Int) :
RuntimeException("Player already owns this quest. {playerId:$playerId, questId:$questId}")
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,41 @@ package tw.waterballsa.utopia.utopiagamification.quest.domain.quests

import tw.waterballsa.utopia.utopiagamification.quest.domain.*
import tw.waterballsa.utopia.utopiagamification.quest.domain.actions.*
import tw.waterballsa.utopia.utopiagamification.quest.domain.quests.QuestIds.Companion.firstMessageActionQuestId
import tw.waterballsa.utopia.utopiagamification.quest.domain.quests.QuestIds.Companion.flagPostQuestId
import tw.waterballsa.utopia.utopiagamification.quest.domain.quests.QuestIds.Companion.joinActivityQuestId
import tw.waterballsa.utopia.utopiagamification.quest.domain.quests.QuestIds.Companion.quizQuestId
import tw.waterballsa.utopia.utopiagamification.quest.domain.quests.QuestIds.Companion.replyToAnyoneInCareerAdvancementTopicChannelQuestId
import tw.waterballsa.utopia.utopiagamification.quest.domain.quests.QuestIds.Companion.selfIntroductionQuestId
import tw.waterballsa.utopia.utopiagamification.quest.domain.quests.QuestIds.Companion.sendContainsImageMessageInEngineerLifeChannelQuestId
import tw.waterballsa.utopia.utopiagamification.quest.domain.quests.QuestIds.Companion.sendMessageInVoiceChannelQuestId
import tw.waterballsa.utopia.utopiagamification.quest.domain.quests.QuestIds.Companion.unlockAcademyQuestId
import tw.waterballsa.utopia.utopiagamification.quest.domain.quests.QuestIds.Companion.watchVideoQuestId
import tw.waterballsa.utopia.utopiagamification.repositories.inmemory.repositoryimpl.InMemoryQuestRepository

private const val unlockEmoji = "🔑"
private const val missionTips = "> (要是你怕自己的訊息太突兀,只要在訊息的開頭加上 `#任務`,保證自在。)"

class QuestIds {
companion object {
const val unlockAcademyQuestId = 1
const val selfIntroductionQuestId = 2
const val firstMessageActionQuestId = 3
const val sendContainsImageMessageInEngineerLifeChannelQuestId = 4
const val replyToAnyoneInCareerAdvancementTopicChannelQuestId = 5
const val watchVideoQuestId = 6
const val flagPostQuestId = 7
const val sendMessageInVoiceChannelQuestId = 8
const val joinActivityQuestId = 9
const val quizQuestId = 10
}
}


val InMemoryQuestRepository.unlockAcademyQuest: Quest
get() = findById(1) ?: save(
get() = findById(unlockAcademyQuestId) ?: save(
Quest(
id = 1,
id = unlockAcademyQuestId,
title = "解鎖學院",
description =
"""
Expand All @@ -32,22 +58,24 @@ val InMemoryQuestRepository.unlockAcademyQuest: Quest
unlockEmoji
),

link = wsa.unlockEntryChannelId.toLink(),

reward = Reward(
100u,
100u,
1.0f,
RoleType.WSA_MEMBER
),

nextQuestId = 2
nextQuestId = selfIntroductionQuestId
)
)


val InMemoryQuestRepository.selfIntroductionQuest: Quest
get() = findById(2) ?: save(
get() = findById(selfIntroductionQuestId) ?: save(
Quest(
id = 2,
id = selfIntroductionQuestId,
title = "自我介紹",
description =
"""
Expand Down Expand Up @@ -91,7 +119,9 @@ val InMemoryQuestRepository.selfIntroductionQuest: Quest
regexRule = getSelfIntroductionRegex()
),

nextQuestId = 3
link = wsa.selfIntroChannelId.toLink(),

nextQuestId = firstMessageActionQuestId
)
)

Expand All @@ -101,9 +131,9 @@ private fun getSelfIntroductionRegex(): RegexRule =
"""【(.|\n)*】(.|\n)*工作職位:?(.|\n)*((公司產業:?(:)?(.|\n)*))?專長:?(.|\n)*興趣:?(.|\n)*簡介:?.(.|\n)*((三件關於我的事,猜猜哪一件是假的:?(:)?(.|\n)*))?""".toRegexRule()

val InMemoryQuestRepository.firstMessageActionQuest: Quest
get() = findById(3) ?: save(
get() = findById(firstMessageActionQuestId) ?: save(
Quest(
id = 3,
id = firstMessageActionQuestId,
title = "新生降落",
description =
"""
Expand Down Expand Up @@ -135,14 +165,16 @@ val InMemoryQuestRepository.firstMessageActionQuest: Quest
ChannelIdRule(wsa.discussionAreaChannelId)
),

nextQuestId = 4
link = wsa.discussionAreaChannelId.toLink(),

nextQuestId = sendContainsImageMessageInEngineerLifeChannelQuestId
)
)

val InMemoryQuestRepository.sendContainsImageMessageInEngineerLifeChannelQuest: Quest
get() = findById(4) ?: save(
get() = findById(sendContainsImageMessageInEngineerLifeChannelQuestId) ?: save(
Quest(
id = 4,
id = sendContainsImageMessageInEngineerLifeChannelQuestId,
title = "融入大家",
description =
"""
Expand Down Expand Up @@ -173,14 +205,16 @@ val InMemoryQuestRepository.sendContainsImageMessageInEngineerLifeChannelQuest:
hasImageRule = BooleanRule.TRUE
),

nextQuestId = 5
link = wsa.engineerLifeChannelId.toLink(),

nextQuestId = replyToAnyoneInCareerAdvancementTopicChannelQuestId
)
)

val InMemoryQuestRepository.replyToAnyoneInCareerAdvancementTopicChannelQuest: Quest
get() = findById(5) ?: save(
get() = findById(replyToAnyoneInCareerAdvancementTopicChannelQuestId) ?: save(
Quest(
id = 5,
id = replyToAnyoneInCareerAdvancementTopicChannelQuestId,
title = "職涯攻略",
description =
"""
Expand Down Expand Up @@ -214,14 +248,16 @@ val InMemoryQuestRepository.replyToAnyoneInCareerAdvancementTopicChannelQuest: Q
hasRepliedRule = BooleanRule.TRUE
),

nextQuestId = 6
link = wsa.careerAdvancementTopicChannelId.toLink(),

nextQuestId = watchVideoQuestId
)
)

val InMemoryQuestRepository.watchVideoQuest: Quest
get() = findById(6) ?: save(
get() = findById(watchVideoQuestId) ?: save(
Quest(
id = 6,
id = watchVideoQuestId,
title = "學院精華影片",
description = """
在學會如何自在地和大家聊天交流和參與話題之後,接下來要來帶你好好逛一下這個學院。
Expand Down Expand Up @@ -250,14 +286,16 @@ val InMemoryQuestRepository.watchVideoQuest: Quest
ChannelIdRule(wsa.featuredVideosChannelId),
),

nextQuestId = 7
link = wsa.featuredVideosChannelId.toLink(),

nextQuestId = flagPostQuestId
)
)

val InMemoryQuestRepository.flagPostQuest: Quest
get() = findById(7) ?: save(
get() = findById(flagPostQuestId) ?: save(
Quest(
id = 7,
id = flagPostQuestId,
title = "全民插旗:把學院當成自己的家",
description =
"""
Expand Down Expand Up @@ -289,14 +327,16 @@ val InMemoryQuestRepository.flagPostQuest: Quest
ChannelIdRule(wsa.flagPostChannelId)
),

nextQuestId = 8
link = wsa.flagPostGuideId.toLink(),

nextQuestId = sendMessageInVoiceChannelQuestId
)
)

val InMemoryQuestRepository.sendMessageInVoiceChannelQuest: Quest
get() = findById(8) ?: save(
get() = findById(sendMessageInVoiceChannelQuestId) ?: save(
Quest(
id = 8,
id = sendMessageInVoiceChannelQuestId,
title = "到處吃瓜",
description =
"""
Expand Down Expand Up @@ -329,14 +369,16 @@ val InMemoryQuestRepository.sendMessageInVoiceChannelQuest: Quest
numberOfVoiceChannelMembersRule = AtLeastRule(2)
),

nextQuestId = 9
link = "任意頻道",

nextQuestId = joinActivityQuestId
)
)

val InMemoryQuestRepository.joinActivityQuest: Quest
get() = findById(9) ?: save(
get() = findById(joinActivityQuestId) ?: save(
Quest(
id = 9,
id = joinActivityQuestId,
title = "參與院長主持的學院節目",
description =
"""
Expand All @@ -353,14 +395,14 @@ val InMemoryQuestRepository.joinActivityQuest: Quest
),

criteria = JoinActivityCriteria("遊戲微服務計畫:水球實況", 60, 40),
nextQuestId = 10
nextQuestId = quizQuestId
)
)

val InMemoryQuestRepository.quizQuest: Quest
get() = findById(10) ?: save(
get() = findById(quizQuestId) ?: save(
Quest(
id = 10,
id = quizQuestId,
title = "考試",
description =
"""
Expand Down Expand Up @@ -389,5 +431,7 @@ val InMemoryQuestRepository.quizQuest: Quest
),

criteria = QuizCriteria("紳士考題", 4, 5),
)


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

import dev.minn.jda.ktx.messages.Embed
import net.dv8tion.jda.api.entities.User
import tw.waterballsa.utopia.utopiagamification.quest.domain.Mission
import tw.waterballsa.utopia.utopiagamification.quest.listeners.presenters.AssignPlayerQuestPresenter
import java.time.LocalDateTime
import java.time.OffsetDateTime
import java.time.ZoneId
Expand All @@ -12,33 +12,32 @@ fun String.toDate(): LocalDateTime = LocalDateTime.parse(this)
fun OffsetDateTime.toTaipeiLocalDateTime(): LocalDateTime =
atZoneSameInstant(ZoneId.of("Asia/Taipei")).toLocalDateTime()


fun Mission.publishToUser(user: User) {
fun AssignPlayerQuestPresenter.ViewModel.publishToUser(user: User) {
user.openPrivateChannel().queue {
it.sendMessageEmbeds(
Embed {
title = quest.title
description = quest.description
title = questTitle
description = questDescription
color = Color.GREEN

field {
name = "任務條件"
//轉成 string 並去除多餘的換行
value = "${quest.criteria}".replace(Regex("\\n{2,}"), "\n")
value = criteria
}

field {
name = "任務位置"
value = quest.criteria.link
value = link
inline = true
}
}
).queue()
}
}


class Color {
companion object {
val GREEN = 706146
const val GREEN = 706146
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import tw.waterballsa.utopia.utopiagamification.quest.domain.Player
import tw.waterballsa.utopia.utopiagamification.quest.extensions.LevelSheet.Companion.toLevelRange
import tw.waterballsa.utopia.utopiagamification.quest.extensions.LevelSheet.LevelRange.Companion.LEVEL_ONE
import tw.waterballsa.utopia.utopiagamification.quest.extensions.publishToUser
import tw.waterballsa.utopia.utopiagamification.quest.listeners.presenters.AssignPlayerQuestPresenter
import tw.waterballsa.utopia.utopiagamification.quest.usecase.ClaimMissionRewardUsecase
import tw.waterballsa.utopia.utopiagamification.repositories.PlayerRepository

Expand Down Expand Up @@ -59,7 +60,9 @@ class UtopiaGamificationQuestListener(
}

override fun presentNextMission(mission: Mission) {
mission.publishToUser(user)
val presenter = AssignPlayerQuestPresenter()
presenter.presentMission(mission)
presenter.viewModel?.publishToUser(user)
}

override fun presentRewardsNotAllowed(mission: Mission) {
Expand Down
Loading

0 comments on commit 0643327

Please sign in to comment.