Skip to content

Commit

Permalink
Merge pull request #1950 from GiganticMinecraft/Separate/fairySound
Browse files Browse the repository at this point in the history
妖精機能の喋るシステムを別subsystemとして切り出す
  • Loading branch information
KisaragiEffective authored Apr 18, 2023
2 parents d1df1fb + c26d9ff commit c6ab3b2
Show file tree
Hide file tree
Showing 14 changed files with 192 additions and 109 deletions.
11 changes: 11 additions & 0 deletions src/main/scala/com/github/unchama/seichiassist/SeichiAssist.scala
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ import com.github.unchama.seichiassist.subsystems.sharedinventory.SharedInventor
import com.github.unchama.seichiassist.subsystems.vote.VoteAPI
import com.github.unchama.seichiassist.subsystems.vote.subsystems.fairy.FairyAPI
import com.github.unchama.seichiassist.subsystems.tradesystems.subsystems.gttosiina.GtToSiinaAPI
import com.github.unchama.seichiassist.subsystems.vote.subsystems.fairyspeech.FairySpeechAPI
import com.github.unchama.seichiassist.task.PlayerDataSaveTask
import com.github.unchama.seichiassist.task.global._
import com.github.unchama.util.{ActionStatus, ClassUtils}
Expand Down Expand Up @@ -465,10 +466,18 @@ class SeichiAssist extends JavaPlugin() {
implicit val breakCountAPI: BreakCountAPI[IO, SyncIO, Player] = breakCountSystem.api
implicit val voteAPI: VoteAPI[IO, Player] = voteSystem.api
implicit val manaApi: ManaApi[IO, SyncIO, Player] = manaSystem.manaApi
implicit val fairySpeechAPI: FairySpeechAPI[IO, Player] = fairySpeechSystem.api

subsystems.vote.subsystems.fairy.System.wired.unsafeRunSync()
}

private lazy val fairySpeechSystem
: subsystems.vote.subsystems.fairyspeech.System[IO, Player] = {
import PluginExecutionContexts.timer

subsystems.vote.subsystems.fairyspeech.System.wired[IO]
}

