Skip to content

Commit

Permalink
refactoryFulfillMissionsUsecase
Browse files Browse the repository at this point in the history
  • Loading branch information
wow890209 committed Nov 19, 2023
1 parent ffdde48 commit edb851d
Show file tree
Hide file tree
Showing 23 changed files with 379 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ class Activity(

private fun Player.toAudience(): Audience = Audience(id)

fun leave(player: Player): JoinActivityAction? {
val audience = audiences[player.id] ?: return null
fun leave(playerId: String): JoinActivityAction? {
val audience = audiences[playerId] ?: return null
val stayDuration = audience.leave()

log.info(""" [leave activity] { "userId" = "${player.id}", "activityName" = $eventName, "stayDuration" = "$stayDuration"} """)
log.info(""" [leave activity] { "userId" = "$playerId", "activityName" = $eventName, "stayDuration" = "$stayDuration"} """)

return JoinActivityAction(
player,
playerId,
eventName,
audiences.size,
stayDuration.toMinutes().toInt()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import org.springframework.stereotype.Component
import tw.waterballsa.utopia.utopiagamification.activity.domain.Activity
import tw.waterballsa.utopia.utopiagamification.activity.domain.Activity.State.*
import tw.waterballsa.utopia.utopiagamification.activity.extensions.DateTimeRange
import tw.waterballsa.utopia.utopiagamification.quest.extensions.publishToUser
import tw.waterballsa.utopia.utopiagamification.quest.extensions.toTaipeiLocalDateTime
import tw.waterballsa.utopia.utopiagamification.quest.listeners.UtopiaGamificationListener
import tw.waterballsa.utopia.utopiagamification.quest.listeners.presenters.PlayerFulfillMissionPresenter
import tw.waterballsa.utopia.utopiagamification.quest.usecase.PlayerFulfillMissionsUsecase
import tw.waterballsa.utopia.utopiagamification.repositories.ActivityRepository
import tw.waterballsa.utopia.utopiagamification.repositories.PlayerRepository
import tw.waterballsa.utopia.utopiagamification.quest.usecase.PlayerFulfillMissionsUsecase
import java.time.LocalDateTime.now

private val log = KotlinLogging.logger {}
Expand All @@ -41,9 +43,11 @@ class EventJoiningListener(

channelLeft?.let {
val stayActivity = activityRepository.findAudienceStayActivity(it.id, player.id) ?: return@let
val action = stayActivity.leave(player) ?: return
playerFulfillMissionsUsecase.execute(action, user.claimMissionRewardPresenter)
val action = stayActivity.leave(user.id) ?: return
val presenter = PlayerFulfillMissionPresenter()
playerFulfillMissionsUsecase.execute(action, presenter)
activityRepository.save(stayActivity)
presenter.viewModel?.publishToUser(user)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package tw.waterballsa.utopia.utopiagamification.quest.domain

abstract class Action(
val player: Player
val playerId: String
) {

abstract fun match(criteria: Criteria): Boolean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package tw.waterballsa.utopia.utopiagamification.quest.domain.actions

import tw.waterballsa.utopia.utopiagamification.quest.domain.Action
import tw.waterballsa.utopia.utopiagamification.quest.domain.Player

// TODO join是參加的意思,不是家入的意思,join 容易被誤會成加入的意思,應該要換成其他英文字
class JoinActivityAction(
player: Player,
playerId: String,
val eventName: String,
val maxMemberCount: Int,
val stayDuration: Int,
) : Action(player) {
) : Action(playerId) {

override fun match(criteria: Criteria): Boolean = criteria is JoinActivityCriteria
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package tw.waterballsa.utopia.utopiagamification.quest.domain.actions

import tw.waterballsa.utopia.utopiagamification.quest.domain.Action
import tw.waterballsa.utopia.utopiagamification.quest.domain.Player

class MessageReactionAction(
player: Player,
player: String,
val messageId: String,
val emoji: String
) : Action(player) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package tw.waterballsa.utopia.utopiagamification.quest.domain.actions

import tw.waterballsa.utopia.utopiagamification.quest.domain.Action
import tw.waterballsa.utopia.utopiagamification.quest.domain.Player


class MessageSentAction(
player: Player,
playerId: String,
val channelId: String,
val context: String,
val hasReplied: Boolean,
val hasImage: Boolean,
val numberOfVoiceChannelMembers: Int,
) : Action(player) {
) : Action(playerId) {

override fun match(criteria: Criteria): Boolean = criteria is MessageSentCriteria
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package tw.waterballsa.utopia.utopiagamification.quest.domain.actions

import tw.waterballsa.utopia.utopiagamification.quest.domain.Action
import tw.waterballsa.utopia.utopiagamification.quest.domain.Player

class PostAction(
player: Player,
playerId: String,
val channelId: String
) : Action(player) {
) : Action(playerId) {

override fun match(criteria: Criteria): Boolean = criteria is PostCriteria
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package tw.waterballsa.utopia.utopiagamification.quest.domain.actions

import mu.KotlinLogging
import tw.waterballsa.utopia.utopiagamification.quest.domain.Action
import tw.waterballsa.utopia.utopiagamification.quest.domain.Player

private val log = KotlinLogging.logger {}

class QuizAction(
player: Player,
playerId: String,
val quizName: String,
val correctCount: Int
) : Action(player) {
) : Action(playerId) {

override fun match(criteria: Criteria): Boolean = criteria is QuizCriteria
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ 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.listeners.RewardButton
import tw.waterballsa.utopia.utopiagamification.quest.listeners.presenters.AssignPlayerQuestPresenter
import tw.waterballsa.utopia.utopiagamification.quest.listeners.presenters.PlayerFulfillMissionPresenter
import java.time.LocalDateTime
import java.time.OffsetDateTime
import java.time.ZoneId
Expand All @@ -12,6 +14,12 @@ fun String.toDate(): LocalDateTime = LocalDateTime.parse(this)
fun OffsetDateTime.toTaipeiLocalDateTime(): LocalDateTime =
atZoneSameInstant(ZoneId.of("Asia/Taipei")).toLocalDateTime()

class Color {
companion object {
const val GREEN = 706146
}
}

fun AssignPlayerQuestPresenter.ViewModel.publishToUser(user: User) {
user.openPrivateChannel().queue {
it.sendMessageEmbeds(
Expand All @@ -35,9 +43,10 @@ fun AssignPlayerQuestPresenter.ViewModel.publishToUser(user: User) {
}
}


class Color {
companion object {
const val GREEN = 706146
fun PlayerFulfillMissionPresenter.ViewModel.publishToUser(user: User) {
user.openPrivateChannel().queue {
it.sendMessage(postMessage)
.addActionRow(RewardButton.toJdaButton(questId))
.queue()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tw.waterballsa.utopia.utopiagamification.quest.listeners
import dev.minn.jda.ktx.interactions.components.button
import net.dv8tion.jda.api.entities.Guild
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent
import net.dv8tion.jda.api.interactions.components.buttons.Button
import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle
import org.springframework.stereotype.Component
import tw.waterballsa.utopia.utopiagamification.quest.extensions.publishToUser
Expand All @@ -20,6 +21,8 @@ class RewardButton {
const val LABEL: String = "領取獎勵"

fun id(questId: Int): String = "$BUTTON_QUEST_TAG-$NAME-$questId"

fun toJdaButton(questId: Int): Button = button(id(questId), LABEL)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import net.dv8tion.jda.api.entities.Guild
import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent
import org.springframework.stereotype.Component
import tw.waterballsa.utopia.utopiagamification.quest.domain.actions.MessageReactionAction
import tw.waterballsa.utopia.utopiagamification.quest.extensions.publishToUser
import tw.waterballsa.utopia.utopiagamification.quest.listeners.presenters.PlayerFulfillMissionPresenter
import tw.waterballsa.utopia.utopiagamification.quest.usecase.PlayerFulfillMissionsUsecase
import tw.waterballsa.utopia.utopiagamification.repositories.PlayerRepository

Expand All @@ -16,16 +18,21 @@ class MessageReactionListener(

override fun onMessageReactionAdd(event: MessageReactionAddEvent) {
with(event) {
//TODO 這個 toPlayer 會有副作用,會註冊玩家,之後會發 pr 解決這個問題
val user = user ?: return
val player = user.toPlayer() ?: return

val action = MessageReactionAction(
player,
user.id,
messageId,
emoji.name
)

playerFulfillMissionsUsecase.execute(action, user.claimMissionRewardPresenter)
val presenter = PlayerFulfillMissionPresenter()

playerFulfillMissionsUsecase.execute(action, presenter)

presenter.viewModel?.publishToUser(user)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import net.dv8tion.jda.api.events.message.MessageReceivedEvent
import net.dv8tion.jda.api.events.message.MessageUpdateEvent
import org.springframework.stereotype.Component
import tw.waterballsa.utopia.utopiagamification.quest.domain.actions.MessageSentAction
import tw.waterballsa.utopia.utopiagamification.quest.extensions.publishToUser
import tw.waterballsa.utopia.utopiagamification.quest.listeners.presenters.PlayerFulfillMissionPresenter
import tw.waterballsa.utopia.utopiagamification.quest.usecase.PlayerFulfillMissionsUsecase
import tw.waterballsa.utopia.utopiagamification.repositories.PlayerRepository

Expand All @@ -23,19 +25,23 @@ class MessageSentListener(
return
}

//TODO 這個 toPlayer 會有副作用,會註冊玩家,之後會發 pr 解決這個問題
val user = author
val player = user.toPlayer() ?: return

val action = MessageSentAction(
player,
user.id,
(channel as? ThreadChannel)?.parentChannel?.id ?: channel.id,
message.contentDisplay,
message.referencedMessage != null,
message.attachments.any { it.isImage },
(channel as? VoiceChannel)?.members?.size ?: 0
)
val presenter = PlayerFulfillMissionPresenter()

playerFulfillMissionsUsecase.execute(action, user.claimMissionRewardPresenter)
playerFulfillMissionsUsecase.execute(action, presenter)

presenter.viewModel?.publishToUser(user)
}
}

Expand All @@ -45,19 +51,24 @@ class MessageSentListener(
return
}

//TODO 這個 toPlayer 會有副作用,會註冊玩家,之後會發 pr 解決這個問題
val user = author
val player = user.toPlayer() ?: return

val action = MessageSentAction(
player,
user.id,
(channel as? ThreadChannel)?.parentChannel?.id ?: channel.id,
message.contentDisplay,
message.referencedMessage != null,
message.attachments.any { it.isImage },
(channel as? VoiceChannel)?.members?.size ?: 0
)

playerFulfillMissionsUsecase.execute(action, user.claimMissionRewardPresenter)
val presenter = PlayerFulfillMissionPresenter()

playerFulfillMissionsUsecase.execute(action, presenter)

presenter.viewModel?.publishToUser(user)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import net.dv8tion.jda.api.entities.Guild
import net.dv8tion.jda.api.events.guild.GuildAuditLogEntryCreateEvent
import org.springframework.stereotype.Component
import tw.waterballsa.utopia.utopiagamification.quest.domain.actions.PostAction
import tw.waterballsa.utopia.utopiagamification.quest.extensions.publishToUser
import tw.waterballsa.utopia.utopiagamification.quest.listeners.presenters.PlayerFulfillMissionPresenter
import tw.waterballsa.utopia.utopiagamification.quest.usecase.PlayerFulfillMissionsUsecase
import tw.waterballsa.utopia.utopiagamification.repositories.PlayerRepository

Expand All @@ -16,16 +18,21 @@ class PostListener(

override fun onGuildAuditLogEntryCreate(event: GuildAuditLogEntryCreateEvent) {
with(event) {
//TODO 這個 toPlayer 會有副作用,會註冊玩家,之後會發 pr 解決這個問題
val user = user ?: return
val channel = guild.getThreadChannelById(entry.targetId)?.parentChannel ?: return
val player = user.toPlayer() ?: return
val channel = guild.getThreadChannelById(entry.targetId)?.parentChannel ?: return

val action = PostAction(
player,
user.id,
channel.id
)

playerFulfillMissionsUsecase.execute(action, user.claimMissionRewardPresenter)
val presenter = PlayerFulfillMissionPresenter()

playerFulfillMissionsUsecase.execute(action, presenter)

presenter.viewModel?.publishToUser(user)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import tw.waterballsa.utopia.jda.domains.QuizPreparationStartEvent
import tw.waterballsa.utopia.jda.domains.UtopiaEvent
import tw.waterballsa.utopia.utopiagamification.quest.domain.Quest
import tw.waterballsa.utopia.utopiagamification.quest.domain.actions.QuizAction
import tw.waterballsa.utopia.utopiagamification.quest.extensions.publishToUser
import tw.waterballsa.utopia.utopiagamification.quest.listeners.presenters.PlayerFulfillMissionPresenter
import tw.waterballsa.utopia.utopiagamification.quest.usecase.PlayerFulfillMissionsUsecase
import tw.waterballsa.utopia.utopiagamification.repositories.MissionRepository
import tw.waterballsa.utopia.utopiagamification.repositories.PlayerRepository
Expand Down Expand Up @@ -60,18 +62,22 @@ class QuizListener(

private fun onQuizEnd(event: QuizEndEvent) {
with(event) {
//TODO 這個 toPlayer 會有副作用,會註冊玩家,之後會發 pr 解決這個問題
val user = jda.retrieveUserById(quizTakerId).complete() ?: return
val player = user.toPlayer() ?: return

val action = QuizAction(
player,
user.id,
quizName,
correctCount
)
val presenter = PlayerFulfillMissionPresenter()

log.info { """[quiz end] { quizTakerId : "$quizTakerId", quizName : "$quizName", correctCount : "$correctCount" } """ }

playerFulfillMissionsUsecase.execute(action, user.claimMissionRewardPresenter)
playerFulfillMissionsUsecase.execute(action, presenter)

presenter.viewModel?.publishToUser(user)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import tw.waterballsa.utopia.utopiagamification.quest.domain.quests.QuestIds.Com
import tw.waterballsa.utopia.utopiagamification.quest.domain.quests.QuestIds.Companion.unlockAcademyQuestId
import tw.waterballsa.utopia.utopiagamification.quest.extensions.publishToUser
import tw.waterballsa.utopia.utopiagamification.quest.listeners.presenters.AssignPlayerQuestPresenter
import tw.waterballsa.utopia.utopiagamification.quest.listeners.presenters.PlayerFulfillMissionPresenter
import tw.waterballsa.utopia.utopiagamification.quest.usecase.AssignPlayerQuestUsecase
import tw.waterballsa.utopia.utopiagamification.repositories.MissionRepository
import tw.waterballsa.utopia.utopiagamification.repositories.PlayerRepository
Expand Down Expand Up @@ -68,7 +69,9 @@ class SlashCommandListener(

with(mission) {
if (state == COMPLETED) {
user.claimMissionRewardPresenter.presentClaimMissionReward(mission)
val presenter = PlayerFulfillMissionPresenter()
presenter.present(mission)
presenter.viewModel?.publishToUser(user)
}

if (state == IN_PROGRESS) {
Expand Down
Loading

0 comments on commit edb851d

Please sign in to comment.