Skip to content

Commit

Permalink
Merge pull request #2147 from GiganticMinecraft/refactorGacha
Browse files Browse the repository at this point in the history
ガチャのリファクタリング
  • Loading branch information
kory33 authored Sep 16, 2023
2 parents 3fc956a + 4214b51 commit c2fe54a
Show file tree
Hide file tree
Showing 46 changed files with 569 additions and 759 deletions.
1 change: 1 addition & 0 deletions .sbtopts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-J-Xmx10g
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
USE seichiassist;

-- gachadataはガチャ景品に対して依存を持つが、ガチャ景品のIDより語尾の数が2つ小さいので、
-- gachadata0_[ガチャ景品ID]という形式にする
UPDATE mine_stack SET object_name = CONVERT(REPLACE(object_name, 'gachadata0_', ''), INT) + 2 WHERE object_name NOT LIKE 'gachadata0_exp_bottle' AND object_name LIKE 'gachadata0_%';

-- 上の変換により、mine_stack_gacha_objectsを永続化する必要がなくなったので削除する
DROP TABLE IF EXISTS mine_stack_gacha_objects;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
USE seichiassist;

ALTER TABLE gachadata ADD FOREIGN KEY (event_id) REFERENCES gacha_events(id) ON DELETE CASCADE;
Original file line number Diff line number Diff line change
Expand Up @@ -409,15 +409,15 @@ class SeichiAssist extends JavaPlugin() {
}

private lazy val gachaPrizeSystem: subsystems.gachaprize.System[IO] =
subsystems.gachaprize.System.wired.unsafeRunSync()
subsystems.gachaprize.System.wired

private implicit lazy val gachaPrizeAPI: GachaPrizeAPI[IO, ItemStack, Player] =
gachaPrizeSystem.api

private lazy val gachaSystem: subsystems.gacha.System[IO, Player] = {
implicit val gachaTicketAPI: GachaTicketAPI[IO] = gachaTicketSystem.api

subsystems.gacha.System.wired[IO].unsafeRunSync()
subsystems.gacha.System.wired[IO]
}

private lazy val consumeGachaTicketSystem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.github.unchama.seichiassist.subsystems.gacha