/* TODO: mineStackSystemは本来privateであるべきだが、mineStackにアイテムを格納するAPIが現状の
BreakUtilの実装から呼び出されている都合上やむを得ずpublicになっている。*/
lazy val mineStackSystem: subsystems.minestack.System[IO, Player, ItemStack] =
Expand All @@ -495,6 +504,7 @@ class SeichiAssist extends JavaPlugin() {
presentSystem,
anywhereEnderSystem,
voteSystem,
fairySpeechSystem,
fairySystem,
gachaPrizeSystem,
idleTimeSystem,
Expand Down Expand Up @@ -660,6 +670,7 @@ class SeichiAssist extends JavaPlugin() {
sharedInventorySystem.api
implicit val voteAPI: VoteAPI[IO, Player] = voteSystem.api
implicit val fairyAPI: FairyAPI[IO, SyncIO, Player] = fairySystem.api
implicit val fairySpeechAPI: FairySpeechAPI[IO, Player] = fairySpeechSystem.api
implicit val donateAPI: DonatePremiumPointAPI[IO] = donateSystem.api
implicit val gachaTicketAPI: GachaTicketAPI[IO] =
gachaTicketSystem.api
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import com.github.unchama.seichiassist.subsystems.ranking.domain.values.{LoginTi
import com.github.unchama.seichiassist.subsystems.sharedinventory.SharedInventoryAPI
import com.github.unchama.seichiassist.subsystems.vote.VoteAPI
import com.github.unchama.seichiassist.subsystems.vote.subsystems.fairy.FairyAPI
import com.github.unchama.seichiassist.subsystems.vote.subsystems.fairyspeech.FairySpeechAPI
import io.chrisdavenport.cats.effect.time.JavaTime
import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack
Expand Down Expand Up @@ -92,7 +93,8 @@ object TopLevelRouter {
gachaPrizeAPI: GachaPrizeAPI[IO, ItemStack, Player],
mineStackAPI: MineStackAPI[IO, Player, ItemStack],
gachaDrawAPI: GachaDrawAPI[IO, Player],
consumeGachaTicketAPI: ConsumeGachaTicketAPI[IO, Player]
consumeGachaTicketAPI: ConsumeGachaTicketAPI[IO, Player],
fairySpeechAPI: FairySpeechAPI[IO, Player]
): TopLevelRouter[IO] = new TopLevelRouter[IO] {
import assortedRankingApi._

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import com.github.unchama.seichiassist.subsystems.vote.subsystems.fairy.domain.p
FairyLore,
FairySummonCost
}
import com.github.unchama.seichiassist.subsystems.vote.subsystems.fairyspeech.FairySpeechAPI
import com.github.unchama.targetedeffect.TargetedEffect.emptyEffect
import com.github.unchama.targetedeffect.commandsender.MessageEffect
import com.github.unchama.targetedeffect.player.FocusedSoundEffect
Expand All @@ -44,7 +45,8 @@ object VoteMenu extends Menu {
class Environment(
implicit val voteAPI: VoteAPI[IO, Player],
val fairyAPI: FairyAPI[IO, SyncIO, Player],
val ioCanOpenFirstPage: IO CanOpen FirstPage.type
val ioCanOpenFirstPage: IO CanOpen FirstPage.type,
val fairySpeechAPI: FairySpeechAPI[IO, Player]
)

/**
Expand Down Expand Up @@ -99,7 +101,8 @@ object VoteMenu extends Menu {

private case class ConstantButtons(player: Player)(
implicit voteAPI: VoteAPI[IO, Player],
fairyAPI: FairyAPI[IO, SyncIO, Player]
fairyAPI: FairyAPI[IO, SyncIO, Player],
fairySpeechAPI: FairySpeechAPI[IO, Player]
) {

import com.github.unchama.seichiassist.concurrent.PluginExecutionContexts.layoutPreparationContext
Expand Down Expand Up @@ -272,7 +275,7 @@ object VoteMenu extends Menu {
val playSoundOffLore = s"$RESET${RED}現在音が鳴らない設定になっています。" +: description

RecomputedButton(for {
fairySpeechSound <- fairyAPI.doPlaySoundOnSpeak(player.getUniqueId)
fairySpeechSound <- fairySpeechAPI.playSoundOnSpeech(player.getUniqueId)
} yield {
Button(
new IconItemStackBuilder(Material.JUKEBOX)
Expand All @@ -285,7 +288,7 @@ object VoteMenu extends Menu {
LeftClickButtonEffect {
SequentialEffect(
FocusedSoundEffect(Sound.BLOCK_STONE_BUTTON_CLICK_ON, 1f, 1f),
DeferredEffect(IO(fairyAPI.toggleSoundOnSpeak))
DeferredEffect(IO(fairySpeechAPI.togglePlaySoundOnSpeech))
)
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,6 @@ object FairyReadAPI {

trait FairySpeechAPI[F[_], Player] {

/**
* @return 妖精が喋るときに音を再生するかをトグルする作用
*/
def toggleSoundOnSpeak: Kleisli[F, Player, Unit]

/**
* @return 妖精が喋ったときに音を再生するか取得する作用
*/
def doPlaySoundOnSpeak(uuid: UUID): F[Boolean]

/**
* @return 妖精がいつ帰るのかを送信する作用
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ package com.github.unchama.seichiassist.subsystems.vote.subsystems.fairy
import cats.data.Kleisli
import cats.effect.{ConcurrentEffect, IO, SyncIO}
import com.github.unchama.concurrent.RepeatingTaskContext
import com.github.unchama.datarepository.bukkit.player.{
BukkitRepositoryControls,
PlayerDataRepository
}
import com.github.unchama.datarepository.bukkit.player.BukkitRepositoryControls
import com.github.unchama.datarepository.template.RepositoryDefinition
import com.github.unchama.minecraft.actions.OnMinecraftServerThread
import com.github.unchama.seichiassist.concurrent.PluginExecutionContexts.timer
Expand All @@ -16,30 +13,22 @@ import com.github.unchama.seichiassist.subsystems.mana.ManaApi
import com.github.unchama.seichiassist.subsystems.minestack.MineStackAPI
import com.github.unchama.seichiassist.subsystems.vote.VoteAPI
import com.github.unchama.seichiassist.subsystems.vote.subsystems.fairy.application.actions.SummonFairy
import com.github.unchama.seichiassist.subsystems.vote.subsystems.fairy.application.repository.{
FairyManaRecoveryRoutineFiberRepositoryDefinition,
SpeechServiceRepositoryDefinitions
}
import com.github.unchama.seichiassist.subsystems.vote.subsystems.fairy.application.repository.FairyManaRecoveryRoutineFiberRepositoryDefinition
import com.github.unchama.seichiassist.subsystems.vote.subsystems.fairy.bukkit.actions.BukkitSummonFairy
import com.github.unchama.seichiassist.subsystems.vote.subsystems.fairy.bukkit.gateway.BukkitFairySpeechGateway
import com.github.unchama.seichiassist.subsystems.vote.subsystems.fairy.bukkit.listeners.FairyPlayerJoinGreeter
import com.github.unchama.seichiassist.subsystems.vote.subsystems.fairy.bukkit.routines.BukkitFairyRoutine
import com.github.unchama.seichiassist.subsystems.vote.subsystems.fairy.bukkit.{
BukkitFairySpeech,
BukkitFairySummonRequest
}
import com.github.unchama.seichiassist.subsystems.vote.subsystems.fairy.domain.property._
import com.github.unchama.seichiassist.subsystems.vote.subsystems.fairy.domain.speech.{
FairySpeech,
FairySpeechGateway
}
import com.github.unchama.seichiassist.subsystems.vote.subsystems.fairy.domain.speech.FairySpeech
import com.github.unchama.seichiassist.subsystems.vote.subsystems.fairy.domain.{
FairyPersistence,
FairySpawnRequestErrorOrSpawn,
FairySummonRequest
}
import com.github.unchama.seichiassist.subsystems.vote.subsystems.fairy.infrastructure.JdbcFairyPersistence
import com.github.unchama.seichiassist.subsystems.vote.subsystems.fairy.service.FairySpeechService
import org.bukkit.entity.Player
import org.bukkit.event.Listener
import org.bukkit.inventory.ItemStack
Expand All @@ -57,45 +46,30 @@ object System {
voteAPI: VoteAPI[IO, Player],
manaApi: ManaApi[IO, SyncIO, Player],
mineStackAPI: MineStackAPI[IO, Player, ItemStack],
fairySpeechAPI: com.github.unchama.seichiassist.subsystems.vote.subsystems.fairyspeech.FairySpeechAPI[
IO,
Player
],
repeatingTaskContext: RepeatingTaskContext,
concurrentEffect: ConcurrentEffect[IO],
minecraftServerThread: OnMinecraftServerThread[IO]
): SyncIO[System[IO, SyncIO, Player]] = {
implicit val persistence: FairyPersistence[IO] = new JdbcFairyPersistence[IO]
implicit val fairySpeechGatewayProvider: Player => FairySpeechGateway[SyncIO] =
new BukkitFairySpeechGateway[SyncIO](_)
val fairySpeechProvider
: PlayerDataRepository[FairySpeechService[SyncIO]] => FairySpeech[IO, Player] =
fairySpeechService => new BukkitFairySpeech[IO, SyncIO](fairySpeechService, persistence)
implicit val fairySpeechProvider: FairySpeech[IO, Player] = new BukkitFairySpeech[IO]
val fairyRoutine = new BukkitFairyRoutine(fairySpeechProvider)

for {
speechServiceRepositoryControls <- BukkitRepositoryControls.createHandles(
RepositoryDefinition
.Phased
.TwoPhased(
SpeechServiceRepositoryDefinitions.initialization[SyncIO, Player],
SpeechServiceRepositoryDefinitions.finalization[SyncIO, Player]
)
)
_fairyRoutine = new BukkitFairyRoutine(
fairySpeechProvider(speechServiceRepositoryControls.repository)
)
fairyRecoveryRoutineFiberRepositoryControls <- BukkitRepositoryControls.createHandles(
RepositoryDefinition
.Phased
.TwoPhased(
FairyManaRecoveryRoutineFiberRepositoryDefinition
.initialization[Player](_fairyRoutine),
.initialization[Player](fairyRoutine),
FairyManaRecoveryRoutineFiberRepositoryDefinition.finalization[Player]
)
)
} yield {
new System[IO, SyncIO, Player] {
val fairySpeechServiceRepository: PlayerDataRepository[FairySpeechService[SyncIO]] =
speechServiceRepositoryControls.repository
implicit val fairySpeech: FairySpeech[IO, Player] = fairySpeechProvider(
fairySpeechServiceRepository
)
implicit val summonFairy: SummonFairy[IO, Player] =
new BukkitSummonFairy[IO, SyncIO]
val summonRequest: FairySummonRequest[IO, Player] =
Expand Down Expand Up @@ -137,19 +111,8 @@ object System {
override def totalConsumedApple: IO[AppleAmount] =
persistence.totalConsumedAppleAmount

override def doPlaySoundOnSpeak(uuid: UUID): IO[Boolean] =
persistence.playSoundOnFairySpeech(uuid)

override def toggleSoundOnSpeak: Kleisli[IO, Player, Unit] = Kleisli { player =>
val uuid = player.getUniqueId
for {
isPlayFairySpeechSound <- doPlaySoundOnSpeak(uuid)
_ <- persistence.setPlaySoundOnSpeech(uuid, !isPlayFairySpeechSound)
} yield ()
}

override def sendDisappearTimeToChat: Kleisli[IO, Player, Unit] = Kleisli {
player => fairySpeech.speechEndTime(player)
player => fairySpeechProvider.speechEndTime(player)
}

override def fairySummonRequest
Expand All @@ -160,7 +123,7 @@ object System {
}

override val managedRepositoryControls: Seq[BukkitRepositoryControls[IO, _]] =
Seq(speechServiceRepositoryControls, fairyRecoveryRoutineFiberRepositoryControls).map(
Seq(fairyRecoveryRoutineFiberRepositoryControls).map(
_.coerceFinalizationContextTo[IO]
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import cats.Applicative
import cats.effect.Sync
import com.github.unchama.datarepository.template.finalization.RepositoryFinalization
import com.github.unchama.datarepository.template.initialization.TwoPhasedRepositoryInitialization
import com.github.unchama.seichiassist.subsystems.vote.subsystems.fairy.domain.speech.FairySpeechGateway
import com.github.unchama.seichiassist.subsystems.vote.subsystems.fairy.service.FairySpeechService
import com.github.unchama.seichiassist.subsystems.vote.subsystems.fairyspeech.domain.FairySpeechGateway
import com.github.unchama.seichiassist.subsystems.vote.subsystems.fairyspeech.service.FairySpeechService

object SpeechServiceRepositoryDefinitions {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package com.github.unchama.seichiassist.subsystems.vote.subsystems.fairy.bukkit

import cats.effect.Sync
import com.github.unchama.datarepository.bukkit.player.PlayerDataRepository
import com.github.unchama.generic.ContextCoercion
import com.github.unchama.seichiassist.subsystems.vote.subsystems.fairy.domain.FairyPersistence
import com.github.unchama.seichiassist.subsystems.vote.subsystems.fairy.domain.property._
import com.github.unchama.seichiassist.subsystems.vote.subsystems.fairy.domain.resources.FairyMessageTable
import com.github.unchama.seichiassist.subsystems.vote.subsystems.fairy.domain.speech.FairySpeech
import com.github.unchama.seichiassist.subsystems.vote.subsystems.fairy.service.FairySpeechService
import com.github.unchama.seichiassist.subsystems.vote.subsystems.fairyspeech.FairySpeechAPI
import io.chrisdavenport.cats.effect.time.JavaTime
import org.bukkit.entity.Player

import java.time.ZoneId
import scala.util.Random

class BukkitFairySpeech[F[_]: Sync: JavaTime, G[_]: ContextCoercion[*[_], F]](
fairySpeechServiceRepository: PlayerDataRepository[FairySpeechService[G]],
fairyPersistence: FairyPersistence[F]
class BukkitFairySpeech[F[_]: Sync: JavaTime](
implicit fairyPersistence: FairyPersistence[F],
fairySpeechAPI: FairySpeechAPI[F, Player]
) extends FairySpeech[F, Player] {

import cats.implicits._
Expand All @@ -39,14 +37,7 @@ class BukkitFairySpeech[F[_]: Sync: JavaTime, G[_]: ContextCoercion[*[_], F]](
nameCalledByFairy = ScreenNameForFairy(player.getName)
fairyMessages = getSummonMessagesByStartHour(startHour, nameCalledByFairy)
message <- randomMessage(fairyMessages)

serviceRepository = fairySpeechServiceRepository(player)
fairySpeechSound <- ContextCoercion {
fairyPersistence.playSoundOnFairySpeech(player.getUniqueId)
}
_ <- ContextCoercion {
serviceRepository.makeSpeech(Seq(message), fairySpeechSound)
}
_ <- fairySpeechAPI.speech(player, Seq(message))
} yield ()

override def speechRandomly(
Expand All @@ -64,44 +55,30 @@ class BukkitFairySpeech[F[_]: Sync: JavaTime, G[_]: ContextCoercion[*[_], F]](
}
for {
message <- randomMessage(messages(nameCalledByFairy))
fairyPlaySound <- fairyPersistence.playSoundOnFairySpeech(player.getUniqueId)
_ <- ContextCoercion {
fairySpeechServiceRepository(player).makeSpeech(Seq(message), fairyPlaySound)
}
_ <- fairySpeechAPI.speech(player, Seq(message))
} yield ()
}

override def speechEndTime(player: Player): F[Unit] = {
for {
endTimeOpt <- fairyPersistence.fairyEndTime(player.getUniqueId)
playSound <- fairyPersistence.playSoundOnFairySpeech(player.getUniqueId)
endTime = endTimeOpt.get.endTime
_ <- ContextCoercion {
fairySpeechServiceRepository(player).makeSpeech(
Seq(FairyMessage(s"僕は${endTime.getHour}:${endTime.getMinute}には帰るよー。")),
playSound
)
}
_ <- fairySpeechAPI.speech(
player,
Seq(FairyMessage(s"僕は${endTime.getHour}:${endTime.getMinute}には帰るよー。"))
)
} yield ()
}

override def welcomeBack(player: Player): F[Unit] = for {
playSound <- fairyPersistence.playSoundOnFairySpeech(player.getUniqueId)
_ <- ContextCoercion {
fairySpeechServiceRepository(player)
.makeSpeech(Seq(FairyMessage(s"おかえり!${player.getName}")), playSound)
}
_ <- fairySpeechAPI.speech(player, Seq(FairyMessage(s"おかえり!${player.getName}")))
} yield ()

override def bye(player: Player): F[Unit] = for {
playSound <- fairyPersistence.playSoundOnFairySpeech(player.getUniqueId)
repository = fairySpeechServiceRepository(player)
_ <- ContextCoercion {
repository.makeSpeech(
Seq(FairyMessage(s"あっ、もうこんな時間だ!"), FairyMessage(s"じゃーねー!${player.getName}")),
playSound
)
}
_ <- fairySpeechAPI.speech(
player,
Seq(FairyMessage(s"あっ、もうこんな時間だ!"), FairyMessage(s"じゃーねー!${player.getName}"))
)
} yield ()

private def randomMessage(fairyMessages: FairyMessageChoice): F[FairyMessage] =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.github.unchama.seichiassist.subsystems.vote.subsystems.fairyspeech

import cats.data.Kleisli
import com.github.unchama.seichiassist.subsystems.vote.subsystems.fairy.domain.property.FairyMessage

import java.util.UUID
import scala.collection.immutable.Seq

trait FairySpeechAPI[F[_], Player] {

/**
* @return `player`に対して`messages`を妖精が喋る作用
*/
def speech(player: Player, messages: Seq[FairyMessage]): F[Unit]

/**
* @return `player`に対して妖精が喋った際に音を再生するかをトグルする作用
*/
def togglePlaySoundOnSpeech: Kleisli[F, Player, Unit]

/**
* @return `player`に対して妖精が喋った際に音を再生するかどうかを取得する作用
*/
def playSoundOnSpeech(player: UUID): F[Boolean]

}

object FairySpeechAPI {

def apply[F[_], Player](implicit ev: FairySpeechAPI[F, Player]): FairySpeechAPI[F, Player] =
ev

}
Loading

0 comments on commit c6ab3b2

Please sign in to comment.