Skip to content

Commit

Permalink
fix: ガチャを引くのに異常に時間が掛かる不具合を修正
Browse files Browse the repository at this point in the history
  • Loading branch information
rito528 committed Sep 23, 2023
1 parent da96fb1 commit 04b4cc7
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ class BlockLineUpTriggerListener[
if (buildAssistData.line_up_minestack_flg == 1) {
mineStackAPI
.mineStackObjectList
.findBySignedItemStack(mainHandItem, player)
.findBySignedItemStacks(Vector(mainHandItem), player)
.unsafeRunSync()
.head
} else None

val maxBlockUsage = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ class TilingSkillTriggerListener[G[_]: ConcurrentEffect, F[
val minestackObjectToUse =
mineStackAPI
.mineStackObjectList
.findBySignedItemStack(offHandItem, player)
.findBySignedItemStacks(Vector(offHandItem), player)
.toIO
.unsafeRunSync()
.head
.filter(_ => buildAssistPlayerData.zs_minestack_flag)

val replaceableMaterials = Set(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class PlayerInventoryListener(
// 石炭とラピスラズリを適切に処理するため、typeとdurabilityを持つクラスを用意
case class ExchangeableMaterial(materialType: Material)

val requiredAmountPerTicket = Map(//todo: こうせきたりない
val requiredAmountPerTicket = Map( // todo: こうせきたりない
ExchangeableMaterial(Material.COAL_ORE) -> 128,
ExchangeableMaterial(Material.IRON_ORE) -> 64,
ExchangeableMaterial(Material.GOLD_ORE) -> 8,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.unchama.seichiassist.listener

import com.github.unchama.seichiassist.ManagedWorld._
import org.bukkit.Material
import org.bukkit.block.data.`type`.Slab
import org.bukkit.event.block.BlockPlaceEvent
import org.bukkit.event.{EventHandler, Listener}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.github.unchama.seichiassist.subsystems.gachaprize.domain.{
}
import com.github.unchama.seichiassist.subsystems.minestack.MineStackAPI
import com.github.unchama.seichiassist.util.InventoryOperations
import com.github.unchama.util.bukkit.ItemStackUtil
import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack

Expand All @@ -28,19 +29,12 @@ class BukkitGrantGachaPrize[F[_]: Sync: OnMinecraftServerThread](
Kleisli { player =>
for {
currentAutoMineStackState <- mineStackAPI.autoMineStack(player)
results <- prizes.traverse { gachaPrize =>
val itemStack = gachaPrize.itemStack
whenAOrElse(currentAutoMineStackState)(
mineStackAPI
.mineStackRepository
.tryIntoMineStack(player, itemStack, itemStack.getAmount)
.map(hasBeenStoredInMineStack => gachaPrize -> hasBeenStoredInMineStack),
gachaPrize -> false
)
}
} yield results.collect {
case (gachaPrize, result) if !result => gachaPrize
}
itemStacks <- Sync[F].delay(prizes.map(_.itemStack))
results <- whenAOrElse(currentAutoMineStackState)(
mineStackAPI.mineStackRepository.tryIntoMineStack(player, itemStacks),
itemStacks
)
} yield prizes.filter(prize => results.contains(prize.itemStack))
}

override def insertIntoPlayerInventoryOrDrop(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ package com.github.unchama.seichiassist.subsystems.gacha.domain

import cats.effect.Sync
import com.github.unchama.seichiassist.subsystems.gachaprize.domain.{
GachaPrizeTableEntry,
GachaPrizeId,
GachaPrizeTableEntry,
GachaProbability,
StaticGachaPrizeFactory
}
import com.github.unchama.generic.Cloneable
import com.github.unchama.seichiassist.subsystems.gachaprize.domain.GachaRarity.GachaRarity

import scala.annotation.tailrec
import scala.util.Random

class LotteryOfGachaItems[F[_]: Sync, ItemStack: Cloneable](
implicit staticGachaPrizeFactory: StaticGachaPrizeFactory[ItemStack]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,15 @@ object GachaRarity {
override def values: IndexedSeq[GachaRarity] = findValues

def of[ItemStack](gachaPrize: GachaPrizeTableEntry[ItemStack]): GachaRarity =
fromGachaProbability(gachaPrize.probability)

def fromGachaProbability(gachaProbability: GachaProbability): GachaRarity = {
GachaRarity
.values
.filter { rarity => rarity.probabilityUpperLimit.value > gachaPrize.probability.value }
.filter(rarity => rarity.probabilityUpperLimit.value > gachaProbability.value)
.minByOption(_.probabilityUpperLimit.value)
.getOrElse(GachaRingoOrExpBottle)
}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1097,37 +1097,44 @@ class BukkitMineStackObjectList[F[_]: Sync](
allMineStackGroups.map(_.filter { group => categoryOf(group) == category })
}

override def findBySignedItemStack(
itemStack: ItemStack,
override def findBySignedItemStacks(
itemStacks: Vector[ItemStack],
player: Player
): F[Option[MineStackObject[ItemStack]]] = for {
gachaPrizes <- gachaPrizeAPI.gachaPrizesWhenGachaEventsIsNotHolding
canBeSignedAsGachaPrize = gachaPrizeAPI.canBeSignedAsGachaPrize
foundGachaPrizeOpt = gachaPrizes.find { gachaPrize =>
if (gachaPrize.signOwner) {
gachaPrize
.materializeWithOwnerSignature(player.getName)(canBeSignedAsGachaPrize)
.isSimilar(itemStack)
} else {
gachaPrize.itemStack.isSimilar(itemStack)
): F[Vector[Option[MineStackObject[ItemStack]]]] = {
for {
gachaPrizes <- gachaPrizeAPI.gachaPrizesWhenGachaEventsIsNotHolding
canBeSignedAsGachaPrize = gachaPrizeAPI.canBeSignedAsGachaPrize
foundOptGachaPrizes = itemStacks.map { itemStack =>
gachaPrizes.find { gachaPrize =>
if (gachaPrize.signOwner) {
gachaPrize
.materializeWithOwnerSignature(player.getName)(canBeSignedAsGachaPrize)
.isSimilar(itemStack)
} else {
gachaPrize.itemStack.isSimilar(itemStack)
}
}
}
}
isGachaPrize = foundGachaPrizeOpt.nonEmpty
mineStackObjects <- allMineStackObjects
} yield {
// ItemStackのLoreはnullの可能性がある
val isSignedItemStack =
Option(itemStack.getItemMeta.getLore).exists(_.contains("所有者:"))
if (isGachaPrize && foundGachaPrizeOpt.get.signOwner) {
mineStackObjects.find { mineStackObject =>
foundGachaPrizeOpt.get.itemStack.isSimilar(mineStackObject.itemStack)
mineStackObjects <- allMineStackObjects
} yield {
foundOptGachaPrizes.zip(itemStacks).map {
case (gachaPrizeOpt, itemStack) =>
val isGachaPrize = gachaPrizeOpt.nonEmpty
// ItemStackのLoreはnullの可能性がある
val isSignedItemStack =
Option(itemStack.getItemMeta.getLore).exists(_.contains("所有者:"))
if (isGachaPrize && gachaPrizeOpt.get.signOwner) {
mineStackObjects.find { mineStackObject =>
gachaPrizeOpt.get.itemStack.isSimilar(mineStackObject.itemStack)
}
} else if (isSignedItemStack) {
// 所有者名が違うとガチャ景品として認識しないが、違ったらそもそも見つかっていない
// 記名が入っていないアイテムは収納できてしまうが仕様
None
} else {
mineStackObjects.find(_.itemStack.isSimilar(itemStack))
}
}
} else if (isSignedItemStack) {
// 所有者名が違うとガチャ景品として認識しないが、違ったらそもそも見つかっていない
// 記名が入っていないアイテムは収納できてしまうが仕様
None
} else {
mineStackObjects.find(_.itemStack.isSimilar(itemStack))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,31 @@ class BukkitMineStackRepository[F[_]: Sync](
)
}

override def tryIntoMineStack(player: Player, itemStack: ItemStack, amount: Int): F[Boolean] =
override def tryIntoMineStack(
player: Player,
itemStack: ItemStack,
amount: Int
): F[Boolean] = {
for {
foundMineStackObject <- mineStackObjectList.findBySignedItemStack(itemStack, player)
_ <- foundMineStackObject.traverse(addStackedAmountOf(player, _, amount))
} yield foundMineStackObject.isDefined
foundMineStackObject <- mineStackObjectList.findBySignedItemStacks(
Vector(itemStack),
player
)
_ <- foundMineStackObject.head.traverse(addStackedAmountOf(player, _, amount))
} yield foundMineStackObject.head.isDefined
}

override def tryIntoMineStack(
player: Player,
itemStacks: Vector[ItemStack]
): F[Vector[ItemStack]] = for {
mineStackObjects <- mineStackObjectList.findBySignedItemStacks(itemStacks, player)
_ <- mineStackObjects.traverse {
case Some(mineStackObject) =>
addStackedAmountOf(player, mineStackObject, mineStackObject.itemStack.getAmount)
case _ => Sync[F].unit
}
} yield itemStacks.diff(mineStackObjects.collect {
case Some(mineStackObject) => mineStackObject.itemStack
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,10 @@ trait MineStackRepository[F[_], Player, ItemStack] {
*/
def tryIntoMineStack(player: Player, itemStack: ItemStack, amount: Int): F[Boolean]

/**
* [[Player]]のMineStackリポジトリに[[ItemStack]]を格納することを試みます。
* @return 格納できなかった[[ItemStack]]のリストを返す作用
*/
def tryIntoMineStack(player: Player, itemStacks: Vector[ItemStack]): F[Vector[ItemStack]]

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ trait MineStackObjectList[F[_], ItemStack, Player] {
* @param itemStack 記名することのできるアイテムは、既に記名されていることを想定している
* @return [[ItemStack]]から[[MineStackObject]]を取得しようとする作用
*/
def findBySignedItemStack(
itemStack: ItemStack,
def findBySignedItemStacks(
itemStack: Vector[ItemStack],
player: Player
): F[Option[MineStackObject[ItemStack]]]
): F[Vector[Option[MineStackObject[ItemStack]]]]

protected implicit val F: Functor[F]

Expand Down

0 comments on commit 04b4cc7

Please sign in to comment.