From 693da1397cbcad84c0e73efc8b9b20c8ca598587 Mon Sep 17 00:00:00 2001 From: outductor Date: Tue, 27 Jun 2023 23:07:54 +0900 Subject: [PATCH] upstream --- .../unchama/seichiassist/menus/VoteMenu.scala | 7 +----- .../vote/subsystems/fairy/FairyAPI.scala | 2 +- .../vote/subsystems/fairy/System.scala | 2 +- .../fairy/domain/FairyPersistence.scala | 2 +- .../infrastructure/JdbcFairyPersistence.scala | 24 ++++++++++--------- 5 files changed, 17 insertions(+), 20 deletions(-) diff --git a/src/main/scala/com/github/unchama/seichiassist/menus/VoteMenu.scala b/src/main/scala/com/github/unchama/seichiassist/menus/VoteMenu.scala index a88db368b9..3752fcf9fe 100644 --- a/src/main/scala/com/github/unchama/seichiassist/menus/VoteMenu.scala +++ b/src/main/scala/com/github/unchama/seichiassist/menus/VoteMenu.scala @@ -383,12 +383,7 @@ object VoteMenu extends Menu { s"$RESET${DARK_GRAY}召喚されたらラッキーだよ!" ) val topFourRankingLore = - List( - topFourRanking.headOption, - topFourRanking.lift(1), - topFourRanking.lift(2), - topFourRanking.lift(3) - ).flatMap(_.flatten).flatMap { rankData => + topFourRanking.flatMap { rankData => List( s"${GRAY}たくさんくれたニンゲン第${rankData.rank}位!", s"${GRAY}なまえ:${rankData.playerName} りんご:${rankData.consumed.amount}個" diff --git a/src/main/scala/com/github/unchama/seichiassist/subsystems/vote/subsystems/fairy/FairyAPI.scala b/src/main/scala/com/github/unchama/seichiassist/subsystems/vote/subsystems/fairy/FairyAPI.scala index 1a037693bb..8127d7192f 100644 --- a/src/main/scala/com/github/unchama/seichiassist/subsystems/vote/subsystems/fairy/FairyAPI.scala +++ b/src/main/scala/com/github/unchama/seichiassist/subsystems/vote/subsystems/fairy/FairyAPI.scala @@ -55,7 +55,7 @@ trait FairyReadAPI[F[_], G[_], Player] { /** * @return 妖精に食べさせたりんごの量の上位`top`件を返す作用 */ - def rankingByMostConsumedApple(top: Int): F[Vector[Option[AppleConsumeAmountRank]]] + def rankingByMostConsumedApple(top: Int): F[Vector[AppleConsumeAmountRank]] /** * @return 妖精が食べたりんごの合計数を返す作用 diff --git a/src/main/scala/com/github/unchama/seichiassist/subsystems/vote/subsystems/fairy/System.scala b/src/main/scala/com/github/unchama/seichiassist/subsystems/vote/subsystems/fairy/System.scala index f02daa6925..72d03d92e0 100644 --- a/src/main/scala/com/github/unchama/seichiassist/subsystems/vote/subsystems/fairy/System.scala +++ b/src/main/scala/com/github/unchama/seichiassist/subsystems/vote/subsystems/fairy/System.scala @@ -105,7 +105,7 @@ object System { override def rankingByMostConsumedApple( top: Int - ): IO[Vector[Option[AppleConsumeAmountRank]]] = + ): IO[Vector[AppleConsumeAmountRank]] = persistence.fetchMostConsumedApplePlayersByFairy(top) override def totalConsumedApple: IO[AppleAmount] = diff --git a/src/main/scala/com/github/unchama/seichiassist/subsystems/vote/subsystems/fairy/domain/FairyPersistence.scala b/src/main/scala/com/github/unchama/seichiassist/subsystems/vote/subsystems/fairy/domain/FairyPersistence.scala index 9331e34689..60de8f9490 100644 --- a/src/main/scala/com/github/unchama/seichiassist/subsystems/vote/subsystems/fairy/domain/FairyPersistence.scala +++ b/src/main/scala/com/github/unchama/seichiassist/subsystems/vote/subsystems/fairy/domain/FairyPersistence.scala @@ -91,7 +91,7 @@ trait FairyPersistence[F[_]] { * @param top 最上位から何番目まで取得するか件数を指定する。0以下であってはならない。 * @return 指定した件数が要素数となり、その並びが消費量の降順になっているような順序つきのコレクションを返す作用。 */ - def fetchMostConsumedApplePlayersByFairy(top: Int): F[Vector[Option[AppleConsumeAmountRank]]] + def fetchMostConsumedApplePlayersByFairy(top: Int): F[Vector[AppleConsumeAmountRank]] /** * @return 妖精が今まで食べたりんごの合計数を返す作用 diff --git a/src/main/scala/com/github/unchama/seichiassist/subsystems/vote/subsystems/fairy/infrastructure/JdbcFairyPersistence.scala b/src/main/scala/com/github/unchama/seichiassist/subsystems/vote/subsystems/fairy/infrastructure/JdbcFairyPersistence.scala index 1e86fc4584..f7f39eb8f9 100644 --- a/src/main/scala/com/github/unchama/seichiassist/subsystems/vote/subsystems/fairy/infrastructure/JdbcFairyPersistence.scala +++ b/src/main/scala/com/github/unchama/seichiassist/subsystems/vote/subsystems/fairy/infrastructure/JdbcFairyPersistence.scala @@ -161,11 +161,12 @@ class JdbcFairyPersistence[F[_]: Sync] extends FairyPersistence[F] { ): F[Option[AppleConsumeAmountRank]] = Sync[F].delay { DB.readOnly { implicit session => - sql"""SELECT vote_fairy.uuid AS uuid,name,given_apple_amount,COUNT(*) AS rank + sql"""SELECT vote_fairy.uuid AS uuid, name, given_apple_amount, + | RANK() OVER(ORDER BY given_apple_amount DESC) AS rank | FROM vote_fairy | INNER JOIN playerdata | ON (playerdata.uuid = vote_fairy.uuid) - | ORDER BY rank DESC;""" + | ORDER BY rank""" .stripMargin .map(rs => rs.string("uuid") -> AppleConsumeAmountRank( @@ -183,19 +184,20 @@ class JdbcFairyPersistence[F[_]: Sync] extends FairyPersistence[F] { override def fetchMostConsumedApplePlayersByFairy( top: Int - ): F[Vector[Option[AppleConsumeAmountRank]]] = + ): F[Vector[AppleConsumeAmountRank]] = Sync[F].delay { DB.readOnly { implicit session => - sql"""SELECT name,given_apple_amount,COUNT(*) AS rank FROM vote_fairy - | INNER JOIN playerdata ON (vote_fairy.uuid = playerdata.uuid) - | ORDER BY rank DESC LIMIT $top;""" + sql"""SELECT name, given_apple_amount, RANK() OVER(ORDER BY given_apple_amount DESC) AS rank FROM vote_fairy + | INNER JOIN playerdata ON (vote_fairy.uuid = playerdata.uuid) + | ORDER BY rank + | LIMIT $top;""" .stripMargin .map { rs => - for { - name <- rs.stringOpt("name") - rank <- rs.intOpt("rank") - givenAppleAmount <- rs.intOpt("given_apple_amount") - } yield AppleConsumeAmountRank(name, rank, AppleAmount(givenAppleAmount)) + val name = rs.string("name") + val rank = rs.int("rank") + val givenAppleAmount = rs.int("given_apple_amount") + + AppleConsumeAmountRank(name, rank, AppleAmount(givenAppleAmount)) } .toList() .apply()