Skip to content

Commit

Permalink
Merge pull request #1324 from GiganticMinecraft/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
KisaragiEffective authored Jan 20, 2022
2 parents 1c33932 + 5dc67fa commit 85d9563
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 21 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import java.io._
ThisBuild / scalaVersion := "2.13.1"
// ThisBuild / version はGitHub Actionsによって自動更新される。
// 次の行は ThisBuild / version := "(\d*)" の形式でなければならない。
ThisBuild / version := "24"
ThisBuild / version := "25"
ThisBuild / organization := "click.seichi"
ThisBuild / description := "ギガンティック☆整地鯖の独自要素を司るプラグイン"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use seichiassist;

-- MySQLのスロークエリを引いた結果遅かったSELECTのクエリに対してINDEXを貼るようにする。
ALTER TABLE playerdata ADD INDEX index_playerdata_playtick(playtick);
15 changes: 15 additions & 0 deletions src/main/scala/com/github/unchama/generic/MapExtra.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,19 @@ object MapExtra {
}
}
.toMap

/**
*
* @param map 元となるMap
* @param base キーの集合
* @param default 埋める値
* @tparam K キー
* @tparam V
* @return
*/
def fillOnBaseSet[K, V](map: Map[K, V], base: Set[K], default: V): Map[K, V] = {
val keys = map.keys.toSet
require(keys.subsetOf(base))
map ++ (base -- keys).map(a => a -> default)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class EntityListener(implicit effectEnvironment: EffectEnvironment,
import ManagedWorld._

level >= SeichiAssist.seichiAssistConfig.getMultipleIDBlockBreaklevel &&
(player.getWorld.isSeichiSkillAllowed || playerData.settings.multipleidbreakflag)
(player.getWorld.isSeichiSkillAllowed && playerData.settings.multipleidbreakflag)
}

import com.github.unchama.seichiassist.data.syntax._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class PlayerBlockBreakListener(implicit effectEnvironment: EffectEnvironment,
val isMultiTypeBreakingSkillEnabled = {
import com.github.unchama.seichiassist.ManagedWorld._
playerLevel >= SeichiAssist.seichiAssistConfig.getMultipleIDBlockBreaklevel &&
(player.getWorld.isSeichiSkillAllowed || playerData.settings.multipleidbreakflag)
(player.getWorld.isSeichiSkillAllowed && playerData.settings.multipleidbreakflag)
}

val totalBreakRangeVolume = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ object FirstPage extends Menu {
val buttonLore = List(
s"$GRAY・整地ワールド間を移動するとき",
s"$GRAY・拠点を建築するとき",
s"に使います",
s"$GRAY に使います",
s"$DARK_RED${UNDERLINE}クリックするとワープします",
s"${DARK_GRAY}command=>[/spawn]"
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package com.github.unchama.seichiassist.subsystems.discordnotification

import cats.effect.{ContextShift, Sync}
import com.github.unchama.seichiassist.SeichiAssist
import cats.effect.{ContextShift, LiftIO, Sync}
import com.github.unchama.seichiassist.meta.subsystem.Subsystem
import com.github.unchama.seichiassist.subsystems.discordnotification.infrastructure.WebhookDiscordNotificationSender
import com.github.unchama.seichiassist.subsystems.discordnotification.infrastructure.{DefaultDiscordNotificationSender, WebhookDiscordNotificationSender}
import io.chrisdavenport.log4cats.Logger

trait System[F[_]] extends Subsystem[F] {
implicit val globalNotification: DiscordNotificationAPI[F]
}

object System {
def wired[F[_] : Sync : ContextShift](configuration: SystemConfiguration): System[F] = new System[F] {
implicit override val globalNotification: DiscordNotificationAPI[F] =
new WebhookDiscordNotificationSender[F](configuration.webhookUrl)
def wired[F[_] : Sync : ContextShift : Logger : LiftIO](configuration: SystemConfiguration): System[F] = new System[F] {
implicit override val globalNotification: DiscordNotificationAPI[F] = {
WebhookDiscordNotificationSender.tryCreate(configuration.webhookUrl).getOrElse(new DefaultDiscordNotificationSender)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.github.unchama.seichiassist.subsystems.discordnotification.infrastructure

import cats.effect.LiftIO
import com.github.unchama.seichiassist.SeichiAssist
import com.github.unchama.seichiassist.subsystems.discordnotification.DiscordNotificationAPI
import io.chrisdavenport.log4cats.Logger

/**
* この実装は[[send]]が呼ばれるたびに警告をロガーに流す以外は何もしない。
*/
final class DefaultDiscordNotificationSender[F[_]: Logger: LiftIO] extends DiscordNotificationAPI[F] {
override def send(message: String): F[Unit] = {
SeichiAssist.instance.loggerF.warn("Discordへの送信が試みられましたが、URLが無効、もしくは与えられていません。コンフィグを確認してください。").to
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package com.github.unchama.seichiassist.subsystems.discordnotification.infrastru

import cats.effect.{ContextShift, Sync}
import com.github.unchama.seichiassist.subsystems.discordnotification.DiscordNotificationAPI
import io.chrisdavenport.log4cats.Logger
import org.bukkit.Bukkit

import java.io.IOException
import java.net.{HttpURLConnection, URL}
import java.net.{HttpURLConnection, MalformedURLException, URL}
import java.nio.charset.StandardCharsets
import scala.util.Using
import scala.util.chaining.scalaUtilChainingOps

class WebhookDiscordNotificationSender[F[_]: Sync: ContextShift](webhookURL: String) extends DiscordNotificationAPI[F] {
class WebhookDiscordNotificationSender[F[_]: Sync: ContextShift] private(webhookURL: String) extends DiscordNotificationAPI[F] {
assert(
webhookURL.nonEmpty,
"GlobalNotificationSenderのURLに空文字列が指定されました。コンフィグを確認してください。"
Expand Down Expand Up @@ -49,3 +50,20 @@ class WebhookDiscordNotificationSender[F[_]: Sync: ContextShift](webhookURL: Str
}
} yield ()
}

object WebhookDiscordNotificationSender {
/**
* [[WebhookDiscordNotificationSender]] を作成することを試みる。
* @param webhookURL Discordに送信されるwebhookのURL
* @tparam F 文脈
* @return 初期化に成功した場合はSome、初期化中に特定の例外が送出された場合はNone。マスクされない例外が送出されたときは、再送出する。
*/
def tryCreate[F[_]: Sync: ContextShift: Logger](webhookURL: String): Option[WebhookDiscordNotificationSender[F]] = {
try {
Some(new WebhookDiscordNotificationSender[F](webhookURL))
} catch {
case _: MalformedURLException => None
case _: AssertionError => None
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.unchama.seichiassist.subsystems.present.infrastructure

import cats.Applicative
import cats.effect.Sync
import com.github.unchama.generic.MapExtra
import com.github.unchama.seichiassist.subsystems.present.domain.OperationResult.DeleteResult
import com.github.unchama.seichiassist.subsystems.present.domain.{GrantRejectReason, PaginationRejectReason, PresentClaimingState, PresentPersistence, RevokeWarning}
import eu.timepit.refined.api.Refined
Expand Down Expand Up @@ -167,7 +168,7 @@ class JdbcBackedPresentPersistence[F[_] : Sync] extends PresentPersistence[F, It
.apply()
}

Right(filledEntries(associatedEntries, idSliceWithPagination).toList)
Right(MapExtra.fillOnBaseSet(associatedEntries.toMap, idSliceWithPagination, PresentClaimingState.Unavailable).toList)
}
}
}
Expand All @@ -185,7 +186,7 @@ class JdbcBackedPresentPersistence[F[_] : Sync] extends PresentPersistence[F, It
.apply()
}

filledEntries(associatedEntries, validPresentIDs).toMap
MapExtra.fillOnBaseSet(associatedEntries.toMap, validPresentIDs.toSet, PresentClaimingState.Unavailable)
}
}

Expand Down Expand Up @@ -223,11 +224,6 @@ class JdbcBackedPresentPersistence[F[_] : Sync] extends PresentPersistence[F, It
ItemStackBlobProxy.blobToItemStack(rs.string("itemstack"))
}

private def filledEntries(knownState: List[(PresentID, PresentClaimingState)], validGlobalId: Iterable[PresentID]): Iterable[(PresentID, PresentClaimingState)] = {
val globalEntries = validGlobalId.map(id => (id, PresentClaimingState.Unavailable))
(globalEntries ++ knownState)
}

private def computeValidPresentCount: F[Long] = Sync[F].delay {
DB.readOnly { implicit session =>
sql"""SELECT COUNT(*) AS c FROM present"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import com.github.unchama.seichiassist.subsystems.seasonalevents.Util.{dateRange
import java.time.LocalDate

object Valentine {
val EVENT_YEAR: Int = 2018
val EVENT_YEAR: Int = 2022
val START_DATE: LocalDate = LocalDate.of(EVENT_YEAR, 2, 13)
val END_DATE: LocalDate = LocalDate.of(EVENT_YEAR, 2, 27)
val itemDropRate: Double = validateItemDropRate(0.3)
val blogArticleUrl: String = validateUrl(s"https://www.seichi.network/post/valentine$EVENT_YEAR")

def isInEvent: Boolean = dateRangeAsSequence(START_DATE, END_DATE).contains(LocalDate.now())
}
}
39 changes: 39 additions & 0 deletions src/test/scala/com/github/unchama/generic/MapExtraSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.github.unchama.generic

import org.scalatest.wordspec.AnyWordSpec

class MapExtraSpec extends AnyWordSpec {
"fillOnBaseSet" should {
"return empty Map" in {
assert(MapExtra.fillOnBaseSet[String, Int](Map(), Set(), 1).isEmpty)
}

"fail if map's key is not sub-set of set" in {
assertThrows[IllegalArgumentException] {
MapExtra.fillOnBaseSet[String, Int](Map("a" -> 2), Set(), 1)
}
}

"return filled Map" in {
assert(
MapExtra.fillOnBaseSet(
Map("a" -> 1, "b" -> 2, "c" -> 3),
Set("a", "b", "c", "d"),
4
) == Map("a" -> 1, "b" -> 2, "c" -> 3, "d" -> 4)
)
}

"return filled Map when given Map is empty" in {
assert(
MapExtra.fillOnBaseSet(
Map(),
Set("A", "B", "C", "D"),
42
) == Map("A" -> 42, "B" -> 42, "C" -> 42, "D" -> 42)
)
}


}
}

0 comments on commit 85d9563

Please sign in to comment.