import cats.data.Kleisli
import cats.effect.ConcurrentEffect
import com.github.unchama.concurrent.NonServerThreadContextShift
import com.github.unchama.minecraft.actions.OnMinecraftServerThread
import com.github.unchama.minecraft.bukkit.algebra.CloneableBukkitItemStack.instance
import com.github.unchama.seichiassist.meta.subsystem.Subsystem
Expand Down Expand Up @@ -39,38 +38,30 @@ trait System[F[_], Player] extends Subsystem[F] {

object System {

import cats.implicits._

def wired[F[_]: ConcurrentEffect: OnMinecraftServerThread: NonServerThreadContextShift](
def wired[F[_]: ConcurrentEffect: OnMinecraftServerThread](
implicit gachaPrizeAPI: GachaPrizeAPI[F, ItemStack, Player],
gachaTicketAPI: GachaTicketAPI[F],
mineStackAPI: MineStackAPI[F, Player, ItemStack]
): F[System[F, Player]] = {
): System[F, Player] = {
implicit val canBeSignedAsGachaPrize: CanBeSignedAsGachaPrize[ItemStack] =
gachaPrizeAPI.canBeSignedAsGachaPrize
implicit val grantGachaPrize: GrantGachaPrize[F, ItemStack] =
implicit val grantGachaPrize: GrantGachaPrize[F, ItemStack, Player] =
new BukkitGrantGachaPrize[F]
implicit val staticGachaPrizeFactory: StaticGachaPrizeFactory[ItemStack] =
gachaPrizeAPI.staticGachaPrizeFactory
implicit val lotteryOfGachaItems: LotteryOfGachaItems[F, ItemStack] =
new LotteryOfGachaItems[F, ItemStack]
implicit val drawGacha: DrawGacha[F, Player] = new BukkitDrawGacha[F]

for {
gachaPrizesListReference <- gachaPrizeAPI.listOfNow
} yield {
implicit val _drawGacha: DrawGacha[F, Player] =
new BukkitDrawGacha[F](gachaPrizesListReference)

new System[F, Player] {
override val api: GachaDrawAPI[F, Player] = (draws: Int) =>
Kleisli { player => _drawGacha.draw(player, draws) }
new System[F, Player] {
override val api: GachaDrawAPI[F, Player] = (draws: Int) =>
Kleisli { player => drawGacha.draw(player, draws) }

override val commands: Map[String, TabExecutor] = Map(
"gacha" -> new GachaCommand[F].executor
)
override val commands: Map[String, TabExecutor] = Map(
"gacha" -> new GachaCommand[F].executor
)

override val listeners: Seq[Listener] = Seq(new PlayerPullGachaListener[F])
}
override val listeners: Seq[Listener] = Seq(new PlayerPullGachaListener[F])
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,44 @@ package com.github.unchama.seichiassist.subsystems.gacha.application.actions
import cats.Monad
import cats.data.Kleisli
import com.github.unchama.seichiassist.subsystems.gacha.domain.GrantState
import com.github.unchama.seichiassist.subsystems.gachaprize.domain.gachaprize.GachaPrize
import org.bukkit.entity.Player
import com.github.unchama.seichiassist.subsystems.gachaprize.domain.GachaPrizeTableEntry

trait GrantGachaPrize[F[_], ItemStack] {
trait GrantGachaPrize[F[_], ItemStack, Player] {

import cats.implicits._

implicit val F: Monad[F]

def tryInsertIntoMineStack(prize: GachaPrize[ItemStack]): Kleisli[F, Player, Boolean]

/**
* @param prizes MineStackに格納したい[[GachaPrizeTableEntry]]のVector
* @return `prizes`をMineStackに格納することを試み、格納できなかった[[GachaPrizeTableEntry]]のVectorを返す作用
*/
def tryInsertIntoMineStack(
prizes: Vector[GachaPrizeTableEntry[ItemStack]]
): Kleisli[F, Player, Vector[GachaPrizeTableEntry[ItemStack]]]

/**
* @param prizes プレイヤーに付与する[[GachaPrizeTableEntry]]のVector
* FIXME: 「記名する」というドメインロジックはシステムのより中核に近いところに移動したい…
* @return `prizes` の各アイテムを (必要ならば記名した上で、) プレイヤーのインベントリに挿入するか、
* それができなかった場合には地面にドロップする作用
*/
def insertIntoPlayerInventoryOrDrop(
prize: GachaPrize[ItemStack],
ownerName: Option[String]
): Kleisli[F, Player, GrantState]
prizes: Vector[GachaPrizeTableEntry[ItemStack]]
): Kleisli[F, Player, Unit]

final def grantGachaPrize(prize: GachaPrize[ItemStack]): Kleisli[F, Player, GrantState] =
final def grantGachaPrize(
prizes: Vector[GachaPrizeTableEntry[ItemStack]]
): Kleisli[F, Player, Vector[(GachaPrizeTableEntry[ItemStack], GrantState)]] =
Kleisli { player =>
for {
insertMineStackResult <- tryInsertIntoMineStack(prize)(player)
grantState <-
if (insertMineStackResult) {
F.pure(GrantState.GrantedMineStack)
} else {
insertIntoPlayerInventoryOrDrop(prize, Some(player.getName))(player)
}
} yield grantState
prizesNotInsertedIntoMineStack <- tryInsertIntoMineStack(prizes)(player)
_ <- insertIntoPlayerInventoryOrDrop(prizesNotInsertedIntoMineStack)(player)
} yield {
val prizesInsertedIntoMineStack = prizes.diff(prizesNotInsertedIntoMineStack)
prizesInsertedIntoMineStack.map(_ -> GrantState.GrantedMineStack) ++
prizesNotInsertedIntoMineStack.map(_ -> GrantState.GrantedInventoryOrDrop)
}
}

}
Loading

0 comments on commit c2fe54a

Please sign in to comment.