diff --git a/src/main/scala/me/arcanis/ffxivbis/service/database/Database.scala b/src/main/scala/me/arcanis/ffxivbis/service/database/Database.scala index 45969cc..19f5832 100644 --- a/src/main/scala/me/arcanis/ffxivbis/service/database/Database.scala +++ b/src/main/scala/me/arcanis/ffxivbis/service/database/Database.scala @@ -18,6 +18,7 @@ import me.arcanis.ffxivbis.service.database.impl.DatabaseImpl import me.arcanis.ffxivbis.storage.DatabaseProfile import scala.concurrent.{ExecutionContext, Future} +import scala.util.{Failure, Success} trait Database extends StrictLogging { @@ -38,6 +39,12 @@ trait Database extends StrictLogging { bis <- if (withBiS) profile.getPiecesBiS(partyId) else Future(Seq.empty) loot <- if (withLoot) profile.getPieces(partyId) else Future(Seq.empty) } yield Party(partyDescription, config, players, bis, loot) + + protected def run[T](fn: => Future[T])(onSuccess: T => Unit): Unit = + fn.onComplete { + case Success(value) => onSuccess(value) + case Failure(exception) => logger.error("exception during performing database request", exception) + } } object Database { diff --git a/src/main/scala/me/arcanis/ffxivbis/service/database/impl/DatabaseBiSHandler.scala b/src/main/scala/me/arcanis/ffxivbis/service/database/impl/DatabaseBiSHandler.scala index ba12d3a..ada5b4a 100644 --- a/src/main/scala/me/arcanis/ffxivbis/service/database/impl/DatabaseBiSHandler.scala +++ b/src/main/scala/me/arcanis/ffxivbis/service/database/impl/DatabaseBiSHandler.scala @@ -16,21 +16,22 @@ trait DatabaseBiSHandler { this: Database => def bisHandler: DatabaseMessage.Handler = { case AddPieceToBis(playerId, piece, client) => - profile.insertPieceBiS(playerId, piece).foreach(_ => client ! ()) + run(profile.insertPieceBiS(playerId, piece))(_ => client ! ()) Behaviors.same case GetBiS(partyId, maybePlayerId, client) => - getParty(partyId, withBiS = true, withLoot = false) - .map(filterParty(_, maybePlayerId)) - .foreach(client ! _) + run { + getParty(partyId, withBiS = true, withLoot = false) + .map(filterParty(_, maybePlayerId)) + }(client ! _) Behaviors.same case RemovePieceFromBiS(playerId, piece, client) => - profile.deletePieceBiS(playerId, piece).foreach(_ => client ! ()) + run(profile.deletePieceBiS(playerId, piece))(_ => client ! ()) Behaviors.same case RemovePiecesFromBiS(playerId, client) => - profile.deletePiecesBiS(playerId).foreach(_ => client ! ()) + run(profile.deletePiecesBiS(playerId))(_ => client ! ()) Behaviors.same } } diff --git a/src/main/scala/me/arcanis/ffxivbis/service/database/impl/DatabaseLootHandler.scala b/src/main/scala/me/arcanis/ffxivbis/service/database/impl/DatabaseLootHandler.scala index 3ae0b98..4752d0a 100644 --- a/src/main/scala/me/arcanis/ffxivbis/service/database/impl/DatabaseLootHandler.scala +++ b/src/main/scala/me/arcanis/ffxivbis/service/database/impl/DatabaseLootHandler.scala @@ -20,23 +20,25 @@ trait DatabaseLootHandler { this: Database => def lootHandler: DatabaseMessage.Handler = { case AddPieceTo(playerId, piece, isFreeLoot, client) => val loot = Loot(-1, piece, Instant.now, isFreeLoot) - profile.insertPiece(playerId, loot).foreach(_ => client ! ()) + run(profile.insertPiece(playerId, loot))(_ => client ! ()) Behaviors.same case GetLoot(partyId, maybePlayerId, client) => - getParty(partyId, withBiS = false, withLoot = true) - .map(filterParty(_, maybePlayerId)) - .foreach(client ! _) + run { + getParty(partyId, withBiS = false, withLoot = true) + .map(filterParty(_, maybePlayerId)) + }(client ! _) Behaviors.same case RemovePieceFrom(playerId, piece, client) => - profile.deletePiece(playerId, piece).foreach(_ => client ! ()) + run(profile.deletePiece(playerId, piece))(_ => client ! ()) Behaviors.same case SuggestLoot(partyId, piece, client) => - getParty(partyId, withBiS = true, withLoot = true) - .map(_.suggestLoot(piece)) - .foreach(client ! _) + run { + getParty(partyId, withBiS = true, withLoot = true) + .map(_.suggestLoot(piece)) + }(client ! _) Behaviors.same } } diff --git a/src/main/scala/me/arcanis/ffxivbis/service/database/impl/DatabasePartyHandler.scala b/src/main/scala/me/arcanis/ffxivbis/service/database/impl/DatabasePartyHandler.scala index fbbef7a..964c791 100644 --- a/src/main/scala/me/arcanis/ffxivbis/service/database/impl/DatabasePartyHandler.scala +++ b/src/main/scala/me/arcanis/ffxivbis/service/database/impl/DatabasePartyHandler.scala @@ -19,47 +19,48 @@ trait DatabasePartyHandler { this: Database => def partyHandler: DatabaseMessage.Handler = { case AddPlayer(player, client) => - profile.insertPlayer(player).foreach(_ => client ! ()) + run(profile.insertPlayer(player))(_ => client ! ()) Behaviors.same case GetParty(partyId, client) => - getParty(partyId, withBiS = true, withLoot = true).foreach(client ! _) + run(getParty(partyId, withBiS = true, withLoot = true))(client ! _) Behaviors.same case GetPartyDescription(partyId, client) => - profile.getPartyDescription(partyId).foreach(client ! _) + run(profile.getPartyDescription(partyId))(client ! _) Behaviors.same case GetPlayer(playerId, client) => - val player = profile - .getPlayerFull(playerId) - .flatMap { maybePlayerData => - Future.traverse(maybePlayerData.toSeq) { playerData => - for { - bis <- profile.getPiecesBiS(playerId) - loot <- profile.getPieces(playerId) - } yield Player( - playerData.id, - playerId.partyId, - playerId.job, - playerId.nick, - BiS(bis.map(_.piece)), - loot, - playerData.link, - playerData.priority - ) + run { + profile + .getPlayerFull(playerId) + .flatMap { maybePlayerData => + Future.traverse(maybePlayerData.toSeq) { playerData => + for { + bis <- profile.getPiecesBiS(playerId) + loot <- profile.getPieces(playerId) + } yield Player( + playerData.id, + playerId.partyId, + playerId.job, + playerId.nick, + BiS(bis.map(_.piece)), + loot, + playerData.link, + playerData.priority + ) + } } - } - .map(_.headOption) - player.foreach(client ! _) + .map(_.headOption) + }(client ! _) Behaviors.same case RemovePlayer(playerId, client) => - profile.deletePlayer(playerId).foreach(_ => client ! ()) + run(profile.deletePlayer(playerId))(_ => client ! ()) Behaviors.same case UpdateParty(description, client) => - profile.insertPartyDescription(description).foreach(_ => client ! ()) + run(profile.insertPartyDescription(description))(_ => client ! ()) Behaviors.same } } diff --git a/src/main/scala/me/arcanis/ffxivbis/service/database/impl/DatabaseUserHandler.scala b/src/main/scala/me/arcanis/ffxivbis/service/database/impl/DatabaseUserHandler.scala index d7a2a08..0e1caaa 100644 --- a/src/main/scala/me/arcanis/ffxivbis/service/database/impl/DatabaseUserHandler.scala +++ b/src/main/scala/me/arcanis/ffxivbis/service/database/impl/DatabaseUserHandler.scala @@ -17,23 +17,23 @@ trait DatabaseUserHandler { this: Database => def userHandler: DatabaseMessage.Handler = { case AddUser(user, isHashedPassword, client) => val toInsert = if (isHashedPassword) user else user.withHashedPassword - profile.insertUser(toInsert).foreach(_ => client ! ()) + run(profile.insertUser(toInsert))(_ => client ! ()) Behaviors.same case DeleteUser(partyId, username, client) => - profile.deleteUser(partyId, username).foreach(_ => client ! ()) + run(profile.deleteUser(partyId, username))(_ => client ! ()) Behaviors.same case Exists(partyId, client) => - profile.exists(partyId).foreach(client ! _) + run(profile.exists(partyId))(client ! _) Behaviors.same case GetUser(partyId, username, client) => - profile.getUser(partyId, username).foreach(client ! _) + run(profile.getUser(partyId, username))(client ! _) Behaviors.same case GetUsers(partyId, client) => - profile.getUsers(partyId).foreach(client ! _) + run(profile.getUsers(partyId))(client ! _) Behaviors.same } }