18 Commits

Author SHA1 Message Date
0ed9e92441 release 0.9.12 2021-12-19 22:44:39 +03:00
1866a1bb12 endwalker support
* added sge and rpr
* changed way to define savage gear
* libraries update
2021-12-19 22:39:20 +03:00
08f7f4571e Release 0.9.11
change loggin, more tests, cosmetic changes
2020-12-12 20:15:14 +03:00
d9cbb6cf00 Release 0.9.10 2020-12-04 12:08:34 +03:00
df8e09f02c fix column names 2020-12-04 12:08:08 +03:00
df1f28c7ef Release 0.9.9 2020-12-04 11:47:36 +03:00
8d516cdb15 use typed actors (#12)
* initial typed actor impl
* fix sending response on ask
2020-12-04 11:45:23 +03:00
2e16a8c1fa multi item support 2020-12-03 03:35:06 +03:00
25b05aa289 identify url in runtime 2020-11-26 23:43:14 +03:00
534ed98459 etro support 2020-11-26 23:27:33 +03:00
0171b229a1 demo with optional loot support 2020-03-17 02:13:21 +03:00
10c107d2c2 crafted items support (#5) 2020-03-13 03:36:25 +03:00
16ce0bf61c add support of party alias 2020-03-09 01:48:24 +03:00
1e6064e081 Feature/timestamp support (#8)
* initial timestamp support

* compilation & test fixes

* do not take default argument
2020-03-08 03:48:25 +03:00
92e2c1d383 less conversions 2020-03-08 02:39:54 +03:00
5eae1d46a2 Release 0.9.8 2020-02-26 01:17:33 +03:00
eb24019965 another iteration with parsing xivapi 2020-02-26 01:16:57 +03:00
173ea9079f release 0.9.7 2020-02-25 03:18:51 +03:00
115 changed files with 2227 additions and 1188 deletions

View File

@ -1,6 +1,6 @@
name := "ffxivbis" name := "ffxivbis"
scalaVersion := "2.13.1" scalaVersion := "2.13.6"
scalacOptions ++= Seq("-deprecation", "-feature") scalacOptions ++= Seq("-deprecation", "-feature")

View File

@ -1,19 +1,33 @@
libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.2.3" val AkkaVersion = "2.6.17"
libraryDependencies += "com.typesafe.scala-logging" %% "scala-logging" % "3.9.2" val AkkaHttpVersion = "10.2.7"
val SlickVersion = "3.3.3"
libraryDependencies += "com.typesafe.akka" %% "akka-http" % "10.1.10" libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.2.9"
libraryDependencies += "com.typesafe.akka" %% "akka-http-spray-json" % "10.1.10" libraryDependencies += "com.typesafe.scala-logging" %% "scala-logging" % "3.9.4"
libraryDependencies += "com.typesafe.akka" %% "akka-stream" % "2.5.23"
libraryDependencies += "com.github.swagger-akka-http" %% "swagger-akka-http" % "2.0.4" libraryDependencies += "com.typesafe.akka" %% "akka-http" % AkkaHttpVersion
libraryDependencies += "com.typesafe.akka" %% "akka-http-spray-json" % AkkaHttpVersion
libraryDependencies += "com.typesafe.akka" %% "akka-actor-typed" % AkkaVersion
libraryDependencies += "com.typesafe.akka" %% "akka-stream" % AkkaVersion
libraryDependencies += "com.github.swagger-akka-http" %% "swagger-akka-http" % "2.6.0"
libraryDependencies += "javax.ws.rs" % "javax.ws.rs-api" % "2.1.1" libraryDependencies += "javax.ws.rs" % "javax.ws.rs-api" % "2.1.1"
libraryDependencies += "io.spray" %% "spray-json" % "1.3.5" libraryDependencies += "io.spray" %% "spray-json" % "1.3.6"
libraryDependencies += "com.lihaoyi" %% "scalatags" % "0.7.0" libraryDependencies += "com.lihaoyi" %% "scalatags" % "0.9.2"
libraryDependencies += "com.typesafe.slick" %% "slick" % "3.3.2" libraryDependencies += "com.typesafe.slick" %% "slick" % SlickVersion
libraryDependencies += "com.typesafe.slick" %% "slick-hikaricp" % "3.3.2" libraryDependencies += "com.typesafe.slick" %% "slick-hikaricp" % SlickVersion
libraryDependencies += "org.flywaydb" % "flyway-core" % "6.0.6" libraryDependencies += "org.flywaydb" % "flyway-core" % "8.2.2"
libraryDependencies += "org.xerial" % "sqlite-jdbc" % "3.28.0" libraryDependencies += "org.xerial" % "sqlite-jdbc" % "3.36.0.3"
libraryDependencies += "org.postgresql" % "postgresql" % "9.3-1104-jdbc4" libraryDependencies += "org.postgresql" % "postgresql" % "42.3.1"
libraryDependencies += "org.mindrot" % "jbcrypt" % "0.3m" libraryDependencies += "org.mindrot" % "jbcrypt" % "0.4"
// testing
libraryDependencies += "org.scalactic" %% "scalactic" % "3.2.10" % "test"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.10" % "test"
libraryDependencies += "com.typesafe.akka" %% "akka-actor-testkit-typed" % AkkaVersion % "test"
libraryDependencies += "com.typesafe.akka" %% "akka-stream-testkit" % AkkaVersion % "test"
libraryDependencies += "com.typesafe.akka" %% "akka-http-testkit" % AkkaHttpVersion % "test"

View File

@ -0,0 +1,5 @@
create table parties (
player_id bigserial unique,
party_name text not null,
party_alias text);
create unique index parties_party_name_idx on parties(party_name);

View File

@ -0,0 +1,17 @@
-- loot
alter table loot add column piece_type text;
update loot set piece_type = 'Tome' where is_tome = 1;
update loot set piece_type = 'Savage' where is_tome = 0;
alter table loot alter column piece_type set not null;
alter table loot drop column is_tome;
-- bis
alter table bis add column piece_type text;
update bis set piece_type = 'Tome' where is_tome = 1;
update bis set piece_type = 'Savage' where is_tome = 0;
alter table bis alter column piece_type set not null;
alter table bis drop column is_tome;

View File

@ -0,0 +1 @@
alter table loot add column is_free_loot integer not null default 0;

View File

@ -0,0 +1,2 @@
drop index bis_piece_player_id_idx;
create index bis_piece_type_player_id_idx on bis(player_id, piece, piece_type);

View File

@ -0,0 +1 @@
alter table parties rename column player_id to party_id;

View File

@ -0,0 +1,5 @@
create table parties (
player_id integer primary key autoincrement,
party_name text not null,
party_alias text);
create unique index parties_party_name_idx on parties(party_name);

View File

@ -0,0 +1,42 @@
-- loot
alter table loot add column piece_type text;
update loot set piece_type = 'Tome' where is_tome = 1;
update loot set piece_type = 'Savage' where is_tome = 0;
create table loot_new (
loot_id integer primary key autoincrement,
player_id integer not null,
created integer not null,
piece text not null,
piece_type text not null,
job text not null,
foreign key (player_id) references players(player_id) on delete cascade);
insert into loot_new select loot_id, player_id, created, piece, piece_type, job from loot;
drop index loot_owner_idx;
drop table loot;
alter table loot_new rename to loot;
create index loot_owner_idx on loot(player_id);
-- bis
alter table bis add column piece_type text;
update bis set piece_type = 'Tome' where is_tome = 1;
update bis set piece_type = 'Savage' where is_tome = 0;
create table bis_new (
player_id integer not null,
created integer not null,
piece text not null,
piece_type text not null,
job text not null,
foreign key (player_id) references players(player_id) on delete cascade);
insert into bis_new select player_id, created, piece, piece_type, job from bis;
drop index bis_piece_player_id_idx;
drop table bis;
alter table bis_new rename to bis;
create unique index bis_piece_player_id_idx on bis(player_id, piece);

View File

@ -0,0 +1,20 @@
alter table loot add column is_free_loot integer;
update loot set is_free_loot = 0;
create table loot_new (
loot_id integer primary key autoincrement,
player_id integer not null,
created integer not null,
piece text not null,
piece_type text not null,
job text not null,
is_free_loot integer not null,
foreign key (player_id) references players(player_id) on delete cascade);
insert into loot_new select loot_id, player_id, created, piece, piece_type, job, is_free_loot from loot;
drop index loot_owner_idx;
drop table loot;
alter table loot_new rename to loot;
create index loot_owner_idx on loot(player_id);

View File

@ -0,0 +1,2 @@
drop index bis_piece_player_id_idx;
create index bis_piece_type_player_id_idx on bis(player_id, piece, piece_type);

View File

@ -0,0 +1,11 @@
create table parties_new (
party_id integer primary key autoincrement,
party_name text not null,
party_alias text);
insert into parties_new select player_id, party_name, party_alias from parties;
drop index parties_party_name_idx;
drop table parties;
alter table parties_new rename to parties;
create unique index parties_party_name_idx on parties(party_name);

View File

@ -8,9 +8,10 @@
</root> </root>
<logger name="me.arcanis.ffxivbis" level="DEBUG" /> <logger name="me.arcanis.ffxivbis" level="DEBUG" />
<logger name="http" level="DEBUG"> <logger name="http" level="DEBUG" additivity="false">
<appender-ref ref="http" /> <appender-ref ref="http" />
</logger> </logger>
<logger name="slick" level="INFO" /> <logger name="slick" level="INFO" />
<logger name="org.flywaydb.core.internal" level="INFO" />
</configuration> </configuration>

View File

@ -1,7 +1,5 @@
me.arcanis.ffxivbis { me.arcanis.ffxivbis {
ariyala { bis-provider {
# ariyala base url, string, required
ariyala-url = "https://ffxiv.ariyala.com"
# xivapi base url, string, required # xivapi base url, string, required
xivapi-url = "https://xivapi.com" xivapi-url = "https://xivapi.com"
# xivapi developer key, string, optional # xivapi developer key, string, optional
@ -56,12 +54,6 @@ me.arcanis.ffxivbis {
port = 8000 port = 8000
# hostname to use in docs, if not set host:port will be used # hostname to use in docs, if not set host:port will be used
#hostname = "127.0.0.1:8000" #hostname = "127.0.0.1:8000"
# rate limits
limits {
intetval = 1m
max-count = 60
}
} }
default-dispatcher { default-dispatcher {

View File

@ -8,45 +8,61 @@
*/ */
package me.arcanis.ffxivbis package me.arcanis.ffxivbis
import akka.actor.{Actor, Props} import akka.actor.typed.{Behavior, PostStop, Signal}
import akka.actor.typed.scaladsl.{AbstractBehavior, ActorContext, Behaviors}
import akka.http.scaladsl.Http import akka.http.scaladsl.Http
import akka.stream.ActorMaterializer import akka.http.scaladsl.server.Route
import akka.stream.Materializer
import com.typesafe.scalalogging.StrictLogging import com.typesafe.scalalogging.StrictLogging
import me.arcanis.ffxivbis.http.RootEndpoint import me.arcanis.ffxivbis.http.RootEndpoint
import me.arcanis.ffxivbis.service.impl.DatabaseImpl import me.arcanis.ffxivbis.service.bis.BisProvider
import me.arcanis.ffxivbis.service.{Ariyala, PartyService} import me.arcanis.ffxivbis.service.{Database, PartyService}
import me.arcanis.ffxivbis.storage.Migration import me.arcanis.ffxivbis.storage.Migration
import scala.concurrent.duration.Duration import scala.concurrent.ExecutionContext
import scala.concurrent.{Await, ExecutionContext}
import scala.util.{Failure, Success} import scala.util.{Failure, Success}
class Application extends Actor with StrictLogging { class Application(context: ActorContext[Nothing])
implicit private val executionContext: ExecutionContext = context.system.dispatcher extends AbstractBehavior[Nothing](context) with StrictLogging {
implicit private val materializer: ActorMaterializer = ActorMaterializer()
private val config = context.system.settings.config logger.info("root supervisor started")
private val host = config.getString("me.arcanis.ffxivbis.web.host") startApplication()
private val port = config.getInt("me.arcanis.ffxivbis.web.port")
override def receive: Receive = Actor.emptyBehavior override def onMessage(msg: Nothing): Behavior[Nothing] = Behaviors.unhandled
Migration(config).onComplete { override def onSignal: PartialFunction[Signal, Behavior[Nothing]] = {
case Success(_) => case PostStop =>
val ariyala = context.system.actorOf(Ariyala.props, "ariyala") logger.info("root supervisor stopped")
val storage = context.system.actorOf(DatabaseImpl.props, "storage") Behaviors.same
val party = context.system.actorOf(PartyService.props(storage), "party") }
val http = new RootEndpoint(context.system, party, ariyala)
logger.info(s"start server at $host:$port") private def startApplication(): Unit = {
val bind = Http()(context.system).bindAndHandle(http.route, host, port) val config = context.system.settings.config
Await.result(context.system.whenTerminated, Duration.Inf) val host = config.getString("me.arcanis.ffxivbis.web.host")
bind.foreach(_.unbind()) val port = config.getInt("me.arcanis.ffxivbis.web.port")
case Failure(exception) => throw exception implicit val executionContext: ExecutionContext = context.system.executionContext
implicit val materializer: Materializer = Materializer(context)
Migration(config) match {
case Success(_) =>
val bisProvider = context.spawn(BisProvider(), "bis-provider")
val storage = context.spawn(Database(), "storage")
val party = context.spawn(PartyService(storage), "party")
val http = new RootEndpoint(context.system, party, bisProvider)
val flow = Route.toFlow(http.route)(context.system)
Http(context.system).newServerAt(host, port).bindFlow(flow)
case Failure(exception) =>
logger.error("exception during migration", exception)
context.system.terminate()
}
} }
} }
object Application { object Application {
def props: Props = Props(new Application)
def apply(): Behavior[Nothing] =
Behaviors.setup[Nothing](context => new Application(context))
} }

View File

@ -8,13 +8,13 @@
*/ */
package me.arcanis.ffxivbis package me.arcanis.ffxivbis
import akka.actor.ActorSystem import akka.actor.typed.ActorSystem
import com.typesafe.config.ConfigFactory import com.typesafe.config.ConfigFactory
object ffxivbis { object ffxivbis {
def main(args: Array[String]): Unit = { def main(args: Array[String]): Unit = {
val config = ConfigFactory.load() val config = ConfigFactory.load()
val actorSystem = ActorSystem("ffxivbis", config) ActorSystem[Nothing](Application(), "ffxivbis", config)
actorSystem.actorOf(Application.props, "ffxivbis")
} }
} }

View File

@ -1,24 +0,0 @@
/*
* Copyright (c) 2019 Evgeniy Alekseev.
*
* This file is part of ffxivbis
* (see https://github.com/arcan1s/ffxivbis).
*
* License: 3-clause BSD, see https://opensource.org/licenses/BSD-3-Clause
*/
package me.arcanis.ffxivbis.http
import akka.actor.ActorRef
import akka.pattern.ask
import akka.util.Timeout
import me.arcanis.ffxivbis.models.{BiS, Job}
import me.arcanis.ffxivbis.service.Ariyala
import scala.concurrent.{ExecutionContext, Future}
class AriyalaHelper(ariyala: ActorRef) {
def downloadBiS(link: String, job: Job.Job)
(implicit executionContext: ExecutionContext, timeout: Timeout): Future[BiS] =
(ariyala ? Ariyala.GetBiS(link, job)).mapTo[BiS]
}

View File

@ -8,22 +8,22 @@
*/ */
package me.arcanis.ffxivbis.http package me.arcanis.ffxivbis.http
import akka.actor.ActorRef import akka.actor.typed.scaladsl.AskPattern.Askable
import akka.actor.typed.{ActorRef, Scheduler}
import akka.http.scaladsl.model.headers._ import akka.http.scaladsl.model.headers._
import akka.http.scaladsl.server.AuthenticationFailedRejection._ import akka.http.scaladsl.server.AuthenticationFailedRejection._
import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server._ import akka.http.scaladsl.server._
import akka.pattern.ask
import akka.util.Timeout import akka.util.Timeout
import me.arcanis.ffxivbis.models.{Permission, User} import me.arcanis.ffxivbis.messages.{GetUser, Message}
import me.arcanis.ffxivbis.service.impl.DatabaseUserHandler import me.arcanis.ffxivbis.models.Permission
import scala.concurrent.{ExecutionContext, Future} import scala.concurrent.{ExecutionContext, Future}
// idea comes from https://synkre.com/bcrypt-for-akka-http-password-encryption/ // idea comes from https://synkre.com/bcrypt-for-akka-http-password-encryption/
trait Authorization { trait Authorization {
def storage: ActorRef def storage: ActorRef[Message]
def authenticateBasicBCrypt[T](realm: String, def authenticateBasicBCrypt[T](realm: String,
authenticate: (String, String) => Future[Option[T]]): Directive1[T] = { authenticate: (String, String) => Future[Option[T]]): Directive1[T] = {
@ -39,23 +39,22 @@ trait Authorization {
} }
} }
def authenticator(scope: Permission.Value)(partyId: String) def authenticator(scope: Permission.Value, partyId: String)(username: String, password: String)
(username: String, password: String) (implicit executionContext: ExecutionContext, timeout: Timeout, scheduler: Scheduler): Future[Option[String]] =
(implicit executionContext: ExecutionContext, timeout: Timeout): Future[Option[String]] = storage.ask(GetUser(partyId, username, _)).map {
(storage ? DatabaseUserHandler.GetUser(partyId, username)).mapTo[Option[User]].map {
case Some(user) if user.verify(password) && user.verityScope(scope) => Some(username) case Some(user) if user.verify(password) && user.verityScope(scope) => Some(username)
case _ => None case _ => None
} }
def authAdmin(partyId: String)(username: String, password: String) def authAdmin(partyId: String)(username: String, password: String)
(implicit executionContext: ExecutionContext, timeout: Timeout): Future[Option[String]] = (implicit executionContext: ExecutionContext, timeout: Timeout, scheduler: Scheduler): Future[Option[String]] =
authenticator(Permission.admin)(partyId)(username, password) authenticator(Permission.admin, partyId)(username, password)
def authGet(partyId: String)(username: String, password: String) def authGet(partyId: String)(username: String, password: String)
(implicit executionContext: ExecutionContext, timeout: Timeout): Future[Option[String]] = (implicit executionContext: ExecutionContext, timeout: Timeout, scheduler: Scheduler): Future[Option[String]] =
authenticator(Permission.get)(partyId)(username, password) authenticator(Permission.get, partyId)(username, password)
def authPost(partyId: String)(username: String, password: String) def authPost(partyId: String)(username: String, password: String)
(implicit executionContext: ExecutionContext, timeout: Timeout): Future[Option[String]] = (implicit executionContext: ExecutionContext, timeout: Timeout, scheduler: Scheduler): Future[Option[String]] =
authenticator(Permission.post)(partyId)(username, password) authenticator(Permission.post, partyId)(username, password)
} }

View File

@ -8,40 +8,45 @@
*/ */
package me.arcanis.ffxivbis.http package me.arcanis.ffxivbis.http
import akka.actor.ActorRef import akka.actor.typed.scaladsl.AskPattern.Askable
import akka.pattern.ask import akka.actor.typed.{ActorRef, Scheduler}
import akka.util.Timeout import akka.util.Timeout
import me.arcanis.ffxivbis.http.api.v1.json.ApiAction import me.arcanis.ffxivbis.http.api.v1.json.ApiAction
import me.arcanis.ffxivbis.messages.{AddPieceToBis, GetBiS, Message, RemovePieceFromBiS, RemovePiecesFromBiS}
import me.arcanis.ffxivbis.models.{Piece, Player, PlayerId} import me.arcanis.ffxivbis.models.{Piece, Player, PlayerId}
import me.arcanis.ffxivbis.service.impl.DatabaseBiSHandler
import scala.concurrent.{ExecutionContext, Future} import scala.concurrent.{ExecutionContext, Future}
class BiSHelper(storage: ActorRef, ariyala: ActorRef) extends AriyalaHelper(ariyala) { trait BiSHelper extends BisProviderHelper {
def storage: ActorRef[Message]
def addPieceBiS(playerId: PlayerId, piece: Piece) def addPieceBiS(playerId: PlayerId, piece: Piece)
(implicit executionContext: ExecutionContext, timeout: Timeout): Future[Int] = (implicit timeout: Timeout, scheduler: Scheduler): Future[Unit] =
(storage ? DatabaseBiSHandler.AddPieceToBis(playerId, piece.withJob(playerId.job))).mapTo[Int] storage.ask(AddPieceToBis(playerId, piece.withJob(playerId.job), _))
def bis(partyId: String, playerId: Option[PlayerId]) def bis(partyId: String, playerId: Option[PlayerId])
(implicit executionContext: ExecutionContext, timeout: Timeout): Future[Seq[Player]] = (implicit timeout: Timeout, scheduler: Scheduler): Future[Seq[Player]] =
(storage ? DatabaseBiSHandler.GetBiS(partyId, playerId)).mapTo[Seq[Player]] storage.ask(GetBiS(partyId, playerId, _))
def doModifyBiS(action: ApiAction.Value, playerId: PlayerId, piece: Piece) def doModifyBiS(action: ApiAction.Value, playerId: PlayerId, piece: Piece)
(implicit executionContext: ExecutionContext, timeout: Timeout): Future[Int] = (implicit timeout: Timeout, scheduler: Scheduler): Future[Unit] =
action match { action match {
case ApiAction.add => addPieceBiS(playerId, piece) case ApiAction.add => addPieceBiS(playerId, piece)
case ApiAction.remove => removePieceBiS(playerId, piece) case ApiAction.remove => removePieceBiS(playerId, piece)
} }
def putBiS(playerId: PlayerId, link: String) def putBiS(playerId: PlayerId, link: String)
(implicit executionContext: ExecutionContext, timeout: Timeout): Future[Unit] = (implicit executionContext: ExecutionContext, timeout: Timeout, scheduler: Scheduler): Future[Unit] = {
downloadBiS(link, playerId.job).flatMap { bis => storage.ask(RemovePiecesFromBiS(playerId, _)).flatMap { _ =>
Future.traverse(bis.pieces)(addPieceBiS(playerId, _)) downloadBiS(link, playerId.job).flatMap { bis =>
}.map(_ => ()) Future.traverse(bis.pieces)(addPieceBiS(playerId, _))
}.map(_ => ())
}
}
def removePieceBiS(playerId: PlayerId, piece: Piece) def removePieceBiS(playerId: PlayerId, piece: Piece)
(implicit executionContext: ExecutionContext, timeout: Timeout): Future[Int] = (implicit timeout: Timeout, scheduler: Scheduler): Future[Unit] =
(storage ? DatabaseBiSHandler.RemovePieceFromBiS(playerId, piece)).mapTo[Int] storage.ask(RemovePieceFromBiS(playerId, piece, _))
} }

View File

@ -0,0 +1,26 @@
/*
* Copyright (c) 2019 Evgeniy Alekseev.
*
* This file is part of ffxivbis
* (see https://github.com/arcan1s/ffxivbis).
*
* License: 3-clause BSD, see https://opensource.org/licenses/BSD-3-Clause
*/
package me.arcanis.ffxivbis.http
import akka.actor.typed.{ActorRef, Scheduler}
import akka.actor.typed.scaladsl.AskPattern.Askable
import akka.util.Timeout
import me.arcanis.ffxivbis.messages.{BiSProviderMessage, DownloadBiS}
import me.arcanis.ffxivbis.models.{BiS, Job}
import scala.concurrent.Future
trait BisProviderHelper {
def provider: ActorRef[BiSProviderMessage]
def downloadBiS(link: String, job: Job.Job)
(implicit timeout: Timeout, scheduler: Scheduler): Future[BiS] =
provider.ask(DownloadBiS(link, job, _))
}

View File

@ -8,38 +8,41 @@
*/ */
package me.arcanis.ffxivbis.http package me.arcanis.ffxivbis.http
import akka.actor.ActorRef import akka.actor.typed.scaladsl.AskPattern.Askable
import akka.pattern.ask import akka.actor.typed.{ActorRef, Scheduler}
import akka.util.Timeout import akka.util.Timeout
import me.arcanis.ffxivbis.http.api.v1.json.ApiAction import me.arcanis.ffxivbis.http.api.v1.json.ApiAction
import me.arcanis.ffxivbis.messages.{AddPieceTo, GetLoot, Message, RemovePieceFrom, SuggestLoot}
import me.arcanis.ffxivbis.models.{Piece, Player, PlayerId, PlayerIdWithCounters} import me.arcanis.ffxivbis.models.{Piece, Player, PlayerId, PlayerIdWithCounters}
import me.arcanis.ffxivbis.service.LootSelector.LootSelectorResult
import me.arcanis.ffxivbis.service.impl.DatabaseLootHandler
import scala.concurrent.{ExecutionContext, Future} import scala.concurrent.{ExecutionContext, Future}
class LootHelper(storage: ActorRef) { trait LootHelper {
def addPieceLoot(playerId: PlayerId, piece: Piece) def storage: ActorRef[Message]
(implicit executionContext: ExecutionContext, timeout: Timeout): Future[Int] =
(storage ? DatabaseLootHandler.AddPieceTo(playerId, piece)).mapTo[Int]
def doModifyLoot(action: ApiAction.Value, playerId: PlayerId, piece: Piece) def addPieceLoot(playerId: PlayerId, piece: Piece, isFreeLoot: Boolean)
(implicit executionContext: ExecutionContext, timeout: Timeout): Future[Int] = (implicit timeout: Timeout, scheduler: Scheduler): Future[Unit] =
action match { storage.ask(
case ApiAction.add => addPieceLoot(playerId, piece) AddPieceTo(playerId, piece, isFreeLoot, _))
case ApiAction.remove => removePieceLoot(playerId, piece)
def doModifyLoot(action: ApiAction.Value, playerId: PlayerId, piece: Piece, maybeFree: Option[Boolean])
(implicit timeout: Timeout, scheduler: Scheduler): Future[Unit] =
(action, maybeFree) match {
case (ApiAction.add, Some(isFreeLoot)) => addPieceLoot(playerId, piece, isFreeLoot)
case (ApiAction.remove, _) => removePieceLoot(playerId, piece)
case _ => throw new IllegalArgumentException(s"Invalid combinantion of action $action and fee loot $maybeFree")
} }
def loot(partyId: String, playerId: Option[PlayerId]) def loot(partyId: String, playerId: Option[PlayerId])
(implicit executionContext: ExecutionContext, timeout: Timeout): Future[Seq[Player]] = (implicit timeout: Timeout, scheduler: Scheduler): Future[Seq[Player]] =
(storage ? DatabaseLootHandler.GetLoot(partyId, playerId)).mapTo[Seq[Player]] storage.ask(GetLoot(partyId, playerId, _))
def removePieceLoot(playerId: PlayerId, piece: Piece) def removePieceLoot(playerId: PlayerId, piece: Piece)
(implicit executionContext: ExecutionContext, timeout: Timeout): Future[Int] = (implicit timeout: Timeout, scheduler: Scheduler): Future[Unit] =
(storage ? DatabaseLootHandler.RemovePieceFrom(playerId, piece)).mapTo[Int] storage.ask(RemovePieceFrom(playerId, piece, _))
def suggestPiece(partyId: String, piece: Piece) def suggestPiece(partyId: String, piece: Piece)
(implicit executionContext: ExecutionContext, timeout: Timeout): Future[Seq[PlayerIdWithCounters]] = (implicit executionContext: ExecutionContext, timeout: Timeout, scheduler: Scheduler): Future[Seq[PlayerIdWithCounters]] =
(storage ? DatabaseLootHandler.SuggestLoot(partyId, piece)).mapTo[LootSelectorResult].map(_.result) storage.ask(SuggestLoot(partyId, piece, _)).map(_.result)
} }

View File

@ -8,46 +8,56 @@
*/ */
package me.arcanis.ffxivbis.http package me.arcanis.ffxivbis.http
import akka.actor.ActorRef import akka.actor.typed.{ActorRef, Scheduler}
import akka.pattern.ask import akka.actor.typed.scaladsl.AskPattern.Askable
import akka.util.Timeout import akka.util.Timeout
import me.arcanis.ffxivbis.http.api.v1.json.ApiAction import me.arcanis.ffxivbis.http.api.v1.json.ApiAction
import me.arcanis.ffxivbis.models.{Party, Player, PlayerId} import me.arcanis.ffxivbis.messages.{AddPieceToBis, AddPlayer, GetParty, GetPartyDescription, GetPlayer, Message, RemovePlayer, UpdateParty}
import me.arcanis.ffxivbis.service.impl.{DatabaseBiSHandler, DatabasePartyHandler} import me.arcanis.ffxivbis.models.{PartyDescription, Player, PlayerId}
import scala.concurrent.{ExecutionContext, Future} import scala.concurrent.{ExecutionContext, Future}
class PlayerHelper(storage: ActorRef, ariyala: ActorRef) extends AriyalaHelper(ariyala) { trait PlayerHelper extends BisProviderHelper {
def storage: ActorRef[Message]
def addPlayer(player: Player) def addPlayer(player: Player)
(implicit executionContext: ExecutionContext, timeout: Timeout): Future[Int] = (implicit executionContext: ExecutionContext, timeout: Timeout, scheduler: Scheduler): Future[Unit] =
(storage ? DatabasePartyHandler.AddPlayer(player)).mapTo[Int].map { res => storage.ask(ref => AddPlayer(player, ref)).map { res =>
player.link match { player.link match {
case Some(link) => case Some(link) =>
downloadBiS(link, player.job).map { bis => downloadBiS(link, player.job).map { bis =>
bis.pieces.map(storage ? DatabaseBiSHandler.AddPieceToBis(player.playerId, _)) bis.pieces.map(piece => storage.ask(AddPieceToBis(player.playerId, piece, _)))
}.map(_ => res) }.map(_ => res)
case None => Future.successful(res) case None => Future.successful(res)
} }
}.flatten }.flatten
def doModifyPlayer(action: ApiAction.Value, player: Player) def doModifyPlayer(action: ApiAction.Value, player: Player)
(implicit executionContext: ExecutionContext, timeout: Timeout): Future[Int] = (implicit executionContext: ExecutionContext, timeout: Timeout, scheduler: Scheduler): Future[Unit] =
action match { action match {
case ApiAction.add => addPlayer(player) case ApiAction.add => addPlayer(player)
case ApiAction.remove => removePlayer(player.playerId) case ApiAction.remove => removePlayer(player.playerId)
} }
def getPartyDescription(partyId: String)
(implicit timeout: Timeout, scheduler: Scheduler): Future[PartyDescription] =
storage.ask(GetPartyDescription(partyId, _))
def getPlayers(partyId: String, maybePlayerId: Option[PlayerId]) def getPlayers(partyId: String, maybePlayerId: Option[PlayerId])
(implicit executionContext: ExecutionContext, timeout: Timeout): Future[Seq[Player]] = (implicit executionContext: ExecutionContext, timeout: Timeout, scheduler: Scheduler): Future[Seq[Player]] =
maybePlayerId match { maybePlayerId match {
case Some(playerId) => case Some(playerId) =>
(storage ? DatabasePartyHandler.GetPlayer(playerId)).mapTo[Option[Player]].map(_.toSeq) storage.ask(GetPlayer(playerId, _)).map(_.toSeq)
case None => case None =>
(storage ? DatabasePartyHandler.GetParty(partyId)).mapTo[Party].map(_.players.values.toSeq) storage.ask(GetParty(partyId, _)).map(_.players.values.toSeq)
} }
def removePlayer(playerId: PlayerId) def removePlayer(playerId: PlayerId)
(implicit executionContext: ExecutionContext, timeout: Timeout): Future[Int] = (implicit timeout: Timeout, scheduler: Scheduler): Future[Unit] =
(storage ? DatabasePartyHandler.RemovePlayer(playerId)).mapTo[Int] storage.ask(RemovePlayer(playerId, _))
def updateDescription(partyDescription: PartyDescription)
(implicit executionContext: ExecutionContext, timeout: Timeout, scheduler: Scheduler): Future[Unit] =
storage.ask(UpdateParty(partyDescription, _))
} }

View File

@ -10,25 +10,29 @@ package me.arcanis.ffxivbis.http
import java.time.Instant import java.time.Instant
import akka.actor.{ActorRef, ActorSystem} import akka.actor.typed.{ActorRef, ActorSystem, Scheduler}
import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server._ import akka.http.scaladsl.server._
import akka.util.Timeout import akka.util.Timeout
import com.typesafe.scalalogging.{Logger, StrictLogging} import com.typesafe.scalalogging.{Logger, StrictLogging}
import me.arcanis.ffxivbis.http.api.v1.RootApiV1Endpoint import me.arcanis.ffxivbis.http.api.v1.RootApiV1Endpoint
import me.arcanis.ffxivbis.http.view.RootView import me.arcanis.ffxivbis.http.view.RootView
import me.arcanis.ffxivbis.messages.{BiSProviderMessage, Message}
class RootEndpoint(system: ActorSystem, storage: ActorRef, ariyala: ActorRef) class RootEndpoint(system: ActorSystem[Nothing],
storage: ActorRef[Message],
provider: ActorRef[BiSProviderMessage])
extends StrictLogging { extends StrictLogging {
import me.arcanis.ffxivbis.utils.Implicits._ import me.arcanis.ffxivbis.utils.Implicits._
private val config = system.settings.config private val config = system.settings.config
implicit val scheduler: Scheduler = system.scheduler
implicit val timeout: Timeout = implicit val timeout: Timeout =
config.getDuration("me.arcanis.ffxivbis.settings.request-timeout") config.getDuration("me.arcanis.ffxivbis.settings.request-timeout")
private val rootApiV1Endpoint: RootApiV1Endpoint = new RootApiV1Endpoint(storage, ariyala, config) private val rootApiV1Endpoint: RootApiV1Endpoint = new RootApiV1Endpoint(storage, provider, config)
private val rootView: RootView = new RootView(storage, ariyala) private val rootView: RootView = new RootView(storage, provider)
private val swagger: Swagger = new Swagger(config) private val swagger: Swagger = new Swagger(config)
private val httpLogger = Logger("http") private val httpLogger = Logger("http")

View File

@ -16,10 +16,11 @@ import io.swagger.v3.oas.models.security.SecurityScheme
import scala.io.Source import scala.io.Source
class Swagger(config: Config) extends SwaggerHttpService { class Swagger(config: Config) extends SwaggerHttpService {
override val apiClasses: Set[Class[_]] = Set( override val apiClasses: Set[Class[_]] = Set(
classOf[api.v1.BiSEndpoint], classOf[api.v1.LootEndpoint], classOf[api.v1.BiSEndpoint], classOf[api.v1.LootEndpoint],
classOf[api.v1.PlayerEndpoint], classOf[api.v1.TypesEndpoint], classOf[api.v1.PartyEndpoint], classOf[api.v1.PlayerEndpoint],
classOf[api.v1.UserEndpoint] classOf[api.v1.TypesEndpoint], classOf[api.v1.UserEndpoint]
) )
override val info: Info = Info( override val info: Info = Info(

View File

@ -8,33 +8,34 @@
*/ */
package me.arcanis.ffxivbis.http package me.arcanis.ffxivbis.http
import akka.actor.ActorRef import akka.actor.typed.scaladsl.AskPattern.Askable
import akka.pattern.ask import akka.actor.typed.{ActorRef, Scheduler}
import akka.util.Timeout import akka.util.Timeout
import me.arcanis.ffxivbis.messages.{AddUser, DeleteUser, GetNewPartyId, GetUser, GetUsers, Message}
import me.arcanis.ffxivbis.models.User import me.arcanis.ffxivbis.models.User
import me.arcanis.ffxivbis.service.PartyService
import me.arcanis.ffxivbis.service.impl.DatabaseUserHandler
import scala.concurrent.{ExecutionContext, Future} import scala.concurrent.Future
class UserHelper(storage: ActorRef) { trait UserHelper {
def storage: ActorRef[Message]
def addUser(user: User, isHashedPassword: Boolean) def addUser(user: User, isHashedPassword: Boolean)
(implicit executionContext: ExecutionContext, timeout: Timeout): Future[Int] = (implicit timeout: Timeout, scheduler: Scheduler): Future[Unit] =
(storage ? DatabaseUserHandler.AddUser(user, isHashedPassword)).mapTo[Int] storage.ask(AddUser(user, isHashedPassword, _))
def newPartyId(implicit executionContext: ExecutionContext, timeout: Timeout): Future[String] = def newPartyId(implicit timeout: Timeout, scheduler: Scheduler): Future[String] =
(storage ? PartyService.GetNewPartyId).mapTo[String] storage.ask(GetNewPartyId)
def user(partyId: String, username: String) def user(partyId: String, username: String)
(implicit executionContext: ExecutionContext, timeout: Timeout): Future[Option[User]] = (implicit timeout: Timeout, scheduler: Scheduler): Future[Option[User]] =
(storage ? DatabaseUserHandler.GetUser(partyId, username)).mapTo[Option[User]] storage.ask(GetUser(partyId, username, _))
def users(partyId: String) def users(partyId: String)
(implicit executionContext: ExecutionContext, timeout: Timeout): Future[Seq[User]] = (implicit timeout: Timeout, scheduler: Scheduler): Future[Seq[User]] =
(storage ? DatabaseUserHandler.GetUsers(partyId)).mapTo[Seq[User]] storage.ask(GetUsers(partyId, _))
def removeUser(partyId: String, username: String) def removeUser(partyId: String, username: String)
(implicit executionContext: ExecutionContext, timeout: Timeout): Future[Int] = (implicit timeout: Timeout, scheduler: Scheduler): Future[Unit] =
(storage ? DatabaseUserHandler.DeleteUser(partyId, username)).mapTo[Int] storage.ask(DeleteUser(partyId, username, _))
} }

View File

@ -8,7 +8,7 @@
*/ */
package me.arcanis.ffxivbis.http.api.v1 package me.arcanis.ffxivbis.http.api.v1
import akka.actor.ActorRef import akka.actor.typed.{ActorRef, Scheduler}
import akka.http.scaladsl.model.{HttpEntity, StatusCodes} import akka.http.scaladsl.model.{HttpEntity, StatusCodes}
import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server._ import akka.http.scaladsl.server._
@ -22,13 +22,16 @@ import io.swagger.v3.oas.annotations.{Operation, Parameter}
import javax.ws.rs._ import javax.ws.rs._
import me.arcanis.ffxivbis.http.api.v1.json._ import me.arcanis.ffxivbis.http.api.v1.json._
import me.arcanis.ffxivbis.http.{Authorization, BiSHelper} import me.arcanis.ffxivbis.http.{Authorization, BiSHelper}
import me.arcanis.ffxivbis.messages.{BiSProviderMessage, Message}
import me.arcanis.ffxivbis.models.PlayerId import me.arcanis.ffxivbis.models.PlayerId
import scala.util.{Failure, Success} import scala.util.{Failure, Success}
@Path("api/v1") @Path("api/v1")
class BiSEndpoint(override val storage: ActorRef, ariyala: ActorRef)(implicit timeout: Timeout) class BiSEndpoint(override val storage: ActorRef[Message],
extends BiSHelper(storage, ariyala) with Authorization with JsonSupport { override val provider: ActorRef[BiSProviderMessage])
(implicit timeout: Timeout, scheduler: Scheduler)
extends BiSHelper with Authorization with JsonSupport {
def route: Route = createBiS ~ getBiS ~ modifyBiS def route: Route = createBiS ~ getBiS ~ modifyBiS

View File

@ -17,7 +17,7 @@ import spray.json._
trait HttpHandler extends StrictLogging { this: JsonSupport => trait HttpHandler extends StrictLogging { this: JsonSupport =>
implicit def exceptionHandler: ExceptionHandler = ExceptionHandler { def exceptionHandler: ExceptionHandler = ExceptionHandler {
case ex: IllegalArgumentException => case ex: IllegalArgumentException =>
complete(StatusCodes.BadRequest, ErrorResponse(ex.getMessage)) complete(StatusCodes.BadRequest, ErrorResponse(ex.getMessage))
@ -26,12 +26,12 @@ trait HttpHandler extends StrictLogging { this: JsonSupport =>
complete(StatusCodes.InternalServerError, ErrorResponse("unknown server error")) complete(StatusCodes.InternalServerError, ErrorResponse("unknown server error"))
} }
implicit def rejectionHandler: RejectionHandler = def rejectionHandler: RejectionHandler =
RejectionHandler.default RejectionHandler.default
.mapRejectionResponse { .mapRejectionResponse {
case response @ HttpResponse(_, _, entity: HttpEntity.Strict, _) => case response @ HttpResponse(_, _, entity: HttpEntity.Strict, _) =>
val message = ErrorResponse(entity.data.utf8String).toJson val message = ErrorResponse(entity.data.utf8String).toJson
response.copy(entity = HttpEntity(ContentTypes.`application/json`, message.compactPrint)) response.withEntity(HttpEntity(ContentTypes.`application/json`, message.compactPrint))
case other => other case other => other
} }
} }

View File

@ -8,7 +8,7 @@
*/ */
package me.arcanis.ffxivbis.http.api.v1 package me.arcanis.ffxivbis.http.api.v1
import akka.actor.ActorRef import akka.actor.typed.{ActorRef, Scheduler}
import akka.http.scaladsl.model.{HttpEntity, StatusCodes} import akka.http.scaladsl.model.{HttpEntity, StatusCodes}
import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server._ import akka.http.scaladsl.server._
@ -22,13 +22,15 @@ import io.swagger.v3.oas.annotations.{Operation, Parameter}
import javax.ws.rs._ import javax.ws.rs._
import me.arcanis.ffxivbis.http.api.v1.json._ import me.arcanis.ffxivbis.http.api.v1.json._
import me.arcanis.ffxivbis.http.{Authorization, LootHelper} import me.arcanis.ffxivbis.http.{Authorization, LootHelper}
import me.arcanis.ffxivbis.messages.Message
import me.arcanis.ffxivbis.models.PlayerId import me.arcanis.ffxivbis.models.PlayerId
import scala.util.{Failure, Success} import scala.util.{Failure, Success}
@Path("api/v1") @Path("api/v1")
class LootEndpoint(override val storage: ActorRef)(implicit timeout: Timeout) class LootEndpoint(override val storage: ActorRef[Message])
extends LootHelper(storage) with Authorization with JsonSupport with HttpHandler { (implicit timeout: Timeout, scheduler: Scheduler)
extends LootHelper with Authorization with JsonSupport with HttpHandler {
def route: Route = getLoot ~ modifyLoot def route: Route = getLoot ~ modifyLoot
@ -103,7 +105,7 @@ class LootEndpoint(override val storage: ActorRef)(implicit timeout: Timeout)
post { post {
entity(as[PieceActionResponse]) { action => entity(as[PieceActionResponse]) { action =>
val playerId = action.playerId.withPartyId(partyId) val playerId = action.playerId.withPartyId(partyId)
onComplete(doModifyLoot(action.action, playerId, action.piece.toPiece)) { onComplete(doModifyLoot(action.action, playerId, action.piece.toPiece, action.isFreeLoot)) {
case Success(_) => complete(StatusCodes.Accepted, HttpEntity.Empty) case Success(_) => complete(StatusCodes.Accepted, HttpEntity.Empty)
case Failure(exception) => throw exception case Failure(exception) => throw exception
} }

View File

@ -0,0 +1,110 @@
/*
* Copyright (c) 2020 Evgeniy Alekseev.
*
* This file is part of ffxivbis
* (see https://github.com/arcan1s/ffxivbis).
*
* License: 3-clause BSD, see https://opensource.org/licenses/BSD-3-Clause
*/
package me.arcanis.ffxivbis.http.api.v1
import akka.actor.typed.{ActorRef, Scheduler}
import akka.http.scaladsl.model.{HttpEntity, StatusCodes}
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server._
import akka.util.Timeout
import io.swagger.v3.oas.annotations.enums.ParameterIn
import io.swagger.v3.oas.annotations.media.{Content, Schema}
import io.swagger.v3.oas.annotations.parameters.RequestBody
import io.swagger.v3.oas.annotations.responses.ApiResponse
import io.swagger.v3.oas.annotations.security.SecurityRequirement
import io.swagger.v3.oas.annotations.{Operation, Parameter}
import javax.ws.rs._
import me.arcanis.ffxivbis.http.api.v1.json._
import me.arcanis.ffxivbis.http.{Authorization, PlayerHelper}
import me.arcanis.ffxivbis.messages.{BiSProviderMessage, Message}
import scala.util.{Failure, Success}
@Path("api/v1")
class PartyEndpoint(override val storage: ActorRef[Message],
override val provider: ActorRef[BiSProviderMessage])
(implicit timeout: Timeout, scheduler: Scheduler)
extends PlayerHelper with Authorization with JsonSupport with HttpHandler {
def route: Route = getPartyDescription ~ modifyPartyDescription
@GET
@Path("party/{partyId}/description")
@Produces(value = Array("application/json"))
@Operation(summary = "get party description", description = "Return the party description",
parameters = Array(
new Parameter(name = "partyId", in = ParameterIn.PATH, description = "unique party ID", example = "abcdefgh"),
),
responses = Array(
new ApiResponse(responseCode = "200", description = "Party description",
content = Array(new Content(schema = new Schema(implementation = classOf[PartyDescriptionResponse])))),
new ApiResponse(responseCode = "401", description = "Supplied authorization is invalid",
content = Array(new Content(schema = new Schema(implementation = classOf[ErrorResponse])))),
new ApiResponse(responseCode = "403", description = "Access is forbidden",
content = Array(new Content(schema = new Schema(implementation = classOf[ErrorResponse])))),
new ApiResponse(responseCode = "500", description = "Internal server error",
content = Array(new Content(schema = new Schema(implementation = classOf[ErrorResponse])))),
),
security = Array(new SecurityRequirement(name = "basic auth", scopes = Array("get"))),
tags = Array("party"),
)
def getPartyDescription: Route =
path("party" / Segment / "description") { partyId =>
extractExecutionContext { implicit executionContext =>
authenticateBasicBCrypt(s"party $partyId", authGet(partyId)) { _ =>
get {
onComplete(getPartyDescription(partyId)) {
case Success(response) => complete(PartyDescriptionResponse.fromDescription(response))
case Failure(exception) => throw exception
}
}
}
}
}
@POST
@Consumes(value = Array("application/json"))
@Path("party/{partyId}/description")
@Operation(summary = "modify party description", description = "Edit party description",
parameters = Array(
new Parameter(name = "partyId", in = ParameterIn.PATH, description = "unique party ID", example = "abcdefgh"),
),
requestBody = new RequestBody(description = "new party description", required = true,
content = Array(new Content(schema = new Schema(implementation = classOf[PartyDescriptionResponse])))),
responses = Array(
new ApiResponse(responseCode = "202", description = "Party description has been modified"),
new ApiResponse(responseCode = "400", description = "Invalid parameters were supplied",
content = Array(new Content(schema = new Schema(implementation = classOf[ErrorResponse])))),
new ApiResponse(responseCode = "401", description = "Supplied authorization is invalid",
content = Array(new Content(schema = new Schema(implementation = classOf[ErrorResponse])))),
new ApiResponse(responseCode = "403", description = "Access is forbidden",
content = Array(new Content(schema = new Schema(implementation = classOf[ErrorResponse])))),
new ApiResponse(responseCode = "500", description = "Internal server error",
content = Array(new Content(schema = new Schema(implementation = classOf[ErrorResponse])))),
),
security = Array(new SecurityRequirement(name = "basic auth", scopes = Array("post"))),
tags = Array("party"),
)
def modifyPartyDescription: Route =
path("party" / Segment / "description") { partyId =>
extractExecutionContext { implicit executionContext =>
authenticateBasicBCrypt(s"party $partyId", authPost(partyId)) { _ =>
post {
entity(as[PartyDescriptionResponse]) { partyDescription =>
val description = partyDescription.copy(partyId = partyId)
onComplete(updateDescription(description.toDescription)) {
case Success(_) => complete(StatusCodes.Accepted, HttpEntity.Empty)
case Failure(exception) => throw exception
}
}
}
}
}
}
}

View File

@ -8,7 +8,7 @@
*/ */
package me.arcanis.ffxivbis.http.api.v1 package me.arcanis.ffxivbis.http.api.v1
import akka.actor.ActorRef import akka.actor.typed.{ActorRef, Scheduler}
import akka.http.scaladsl.model.{HttpEntity, StatusCodes} import akka.http.scaladsl.model.{HttpEntity, StatusCodes}
import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server._ import akka.http.scaladsl.server._
@ -22,13 +22,16 @@ import io.swagger.v3.oas.annotations.{Operation, Parameter}
import javax.ws.rs._ import javax.ws.rs._
import me.arcanis.ffxivbis.http.api.v1.json._ import me.arcanis.ffxivbis.http.api.v1.json._
import me.arcanis.ffxivbis.http.{Authorization, PlayerHelper} import me.arcanis.ffxivbis.http.{Authorization, PlayerHelper}
import me.arcanis.ffxivbis.messages.{BiSProviderMessage, Message}
import me.arcanis.ffxivbis.models.PlayerId import me.arcanis.ffxivbis.models.PlayerId
import scala.util.{Failure, Success} import scala.util.{Failure, Success}
@Path("api/v1") @Path("api/v1")
class PlayerEndpoint(override val storage: ActorRef, ariyala: ActorRef)(implicit timeout: Timeout) class PlayerEndpoint(override val storage: ActorRef[Message],
extends PlayerHelper(storage, ariyala) with Authorization with JsonSupport with HttpHandler { override val provider: ActorRef[BiSProviderMessage])
(implicit timeout: Timeout, scheduler: Scheduler)
extends PlayerHelper with Authorization with JsonSupport with HttpHandler {
def route: Route = getParty ~ modifyParty def route: Route = getParty ~ modifyParty

View File

@ -8,28 +8,31 @@
*/ */
package me.arcanis.ffxivbis.http.api.v1 package me.arcanis.ffxivbis.http.api.v1
import akka.actor.ActorRef import akka.actor.typed.{ActorRef, Scheduler}
import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route import akka.http.scaladsl.server.Route
import akka.util.Timeout import akka.util.Timeout
import com.typesafe.config.Config import com.typesafe.config.Config
import me.arcanis.ffxivbis.http.api.v1.json.JsonSupport import me.arcanis.ffxivbis.http.api.v1.json.JsonSupport
import me.arcanis.ffxivbis.messages.{BiSProviderMessage, Message}
class RootApiV1Endpoint(storage: ActorRef, ariyala: ActorRef, config: Config) class RootApiV1Endpoint(storage: ActorRef[Message],
(implicit timeout: Timeout) provider: ActorRef[BiSProviderMessage],
config: Config)(implicit timeout: Timeout, scheduler: Scheduler)
extends JsonSupport with HttpHandler { extends JsonSupport with HttpHandler {
private val biSEndpoint = new BiSEndpoint(storage, ariyala) private val biSEndpoint = new BiSEndpoint(storage, provider)
private val lootEndpoint = new LootEndpoint(storage) private val lootEndpoint = new LootEndpoint(storage)
private val playerEndpoint = new PlayerEndpoint(storage, ariyala) private val partyEndpoint = new PartyEndpoint(storage, provider)
private val playerEndpoint = new PlayerEndpoint(storage, provider)
private val typesEndpoint = new TypesEndpoint(config) private val typesEndpoint = new TypesEndpoint(config)
private val userEndpoint = new UserEndpoint(storage) private val userEndpoint = new UserEndpoint(storage)
def route: Route = def route: Route =
handleExceptions(exceptionHandler) { handleExceptions(exceptionHandler) {
handleRejections(rejectionHandler) { handleRejections(rejectionHandler) {
biSEndpoint.route ~ lootEndpoint.route ~ playerEndpoint.route ~ biSEndpoint.route ~ lootEndpoint.route ~ partyEndpoint.route ~
typesEndpoint.route ~ userEndpoint.route playerEndpoint.route ~ typesEndpoint.route ~ userEndpoint.route
} }
} }
} }

View File

@ -16,12 +16,12 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse
import io.swagger.v3.oas.annotations.Operation import io.swagger.v3.oas.annotations.Operation
import javax.ws.rs._ import javax.ws.rs._
import me.arcanis.ffxivbis.http.api.v1.json._ import me.arcanis.ffxivbis.http.api.v1.json._
import me.arcanis.ffxivbis.models.{Job, Party, Permission, Piece} import me.arcanis.ffxivbis.models.{Job, Party, Permission, Piece, PieceType}
@Path("api/v1") @Path("api/v1")
class TypesEndpoint(config: Config) extends JsonSupport { class TypesEndpoint(config: Config) extends JsonSupport {
def route: Route = getJobs ~ getPermissions ~ getPieces ~ getPriority def route: Route = getJobs ~ getPermissions ~ getPieces ~ getPieceTypes ~ getPriority
@GET @GET
@Path("types/jobs") @Path("types/jobs")
@ -86,6 +86,27 @@ class TypesEndpoint(config: Config) extends JsonSupport {
} }
} }
@GET
@Path("types/pieces/types")
@Produces(value = Array("application/json"))
@Operation(summary = "piece types list", description = "Returns the available piece types",
responses = Array(
new ApiResponse(responseCode = "200", description = "List of available piece types",
content = Array(new Content(
array = new ArraySchema(schema = new Schema(implementation = classOf[String]))
))),
new ApiResponse(responseCode = "500", description = "Internal server error",
content = Array(new Content(schema = new Schema(implementation = classOf[ErrorResponse])))),
),
tags = Array("types"),
)
def getPieceTypes: Route =
path("types" / "pieces" / "types") {
get {
complete(PieceType.available.map(_.toString))
}
}
@GET @GET
@Path("types/priority") @Path("types/priority")
@Produces(value = Array("application/json")) @Produces(value = Array("application/json"))

View File

@ -8,7 +8,7 @@
*/ */
package me.arcanis.ffxivbis.http.api.v1 package me.arcanis.ffxivbis.http.api.v1
import akka.actor.ActorRef import akka.actor.typed.{ActorRef, Scheduler}
import akka.http.scaladsl.model.{HttpEntity, StatusCodes} import akka.http.scaladsl.model.{HttpEntity, StatusCodes}
import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server._ import akka.http.scaladsl.server._
@ -22,13 +22,15 @@ import io.swagger.v3.oas.annotations.{Operation, Parameter}
import javax.ws.rs._ import javax.ws.rs._
import me.arcanis.ffxivbis.http.api.v1.json._ import me.arcanis.ffxivbis.http.api.v1.json._
import me.arcanis.ffxivbis.http.{Authorization, UserHelper} import me.arcanis.ffxivbis.http.{Authorization, UserHelper}
import me.arcanis.ffxivbis.messages.Message
import me.arcanis.ffxivbis.models.Permission import me.arcanis.ffxivbis.models.Permission
import scala.util.{Failure, Success} import scala.util.{Failure, Success}
@Path("api/v1") @Path("api/v1")
class UserEndpoint(override val storage: ActorRef)(implicit timeout: Timeout) class UserEndpoint(override val storage: ActorRef[Message])
extends UserHelper(storage) with Authorization with JsonSupport { (implicit timeout: Timeout, scheduler: Scheduler)
extends UserHelper with Authorization with JsonSupport {
def route: Route = createParty ~ createUser ~ deleteUser ~ getUsers def route: Route = createParty ~ createUser ~ deleteUser ~ getUsers

View File

@ -8,6 +8,8 @@
*/ */
package me.arcanis.ffxivbis.http.api.v1.json package me.arcanis.ffxivbis.http.api.v1.json
import java.time.Instant
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import me.arcanis.ffxivbis.models.Permission import me.arcanis.ffxivbis.models.Permission
import spray.json._ import spray.json._
@ -24,16 +26,27 @@ trait JsonSupport extends SprayJsonSupport with DefaultJsonProtocol {
} }
} }
implicit val instantFormat: RootJsonFormat[Instant] = new RootJsonFormat[Instant] {
override def write(obj: Instant): JsValue = obj.toString.toJson
override def read(json: JsValue): Instant = json match {
case JsNumber(value) => Instant.ofEpochMilli(value.toLongExact)
case JsString(value) => Instant.parse(value)
case other => deserializationError(s"String or number expected, got $other")
}
}
implicit val actionFormat: RootJsonFormat[ApiAction.Value] = enumFormat(ApiAction) implicit val actionFormat: RootJsonFormat[ApiAction.Value] = enumFormat(ApiAction)
implicit val permissionFormat: RootJsonFormat[Permission.Value] = enumFormat(Permission) implicit val permissionFormat: RootJsonFormat[Permission.Value] = enumFormat(Permission)
implicit val errorFormat: RootJsonFormat[ErrorResponse] = jsonFormat1(ErrorResponse.apply) implicit val errorFormat: RootJsonFormat[ErrorResponse] = jsonFormat1(ErrorResponse.apply)
implicit val partyIdFormat: RootJsonFormat[PartyIdResponse] = jsonFormat1(PartyIdResponse.apply) implicit val partyIdFormat: RootJsonFormat[PartyIdResponse] = jsonFormat1(PartyIdResponse.apply)
implicit val pieceFormat: RootJsonFormat[PieceResponse] = jsonFormat3(PieceResponse.apply) implicit val pieceFormat: RootJsonFormat[PieceResponse] = jsonFormat3(PieceResponse.apply)
implicit val lootFormat: RootJsonFormat[LootResponse] = jsonFormat3(LootResponse.apply)
implicit val partyDescriptionFormat: RootJsonFormat[PartyDescriptionResponse] = jsonFormat2(PartyDescriptionResponse.apply)
implicit val playerFormat: RootJsonFormat[PlayerResponse] = jsonFormat7(PlayerResponse.apply) implicit val playerFormat: RootJsonFormat[PlayerResponse] = jsonFormat7(PlayerResponse.apply)
implicit val playerActionFormat: RootJsonFormat[PlayerActionResponse] = jsonFormat2(PlayerActionResponse.apply) implicit val playerActionFormat: RootJsonFormat[PlayerActionResponse] = jsonFormat2(PlayerActionResponse.apply)
implicit val playerIdFormat: RootJsonFormat[PlayerIdResponse] = jsonFormat3(PlayerIdResponse.apply) implicit val playerIdFormat: RootJsonFormat[PlayerIdResponse] = jsonFormat3(PlayerIdResponse.apply)
implicit val pieceActionFormat: RootJsonFormat[PieceActionResponse] = jsonFormat3(PieceActionResponse.apply) implicit val pieceActionFormat: RootJsonFormat[PieceActionResponse] = jsonFormat4(PieceActionResponse.apply)
implicit val playerBiSLinkFormat: RootJsonFormat[PlayerBiSLinkResponse] = jsonFormat2(PlayerBiSLinkResponse.apply) implicit val playerBiSLinkFormat: RootJsonFormat[PlayerBiSLinkResponse] = jsonFormat2(PlayerBiSLinkResponse.apply)
implicit val playerIdWithCountersFormat: RootJsonFormat[PlayerIdWithCountersResponse] = implicit val playerIdWithCountersFormat: RootJsonFormat[PlayerIdWithCountersResponse] =
jsonFormat9(PlayerIdWithCountersResponse.apply) jsonFormat9(PlayerIdWithCountersResponse.apply)

View File

@ -0,0 +1,20 @@
package me.arcanis.ffxivbis.http.api.v1.json
import java.time.Instant
import io.swagger.v3.oas.annotations.media.Schema
import me.arcanis.ffxivbis.models.Loot
case class LootResponse(
@Schema(description = "looted piece", required = true) piece: PieceResponse,
@Schema(description = "loot timestamp", required = true) timestamp: Instant,
@Schema(description = "is loot free for all", required = true) isFreeLoot: Boolean) {
def toLoot: Loot = Loot(-1, piece.toPiece, timestamp, isFreeLoot)
}
object LootResponse {
def fromLoot(loot: Loot): LootResponse =
LootResponse(PieceResponse.fromPiece(loot.piece), loot.timestamp, loot.isFreeLoot)
}

View File

@ -0,0 +1,25 @@
/*
* Copyright (c) 2020 Evgeniy Alekseev.
*
* This file is part of ffxivbis
* (see https://github.com/arcan1s/ffxivbis).
*
* License: 3-clause BSD, see https://opensource.org/licenses/BSD-3-Clause
*/
package me.arcanis.ffxivbis.http.api.v1.json
import io.swagger.v3.oas.annotations.media.Schema
import me.arcanis.ffxivbis.models.PartyDescription
case class PartyDescriptionResponse(
@Schema(description = "party id", required = true) partyId: String,
@Schema(description = "party name") partyAlias: Option[String]) {
def toDescription: PartyDescription = PartyDescription(partyId, partyAlias)
}
object PartyDescriptionResponse {
def fromDescription(description: PartyDescription): PartyDescriptionResponse =
PartyDescriptionResponse(description.partyId, description.partyAlias)
}

View File

@ -13,4 +13,5 @@ import io.swagger.v3.oas.annotations.media.Schema
case class PieceActionResponse( case class PieceActionResponse(
@Schema(description = "action to perform", required = true, `type` = "string", allowableValues = Array("add", "remove")) action: ApiAction.Value, @Schema(description = "action to perform", required = true, `type` = "string", allowableValues = Array("add", "remove")) action: ApiAction.Value,
@Schema(description = "piece description", required = true) piece: PieceResponse, @Schema(description = "piece description", required = true) piece: PieceResponse,
@Schema(description = "player description", required = true) playerId: PlayerIdResponse) @Schema(description = "player description", required = true) playerId: PlayerIdResponse,
@Schema(description = "is piece free to roll or not") isFreeLoot: Option[Boolean])

View File

@ -9,16 +9,18 @@
package me.arcanis.ffxivbis.http.api.v1.json package me.arcanis.ffxivbis.http.api.v1.json
import io.swagger.v3.oas.annotations.media.Schema import io.swagger.v3.oas.annotations.media.Schema
import me.arcanis.ffxivbis.models.{Job, Piece} import me.arcanis.ffxivbis.models.{Job, Piece, PieceType}
case class PieceResponse( case class PieceResponse(
@Schema(description = "is piece tome gear", required = true) isTome: Boolean, @Schema(description = "piece type", required = true) pieceType: String,
@Schema(description = "job name to which piece belong or AnyJob", required = true, example = "DNC") job: String, @Schema(description = "job name to which piece belong or AnyJob", required = true, example = "DNC") job: String,
@Schema(description = "piece name", required = true, example = "body") piece: String) { @Schema(description = "piece name", required = true, example = "body") piece: String) {
def toPiece: Piece = Piece(piece, isTome, Job.withName(job))
def toPiece: Piece = Piece(piece, PieceType.withName(pieceType), Job.withName(job))
} }
object PieceResponse { object PieceResponse {
def fromPiece(piece: Piece): PieceResponse = def fromPiece(piece: Piece): PieceResponse =
PieceResponse(piece.isTome, piece.job.toString, piece.piece) PieceResponse(piece.pieceType.toString, piece.job.toString, piece.piece)
} }

View File

@ -15,11 +15,13 @@ case class PlayerIdResponse(
@Schema(description = "unique party ID. Required in responses", example = "abcdefgh") partyId: Option[String], @Schema(description = "unique party ID. Required in responses", example = "abcdefgh") partyId: Option[String],
@Schema(description = "job name", required = true, example = "DNC") job: String, @Schema(description = "job name", required = true, example = "DNC") job: String,
@Schema(description = "player nick name", required = true, example = "Siuan Sanche") nick: String) { @Schema(description = "player nick name", required = true, example = "Siuan Sanche") nick: String) {
def withPartyId(partyId: String): PlayerId = def withPartyId(partyId: String): PlayerId =
PlayerId(partyId, Job.withName(job), nick) PlayerId(partyId, Job.withName(job), nick)
} }
object PlayerIdResponse { object PlayerIdResponse {
def fromPlayerId(playerId: PlayerId): PlayerIdResponse = def fromPlayerId(playerId: PlayerId): PlayerIdResponse =
PlayerIdResponse(Some(playerId.partyId), playerId.job.toString, playerId.nick) PlayerIdResponse(Some(playerId.partyId), playerId.job.toString, playerId.nick)
} }

View File

@ -23,6 +23,7 @@ case class PlayerIdWithCountersResponse(
@Schema(description = "total count of looted pieces", required = true) lootCountTotal: Int) @Schema(description = "total count of looted pieces", required = true) lootCountTotal: Int)
object PlayerIdWithCountersResponse { object PlayerIdWithCountersResponse {
def fromPlayerId(playerIdWithCounters: PlayerIdWithCounters): PlayerIdWithCountersResponse = def fromPlayerId(playerIdWithCounters: PlayerIdWithCounters): PlayerIdWithCountersResponse =
PlayerIdWithCountersResponse( PlayerIdWithCountersResponse(
playerIdWithCounters.partyId, playerIdWithCounters.partyId,

View File

@ -16,18 +16,22 @@ case class PlayerResponse(
@Schema(description = "job name", required = true, example = "DNC") job: String, @Schema(description = "job name", required = true, example = "DNC") job: String,
@Schema(description = "player nick name", required = true, example = "Siuan Sanche") nick: String, @Schema(description = "player nick name", required = true, example = "Siuan Sanche") nick: String,
@Schema(description = "pieces in best in slot") bis: Option[Seq[PieceResponse]], @Schema(description = "pieces in best in slot") bis: Option[Seq[PieceResponse]],
@Schema(description = "looted pieces") loot: Option[Seq[PieceResponse]], @Schema(description = "looted pieces") loot: Option[Seq[LootResponse]],
@Schema(description = "link to best in slot", example = "https://ffxiv.ariyala.com/19V5R") link: Option[String], @Schema(description = "link to best in slot", example = "https://ffxiv.ariyala.com/19V5R") link: Option[String],
@Schema(description = "player loot priority", `type` = "number") priority: Option[Int]) { @Schema(description = "player loot priority", `type` = "number") priority: Option[Int]) {
def toPlayer: Player = def toPlayer: Player =
Player(partyId, Job.withName(job), nick, Player(-1, partyId, Job.withName(job), nick,
BiS(bis.getOrElse(Seq.empty).map(_.toPiece)), loot.getOrElse(Seq.empty).map(_.toPiece), BiS(bis.getOrElse(Seq.empty).map(_.toPiece)),
loot.getOrElse(Seq.empty).map(_.toLoot),
link, priority.getOrElse(0)) link, priority.getOrElse(0))
} }
object PlayerResponse { object PlayerResponse {
def fromPlayer(player: Player): PlayerResponse = def fromPlayer(player: Player): PlayerResponse =
PlayerResponse(player.partyId, player.job.toString, player.nick, PlayerResponse(player.partyId, player.job.toString, player.nick,
Some(player.bis.pieces.map(PieceResponse.fromPiece)), Some(player.loot.map(PieceResponse.fromPiece)), Some(player.bis.pieces.map(PieceResponse.fromPiece)),
Some(player.loot.map(LootResponse.fromLoot)),
player.link, Some(player.priority)) player.link, Some(player.priority))
} }

View File

@ -16,11 +16,13 @@ case class UserResponse(
@Schema(description = "username to login to party", required = true, example = "siuan") username: String, @Schema(description = "username to login to party", required = true, example = "siuan") username: String,
@Schema(description = "password to login to party", required = true, example = "pa55w0rd") password: String, @Schema(description = "password to login to party", required = true, example = "pa55w0rd") password: String,
@Schema(description = "user permission", defaultValue = "get", allowableValues = Array("get", "post", "admin")) permission: Option[Permission.Value] = None) { @Schema(description = "user permission", defaultValue = "get", allowableValues = Array("get", "post", "admin")) permission: Option[Permission.Value] = None) {
def toUser: User = def toUser: User =
User(partyId, username, password, permission.getOrElse(Permission.get)) User(partyId, username, password, permission.getOrElse(Permission.get))
} }
object UserResponse { object UserResponse {
def fromUser(user: User): UserResponse = def fromUser(user: User): UserResponse =
UserResponse(user.partyId, user.username, "", Some(user.permission)) UserResponse(user.partyId, user.username, "", Some(user.permission))
} }

View File

@ -8,15 +8,20 @@
*/ */
package me.arcanis.ffxivbis.http.view package me.arcanis.ffxivbis.http.view
import akka.actor.ActorRef import akka.actor.typed.{ActorRef, Scheduler}
import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server._ import akka.http.scaladsl.server._
import akka.util.Timeout import akka.util.Timeout
import me.arcanis.ffxivbis.http.Authorization import me.arcanis.ffxivbis.http.{Authorization, PlayerHelper}
import me.arcanis.ffxivbis.messages.{BiSProviderMessage, Message}
class BasePartyView(override val storage: ActorRef)(implicit timeout: Timeout) import scala.util.{Failure, Success}
extends Authorization {
class BasePartyView(override val storage: ActorRef[Message],
override val provider: ActorRef[BiSProviderMessage])
(implicit timeout: Timeout, scheduler: Scheduler)
extends PlayerHelper with Authorization {
def route: Route = getIndex def route: Route = getIndex
@ -25,8 +30,10 @@ class BasePartyView(override val storage: ActorRef)(implicit timeout: Timeout)
extractExecutionContext { implicit executionContext => extractExecutionContext { implicit executionContext =>
authenticateBasicBCrypt(s"party $partyId", authGet(partyId)) { _ => authenticateBasicBCrypt(s"party $partyId", authGet(partyId)) { _ =>
get { get {
complete { onComplete(getPartyDescription(partyId)) {
(StatusCodes.OK, RootView.toHtml(BasePartyView.template(partyId))) case Success(description) =>
complete(StatusCodes.OK, RootView.toHtml(BasePartyView.template(partyId, description.alias)))
case Failure(exception) => throw exception
} }
} }
} }
@ -42,16 +49,16 @@ object BasePartyView {
def root(partyId: String): Text.TypedTag[String] = def root(partyId: String): Text.TypedTag[String] =
a(href:=s"/party/$partyId", title:="root")("root") a(href:=s"/party/$partyId", title:="root")("root")
def template(partyId: String): String = def template(partyId: String, alias: String): String =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">" + "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">" +
html(lang:="en", html(lang:="en",
head( head(
titleTag(s"Party $partyId"), titleTag(s"Party $alias"),
link(rel:="stylesheet", `type`:="text/css", href:="/static/styles.css") link(rel:="stylesheet", `type`:="text/css", href:="/static/styles.css")
), ),
body( body(
h2(s"Party $partyId"), h2(s"Party $alias"),
br, br,
h2(a(href:=s"/party/$partyId/players", title:="party")("party")), h2(a(href:=s"/party/$partyId/players", title:="party")("party")),
h2(a(href:=s"/party/$partyId/bis", title:="bis management")("best in slot")), h2(a(href:=s"/party/$partyId/bis", title:="bis management")("best in slot")),

View File

@ -8,19 +8,22 @@
*/ */
package me.arcanis.ffxivbis.http.view package me.arcanis.ffxivbis.http.view
import akka.actor.ActorRef import akka.actor.typed.{ActorRef, Scheduler}
import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server._ import akka.http.scaladsl.server._
import akka.util.Timeout import akka.util.Timeout
import me.arcanis.ffxivbis.http.{Authorization, BiSHelper} import me.arcanis.ffxivbis.http.{Authorization, BiSHelper}
import me.arcanis.ffxivbis.models.{Piece, Player, PlayerId} import me.arcanis.ffxivbis.messages.{BiSProviderMessage, Message}
import me.arcanis.ffxivbis.models.{Piece, PieceType, Player, PlayerId}
import scala.concurrent.{ExecutionContext, Future} import scala.concurrent.{ExecutionContext, Future}
import scala.util.Try import scala.util.Try
class BiSView(override val storage: ActorRef, ariyala: ActorRef)(implicit timeout: Timeout) class BiSView(override val storage: ActorRef[Message],
extends BiSHelper(storage, ariyala) with Authorization { override val provider: ActorRef[BiSProviderMessage])
(implicit timeout: Timeout, scheduler: Scheduler)
extends BiSHelper with Authorization {
def route: Route = getBiS ~ modifyBiS def route: Route = getBiS ~ modifyBiS
@ -46,10 +49,10 @@ class BiSView(override val storage: ActorRef, ariyala: ActorRef)(implicit timeou
extractExecutionContext { implicit executionContext => extractExecutionContext { implicit executionContext =>
authenticateBasicBCrypt(s"party $partyId", authPost(partyId)) { _ => authenticateBasicBCrypt(s"party $partyId", authPost(partyId)) { _ =>
post { post {
formFields("player".as[String], "piece".as[String].?, "is_tome".as[String].?, "link".as[String].?, "action".as[String]) { formFields("player".as[String], "piece".as[String].?, "piece_type".as[String].?, "link".as[String].?, "action".as[String]) {
(player, maybePiece, maybeIsTome, maybeLink, action) => (player, maybePiece, maybePieceType, maybeLink, action) =>
onComplete(modifyBiSCall(partyId, player, maybePiece, maybeIsTome, maybeLink, action)) { onComplete(modifyBiSCall(partyId, player, maybePiece, maybePieceType, maybeLink, action)) { _ =>
case _ => redirect(s"/party/$partyId/bis", StatusCodes.Found) redirect(s"/party/$partyId/bis", StatusCodes.Found)
} }
} }
} }
@ -58,25 +61,25 @@ class BiSView(override val storage: ActorRef, ariyala: ActorRef)(implicit timeou
} }
private def modifyBiSCall(partyId: String, player: String, private def modifyBiSCall(partyId: String, player: String,
maybePiece: Option[String], maybeIsTome: Option[String], maybePiece: Option[String], maybePieceType: Option[String],
maybeLink: Option[String], action: String) maybeLink: Option[String], action: String)
(implicit executionContext: ExecutionContext, timeout: Timeout): Future[Unit] = { (implicit executionContext: ExecutionContext, timeout: Timeout): Future[Unit] = {
import me.arcanis.ffxivbis.utils.Implicits._ def getPiece(playerId: PlayerId, piece: String, pieceType: String) =
Try(Piece(piece, PieceType.withName(pieceType), playerId.job)).toOption
def getPiece(playerId: PlayerId, piece: String) = def bisAction(playerId: PlayerId, piece: String, pieceType: String)(fn: Piece => Future[Unit]) =
Try(Piece(piece, maybeIsTome, playerId.job)).toOption getPiece(playerId, piece, pieceType) match {
case Some(item) => fn(item)
case _ => Future.failed(new Error(s"Could not construct piece from `$piece ($pieceType)`"))
}
PlayerId(partyId, player) match { PlayerId(partyId, player) match {
case Some(playerId) => (maybePiece, action, maybeLink) match { case Some(playerId) => (maybePiece, maybePieceType, action, maybeLink) match {
case (Some(piece), "add", _) => getPiece(playerId, piece) match { case (Some(piece), Some(pieceType), "add", _) =>
case Some(item) => addPieceBiS(playerId, item).map(_ => ()) bisAction(playerId, piece, pieceType)(addPieceBiS(playerId, _))
case _ => Future.failed(new Error(s"Could not construct piece from `$piece`")) case (Some(piece), Some(pieceType), "remove", _) =>
} bisAction(playerId, piece, pieceType)(removePieceBiS(playerId, _))
case (Some(piece), "remove", _) => getPiece(playerId, piece) match { case (_, _, "create", Some(link)) => putBiS(playerId, link)
case Some(item) => removePieceBiS(playerId, item).map(_ => ())
case _ => Future.failed(new Error(s"Could not construct piece from `$piece`"))
}
case (_, "create", Some(link)) => putBiS(playerId, link).map(_ => ())
case _ => Future.failed(new Error(s"Could not perform $action")) case _ => Future.failed(new Error(s"Could not perform $action"))
} }
case _ => Future.failed(new Error(s"Could not construct player id from `$player`")) case _ => Future.failed(new Error(s"Could not construct player id from `$player`"))
@ -107,13 +110,13 @@ object BiSView {
(for (player <- party) yield option(player.playerId.toString)), (for (player <- party) yield option(player.playerId.toString)),
select(name:="piece", id:="piece", title:="piece") select(name:="piece", id:="piece", title:="piece")
(for (piece <- Piece.available) yield option(piece)), (for (piece <- Piece.available) yield option(piece)),
input(name:="is_tome", id:="is_tome", title:="is tome", `type`:="checkbox"), select(name:="piece_type", id:="piece_type", title:="piece type")
label(`for`:="is_tome")("is tome gear"), (for (pieceType <- PieceType.available) yield option(pieceType.toString)),
input(name:="action", id:="action", `type`:="hidden", value:="add"), input(name:="action", id:="action", `type`:="hidden", value:="add"),
input(name:="add", id:="add", `type`:="submit", value:="add") input(name:="add", id:="add", `type`:="submit", value:="add")
), ),
form(action:="/bis", method:="post")( form(action:=s"/party/$partyId/bis", method:="post")(
select(name:="player", id:="player", title:="player") select(name:="player", id:="player", title:="player")
(for (player <- party) yield option(player.playerId.toString)), (for (player <- party) yield option(player.playerId.toString)),
input(name:="link", id:="link", placeholder:="player bis link", title:="link", `type`:="text"), input(name:="link", id:="link", placeholder:="player bis link", title:="link", `type`:="text"),
@ -125,18 +128,18 @@ object BiSView {
tr( tr(
th("player"), th("player"),
th("piece"), th("piece"),
th("is tome"), th("piece type"),
th("") th("")
), ),
for (player <- party; piece <- player.bis.pieces) yield tr( for (player <- party; piece <- player.bis.pieces) yield tr(
td(`class`:="include_search")(player.playerId.toString), td(`class`:="include_search")(player.playerId.toString),
td(`class`:="include_search")(piece.piece), td(`class`:="include_search")(piece.piece),
td(piece.isTomeToString), td(piece.pieceType.toString),
td( td(
form(action:=s"/party/$partyId/bis", method:="post")( form(action:=s"/party/$partyId/bis", method:="post")(
input(name:="player", id:="player", `type`:="hidden", value:=player.playerId.toString), input(name:="player", id:="player", `type`:="hidden", value:=player.playerId.toString),
input(name:="piece", id:="piece", `type`:="hidden", value:=piece.piece), input(name:="piece", id:="piece", `type`:="hidden", value:=piece.piece),
input(name:="is_tome", id:="is_tome", `type`:="hidden", value:=piece.isTomeToString), input(name:="piece_type", id:="piece_type", `type`:="hidden", value:=piece.pieceType.toString),
input(name:="action", id:="action", `type`:="hidden", value:="remove"), input(name:="action", id:="action", `type`:="hidden", value:="remove"),
input(name:="remove", id:="remove", `type`:="submit", value:="x") input(name:="remove", id:="remove", `type`:="submit", value:="x")
) )

View File

@ -12,6 +12,7 @@ import scalatags.Text
import scalatags.Text.all._ import scalatags.Text.all._
object ErrorView { object ErrorView {
def template(error: Option[String]): Text.TypedTag[String] = error match { def template(error: Option[String]): Text.TypedTag[String] = error match {
case Some(text) => p(id:="error", s"Error occurs: $text") case Some(text) => p(id:="error", s"Error occurs: $text")
case None => p("") case None => p("")

View File

@ -12,6 +12,7 @@ import scalatags.Text
import scalatags.Text.all._ import scalatags.Text.all._
object ExportToCSVView { object ExportToCSVView {
def template: Text.TypedTag[String] = def template: Text.TypedTag[String] =
div( div(
button(onclick:="exportTableToCsv('result.csv')")("Export to CSV"), button(onclick:="exportTableToCsv('result.csv')")("Export to CSV"),

View File

@ -8,18 +8,22 @@
*/ */
package me.arcanis.ffxivbis.http.view package me.arcanis.ffxivbis.http.view
import akka.actor.ActorRef import akka.actor.typed.{ActorRef, Scheduler}
import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server._ import akka.http.scaladsl.server._
import akka.util.Timeout import akka.util.Timeout
import me.arcanis.ffxivbis.http.UserHelper import me.arcanis.ffxivbis.http.{PlayerHelper, UserHelper}
import me.arcanis.ffxivbis.models.{Permission, User} import me.arcanis.ffxivbis.messages.{BiSProviderMessage, Message}
import me.arcanis.ffxivbis.models.{PartyDescription, Permission, User}
import scala.concurrent.Future
import scala.util.{Failure, Success} import scala.util.{Failure, Success}
class IndexView(storage: ActorRef)(implicit timeout: Timeout) class IndexView(override val storage: ActorRef[Message],
extends UserHelper(storage) { override val provider: ActorRef[BiSProviderMessage])
(implicit timeout: Timeout, scheduler: Scheduler)
extends PlayerHelper with UserHelper {
def route: Route = createParty ~ getIndex def route: Route = createParty ~ getIndex
@ -27,13 +31,17 @@ class IndexView(storage: ActorRef)(implicit timeout: Timeout)
path("party") { path("party") {
extractExecutionContext { implicit executionContext => extractExecutionContext { implicit executionContext =>
post { post {
formFields("username".as[String], "password".as[String]) { (username, password) => formFields("username".as[String], "password".as[String], "alias".as[String].?) { (username, password, maybeAlias) =>
onComplete(newPartyId) { onComplete {
case Success(partyId) => newPartyId.flatMap { partyId =>
val user = User(partyId, username, password, Permission.admin) val user = User(partyId, username, password, Permission.admin)
onComplete(addUser(user, isHashedPassword = false)) { addUser(user, isHashedPassword = false).flatMap { _ =>
case _ => redirect(s"/party/$partyId", StatusCodes.Found) if (maybeAlias.getOrElse("").isEmpty) Future.successful(partyId)
else updateDescription(PartyDescription(partyId, maybeAlias)).map(_ => partyId)
} }
}
} {
case Success(partyId) => redirect(s"/party/$partyId", StatusCodes.Found)
case Failure(exception) => throw exception case Failure(exception) => throw exception
} }
} }
@ -67,6 +75,7 @@ object IndexView {
body( body(
form(action:=s"party", method:="post")( form(action:=s"party", method:="post")(
label("create a new party"), label("create a new party"),
input(name:="alias", id:="alias", placeholder:="party alias", title:="alias", `type`:="text"),
input(name:="username", id:="username", placeholder:="username", title:="username", `type`:="text"), input(name:="username", id:="username", placeholder:="username", title:="username", `type`:="text"),
input(name:="password", id:="password", placeholder:="password", title:="password", `type`:="password"), input(name:="password", id:="password", placeholder:="password", title:="password", `type`:="password"),
input(name:="add", id:="add", `type`:="submit", value:="add") input(name:="add", id:="add", `type`:="submit", value:="add")

View File

@ -8,19 +8,21 @@
*/ */
package me.arcanis.ffxivbis.http.view package me.arcanis.ffxivbis.http.view
import akka.actor.ActorRef import akka.actor.typed.{ActorRef, Scheduler}
import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route import akka.http.scaladsl.server.Route
import akka.util.Timeout import akka.util.Timeout
import me.arcanis.ffxivbis.http.{Authorization, LootHelper} import me.arcanis.ffxivbis.http.{Authorization, LootHelper}
import me.arcanis.ffxivbis.models.{Job, Piece, PlayerIdWithCounters} import me.arcanis.ffxivbis.messages.Message
import me.arcanis.ffxivbis.models.{Job, Piece, PieceType, PlayerIdWithCounters}
import scala.concurrent.{ExecutionContext, Future} import scala.concurrent.{ExecutionContext, Future}
import scala.util.{Failure, Success, Try} import scala.util.{Failure, Success, Try}
class LootSuggestView(override val storage: ActorRef)(implicit timeout: Timeout) class LootSuggestView(override val storage: ActorRef[Message])
extends LootHelper(storage) with Authorization { (implicit timeout: Timeout, scheduler: Scheduler)
extends LootHelper with Authorization {
def route: Route = getIndex ~ suggestLoot def route: Route = getIndex ~ suggestLoot
@ -30,7 +32,7 @@ class LootSuggestView(override val storage: ActorRef)(implicit timeout: Timeout)
authenticateBasicBCrypt(s"party $partyId", authGet(partyId)) { _ => authenticateBasicBCrypt(s"party $partyId", authGet(partyId)) { _ =>
get { get {
complete { complete {
val text = LootSuggestView.template(partyId, Seq.empty, None, None) val text = LootSuggestView.template(partyId, Seq.empty, None, false, None)
(StatusCodes.OK, RootView.toHtml(text)) (StatusCodes.OK, RootView.toHtml(text))
} }
} }
@ -43,18 +45,20 @@ class LootSuggestView(override val storage: ActorRef)(implicit timeout: Timeout)
extractExecutionContext { implicit executionContext => extractExecutionContext { implicit executionContext =>
authenticateBasicBCrypt(s"party $partyId", authGet(partyId)) { _ => authenticateBasicBCrypt(s"party $partyId", authGet(partyId)) { _ =>
post { post {
formFields("piece".as[String], "job".as[String], "is_tome".as[String].?) { (piece, job, maybeTome) => formFields("piece".as[String], "job".as[String], "piece_type".as[String], "free_loot".as[String].?) {
import me.arcanis.ffxivbis.utils.Implicits._ (piece, job, pieceType, maybeFreeLoot) =>
val maybePiece = Try(Piece(piece, maybeTome, Job.withName(job))).toOption import me.arcanis.ffxivbis.utils.Implicits._
onComplete(suggestLootCall(partyId, maybePiece)) { val maybePiece = Try(Piece(piece, PieceType.withName(pieceType), Job.withName(job))).toOption
case Success(players) =>
val text = LootSuggestView.template(partyId, players, maybePiece, None) onComplete(suggestLootCall(partyId, maybePiece)) {
complete(StatusCodes.OK, RootView.toHtml(text)) case Success(players) =>
case Failure(exception) => val text = LootSuggestView.template(partyId, players, maybePiece, maybeFreeLoot, None)
val text = LootSuggestView.template(partyId, Seq.empty, maybePiece, Some(exception.getMessage)) complete(StatusCodes.OK, RootView.toHtml(text))
complete(StatusCodes.OK, RootView.toHtml(text)) case Failure(exception) =>
} val text = LootSuggestView.template(partyId, Seq.empty, None, false, Some(exception.getMessage))
complete(StatusCodes.OK, RootView.toHtml(text))
}
} }
} }
} }
@ -73,7 +77,8 @@ object LootSuggestView {
import scalatags.Text.all._ import scalatags.Text.all._
import scalatags.Text.tags2.{title => titleTag} import scalatags.Text.tags2.{title => titleTag}
def template(partyId: String, party: Seq[PlayerIdWithCounters], piece: Option[Piece], error: Option[String]): String = def template(partyId: String, party: Seq[PlayerIdWithCounters], piece: Option[Piece],
isFreeLoot: Boolean, error: Option[String]): String =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">" + "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">" +
html(lang:="en", html(lang:="en",
head( head(
@ -92,8 +97,10 @@ object LootSuggestView {
(for (piece <- Piece.available) yield option(piece)), (for (piece <- Piece.available) yield option(piece)),
select(name:="job", id:="job", title:="job") select(name:="job", id:="job", title:="job")
(for (job <- Job.availableWithAnyJob) yield option(job.toString)), (for (job <- Job.availableWithAnyJob) yield option(job.toString)),
input(name:="is_tome", id:="is_tome", title:="is tome", `type`:="checkbox"), select(name:="piece_type", id:="piece_type", title:="piece type")
label(`for`:="is_tome")("is tome gear"), (for (pieceType <- PieceType.available) yield option(pieceType.toString)),
input(name:="free_loot", id:="free_loot", title:="is free loot", `type`:="checkbox"),
label(`for`:="free_loot")("is free loot"),
input(name:="suggest", id:="suggest", `type`:="submit", value:="suggest") input(name:="suggest", id:="suggest", `type`:="submit", value:="suggest")
), ),
@ -116,7 +123,8 @@ object LootSuggestView {
form(action:=s"/party/$partyId/loot", method:="post")( form(action:=s"/party/$partyId/loot", method:="post")(
input(name:="player", id:="player", `type`:="hidden", value:=player.playerId.toString), input(name:="player", id:="player", `type`:="hidden", value:=player.playerId.toString),
input(name:="piece", id:="piece", `type`:="hidden", value:=piece.map(_.piece).getOrElse("")), input(name:="piece", id:="piece", `type`:="hidden", value:=piece.map(_.piece).getOrElse("")),
input(name:="is_tome", id:="is_tome", `type`:="hidden", value:=piece.map(_.isTomeToString).getOrElse("")), input(name:="piece_type", id:="piece_type", `type`:="hidden", value:=piece.map(_.pieceType.toString).getOrElse("")),
input(name:="free_loot", id:="free_loot", `type`:="hidden", value:=(if (isFreeLoot) "yes" else "no")),
input(name:="action", id:="action", `type`:="hidden", value:="add"), input(name:="action", id:="action", `type`:="hidden", value:="add"),
input(name:="add", id:="add", `type`:="submit", value:="add") input(name:="add", id:="add", `type`:="submit", value:="add")
) )

View File

@ -8,19 +8,21 @@
*/ */
package me.arcanis.ffxivbis.http.view package me.arcanis.ffxivbis.http.view
import akka.actor.ActorRef import akka.actor.typed.{ActorRef, Scheduler}
import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route import akka.http.scaladsl.server.Route
import akka.util.Timeout import akka.util.Timeout
import me.arcanis.ffxivbis.http.{Authorization, LootHelper} import me.arcanis.ffxivbis.http.{Authorization, LootHelper}
import me.arcanis.ffxivbis.models.{Piece, Player, PlayerId} import me.arcanis.ffxivbis.messages.Message
import me.arcanis.ffxivbis.models.{Piece, PieceType, Player, PlayerId}
import scala.concurrent.{ExecutionContext, Future} import scala.concurrent.{ExecutionContext, Future}
import scala.util.Try import scala.util.Try
class LootView (override val storage: ActorRef)(implicit timeout: Timeout) class LootView(override val storage: ActorRef[Message])
extends LootHelper(storage) with Authorization { (implicit timeout: Timeout, scheduler: Scheduler)
extends LootHelper with Authorization {
def route: Route = getLoot ~ modifyLoot def route: Route = getLoot ~ modifyLoot
@ -46,10 +48,10 @@ class LootView (override val storage: ActorRef)(implicit timeout: Timeout)
extractExecutionContext { implicit executionContext => extractExecutionContext { implicit executionContext =>
authenticateBasicBCrypt(s"party $partyId", authPost(partyId)) { _ => authenticateBasicBCrypt(s"party $partyId", authPost(partyId)) { _ =>
post { post {
formFields("player".as[String], "piece".as[String], "is_tome".as[String].?, "action".as[String]) { formFields("player".as[String], "piece".as[String], "piece_type".as[String], "action".as[String], "free_loot".as[String].?) {
(player, maybePiece, maybeIsTome, action) => (player, piece, pieceType, action, isFreeLoot) =>
onComplete(modifyLootCall(partyId, player, maybePiece, maybeIsTome, action)) { onComplete(modifyLootCall(partyId, player, piece, pieceType, isFreeLoot, action)) { _ =>
case _ => redirect(s"/party/$partyId/loot", StatusCodes.Found) redirect(s"/party/$partyId/loot", StatusCodes.Found)
} }
} }
} }
@ -57,20 +59,20 @@ class LootView (override val storage: ActorRef)(implicit timeout: Timeout)
} }
} }
private def modifyLootCall(partyId: String, player: String, private def modifyLootCall(partyId: String, player: String, maybePiece: String,
maybePiece: String, maybeIsTome: Option[String], maybePieceType: String, maybeFreeLoot: Option[String],
action: String) action: String)
(implicit executionContext: ExecutionContext, timeout: Timeout): Future[Unit] = { (implicit executionContext: ExecutionContext, timeout: Timeout): Future[Unit] = {
import me.arcanis.ffxivbis.utils.Implicits._ import me.arcanis.ffxivbis.utils.Implicits._
def getPiece(playerId: PlayerId) = def getPiece(playerId: PlayerId) =
Try(Piece(maybePiece, maybeIsTome, playerId.job)).toOption Try(Piece(maybePiece, PieceType.withName(maybePieceType), playerId.job)).toOption
PlayerId(partyId, player) match { PlayerId(partyId, player) match {
case Some(playerId) => (getPiece(playerId), action) match { case Some(playerId) => (getPiece(playerId), action) match {
case (Some(piece), "add") => addPieceLoot(playerId, piece).map(_ => ()) case (Some(piece), "add") => addPieceLoot(playerId, piece, maybeFreeLoot)
case (Some(piece), "remove") => removePieceLoot(playerId, piece).map(_ => ()) case (Some(piece), "remove") => removePieceLoot(playerId, piece)
case _ => Future.failed(new Error(s"Could not construct piece from `$maybePiece`")) case _ => Future.failed(new Error(s"Could not construct piece from `$maybePiece ($maybePieceType)`"))
} }
case _ => Future.failed(new Error(s"Could not construct player id from `$player`")) case _ => Future.failed(new Error(s"Could not construct player id from `$player`"))
} }
@ -100,8 +102,10 @@ object LootView {
(for (player <- party) yield option(player.playerId.toString)), (for (player <- party) yield option(player.playerId.toString)),
select(name:="piece", id:="piece", title:="piece") select(name:="piece", id:="piece", title:="piece")
(for (piece <- Piece.available) yield option(piece)), (for (piece <- Piece.available) yield option(piece)),
input(name:="is_tome", id:="is_tome", title:="is tome", `type`:="checkbox"), select(name:="piece_type", id:="piece_type", title:="piece type")
label(`for`:="is_tome")("is tome gear"), (for (pieceType <- PieceType.available) yield option(pieceType.toString)),
input(name:="free_loot", id:="free_loot", title:="is free loot", `type`:="checkbox"),
label(`for`:="free_loot")("is free loot"),
input(name:="action", id:="action", `type`:="hidden", value:="add"), input(name:="action", id:="action", `type`:="hidden", value:="add"),
input(name:="add", id:="add", `type`:="submit", value:="add") input(name:="add", id:="add", `type`:="submit", value:="add")
), ),
@ -110,18 +114,23 @@ object LootView {
tr( tr(
th("player"), th("player"),
th("piece"), th("piece"),
th("is tome"), th("piece type"),
th("is free loot"),
th("timestamp"),
th("") th("")
), ),
for (player <- party; piece <- player.loot) yield tr( for (player <- party; loot <- player.loot) yield tr(
td(`class`:="include_search")(player.playerId.toString), td(`class`:="include_search")(player.playerId.toString),
td(`class`:="include_search")(piece.piece), td(`class`:="include_search")(loot.piece.piece),
td(piece.isTomeToString), td(loot.piece.pieceType.toString),
td(loot.isFreeLootToString),
td(loot.timestamp.toString),
td( td(
form(action:=s"/party/$partyId/loot", method:="post")( form(action:=s"/party/$partyId/loot", method:="post")(
input(name:="player", id:="player", `type`:="hidden", value:=player.playerId.toString), input(name:="player", id:="player", `type`:="hidden", value:=player.playerId.toString),
input(name:="piece", id:="piece", `type`:="hidden", value:=piece.piece), input(name:="piece", id:="piece", `type`:="hidden", value:=loot.piece.piece),
input(name:="is_tome", id:="is_tome", `type`:="hidden", value:=piece.isTomeToString), input(name:="piece_type", id:="piece_type", `type`:="hidden", value:=loot.piece.pieceType.toString),
input(name:="free_loot", id:="free_loot", `type`:="hidden", value:=loot.isFreeLootToString),
input(name:="action", id:="action", `type`:="hidden", value:="remove"), input(name:="action", id:="action", `type`:="hidden", value:="remove"),
input(name:="remove", id:="remove", `type`:="submit", value:="x") input(name:="remove", id:="remove", `type`:="submit", value:="x")
) )

View File

@ -8,18 +8,21 @@
*/ */
package me.arcanis.ffxivbis.http.view package me.arcanis.ffxivbis.http.view
import akka.actor.ActorRef import akka.actor.typed.{ActorRef, Scheduler}
import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route import akka.http.scaladsl.server.Route
import akka.util.Timeout import akka.util.Timeout
import me.arcanis.ffxivbis.http.{Authorization, PlayerHelper} import me.arcanis.ffxivbis.http.{Authorization, PlayerHelper}
import me.arcanis.ffxivbis.messages.{BiSProviderMessage, Message}
import me.arcanis.ffxivbis.models._ import me.arcanis.ffxivbis.models._
import scala.concurrent.{ExecutionContext, Future} import scala.concurrent.{ExecutionContext, Future}
class PlayerView(override val storage: ActorRef, ariyala: ActorRef)(implicit timeout: Timeout) class PlayerView(override val storage: ActorRef[Message],
extends PlayerHelper(storage, ariyala) with Authorization { override val provider: ActorRef[BiSProviderMessage])
(implicit timeout: Timeout, scheduler: Scheduler)
extends PlayerHelper with Authorization {
def route: Route = getParty ~ modifyParty def route: Route = getParty ~ modifyParty
@ -47,8 +50,8 @@ class PlayerView(override val storage: ActorRef, ariyala: ActorRef)(implicit tim
post { post {
formFields("nick".as[String], "job".as[String], "priority".as[Int].?, "link".as[String].?, "action".as[String]) { formFields("nick".as[String], "job".as[String], "priority".as[Int].?, "link".as[String].?, "action".as[String]) {
(nick, job, maybePriority, maybeLink, action) => (nick, job, maybePriority, maybeLink, action) =>
onComplete(modifyPartyCall(partyId, nick, job, maybePriority, maybeLink, action)) { onComplete(modifyPartyCall(partyId, nick, job, maybePriority, maybeLink, action)) { _ =>
case _ => redirect(s"/party/$partyId/players", StatusCodes.Found) redirect(s"/party/$partyId/players", StatusCodes.Found)
} }
} }
} }
@ -62,11 +65,11 @@ class PlayerView(override val storage: ActorRef, ariyala: ActorRef)(implicit tim
(implicit executionContext: ExecutionContext, timeout: Timeout): Future[Unit] = { (implicit executionContext: ExecutionContext, timeout: Timeout): Future[Unit] = {
def maybePlayerId = PlayerId(partyId, Some(nick), Some(job)) def maybePlayerId = PlayerId(partyId, Some(nick), Some(job))
def player(playerId: PlayerId) = def player(playerId: PlayerId) =
Player(partyId, playerId.job, playerId.nick, BiS(), Seq.empty, maybeLink, maybePriority.getOrElse(0)) Player(-1, partyId, playerId.job, playerId.nick, BiS.empty, Seq.empty, maybeLink, maybePriority.getOrElse(0))
(action, maybePlayerId) match { (action, maybePlayerId) match {
case ("add", Some(playerId)) => addPlayer(player(playerId)).map(_ => ()) case ("add", Some(playerId)) => addPlayer(player(playerId))
case ("remove", Some(playerId)) => removePlayer(playerId).map(_ => ()) case ("remove", Some(playerId)) => removePlayer(playerId)
case _ => Future.failed(new Error(s"Could not perform $action with $nick ($job)")) case _ => Future.failed(new Error(s"Could not perform $action with $nick ($job)"))
} }
} }

View File

@ -8,21 +8,24 @@
*/ */
package me.arcanis.ffxivbis.http.view package me.arcanis.ffxivbis.http.view
import akka.actor.ActorRef import akka.actor.typed.{ActorRef, Scheduler}
import akka.http.scaladsl.model.{ContentTypes, HttpEntity} import akka.http.scaladsl.model.{ContentTypes, HttpEntity}
import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route import akka.http.scaladsl.server.Route
import akka.util.Timeout import akka.util.Timeout
import me.arcanis.ffxivbis.messages.{BiSProviderMessage, Message}
class RootView(storage: ActorRef, ariyala: ActorRef)(implicit timeout: Timeout) { class RootView(storage: ActorRef[Message],
provider: ActorRef[BiSProviderMessage])
(implicit timeout: Timeout, scheduler: Scheduler) {
private val basePartyView = new BasePartyView(storage) private val basePartyView = new BasePartyView(storage, provider)
private val indexView = new IndexView(storage) private val indexView = new IndexView(storage, provider)
private val biSView = new BiSView(storage, ariyala) private val biSView = new BiSView(storage, provider)
private val lootView = new LootView(storage) private val lootView = new LootView(storage)
private val lootSuggestView = new LootSuggestView(storage) private val lootSuggestView = new LootSuggestView(storage)
private val playerView = new PlayerView(storage, ariyala) private val playerView = new PlayerView(storage, provider)
private val userView = new UserView(storage) private val userView = new UserView(storage)
def route: Route = def route: Route =
@ -31,6 +34,7 @@ class RootView(storage: ActorRef, ariyala: ActorRef)(implicit timeout: Timeout)
} }
object RootView { object RootView {
def toHtml(template: String): HttpEntity.Strict = def toHtml(template: String): HttpEntity.Strict =
HttpEntity(ContentTypes.`text/html(UTF-8)`, template) HttpEntity(ContentTypes.`text/html(UTF-8)`, template)
} }

View File

@ -12,6 +12,7 @@ import scalatags.Text
import scalatags.Text.all._ import scalatags.Text.all._
object SearchLineView { object SearchLineView {
def template: Text.TypedTag[String] = def template: Text.TypedTag[String] =
div( div(
input( input(

View File

@ -8,19 +8,21 @@
*/ */
package me.arcanis.ffxivbis.http.view package me.arcanis.ffxivbis.http.view
import akka.actor.ActorRef import akka.actor.typed.{ActorRef, Scheduler}
import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route import akka.http.scaladsl.server.Route
import akka.util.Timeout import akka.util.Timeout
import me.arcanis.ffxivbis.http.{Authorization, UserHelper} import me.arcanis.ffxivbis.http.{Authorization, UserHelper}
import me.arcanis.ffxivbis.messages.Message
import me.arcanis.ffxivbis.models.{Permission, User} import me.arcanis.ffxivbis.models.{Permission, User}
import scala.concurrent.{ExecutionContext, Future} import scala.concurrent.{ExecutionContext, Future}
import scala.util.Try import scala.util.Try
class UserView(override val storage: ActorRef)(implicit timeout: Timeout) class UserView(override val storage: ActorRef[Message])
extends UserHelper(storage) with Authorization { (implicit timeout: Timeout, scheduler: Scheduler)
extends UserHelper with Authorization {
def route: Route = getUsers ~ modifyUsers def route: Route = getUsers ~ modifyUsers
@ -66,10 +68,10 @@ class UserView(override val storage: ActorRef)(implicit timeout: Timeout)
action match { action match {
case "add" => (maybePassword, permission) match { case "add" => (maybePassword, permission) match {
case (Some(password), Some(permission)) => addUser(User(partyId, username, password, permission), isHashedPassword = false).map(_ => ()) case (Some(password), Some(permission)) => addUser(User(partyId, username, password, permission), isHashedPassword = false)
case _ => Future.failed(new Error(s"Could not construct permission/password from `$maybePermission`/`$maybePassword`")) case _ => Future.failed(new Error(s"Could not construct permission/password from `$maybePermission`/`$maybePassword`"))
} }
case "remove" => removeUser(partyId, username).map(_ => ()) case "remove" => removeUser(partyId, username)
case _ => Future.failed(new Error(s"Could not perform $action")) case _ => Future.failed(new Error(s"Could not perform $action"))
} }
} }

View File

@ -0,0 +1,8 @@
package me.arcanis.ffxivbis.messages
import akka.actor.typed.ActorRef
import me.arcanis.ffxivbis.models.{BiS, Job}
sealed trait BiSProviderMessage
case class DownloadBiS(link: String, job: Job.Job, replyTo: ActorRef[BiS]) extends BiSProviderMessage

View File

@ -0,0 +1,10 @@
package me.arcanis.ffxivbis.messages
import akka.actor.typed.ActorRef
import me.arcanis.ffxivbis.models.Party
case class ForgetParty(partyId: String) extends Message
case class GetNewPartyId(replyTo: ActorRef[String]) extends Message
case class StoreParty(partyId: String, party: Party) extends Message

View File

@ -0,0 +1,77 @@
package me.arcanis.ffxivbis.messages
import akka.actor.typed.{ActorRef, Behavior}
import me.arcanis.ffxivbis.models.{Party, PartyDescription, Piece, Player, PlayerId, User}
import me.arcanis.ffxivbis.service.LootSelector
sealed trait DatabaseMessage extends Message {
def partyId: String
}
object DatabaseMessage {
type Handler = PartialFunction[DatabaseMessage, Behavior[DatabaseMessage]]
}
// bis handler
case class AddPieceToBis(playerId: PlayerId, piece: Piece, replyTo: ActorRef[Unit]) extends DatabaseMessage {
override def partyId: String = playerId.partyId
}
case class GetBiS(partyId: String, playerId: Option[PlayerId], replyTo: ActorRef[Seq[Player]]) extends DatabaseMessage
case class RemovePieceFromBiS(playerId: PlayerId, piece: Piece, replyTo: ActorRef[Unit]) extends DatabaseMessage {
override def partyId: String = playerId.partyId
}
case class RemovePiecesFromBiS(playerId: PlayerId, replyTo: ActorRef[Unit]) extends DatabaseMessage {
override def partyId: String = playerId.partyId
}
// loot handler
case class AddPieceTo(playerId: PlayerId, piece: Piece, isFreeLoot: Boolean, replyTo: ActorRef[Unit]) extends DatabaseMessage {
override def partyId: String = playerId.partyId
}
case class GetLoot(partyId: String, playerId: Option[PlayerId], replyTo: ActorRef[Seq[Player]]) extends DatabaseMessage
case class RemovePieceFrom(playerId: PlayerId, piece: Piece, replyTo: ActorRef[Unit]) extends DatabaseMessage {
override def partyId: String = playerId.partyId
}
case class SuggestLoot(partyId: String, piece: Piece, replyTo: ActorRef[LootSelector.LootSelectorResult]) extends DatabaseMessage
// party handler
case class AddPlayer(player: Player, replyTo: ActorRef[Unit]) extends DatabaseMessage {
override def partyId: String = player.partyId
}
case class GetParty(partyId: String, replyTo: ActorRef[Party]) extends DatabaseMessage
case class GetPartyDescription(partyId: String, replyTo: ActorRef[PartyDescription]) extends DatabaseMessage
case class GetPlayer(playerId: PlayerId, replyTo: ActorRef[Option[Player]]) extends DatabaseMessage {
override def partyId: String = playerId.partyId
}
case class RemovePlayer(playerId: PlayerId, replyTo: ActorRef[Unit]) extends DatabaseMessage {
override def partyId: String = playerId.partyId
}
case class UpdateParty(partyDescription: PartyDescription, replyTo: ActorRef[Unit]) extends DatabaseMessage {
override def partyId: String = partyDescription.partyId
}
// user handler
case class AddUser(user: User, isHashedPassword: Boolean, replyTo: ActorRef[Unit]) extends DatabaseMessage {
override def partyId: String = user.partyId
}
case class DeleteUser(partyId: String, username: String, replyTo: ActorRef[Unit]) extends DatabaseMessage
case class Exists(partyId: String, replyTo: ActorRef[Boolean]) extends DatabaseMessage
case class GetUser(partyId: String, username: String, replyTo: ActorRef[Option[User]]) extends DatabaseMessage
case class GetUsers(partyId: String, replyTo: ActorRef[Seq[User]]) extends DatabaseMessage

View File

@ -0,0 +1,9 @@
package me.arcanis.ffxivbis.messages
import akka.actor.typed.Behavior
trait Message
object Message {
type Handler = PartialFunction[Message, Behavior[Message]]
}

View File

@ -8,21 +8,7 @@
*/ */
package me.arcanis.ffxivbis.models package me.arcanis.ffxivbis.models
case class BiS(weapon: Option[Piece], case class BiS(pieces: Seq[Piece]) {
head: Option[Piece],
body: Option[Piece],
hands: Option[Piece],
waist: Option[Piece],
legs: Option[Piece],
feet: Option[Piece],
ears: Option[Piece],
neck: Option[Piece],
wrist: Option[Piece],
leftRing: Option[Piece],
rightRing: Option[Piece]) {
val pieces: Seq[Piece] =
Seq(weapon, head, body, hands, waist, legs, feet, ears, neck, wrist, leftRing, rightRing).flatten
def hasPiece(piece: Piece): Boolean = piece match { def hasPiece(piece: Piece): Boolean = piece match {
case upgrade: PieceUpgrade => upgrades.contains(upgrade) case upgrade: PieceUpgrade => upgrades.contains(upgrade)
@ -31,50 +17,27 @@ case class BiS(weapon: Option[Piece],
def upgrades: Map[PieceUpgrade, Int] = def upgrades: Map[PieceUpgrade, Int] =
pieces.groupBy(_.upgrade).foldLeft(Map.empty[PieceUpgrade, Int]) { pieces.groupBy(_.upgrade).foldLeft(Map.empty[PieceUpgrade, Int]) {
case (acc, (Some(k), v)) => acc + (k -> v.length) case (acc, (Some(k), v)) => acc + (k -> v.size)
case (acc, _) => acc case (acc, _) => acc
} withDefaultValue 0 } withDefaultValue 0
def withPiece(piece: Piece): BiS = copyWithPiece(piece.piece, Some(piece)) def withPiece(piece: Piece): BiS = copy(pieces :+ piece)
def withoutPiece(piece: Piece): BiS = copyWithPiece(piece.piece, None) def withoutPiece(piece: Piece): BiS = copy(pieces.filterNot(_.strictEqual(piece)))
private def copyWithPiece(name: String, piece: Option[Piece]): BiS = { override def equals(obj: Any): Boolean = {
val params = Map( def comparePieces(left: Seq[Piece], right: Seq[Piece]): Boolean =
"weapon" -> weapon, left.groupBy(identity).view.mapValues(_.size).forall {
"head" -> head, case (key, count) => right.count(_.strictEqual(key)) == count
"body" -> body, }
"hands" -> hands,
"waist" -> waist, obj match {
"legs" -> legs, case left: BiS => comparePieces(left.pieces, pieces)
"feet" -> feet, case _ => false
"ears" -> ears, }
"neck" -> neck,
"wrist" -> wrist,
"left ring" -> leftRing,
"right ring" -> rightRing
) + (name -> piece)
BiS(params)
} }
} }
object BiS { object BiS {
def apply(data: Map[String, Option[Piece]]): BiS =
BiS(
data.get("weapon").flatten,
data.get("head").flatten,
data.get("body").flatten,
data.get("hands").flatten,
data.get("waist").flatten,
data.get("legs").flatten,
data.get("feet").flatten,
data.get("ears").flatten,
data.get("neck").flatten,
data.get("wrist").flatten,
data.get("left ring").flatten,
data.get("right ring").flatten)
def apply(): BiS = BiS(Seq.empty) def empty: BiS = BiS(Seq.empty)
def apply(pieces: Seq[Piece]): BiS =
BiS(pieces.map(piece => piece.piece -> Some(piece)).toMap)
} }

View File

@ -9,6 +9,7 @@
package me.arcanis.ffxivbis.models package me.arcanis.ffxivbis.models
object Job { object Job {
sealed trait RightSide sealed trait RightSide
object AccessoriesDex extends RightSide object AccessoriesDex extends RightSide
object AccessoriesInt extends RightSide object AccessoriesInt extends RightSide
@ -25,13 +26,15 @@ object Job {
object BodyTanks extends LeftSide object BodyTanks extends LeftSide
object BodyRanges extends LeftSide object BodyRanges extends LeftSide
sealed trait Job { sealed trait Job extends Equals {
def leftSide: LeftSide def leftSide: LeftSide
def rightSide: RightSide def rightSide: RightSide
// conversion to string to avoid recursion // conversion to string to avoid recursion
override def canEqual(that: Any): Boolean = that.isInstanceOf[Job]
override def equals(obj: Any): Boolean = { override def equals(obj: Any): Boolean = {
def canEqual(obj: Any): Boolean = obj.isInstanceOf[Job]
def equality(objRepr: String): Boolean = objRepr match { def equality(objRepr: String): Boolean = objRepr match {
case _ if objRepr == AnyJob.toString => true case _ if objRepr == AnyJob.toString => true
case _ if this.toString == AnyJob.toString => true case _ if this.toString == AnyJob.toString => true
@ -59,6 +62,10 @@ object Job {
val leftSide: LeftSide = BodyMnks val leftSide: LeftSide = BodyMnks
val rightSide: RightSide = AccessoriesStr val rightSide: RightSide = AccessoriesStr
} }
trait Drgs extends Job {
val leftSide: LeftSide = BodyDrgs
val rightSide: RightSide = AccessoriesStr
}
trait Tanks extends Job { trait Tanks extends Job {
val leftSide: LeftSide = BodyTanks val leftSide: LeftSide = BodyTanks
val rightSide: RightSide = AccessoriesVit val rightSide: RightSide = AccessoriesVit
@ -76,12 +83,11 @@ object Job {
case object WHM extends Healers case object WHM extends Healers
case object SCH extends Healers case object SCH extends Healers
case object AST extends Healers case object AST extends Healers
case object SGE extends Healers
case object MNK extends Mnks case object MNK extends Mnks
case object DRG extends Job { case object DRG extends Drgs
val leftSide: LeftSide = BodyDrgs case object RPR extends Drgs
val rightSide: RightSide = AccessoriesStr
}
case object NIN extends Job { case object NIN extends Job {
val leftSide: LeftSide = BodyNins val leftSide: LeftSide = BodyNins
val rightSide: RightSide = AccessoriesDex val rightSide: RightSide = AccessoriesDex
@ -97,13 +103,13 @@ object Job {
case object RDM extends Casters case object RDM extends Casters
lazy val available: Seq[Job] = lazy val available: Seq[Job] =
Seq(PLD, WAR, DRK, GNB, WHM, SCH, AST, MNK, DRG, NIN, SAM, BRD, MCH, DNC, BLM, SMN, RDM) Seq(PLD, WAR, DRK, GNB, WHM, SCH, AST, SGE, MNK, DRG, RPR, NIN, SAM, BRD, MCH, DNC, BLM, SMN, RDM)
lazy val availableWithAnyJob: Seq[Job] = available.prepended(AnyJob) lazy val availableWithAnyJob: Seq[Job] = available.prepended(AnyJob)
def withName(job: String): Job.Job = def withName(job: String): Job.Job =
availableWithAnyJob.find(_.toString.equalsIgnoreCase(job.toUpperCase)) match { availableWithAnyJob.find(_.toString.equalsIgnoreCase(job)) match {
case Some(value) => value case Some(value) => value
case None if job.isEmpty => AnyJob case None if job.isEmpty => AnyJob
case _ => throw new IllegalArgumentException("Invalid or unknown job") case _ => throw new IllegalArgumentException(s"Invalid or unknown job $job")
} }
} }

View File

@ -8,4 +8,9 @@
*/ */
package me.arcanis.ffxivbis.models package me.arcanis.ffxivbis.models
case class Loot(playerId: Long, piece: Piece) import java.time.Instant
case class Loot(playerId: Long, piece: Piece, timestamp: Instant, isFreeLoot: Boolean) {
def isFreeLootToString: String = if (isFreeLoot) "yes" else "no"
}

View File

@ -15,15 +15,15 @@ import me.arcanis.ffxivbis.service.LootSelector
import scala.jdk.CollectionConverters._ import scala.jdk.CollectionConverters._
import scala.util.Random import scala.util.Random
case class Party(partyId: String, rules: Seq[String], players: Map[PlayerId, Player]) case class Party(partyDescription: PartyDescription, rules: Seq[String], players: Map[PlayerId, Player])
extends StrictLogging { extends StrictLogging {
require(players.keys.forall(_.partyId == partyId), "party id must be same") require(players.keys.forall(_.partyId == partyDescription.partyId), "party id must be same")
def getPlayers: Seq[Player] = players.values.toSeq def getPlayers: Seq[Player] = players.values.toSeq
def player(playerId: PlayerId): Option[Player] = players.get(playerId) def player(playerId: PlayerId): Option[Player] = players.get(playerId)
def withPlayer(player: Player): Party = def withPlayer(player: Player): Party =
try { try {
require(player.partyId == partyId, "player must belong to this party") require(player.partyId == partyDescription.partyId, "player must belong to this party")
copy(players = players + (player.playerId -> player)) copy(players = players + (player.playerId -> player))
} catch { } catch {
case exception: Exception => case exception: Exception =>
@ -36,20 +36,18 @@ case class Party(partyId: String, rules: Seq[String], players: Map[PlayerId, Pla
} }
object Party { object Party {
def apply(partyId: Option[String], config: Config): Party =
new Party(partyId.getOrElse(randomPartyId), getRules(config), Map.empty)
def apply(partyId: String, config: Config, def apply(party: PartyDescription, config: Config,
players: Map[Long, Player], bis: Seq[Loot], loot: Seq[Loot]): Party = { players: Map[Long, Player], bis: Seq[Loot], loot: Seq[Loot]): Party = {
val bisByPlayer = bis.groupBy(_.playerId).view.mapValues(piece => BiS(piece.map(_.piece))) val bisByPlayer = bis.groupBy(_.playerId).view.mapValues(piece => BiS(piece.map(_.piece)))
val lootByPlayer = loot.groupBy(_.playerId).view.mapValues(_.map(_.piece)) val lootByPlayer = loot.groupBy(_.playerId).view
val playersWithItems = players.foldLeft(Map.empty[PlayerId, Player]) { val playersWithItems = players.foldLeft(Map.empty[PlayerId, Player]) {
case (acc, (playerId, player)) => case (acc, (playerId, player)) =>
acc + (player.playerId -> player acc + (player.playerId -> player
.withBiS(bisByPlayer.get(playerId)) .withBiS(bisByPlayer.get(playerId))
.withLoot(lootByPlayer.getOrElse(playerId, Seq.empty))) .withLoot(lootByPlayer.getOrElse(playerId, Seq.empty)))
} }
Party(partyId, getRules(config), playersWithItems) Party(party, getRules(config), playersWithItems)
} }
def getRules(config: Config): Seq[String] = def getRules(config: Config): Seq[String] =

View File

@ -0,0 +1,19 @@
/*
* Copyright (c) 2020 Evgeniy Alekseev.
*
* This file is part of ffxivbis
* (see https://github.com/arcan1s/ffxivbis).
*
* License: 3-clause BSD, see https://opensource.org/licenses/BSD-3-Clause
*/
package me.arcanis.ffxivbis.models
case class PartyDescription(partyId: String, partyAlias: Option[String]) {
def alias: String = partyAlias.getOrElse(partyId)
}
object PartyDescription {
def empty(partyId: String): PartyDescription = PartyDescription(partyId, None)
}

View File

@ -8,79 +8,85 @@
*/ */
package me.arcanis.ffxivbis.models package me.arcanis.ffxivbis.models
sealed trait Piece { sealed trait Piece extends Equals {
def isTome: Boolean
def pieceType: PieceType.PieceType
def job: Job.Job def job: Job.Job
def piece: String def piece: String
def withJob(other: Job.Job): Piece def withJob(other: Job.Job): Piece
def isTomeToString: String = if (isTome) "yes" else "no" def upgrade: Option[PieceUpgrade] = {
def upgrade: Option[PieceUpgrade] = this match { val isTome = pieceType == PieceType.Tome
case _ if !isTome => None Some(this).collect {
case _: Waist => Some(AccessoryUpgrade) case _: PieceAccessory if isTome => AccessoryUpgrade
case _: PieceAccessory => Some(AccessoryUpgrade) case _: PieceBody if isTome => BodyUpgrade
case _: PieceBody => Some(BodyUpgrade) case _: PieceWeapon if isTome => WeaponUpgrade
case _: PieceWeapon => Some(WeaponUpgrade) }
} }
// used for ring comparison
def strictEqual(obj: Any): Boolean = equals(obj)
} }
trait PieceAccessory extends Piece trait PieceAccessory extends Piece
trait PieceBody extends Piece trait PieceBody extends Piece
trait PieceUpgrade extends Piece { trait PieceUpgrade extends Piece {
val isTome: Boolean = true val pieceType: PieceType.PieceType = PieceType.Tome
val job: Job.Job = Job.AnyJob val job: Job.Job = Job.AnyJob
def withJob(other: Job.Job): Piece = this def withJob(other: Job.Job): Piece = this
} }
trait PieceWeapon extends Piece trait PieceWeapon extends Piece
case class Weapon(override val isTome: Boolean, override val job: Job.Job) extends PieceWeapon { case class Weapon(override val pieceType: PieceType.PieceType, override val job: Job.Job) extends PieceWeapon {
val piece: String = "weapon" val piece: String = "weapon"
def withJob(other: Job.Job): Piece = copy(job = other) def withJob(other: Job.Job): Piece = copy(job = other)
} }
case class Head(override val isTome: Boolean, override val job: Job.Job) extends PieceBody { case class Head(override val pieceType: PieceType.PieceType, override val job: Job.Job) extends PieceBody {
val piece: String = "head" val piece: String = "head"
def withJob(other: Job.Job): Piece = copy(job = other) def withJob(other: Job.Job): Piece = copy(job = other)
} }
case class Body(override val isTome: Boolean, override val job: Job.Job) extends PieceBody { case class Body(override val pieceType: PieceType.PieceType, override val job: Job.Job) extends PieceBody {
val piece: String = "body" val piece: String = "body"
def withJob(other: Job.Job): Piece = copy(job = other) def withJob(other: Job.Job): Piece = copy(job = other)
} }
case class Hands(override val isTome: Boolean, override val job: Job.Job) extends PieceBody { case class Hands(override val pieceType: PieceType.PieceType, override val job: Job.Job) extends PieceBody {
val piece: String = "hands" val piece: String = "hands"
def withJob(other: Job.Job): Piece = copy(job = other) def withJob(other: Job.Job): Piece = copy(job = other)
} }
case class Waist(override val isTome: Boolean, override val job: Job.Job) extends PieceBody { case class Legs(override val pieceType: PieceType.PieceType, override val job: Job.Job) extends PieceBody {
val piece: String = "waist"
def withJob(other: Job.Job): Piece = copy(job = other)
}
case class Legs(override val isTome: Boolean, override val job: Job.Job) extends PieceBody {
val piece: String = "legs" val piece: String = "legs"
def withJob(other: Job.Job): Piece = copy(job = other) def withJob(other: Job.Job): Piece = copy(job = other)
} }
case class Feet(override val isTome: Boolean, override val job: Job.Job) extends PieceBody { case class Feet(override val pieceType: PieceType.PieceType, override val job: Job.Job) extends PieceBody {
val piece: String = "feet" val piece: String = "feet"
def withJob(other: Job.Job): Piece = copy(job = other) def withJob(other: Job.Job): Piece = copy(job = other)
} }
case class Ears(override val isTome: Boolean, override val job: Job.Job) extends PieceAccessory { case class Ears(override val pieceType: PieceType.PieceType, override val job: Job.Job) extends PieceAccessory {
val piece: String = "ears" val piece: String = "ears"
def withJob(other: Job.Job): Piece = copy(job = other) def withJob(other: Job.Job): Piece = copy(job = other)
} }
case class Neck(override val isTome: Boolean, override val job: Job.Job) extends PieceAccessory { case class Neck(override val pieceType: PieceType.PieceType, override val job: Job.Job) extends PieceAccessory {
val piece: String = "neck" val piece: String = "neck"
def withJob(other: Job.Job): Piece = copy(job = other) def withJob(other: Job.Job): Piece = copy(job = other)
} }
case class Wrist(override val isTome: Boolean, override val job: Job.Job) extends PieceAccessory { case class Wrist(override val pieceType: PieceType.PieceType, override val job: Job.Job) extends PieceAccessory {
val piece: String = "wrist" val piece: String = "wrist"
def withJob(other: Job.Job): Piece = copy(job = other) def withJob(other: Job.Job): Piece = copy(job = other)
} }
case class Ring(override val isTome: Boolean, override val job: Job.Job, override val piece: String = "ring") case class Ring(override val pieceType: PieceType.PieceType, override val job: Job.Job, override val piece: String = "ring")
extends PieceAccessory { extends PieceAccessory {
def withJob(other: Job.Job): Piece = copy(job = other) def withJob(other: Job.Job): Piece = copy(job = other)
override def equals(obj: Any): Boolean = obj match { override def equals(obj: Any): Boolean = obj match {
case Ring(thatIsTome, thatJob, _) => (thatIsTome == isTome) && (thatJob == job) case Ring(thatPieceType, thatJob, _) => (thatPieceType == pieceType) && (thatJob == job)
case _ => false
}
override def strictEqual(obj: Any): Boolean = obj match {
case ring: Ring => equals(obj) && (ring.piece == this.piece)
case _ => false case _ => false
} }
} }
@ -96,19 +102,18 @@ case object WeaponUpgrade extends PieceUpgrade {
} }
object Piece { object Piece {
def apply(piece: String, isTome: Boolean, job: Job.Job = Job.AnyJob): Piece = def apply(piece: String, pieceType: PieceType.PieceType, job: Job.Job = Job.AnyJob): Piece =
piece.toLowerCase match { piece.toLowerCase match {
case "weapon" => Weapon(isTome, job) case "weapon" => Weapon(pieceType, job)
case "head" => Head(isTome, job) case "head" => Head(pieceType, job)
case "body" => Body(isTome, job) case "body" => Body(pieceType, job)
case "hands" => Hands(isTome, job) case "hands" => Hands(pieceType, job)
case "waist" => Waist(isTome, job) case "legs" => Legs(pieceType, job)
case "legs" => Legs(isTome, job) case "feet" => Feet(pieceType, job)
case "feet" => Feet(isTome, job) case "ears" => Ears(pieceType, job)
case "ears" => Ears(isTome, job) case "neck" => Neck(pieceType, job)
case "neck" => Neck(isTome, job) case "wrist" | "wrists" => Wrist(pieceType, job)
case "wrist" => Wrist(isTome, job) case ring @ ("ring" | "left ring" | "right ring") => Ring(pieceType, job, ring)
case ring @ ("ring" | "left ring" | "right ring") => Ring(isTome, job, ring)
case "accessory upgrade" => AccessoryUpgrade case "accessory upgrade" => AccessoryUpgrade
case "body upgrade" => BodyUpgrade case "body upgrade" => BodyUpgrade
case "weapon upgrade" => WeaponUpgrade case "weapon upgrade" => WeaponUpgrade
@ -116,7 +121,7 @@ object Piece {
} }
lazy val available: Seq[String] = Seq("weapon", lazy val available: Seq[String] = Seq("weapon",
"head", "body", "hands", "waist", "legs", "feet", "head", "body", "hands", "legs", "feet",
"ears", "neck", "wrist", "left ring", "right ring", "ears", "neck", "wrist", "left ring", "right ring",
"accessory upgrade", "body upgrade", "weapon upgrade") "accessory upgrade", "body upgrade", "weapon upgrade")
} }

View File

@ -0,0 +1,20 @@
package me.arcanis.ffxivbis.models
object PieceType {
sealed trait PieceType
case object Crafted extends PieceType
case object Tome extends PieceType
case object Savage extends PieceType
case object Artifact extends PieceType
lazy val available: Seq[PieceType] =
Seq(Crafted, Tome, Savage, Artifact)
def withName(pieceType: String): PieceType =
available.find(_.toString.equalsIgnoreCase(pieceType)) match {
case Some(value) => value
case _ => throw new IllegalArgumentException(s"Invalid or unknown piece type $pieceType")
}
}

View File

@ -8,11 +8,12 @@
*/ */
package me.arcanis.ffxivbis.models package me.arcanis.ffxivbis.models
case class Player(partyId: String, case class Player(id: Long,
partyId: String,
job: Job.Job, job: Job.Job,
nick: String, nick: String,
bis: BiS, bis: BiS,
loot: Seq[Piece], loot: Seq[Loot],
link: Option[String] = None, link: Option[String] = None,
priority: Int = 0) { priority: Int = 0) {
require(job ne Job.AnyJob, "AnyJob is not allowed") require(job ne Job.AnyJob, "AnyJob is not allowed")
@ -27,10 +28,10 @@ case class Player(partyId: String,
partyId, job, nick, isRequired(piece), priority, partyId, job, nick, isRequired(piece), priority,
bisCountTotal(piece), lootCount(piece), bisCountTotal(piece), lootCount(piece),
lootCountBiS(piece), lootCountTotal(piece)) lootCountBiS(piece), lootCountTotal(piece))
def withLoot(piece: Piece): Player = withLoot(Seq(piece)) def withLoot(piece: Loot): Player = withLoot(Seq(piece))
def withLoot(list: Seq[Piece]): Player = list match { def withLoot(list: Seq[Loot]): Player = {
case Nil => this require(loot.forall(_.playerId == id), "player id must be same")
case _ => copy(loot = list) copy(loot = loot ++ list)
} }
def isRequired(piece: Option[Piece]): Boolean = { def isRequired(piece: Option[Piece]): Boolean = {
@ -42,12 +43,12 @@ case class Player(partyId: String,
} }
} }
def bisCountTotal(piece: Option[Piece]): Int = bis.pieces.count(!_.isTome) def bisCountTotal(piece: Option[Piece]): Int = bis.pieces.count(_.pieceType == PieceType.Savage)
def lootCount(piece: Option[Piece]): Int = piece match { def lootCount(piece: Option[Piece]): Int = piece match {
case Some(p) => loot.count(_ == p) case Some(p) => loot.count(item => !item.isFreeLoot && item.piece == p)
case None => lootCountTotal(piece) case None => lootCountTotal(piece)
} }
def lootCountBiS(piece: Option[Piece]): Int = loot.count(bis.hasPiece) def lootCountBiS(piece: Option[Piece]): Int = loot.map(_.piece).count(bis.hasPiece)
def lootCountTotal(piece: Option[Piece]): Int = loot.length def lootCountTotal(piece: Option[Piece]): Int = loot.count(!_.isFreeLoot)
def lootPriority(piece: Piece): Int = priority def lootPriority(piece: Piece): Int = priority
} }

View File

@ -12,6 +12,7 @@ import scala.util.Try
import scala.util.matching.Regex import scala.util.matching.Regex
trait PlayerIdBase { trait PlayerIdBase {
def job: Job.Job def job: Job.Job
def nick: String def nick: String
@ -21,6 +22,7 @@ trait PlayerIdBase {
case class PlayerId(partyId: String, job: Job.Job, nick: String) extends PlayerIdBase case class PlayerId(partyId: String, job: Job.Job, nick: String) extends PlayerIdBase
object PlayerId { object PlayerId {
def apply(partyId: String, maybeNick: Option[String], maybeJob: Option[String]): Option[PlayerId] = def apply(partyId: String, maybeNick: Option[String], maybeJob: Option[String]): Option[PlayerId] =
(maybeNick, maybeJob) match { (maybeNick, maybeJob) match {
case (Some(nick), Some(job)) => Try(PlayerId(partyId, Job.withName(job), nick)).toOption case (Some(nick), Some(job)) => Try(PlayerId(partyId, Job.withName(job), nick)).toOption

View File

@ -38,6 +38,7 @@ case class PlayerIdWithCounters(partyId: String,
} }
object PlayerIdWithCounters { object PlayerIdWithCounters {
private case class PlayerCountersComparator(values: Int*) { private case class PlayerCountersComparator(values: Int*) {
def >(that: PlayerCountersComparator): Boolean = { def >(that: PlayerCountersComparator): Boolean = {
@scala.annotation.tailrec @scala.annotation.tailrec

View File

@ -1,142 +0,0 @@
/*
* Copyright (c) 2019 Evgeniy Alekseev.
*
* This file is part of ffxivbis
* (see https://github.com/arcan1s/ffxivbis).
*
* License: 3-clause BSD, see https://opensource.org/licenses/BSD-3-Clause
*/
package me.arcanis.ffxivbis.service
import java.nio.file.Paths
import akka.actor.{Actor, Props}
import akka.http.scaladsl.Http
import akka.http.scaladsl.model._
import akka.pattern.pipe
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{Keep, Sink}
import akka.util.ByteString
import com.typesafe.scalalogging.StrictLogging
import me.arcanis.ffxivbis.models.{BiS, Job, Piece}
import spray.json._
import scala.concurrent.{ExecutionContext, Future}
import scala.util.Try
class Ariyala extends Actor with StrictLogging {
import Ariyala._
private val settings = context.system.settings.config
private val ariyalaUrl = settings.getString("me.arcanis.ffxivbis.ariyala.ariyala-url")
private val xivapiUrl = settings.getString("me.arcanis.ffxivbis.ariyala.xivapi-url")
private val xivapiKey = Try(settings.getString("me.arcanis.ffxivbis.ariyala.xivapi-key")).toOption
private val http = Http()(context.system)
implicit private val materializer: ActorMaterializer = ActorMaterializer()
implicit private val executionContext: ExecutionContext =
context.system.dispatchers.lookup("me.arcanis.ffxivbis.default-dispatcher")
override def receive: Receive = {
case GetBiS(link, job) =>
val client = sender()
get(link, job).map(BiS(_)).pipeTo(client)
}
override def postStop(): Unit = {
http.shutdownAllConnectionPools()
super.postStop()
}
private def get(link: String, job: Job.Job): Future[Seq[Piece]] = {
val id = Paths.get(link).normalize.getFileName.toString
val uri = Uri(ariyalaUrl)
.withPath(Uri.Path / "store.app")
.withQuery(Uri.Query(Map("identifier" -> id)))
sendRequest(uri, Ariyala.parseAriyalaJsonToPieces(job, getIsTome))
}
private def getIsTome(itemIds: Seq[Long]): Future[Map[Long, Boolean]] = {
val uri = Uri(xivapiUrl)
.withPath(Uri.Path / "item")
.withQuery(Uri.Query(Map(
"columns" -> Seq("ID", "Lot").mkString(","),
"ids" -> itemIds.mkString(","),
"private_key" -> xivapiKey.getOrElse("")
)))
sendRequest(uri, Ariyala.parseXivapiJson)
}
private def sendRequest[T](uri: Uri, parser: JsObject => Future[T]): Future[T] =
http.singleRequest(HttpRequest(uri = uri)).map {
case HttpResponse(status, _, entity, _) if status.isSuccess() =>
entity.dataBytes
.fold(ByteString.empty)(_ ++ _)
.map(_.utf8String)
.map(result => parser(result.parseJson.asJsObject))
.toMat(Sink.head)(Keep.right)
.run().flatten
case other => Future.failed(new Error(s"Invalid response from server $other"))
}.flatten
}
object Ariyala {
def props: Props = Props(new Ariyala)
case class GetBiS(link: String, job: Job.Job)
private def parseAriyalaJson(job: Job.Job)(js: JsObject)
(implicit executionContext: ExecutionContext): Future[Map[String, Long]] =
Future {
val apiJob = js.fields.get("content") match {
case Some(JsString(value)) => value
case other => throw deserializationError(s"Invalid job name $other")
}
js.fields.get("datasets") match {
case Some(datasets: JsObject) =>
val fields = datasets.fields
fields.getOrElse(apiJob, fields(job.toString)).asJsObject
.fields("normal").asJsObject
.fields("items").asJsObject
.fields.foldLeft(Map.empty[String, Long]) {
case (acc, (key, JsNumber(id))) => remapKey(key).map(k => acc + (k -> id.toLong)).getOrElse(acc)
case (acc, _) => acc
}
case other => throw deserializationError(s"Invalid json $other")
}
}
private def parseAriyalaJsonToPieces(job: Job.Job, isTome: Seq[Long] => Future[Map[Long, Boolean]])(js: JsObject)
(implicit executionContext: ExecutionContext): Future[Seq[Piece]] =
parseAriyalaJson(job)(js).flatMap { pieces =>
isTome(pieces.values.toSeq).map { tomePieces =>
pieces.view.mapValues(tomePieces).map {
case (piece, isTomePiece) => Piece(piece, isTomePiece, job)
}.toSeq
}
}
private def parseXivapiJson(js: JsObject)
(implicit executionContext: ExecutionContext): Future[Map[Long, Boolean]] =
Future {
js.fields("Results") match {
case array: JsArray =>
array.elements.map(_.asJsObject.getFields("ID", "Lot") match {
case Seq(JsNumber(id), JsNumber(isTome)) => id.toLong -> (isTome == 0)
case other => throw deserializationError(s"Could not parse $other")
}).toMap
case other => throw deserializationError(s"Could not parse $other")
}
}
private def remapKey(key: String): Option[String] = key match {
case "mainhand" => Some("weapon")
case "chest" => Some("body")
case "ringLeft" => Some("left ring")
case "ringRight" => Some("right ring")
case "head" | "hands" | "waist" | "legs" | "feet" | "ears" | "neck" | "wrist" => Some(key)
case _ => None
}
}

View File

@ -8,38 +8,40 @@
*/ */
package me.arcanis.ffxivbis.service package me.arcanis.ffxivbis.service
import akka.actor.Actor import akka.actor.typed.Behavior
import akka.actor.typed.scaladsl.Behaviors
import com.typesafe.config.Config
import com.typesafe.scalalogging.StrictLogging import com.typesafe.scalalogging.StrictLogging
import me.arcanis.ffxivbis.messages.{DatabaseMessage}
import me.arcanis.ffxivbis.models.{Party, Player, PlayerId} import me.arcanis.ffxivbis.models.{Party, Player, PlayerId}
import me.arcanis.ffxivbis.service.impl.DatabaseImpl
import me.arcanis.ffxivbis.storage.DatabaseProfile import me.arcanis.ffxivbis.storage.DatabaseProfile
import scala.concurrent.{ExecutionContext, Future} import scala.concurrent.{ExecutionContext, Future}
trait Database extends Actor with StrictLogging { trait Database extends StrictLogging {
implicit def executionContext: ExecutionContext implicit def executionContext: ExecutionContext
def config: Config
def profile: DatabaseProfile def profile: DatabaseProfile
override def postStop(): Unit = {
profile.db.close()
super.postStop()
}
def filterParty(party: Party, maybePlayerId: Option[PlayerId]): Seq[Player] = def filterParty(party: Party, maybePlayerId: Option[PlayerId]): Seq[Player] =
(party, maybePlayerId) match { maybePlayerId match {
case (_, Some(playerId)) => party.player(playerId).map(Seq(_)).getOrElse(Seq.empty) case Some(playerId) => party.player(playerId).map(Seq(_)).getOrElse(Seq.empty)
case (_, _) => party.getPlayers case _ => party.getPlayers
} }
def getParty(partyId: String, withBiS: Boolean, withLoot: Boolean): Future[Party] = def getParty(partyId: String, withBiS: Boolean, withLoot: Boolean): Future[Party] =
for { for {
partyDescription <- profile.getPartyDescription(partyId)
players <- profile.getParty(partyId) players <- profile.getParty(partyId)
bis <- if (withBiS) profile.getPiecesBiS(partyId) else Future(Seq.empty) bis <- if (withBiS) profile.getPiecesBiS(partyId) else Future(Seq.empty)
loot <- if (withLoot) profile.getPieces(partyId) else Future(Seq.empty) loot <- if (withLoot) profile.getPieces(partyId) else Future(Seq.empty)
} yield Party(partyId, context.system.settings.config, players, bis, loot) } yield Party(partyDescription, config, players, bis, loot)
} }
object Database { object Database {
trait DatabaseRequest {
def partyId: String def apply(): Behavior[DatabaseMessage] =
} Behaviors.setup[DatabaseMessage](context => new DatabaseImpl(context))
} }

View File

@ -21,6 +21,7 @@ class LootSelector(players: Seq[Player], piece: Piece, orderBy: Seq[String]) {
} }
object LootSelector { object LootSelector {
def apply(players: Seq[Player], piece: Piece, orderBy: Seq[String]): LootSelectorResult = def apply(players: Seq[Player], piece: Piece, orderBy: Seq[String]): LootSelectorResult =
new LootSelector(players, piece, orderBy).suggest new LootSelector(players, piece, orderBy).suggest

View File

@ -8,57 +8,65 @@
*/ */
package me.arcanis.ffxivbis.service package me.arcanis.ffxivbis.service
import akka.actor.{Actor, ActorRef, Props} import akka.actor.typed.scaladsl.AskPattern.Askable
import akka.pattern.{ask, pipe} import akka.actor.typed.{ActorRef, Behavior, DispatcherSelector, Scheduler}
import akka.actor.typed.scaladsl.{AbstractBehavior, ActorContext, Behaviors}
import akka.util.Timeout import akka.util.Timeout
import com.typesafe.scalalogging.StrictLogging import com.typesafe.scalalogging.StrictLogging
import me.arcanis.ffxivbis.messages.{DatabaseMessage, Exists, ForgetParty, GetNewPartyId, GetParty, Message, StoreParty}
import me.arcanis.ffxivbis.models.Party import me.arcanis.ffxivbis.models.Party
import scala.concurrent.duration.FiniteDuration import scala.concurrent.duration.FiniteDuration
import scala.concurrent.{ExecutionContext, Future} import scala.concurrent.{ExecutionContext, Future}
class PartyService(storage: ActorRef) extends Actor with StrictLogging { class PartyService(context: ActorContext[Message], storage: ActorRef[DatabaseMessage])
import PartyService._ extends AbstractBehavior[Message](context) with StrictLogging {
import me.arcanis.ffxivbis.utils.Implicits._ import me.arcanis.ffxivbis.utils.Implicits._
private val cacheTimeout: FiniteDuration = private val cacheTimeout: FiniteDuration =
context.system.settings.config.getDuration("me.arcanis.ffxivbis.settings.cache-timeout") context.system.settings.config.getDuration("me.arcanis.ffxivbis.settings.cache-timeout")
implicit private val executionContext: ExecutionContext = implicit private val executionContext: ExecutionContext = {
context.system.dispatchers.lookup("me.arcanis.ffxivbis.default-dispatcher") val selector = DispatcherSelector.fromConfig("me.arcanis.ffxivbis.default-dispatcher")
context.system.dispatchers.lookup(selector)
}
implicit private val timeout: Timeout = implicit private val timeout: Timeout =
context.system.settings.config.getDuration("me.arcanis.ffxivbis.settings.request-timeout") context.system.settings.config.getDuration("me.arcanis.ffxivbis.settings.request-timeout")
implicit private val scheduler: Scheduler = context.system.scheduler
override def receive: Receive = handle(Map.empty) override def onMessage(msg: Message): Behavior[Message] = handle(Map.empty)(msg)
private def handle(cache: Map[String, Party]): Receive = { private def handle(cache: Map[String, Party]): Message.Handler = {
case ForgetParty(partyId) => case ForgetParty(partyId) =>
context become handle(cache - partyId) Behaviors.receiveMessage(handle(cache - partyId))
case GetNewPartyId => case GetNewPartyId(client) =>
val client = sender() getPartyId.foreach(client ! _)
getPartyId.pipeTo(client) Behaviors.same
case req @ impl.DatabasePartyHandler.GetParty(partyId) => case StoreParty(partyId, party) =>
val client = sender() Behaviors.receiveMessage(handle(cache.updated(partyId, party)))
case GetParty(partyId, client) =>
val party = cache.get(partyId) match { val party = cache.get(partyId) match {
case Some(party) => Future.successful(party) case Some(party) => Future.successful(party)
case None => case None =>
(storage ? req).mapTo[Party].map { party => storage.ask(ref => GetParty(partyId, ref)).map { party =>
context become handle(cache + (partyId -> party)) context.self ! StoreParty(partyId, party)
context.system.scheduler.scheduleOnce(cacheTimeout, self, ForgetParty(partyId)) context.system.scheduler.scheduleOnce(cacheTimeout, () => context.self ! ForgetParty(partyId))
party party
} }
} }
party.pipeTo(client) party.foreach(client ! _)
Behaviors.same
case req: Database.DatabaseRequest => case req: DatabaseMessage =>
self ! ForgetParty(req.partyId) storage ! req
storage.forward(req) Behaviors.receiveMessage(handle(cache - req.partyId))
} }
private def getPartyId: Future[String] = { private def getPartyId: Future[String] = {
val partyId = Party.randomPartyId val partyId = Party.randomPartyId
(storage ? impl.DatabaseUserHandler.Exists(partyId)).mapTo[Boolean].flatMap { storage.ask(ref => Exists(partyId, ref)).flatMap {
case true => getPartyId case true => getPartyId
case false => Future.successful(partyId) case false => Future.successful(partyId)
} }
@ -66,8 +74,7 @@ class PartyService(storage: ActorRef) extends Actor with StrictLogging {
} }
object PartyService { object PartyService {
def props(storage: ActorRef): Props = Props(new PartyService(storage))
case class ForgetParty(partyId: String) def apply(storage: ActorRef[DatabaseMessage]): Behavior[Message] =
case object GetNewPartyId Behaviors.setup[Message](context => new PartyService(context, storage))
} }

View File

@ -1,47 +0,0 @@
package me.arcanis.ffxivbis.service
import java.time.Instant
import akka.actor.Actor
import scala.concurrent.ExecutionContext
import scala.concurrent.duration.FiniteDuration
class RateLimiter extends Actor {
import RateLimiter._
import me.arcanis.ffxivbis.utils.Implicits._
implicit private val executionContext: ExecutionContext = context.system.dispatcher
private val maxRequestCount: Int = context.system.settings.config.getInt("me.arcanis.ffxivbis.web.limits.max-count")
private val requestInterval: FiniteDuration = context.system.settings.config.getDuration("me.arcanis.ffxivbis.web.limits.interval")
override def receive: Receive = handle(Map.empty)
private def handle(cache: Map[String, Usage]): Receive = {
case username: String =>
val client = sender()
val usage = if (cache.contains(username)) {
cache(username)
} else {
context.system.scheduler.scheduleOnce(requestInterval, self, Reset(username))
Usage()
}
context become handle(cache + (username -> usage.increment))
val response = if (usage.count > maxRequestCount) Some(usage.left) else None
client ! response
case Reset(username) =>
context become handle(cache - username)
}
}
object RateLimiter {
private case class Usage(count: Int = 0, since: Instant = Instant.now) {
def increment: Usage = copy(count = count + 1)
def left: Long = (Instant.now.toEpochMilli - since.toEpochMilli) / 1000
}
case class Reset(username: String)
}

View File

@ -0,0 +1,44 @@
/*
* Copyright (c) 2019 Evgeniy Alekseev.
*
* This file is part of ffxivbis
* (see https://github.com/arcan1s/ffxivbis).
*
* License: 3-clause BSD, see https://opensource.org/licenses/BSD-3-Clause
*/
package me.arcanis.ffxivbis.service.bis
import akka.http.scaladsl.model.Uri
import me.arcanis.ffxivbis.models.Job
import spray.json.{JsNumber, JsObject, JsString, deserializationError}
import scala.concurrent.{ExecutionContext, Future}
object Ariyala {
def idParser(job: Job.Job, js: JsObject)
(implicit executionContext: ExecutionContext): Future[Map[String, Long]] =
Future {
val apiJob = js.fields.get("content") match {
case Some(JsString(value)) => value
case other => throw deserializationError(s"Invalid job name $other")
}
js.fields.get("datasets") match {
case Some(datasets: JsObject) =>
val fields = datasets.fields
fields.getOrElse(apiJob, fields(job.toString)).asJsObject
.fields("normal").asJsObject
.fields("items").asJsObject
.fields.foldLeft(Map.empty[String, Long]) {
case (acc, (key, JsNumber(id))) => BisProvider.remapKey(key).map(k => acc + (k -> id.toLong)).getOrElse(acc)
case (acc, _) => acc
}
case other => throw deserializationError(s"Invalid json $other")
}
}
def uri(root: Uri, id: String): Uri =
root
.withPath(Uri.Path / "store.app")
.withQuery(Uri.Query(Map("identifier" -> id)))
}

View File

@ -0,0 +1,83 @@
/*
* Copyright (c) 2019 Evgeniy Alekseev.
*
* This file is part of ffxivbis
* (see https://github.com/arcan1s/ffxivbis).
*
* License: 3-clause BSD, see https://opensource.org/licenses/BSD-3-Clause
*/
package me.arcanis.ffxivbis.service.bis
import java.nio.file.Paths
import akka.actor.ClassicActorSystemProvider
import akka.actor.typed.{Behavior, PostStop, Signal}
import akka.actor.typed.scaladsl.{AbstractBehavior, ActorContext, Behaviors}
import akka.http.scaladsl.model._
import com.typesafe.scalalogging.StrictLogging
import me.arcanis.ffxivbis.messages.{BiSProviderMessage, DownloadBiS}
import me.arcanis.ffxivbis.models.{BiS, Job, Piece, PieceType}
import spray.json._
import scala.concurrent.{ExecutionContext, Future}
class BisProvider(context: ActorContext[BiSProviderMessage])
extends AbstractBehavior[BiSProviderMessage](context) with XivApi with StrictLogging {
override def system: ClassicActorSystemProvider = context.system
override def onMessage(msg: BiSProviderMessage): Behavior[BiSProviderMessage] =
msg match {
case DownloadBiS(link, job, client) =>
get(link, job).map(BiS(_)).foreach(client ! _)
Behaviors.same
}
override def onSignal: PartialFunction[Signal, Behavior[BiSProviderMessage]] = {
case PostStop =>
shutdown()
Behaviors.same
}
private def get(link: String, job: Job.Job): Future[Seq[Piece]] = {
val url = Uri(link)
val id = Paths.get(link).normalize.getFileName.toString
val (idParser, uri) =
if (url.authority.host.address().contains("etro")) {
(Etro.idParser(_, _), Etro.uri(url, id))
} else {
(Ariyala.idParser(_, _), Ariyala.uri(url, id))
}
sendRequest(uri, BisProvider.parseBisJsonToPieces(job, idParser, getPieceType))
}
}
object BisProvider {
def apply(): Behavior[BiSProviderMessage] =
Behaviors.setup[BiSProviderMessage](context => new BisProvider(context))
private def parseBisJsonToPieces(job: Job.Job,
idParser: (Job.Job, JsObject) => Future[Map[String, Long]],
pieceTypes: Seq[Long] => Future[Map[Long, PieceType.PieceType]])
(js: JsObject)
(implicit executionContext: ExecutionContext): Future[Seq[Piece]] =
idParser(job, js).flatMap { pieces =>
pieceTypes(pieces.values.toSeq).map { types =>
pieces.view.mapValues(types).map {
case (piece, pieceType) => Piece(piece, pieceType, job)
}.toSeq
}
}
def remapKey(key: String): Option[String] = key match {
case "mainhand" => Some("weapon")
case "chest" => Some("body")
case "ringLeft" | "fingerL" => Some("left ring")
case "ringRight" | "fingerR" => Some("right ring")
case "weapon" | "head" | "body" | "hands" | "legs" | "feet" | "ears" | "neck" | "wrist" | "wrists" => Some(key)
case _ => None
}
}

View File

@ -0,0 +1,30 @@
/*
* Copyright (c) 2019 Evgeniy Alekseev.
*
* This file is part of ffxivbis
* (see https://github.com/arcan1s/ffxivbis).
*
* License: 3-clause BSD, see https://opensource.org/licenses/BSD-3-Clause
*/
package me.arcanis.ffxivbis.service.bis
import akka.http.scaladsl.model.Uri
import me.arcanis.ffxivbis.models.Job
import spray.json.{JsNumber, JsObject}
import scala.concurrent.{ExecutionContext, Future}
object Etro {
def idParser(job: Job.Job, js: JsObject)
(implicit executionContext: ExecutionContext): Future[Map[String, Long]] =
Future {
js.fields.foldLeft(Map.empty[String, Long]) {
case (acc, (key, JsNumber(id))) => BisProvider.remapKey(key).map(k => acc + (k -> id.toLong)).getOrElse(acc)
case (acc, _) => acc
}
}
def uri(root: Uri, id: String): Uri =
root.withPath(Uri.Path / "api" / "gearsets" / id)
}

View File

@ -0,0 +1,47 @@
/*
* Copyright (c) 2019 Evgeniy Alekseev.
*
* This file is part of ffxivbis
* (see https://github.com/arcan1s/ffxivbis).
*
* License: 3-clause BSD, see https://opensource.org/licenses/BSD-3-Clause
*/
package me.arcanis.ffxivbis.service.bis
import akka.actor.ClassicActorSystemProvider
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.headers.Location
import akka.http.scaladsl.model.{HttpRequest, HttpResponse, Uri}
import akka.stream.Materializer
import akka.stream.scaladsl.{Keep, Sink}
import akka.util.ByteString
import spray.json._
import scala.concurrent.{ExecutionContext, Future}
trait RequestExecutor {
def system: ClassicActorSystemProvider
private val http = Http()(system)
implicit val materializer: Materializer = Materializer.createMaterializer(system)
implicit val executionContext: ExecutionContext =
system.classicSystem.dispatchers.lookup("me.arcanis.ffxivbis.default-dispatcher")
def sendRequest[T](uri: Uri, parser: JsObject => Future[T]): Future[T] =
http.singleRequest(HttpRequest(uri = uri)).map {
case r: HttpResponse if r.status.isRedirection() =>
val location = r.header[Location].get.uri
sendRequest(uri.withPath(location.path), parser)
case HttpResponse(status, _, entity, _) if status.isSuccess() =>
entity.dataBytes
.fold(ByteString.empty)(_ ++ _)
.map(_.utf8String)
.map(result => parser(result.parseJson.asJsObject))
.toMat(Sink.head)(Keep.right)
.run().flatten
case other => Future.failed(new Error(s"Invalid response from server $other"))
}.flatten
def shutdown(): Unit = http.shutdownAllConnectionPools()
}

View File

@ -0,0 +1,112 @@
/*
* Copyright (c) 2019 Evgeniy Alekseev.
*
* This file is part of ffxivbis
* (see https://github.com/arcan1s/ffxivbis).
*
* License: 3-clause BSD, see https://opensource.org/licenses/BSD-3-Clause
*/
package me.arcanis.ffxivbis.service.bis
import akka.http.scaladsl.model.Uri
import me.arcanis.ffxivbis.models.PieceType
import spray.json._
import scala.concurrent.{ExecutionContext, Future}
import scala.util.Try
trait XivApi extends RequestExecutor {
private val config = system.classicSystem.settings.config
private val xivapiUrl = config.getString("me.arcanis.ffxivbis.bis-provider.xivapi-url")
private val xivapiKey = Try(config.getString("me.arcanis.ffxivbis.bis-provider.xivapi-key")).toOption
def getPieceType(itemIds: Seq[Long]): Future[Map[Long, PieceType.PieceType]] = {
val uriForItems = Uri(xivapiUrl)
.withPath(Uri.Path / "item")
.withQuery(Uri.Query(Map(
"columns" -> Seq("ID", "GameContentLinks").mkString(","),
"ids" -> itemIds.mkString(","),
"private_key" -> xivapiKey.getOrElse("")
)))
sendRequest(uriForItems, XivApi.parseXivapiJsonToShop).flatMap { shops =>
val shopIds = shops.values.map(_._2).toSet
val columns = shops.values.map(pair => s"ItemCost${pair._1}").toSet
val uriForShops = Uri(xivapiUrl)
.withPath(Uri.Path / "specialshop")
.withQuery(Uri.Query(Map(
"columns" -> (columns + "ID").mkString(","),
"ids" -> shopIds.mkString(","),
"private_key" -> xivapiKey.getOrElse("")
)))
sendRequest(uriForShops, XivApi.parseXivapiJsonToType(shops))
}
}
}
object XivApi {
private def parseXivapiJsonToShop(js: JsObject)
(implicit executionContext: ExecutionContext): Future[Map[Long, (String, Long)]] = {
def extractTraderId(js: JsObject) = {
js.fields
.get("Recipe").map(_ => "crafted" -> -1L) // you can craft this item
.orElse { // lets try shop items
js.fields("SpecialShop").asJsObject
.fields.collectFirst {
case (shopName, JsArray(array)) if shopName.startsWith("ItemReceive") =>
val shopId = array.head match {
case JsNumber(id) => id.toLong
case other => throw deserializationError(s"Could not parse $other")
}
shopName.replace("ItemReceive", "") -> shopId
}
}.getOrElse(throw deserializationError(s"Could not parse $js"))
}
Future {
js.fields("Results") match {
case array: JsArray =>
array.elements.map(_.asJsObject.getFields("ID", "GameContentLinks") match {
case Seq(JsNumber(id), shop) => id.toLong -> extractTraderId(shop.asJsObject)
case other => throw deserializationError(s"Could not parse $other")
}).toMap
case other => throw deserializationError(s"Could not parse $other")
}
}
}
private def parseXivapiJsonToType(shops: Map[Long, (String, Long)])(js: JsObject)
(implicit executionContext: ExecutionContext): Future[Map[Long, PieceType.PieceType]] =
Future {
val shopMap = js.fields("Results") match {
case array: JsArray =>
array.elements.map { shop =>
shop.asJsObject.fields("ID") match {
case JsNumber(id) => id.toLong -> shop.asJsObject
case other => throw deserializationError(s"Could not parse $other")
}
}.toMap
case other => throw deserializationError(s"Could not parse $other")
}
shops.map { case (itemId, (index, shopId)) =>
val pieceType =
if (index == "crafted" && shopId == -1) PieceType.Crafted
else {
Try(shopMap(shopId).fields(s"ItemCost$index").asJsObject)
.toOption
.getOrElse(throw new Exception(s"${shopMap(shopId).fields(s"ItemCost$index")}, $index"))
.getFields("IsUnique", "StackSize") match {
case Seq(JsNumber(isUnique), JsNumber(stackSize)) =>
if (isUnique == 1 || stackSize.toLong != 999) PieceType.Tome // either upgraded gear or tomes found
else PieceType.Savage
case other => throw deserializationError(s"Could not parse $other")
}
}
itemId -> pieceType
}
}
}

View File

@ -8,36 +8,30 @@
*/ */
package me.arcanis.ffxivbis.service.impl package me.arcanis.ffxivbis.service.impl
import akka.pattern.pipe import akka.actor.typed.scaladsl.Behaviors
import me.arcanis.ffxivbis.models.{Piece, PlayerId} import me.arcanis.ffxivbis.messages.{AddPieceToBis, DatabaseMessage, GetBiS, RemovePieceFromBiS, RemovePiecesFromBiS}
import me.arcanis.ffxivbis.service.Database import me.arcanis.ffxivbis.service.Database
trait DatabaseBiSHandler { this: Database => trait DatabaseBiSHandler { this: Database =>
import DatabaseBiSHandler._
def bisHandler: Receive = { def bisHandler: DatabaseMessage.Handler = {
case AddPieceToBis(playerId, piece) => case AddPieceToBis(playerId, piece, client) =>
val client = sender() profile.insertPieceBiS(playerId, piece).foreach(_ => client ! ())
profile.insertPieceBiS(playerId, piece).pipeTo(client) Behaviors.same
case GetBiS(partyId, maybePlayerId) => case GetBiS(partyId, maybePlayerId, client) =>
val client = sender()
getParty(partyId, withBiS = true, withLoot = false) getParty(partyId, withBiS = true, withLoot = false)
.map(filterParty(_, maybePlayerId)) .map(filterParty(_, maybePlayerId))
.pipeTo(client) .foreach(client ! _)
Behaviors.same
case RemovePieceFromBiS(playerId, piece) => case RemovePieceFromBiS(playerId, piece, client) =>
val client = sender() profile.deletePieceBiS(playerId, piece).foreach(_ => client ! ())
profile.deletePieceBiS(playerId, piece).pipeTo(client) Behaviors.same
case RemovePiecesFromBiS(playerId, client) =>
profile.deletePiecesBiS(playerId).foreach(_ => client ! ())
Behaviors.same
} }
} }
object DatabaseBiSHandler {
case class AddPieceToBis(playerId: PlayerId, piece: Piece) extends Database.DatabaseRequest {
override def partyId: String = playerId.partyId
}
case class GetBiS(partyId: String, playerId: Option[PlayerId]) extends Database.DatabaseRequest
case class RemovePieceFromBiS(playerId: PlayerId, piece: Piece) extends Database.DatabaseRequest {
override def partyId: String = playerId.partyId
}
}

View File

@ -8,24 +8,29 @@
*/ */
package me.arcanis.ffxivbis.service.impl package me.arcanis.ffxivbis.service.impl
import akka.actor.Props import akka.actor.typed.{Behavior, DispatcherSelector}
import akka.actor.typed.scaladsl.{AbstractBehavior, ActorContext}
import com.typesafe.config.Config
import me.arcanis.ffxivbis.messages.DatabaseMessage
import me.arcanis.ffxivbis.service.Database import me.arcanis.ffxivbis.service.Database
import me.arcanis.ffxivbis.storage.DatabaseProfile import me.arcanis.ffxivbis.storage.DatabaseProfile
import scala.concurrent.ExecutionContext import scala.concurrent.ExecutionContext
class DatabaseImpl extends Database class DatabaseImpl(context: ActorContext[DatabaseMessage])
extends AbstractBehavior[DatabaseMessage](context) with Database
with DatabaseBiSHandler with DatabaseLootHandler with DatabaseBiSHandler with DatabaseLootHandler
with DatabasePartyHandler with DatabaseUserHandler { with DatabasePartyHandler with DatabaseUserHandler {
implicit val executionContext: ExecutionContext = override implicit val executionContext: ExecutionContext = {
context.system.dispatchers.lookup("me.arcanis.ffxivbis.default-dispatcher") val selector = DispatcherSelector.fromConfig("me.arcanis.ffxivbis.default-dispatcher")
val profile = new DatabaseProfile(executionContext, context.system.settings.config) context.system.dispatchers.lookup(selector)
}
override val config: Config = context.system.settings.config
override val profile: DatabaseProfile = new DatabaseProfile(executionContext, config)
override def receive: Receive = override def onMessage(msg: DatabaseMessage): Behavior[DatabaseMessage] = handle(msg)
private def handle: DatabaseMessage.Handler =
bisHandler orElse lootHandler orElse partyHandler orElse userHandler bisHandler orElse lootHandler orElse partyHandler orElse userHandler
} }
object DatabaseImpl {
def props: Props = Props(new DatabaseImpl)
}

View File

@ -8,41 +8,35 @@
*/ */
package me.arcanis.ffxivbis.service.impl package me.arcanis.ffxivbis.service.impl
import akka.pattern.pipe import java.time.Instant
import me.arcanis.ffxivbis.models.{Piece, PlayerId}
import akka.actor.typed.scaladsl.Behaviors
import me.arcanis.ffxivbis.messages.{AddPieceTo, DatabaseMessage, GetLoot, RemovePieceFrom, SuggestLoot}
import me.arcanis.ffxivbis.models.Loot
import me.arcanis.ffxivbis.service.Database import me.arcanis.ffxivbis.service.Database
trait DatabaseLootHandler { this: Database => trait DatabaseLootHandler { this: Database =>
import DatabaseLootHandler._
def lootHandler: Receive = { def lootHandler: DatabaseMessage.Handler = {
case AddPieceTo(playerId, piece) => case AddPieceTo(playerId, piece, isFreeLoot, client) =>
val client = sender() val loot = Loot(-1, piece, Instant.now, isFreeLoot)
profile.insertPiece(playerId, piece).pipeTo(client) profile.insertPiece(playerId, loot).foreach(_ => client ! ())
Behaviors.same
case GetLoot(partyId, maybePlayerId) => case GetLoot(partyId, maybePlayerId, client) =>
val client = sender()
getParty(partyId, withBiS = false, withLoot = true) getParty(partyId, withBiS = false, withLoot = true)
.map(filterParty(_, maybePlayerId)) .map(filterParty(_, maybePlayerId))
.pipeTo(client) .foreach(client ! _)
Behaviors.same
case RemovePieceFrom(playerId, piece) => case RemovePieceFrom(playerId, piece, client) =>
val client = sender() profile.deletePiece(playerId, piece).foreach(_ => client ! ())
profile.deletePiece(playerId, piece).pipeTo(client) Behaviors.same
case SuggestLoot(partyId, piece) => case SuggestLoot(partyId, piece, client) =>
val client = sender() getParty(partyId, withBiS = true, withLoot = true)
getParty(partyId, withBiS = true, withLoot = true).map(_.suggestLoot(piece)).pipeTo(client) .map(_.suggestLoot(piece))
.foreach(client ! _)
Behaviors.same
} }
} }
object DatabaseLootHandler {
case class AddPieceTo(playerId: PlayerId, piece: Piece) extends Database.DatabaseRequest {
override def partyId: String = playerId.partyId
}
case class GetLoot(partyId: String, playerId: Option[PlayerId]) extends Database.DatabaseRequest
case class RemovePieceFrom(playerId: PlayerId, piece: Piece) extends Database.DatabaseRequest {
override def partyId: String = playerId.partyId
}
case class SuggestLoot(partyId: String, piece: Piece) extends Database.DatabaseRequest
}

View File

@ -8,53 +8,48 @@
*/ */
package me.arcanis.ffxivbis.service.impl package me.arcanis.ffxivbis.service.impl
import akka.pattern.pipe import akka.actor.typed.scaladsl.Behaviors
import me.arcanis.ffxivbis.models.{BiS, Player, PlayerId} import me.arcanis.ffxivbis.messages.{AddPlayer, DatabaseMessage, GetParty, GetPartyDescription, GetPlayer, RemovePlayer, UpdateParty}
import me.arcanis.ffxivbis.models.{BiS, Player}
import me.arcanis.ffxivbis.service.Database import me.arcanis.ffxivbis.service.Database
import scala.concurrent.Future import scala.concurrent.Future
trait DatabasePartyHandler { this: Database => trait DatabasePartyHandler { this: Database =>
import DatabasePartyHandler._
def partyHandler: Receive = { def partyHandler: DatabaseMessage.Handler = {
case AddPlayer(player) => case AddPlayer(player, client) =>
val client = sender() profile.insertPlayer(player).foreach(_ => client ! ())
profile.insertPlayer(player).pipeTo(client) Behaviors.same
case GetParty(partyId) => case GetParty(partyId, client) =>
val client = sender() getParty(partyId, withBiS = true, withLoot = true).foreach(client ! _)
getParty(partyId, withBiS = true, withLoot = true).pipeTo(client) Behaviors.same
case GetPlayer(playerId) => case GetPartyDescription(partyId, client) =>
val client = sender() profile.getPartyDescription(partyId).foreach(client ! _)
Behaviors.same
case GetPlayer(playerId, client) =>
val player = profile.getPlayerFull(playerId).flatMap { maybePlayerData => val player = profile.getPlayerFull(playerId).flatMap { maybePlayerData =>
Future.traverse(maybePlayerData.toSeq) { playerData => Future.traverse(maybePlayerData.toSeq) { playerData =>
for { for {
bis <- profile.getPiecesBiS(playerId) bis <- profile.getPiecesBiS(playerId)
loot <- profile.getPieces(playerId) loot <- profile.getPieces(playerId)
} yield Player(playerId.partyId, playerId.job, playerId.nick, } yield Player(playerData.id, playerId.partyId, playerId.job,
BiS(bis.map(_.piece)), loot.map(_.piece), playerId.nick, BiS(bis.map(_.piece)), loot,
playerData.link, playerData.priority) playerData.link, playerData.priority)
} }
}.map(_.headOption) }.map(_.headOption)
player.pipeTo(client) player.foreach(client ! _)
Behaviors.same
case RemovePlayer(playerId) => case RemovePlayer(playerId, client) =>
val client = sender() profile.deletePlayer(playerId).foreach(_ => client ! ())
profile.deletePlayer(playerId).pipeTo(client) Behaviors.same
}
} case UpdateParty(description, client) =>
profile.insertPartyDescription(description).foreach(_ => client ! ())
object DatabasePartyHandler { Behaviors.same
case class AddPlayer(player: Player) extends Database.DatabaseRequest {
override def partyId: String = player.partyId
}
case class GetParty(partyId: String) extends Database.DatabaseRequest
case class GetPlayer(playerId: PlayerId) extends Database.DatabaseRequest {
override def partyId: String = playerId.partyId
}
case class RemovePlayer(playerId: PlayerId) extends Database.DatabaseRequest {
override def partyId: String = playerId.partyId
} }
} }

View File

@ -8,43 +8,32 @@
*/ */
package me.arcanis.ffxivbis.service.impl package me.arcanis.ffxivbis.service.impl
import akka.pattern.pipe import akka.actor.typed.scaladsl.Behaviors
import me.arcanis.ffxivbis.models.User import me.arcanis.ffxivbis.messages.{AddUser, DatabaseMessage, DeleteUser, Exists, GetUser, GetUsers}
import me.arcanis.ffxivbis.service.Database import me.arcanis.ffxivbis.service.Database
trait DatabaseUserHandler { this: Database => trait DatabaseUserHandler { this: Database =>
import DatabaseUserHandler._
def userHandler: Receive = { def userHandler: DatabaseMessage.Handler = {
case AddUser(user, isHashedPassword) => case AddUser(user, isHashedPassword, client) =>
val client = sender()
val toInsert = if (isHashedPassword) user else user.withHashedPassword val toInsert = if (isHashedPassword) user else user.withHashedPassword
profile.insertUser(toInsert).pipeTo(client) profile.insertUser(toInsert).foreach(_ => client ! ())
Behaviors.same
case DeleteUser(partyId, username) => case DeleteUser(partyId, username, client) =>
val client = sender() profile.deleteUser(partyId, username).foreach(_ => client ! ())
profile.deleteUser(partyId, username).pipeTo(client) Behaviors.same
case Exists(partyId) => case Exists(partyId, client) =>
val client = sender() profile.exists(partyId).foreach(client ! _)
profile.exists(partyId).pipeTo(client) Behaviors.same
case GetUser(partyId, username) => case GetUser(partyId, username, client) =>
val client = sender() profile.getUser(partyId, username).foreach(client ! _)
profile.getUser(partyId, username).pipeTo(client) Behaviors.same
case GetUsers(partyId) => case GetUsers(partyId, client) =>
val client = sender() profile.getUsers(partyId).foreach(client ! _)
profile.getUsers(partyId).pipeTo(client) Behaviors.same
} }
} }
object DatabaseUserHandler {
case class AddUser(user: User, isHashedPassword: Boolean) extends Database.DatabaseRequest {
override def partyId: String = user.partyId
}
case class DeleteUser(partyId: String, username: String) extends Database.DatabaseRequest
case class Exists(partyId: String) extends Database.DatabaseRequest
case class GetUser(partyId: String, username: String) extends Database.DatabaseRequest
case class GetUsers(partyId: String) extends Database.DatabaseRequest
}

View File

@ -8,7 +8,9 @@
*/ */
package me.arcanis.ffxivbis.storage package me.arcanis.ffxivbis.storage
import me.arcanis.ffxivbis.models.{Job, Loot, Piece} import java.time.Instant
import me.arcanis.ffxivbis.models.{Job, Loot, Piece, PieceType}
import slick.lifted.ForeignKeyQuery import slick.lifted.ForeignKeyQuery
import scala.concurrent.Future import scala.concurrent.Future
@ -16,24 +18,27 @@ import scala.concurrent.Future
trait BiSProfile { this: DatabaseProfile => trait BiSProfile { this: DatabaseProfile =>
import dbConfig.profile.api._ import dbConfig.profile.api._
case class BiSRep(playerId: Long, created: Long, piece: String, isTome: Int, job: String) { case class BiSRep(playerId: Long, created: Long, piece: String,
def toLoot: Loot = Loot(playerId, Piece(piece, isTome == 1, Job.withName(job))) pieceType: String, job: String) {
def toLoot: Loot = Loot(
playerId, Piece(piece, PieceType.withName(pieceType), Job.withName(job)),
Instant.ofEpochMilli(created), isFreeLoot = false)
} }
object BiSRep { object BiSRep {
def fromPiece(playerId: Long, piece: Piece) = def fromPiece(playerId: Long, piece: Piece): BiSRep =
BiSRep(playerId, DatabaseProfile.now, piece.piece, if (piece.isTome) 1 else 0, BiSRep(playerId, DatabaseProfile.now, piece.piece,
piece.job.toString) piece.pieceType.toString, piece.job.toString)
} }
class BiSPieces(tag: Tag) extends Table[BiSRep](tag, "bis") { class BiSPieces(tag: Tag) extends Table[BiSRep](tag, "bis") {
def playerId: Rep[Long] = column[Long]("player_id", O.PrimaryKey) def playerId: Rep[Long] = column[Long]("player_id", O.PrimaryKey)
def created: Rep[Long] = column[Long]("created") def created: Rep[Long] = column[Long]("created")
def piece: Rep[String] = column[String]("piece", O.PrimaryKey) def piece: Rep[String] = column[String]("piece", O.PrimaryKey)
def isTome: Rep[Int] = column[Int]("is_tome") def pieceType: Rep[String] = column[String]("piece_type")
def job: Rep[String] = column[String]("job") def job: Rep[String] = column[String]("job")
def * = def * =
(playerId, created, piece, isTome, job) <> ((BiSRep.apply _).tupled, BiSRep.unapply) (playerId, created, piece, pieceType, job) <> ((BiSRep.apply _).tupled, BiSRep.unapply)
def fkPlayerId: ForeignKeyQuery[Players, PlayerRep] = def fkPlayerId: ForeignKeyQuery[Players, PlayerRep] =
foreignKey("player_id", playerId, playersTable)(_.playerId, onDelete = ForeignKeyAction.Cascade) foreignKey("player_id", playerId, playersTable)(_.playerId, onDelete = ForeignKeyAction.Cascade)
@ -41,14 +46,21 @@ trait BiSProfile { this: DatabaseProfile =>
def deletePieceBiSById(piece: Piece)(playerId: Long): Future[Int] = def deletePieceBiSById(piece: Piece)(playerId: Long): Future[Int] =
db.run(pieceBiS(BiSRep.fromPiece(playerId, piece)).delete) db.run(pieceBiS(BiSRep.fromPiece(playerId, piece)).delete)
def deletePiecesBiSById(playerId: Long): Future[Int] =
db.run(piecesBiS(Seq(playerId)).delete)
def getPiecesBiSById(playerId: Long): Future[Seq[Loot]] = getPiecesBiSById(Seq(playerId)) def getPiecesBiSById(playerId: Long): Future[Seq[Loot]] = getPiecesBiSById(Seq(playerId))
def getPiecesBiSById(playerIds: Seq[Long]): Future[Seq[Loot]] = def getPiecesBiSById(playerIds: Seq[Long]): Future[Seq[Loot]] =
db.run(piecesBiS(playerIds).result).map(_.map(_.toLoot)) db.run(piecesBiS(playerIds).result).map(_.map(_.toLoot))
def insertPieceBiSById(piece: Piece)(playerId: Long): Future[Int] = def insertPieceBiSById(piece: Piece)(playerId: Long): Future[Int] =
db.run(bisTable.insertOrUpdate(BiSRep.fromPiece(playerId, piece))) getPiecesBiSById(playerId).flatMap {
case pieces if pieces.exists(loot => loot.piece.strictEqual(piece)) => Future.successful(0)
case _ => db.run(bisTable.insertOrUpdate(BiSRep.fromPiece(playerId, piece)))
}
private def pieceBiS(piece: BiSRep) = private def pieceBiS(piece: BiSRep) =
piecesBiS(Seq(piece.playerId)).filter(_.piece === piece.piece) piecesBiS(Seq(piece.playerId)).filter { stored =>
(stored.piece === piece.piece) && (stored.pieceType === piece.pieceType)
}
private def piecesBiS(playerIds: Seq[Long]) = private def piecesBiS(playerIds: Seq[Long]) =
bisTable.filter(_.playerId.inSet(playerIds.toSet)) bisTable.filter(_.playerId.inSet(playerIds.toSet))
} }

View File

@ -18,7 +18,7 @@ import slick.jdbc.JdbcProfile
import scala.concurrent.{ExecutionContext, Future} import scala.concurrent.{ExecutionContext, Future}
class DatabaseProfile(context: ExecutionContext, config: Config) class DatabaseProfile(context: ExecutionContext, config: Config)
extends BiSProfile with LootProfile with PlayersProfile with UsersProfile { extends BiSProfile with LootProfile with PartyProfile with PlayersProfile with UsersProfile {
implicit val executionContext: ExecutionContext = context implicit val executionContext: ExecutionContext = context
@ -29,12 +29,15 @@ class DatabaseProfile(context: ExecutionContext, config: Config)
val bisTable: TableQuery[BiSPieces] = TableQuery[BiSPieces] val bisTable: TableQuery[BiSPieces] = TableQuery[BiSPieces]
val lootTable: TableQuery[LootPieces] = TableQuery[LootPieces] val lootTable: TableQuery[LootPieces] = TableQuery[LootPieces]
val partiesTable: TableQuery[Parties] = TableQuery[Parties]
val playersTable: TableQuery[Players] = TableQuery[Players] val playersTable: TableQuery[Players] = TableQuery[Players]
val usersTable: TableQuery[Users] = TableQuery[Users] val usersTable: TableQuery[Users] = TableQuery[Users]
// generic bis api // generic bis api
def deletePieceBiS(playerId: PlayerId, piece: Piece): Future[Int] = def deletePieceBiS(playerId: PlayerId, piece: Piece): Future[Int] =
byPlayerId(playerId, deletePieceBiSById(piece)) byPlayerId(playerId, deletePieceBiSById(piece))
def deletePiecesBiS(playerId: PlayerId): Future[Int] =
byPlayerId(playerId, deletePiecesBiSById)
def getPiecesBiS(playerId: PlayerId): Future[Seq[Loot]] = def getPiecesBiS(playerId: PlayerId): Future[Seq[Loot]] =
byPlayerId(playerId, getPiecesBiSById) byPlayerId(playerId, getPiecesBiSById)
def getPiecesBiS(partyId: String): Future[Seq[Loot]] = def getPiecesBiS(partyId: String): Future[Seq[Loot]] =
@ -43,25 +46,29 @@ class DatabaseProfile(context: ExecutionContext, config: Config)
byPlayerId(playerId, insertPieceBiSById(piece)) byPlayerId(playerId, insertPieceBiSById(piece))
// generic loot api // generic loot api
def deletePiece(playerId: PlayerId, piece: Piece): Future[Int] = def deletePiece(playerId: PlayerId, piece: Piece): Future[Int] = {
byPlayerId(playerId, deletePieceById(piece)) // we don't really care here about loot
val loot = Loot(-1, piece, Instant.now, isFreeLoot = false)
byPlayerId(playerId, deletePieceById(loot))
}
def getPieces(playerId: PlayerId): Future[Seq[Loot]] = def getPieces(playerId: PlayerId): Future[Seq[Loot]] =
byPlayerId(playerId, getPiecesById) byPlayerId(playerId, getPiecesById)
def getPieces(partyId: String): Future[Seq[Loot]] = def getPieces(partyId: String): Future[Seq[Loot]] =
byPartyId(partyId, getPiecesById) byPartyId(partyId, getPiecesById)
def insertPiece(playerId: PlayerId, piece: Piece): Future[Int] = def insertPiece(playerId: PlayerId, loot: Loot): Future[Int] =
byPlayerId(playerId, insertPieceById(piece)) byPlayerId(playerId, insertPieceById(loot))
private def byPartyId[T](partyId: String, callback: Seq[Long] => Future[T]): Future[T] = private def byPartyId[T](partyId: String, callback: Seq[Long] => Future[T]): Future[T] =
getPlayers(partyId).map(callback).flatten getPlayers(partyId).flatMap(callback)
private def byPlayerId[T](playerId: PlayerId, callback: Long => Future[T]): Future[T] = private def byPlayerId[T](playerId: PlayerId, callback: Long => Future[T]): Future[T] =
getPlayer(playerId).map { getPlayer(playerId).flatMap {
case Some(id) => callback(id) case Some(id) => callback(id)
case None => Future.failed(new Error(s"Could not find player $playerId")) case None => Future.failed(new Error(s"Could not find player $playerId"))
}.flatten }
} }
object DatabaseProfile { object DatabaseProfile {
def now: Long = Instant.now.toEpochMilli def now: Long = Instant.now.toEpochMilli
def getSection(config: Config): Config = { def getSection(config: Config): Config = {
val section = config.getString("me.arcanis.ffxivbis.database.mode") val section = config.getString("me.arcanis.ffxivbis.database.mode")

View File

@ -8,7 +8,9 @@
*/ */
package me.arcanis.ffxivbis.storage package me.arcanis.ffxivbis.storage
import me.arcanis.ffxivbis.models.{Job, Loot, Piece} import java.time.Instant
import me.arcanis.ffxivbis.models.{Job, Loot, Piece, PieceType}
import slick.lifted.{ForeignKeyQuery, Index} import slick.lifted.{ForeignKeyQuery, Index}
import scala.concurrent.Future import scala.concurrent.Future
@ -16,14 +18,19 @@ import scala.concurrent.Future
trait LootProfile { this: DatabaseProfile => trait LootProfile { this: DatabaseProfile =>
import dbConfig.profile.api._ import dbConfig.profile.api._
case class LootRep(lootId: Option[Long], playerId: Long, created: Long, piece: String, case class LootRep(lootId: Option[Long], playerId: Long, created: Long,
isTome: Int, job: String) { piece: String, pieceType: String, job: String,
def toLoot: Loot = Loot(playerId, Piece(piece, isTome == 1, Job.withName(job))) isFreeLoot: Int) {
def toLoot: Loot = Loot(
playerId,
Piece(piece, PieceType.withName(pieceType), Job.withName(job)),
Instant.ofEpochMilli(created), isFreeLoot == 1)
} }
object LootRep { object LootRep {
def fromPiece(playerId: Long, piece: Piece) = def fromLoot(playerId: Long, loot: Loot): LootRep =
LootRep(None, playerId, DatabaseProfile.now, piece.piece, if (piece.isTome) 1 else 0, LootRep(None, playerId, loot.timestamp.toEpochMilli, loot.piece.piece,
piece.job.toString) loot.piece.pieceType.toString, loot.piece.job.toString,
if (loot.isFreeLoot) 1 else 0)
} }
class LootPieces(tag: Tag) extends Table[LootRep](tag, "loot") { class LootPieces(tag: Tag) extends Table[LootRep](tag, "loot") {
@ -31,11 +38,12 @@ trait LootProfile { this: DatabaseProfile =>
def playerId: Rep[Long] = column[Long]("player_id") def playerId: Rep[Long] = column[Long]("player_id")
def created: Rep[Long] = column[Long]("created") def created: Rep[Long] = column[Long]("created")
def piece: Rep[String] = column[String]("piece") def piece: Rep[String] = column[String]("piece")
def isTome: Rep[Int] = column[Int]("is_tome") def pieceType: Rep[String] = column[String]("piece_type")
def job: Rep[String] = column[String]("job") def job: Rep[String] = column[String]("job")
def isFreeLoot: Rep[Int] = column[Int]("is_free_loot")
def * = def * =
(lootId.?, playerId, created, piece, isTome, job) <> ((LootRep.apply _).tupled, LootRep.unapply) (lootId.?, playerId, created, piece, pieceType, job, isFreeLoot) <> ((LootRep.apply _).tupled, LootRep.unapply)
def fkPlayerId: ForeignKeyQuery[Players, PlayerRep] = def fkPlayerId: ForeignKeyQuery[Players, PlayerRep] =
foreignKey("player_id", playerId, playersTable)(_.playerId, onDelete = ForeignKeyAction.Cascade) foreignKey("player_id", playerId, playersTable)(_.playerId, onDelete = ForeignKeyAction.Cascade)
@ -43,16 +51,16 @@ trait LootProfile { this: DatabaseProfile =>
index("loot_owner_idx", (playerId), unique = false) index("loot_owner_idx", (playerId), unique = false)
} }
def deletePieceById(piece: Piece)(playerId: Long): Future[Int] = def deletePieceById(loot: Loot)(playerId: Long): Future[Int] =
db.run(pieceLoot(LootRep.fromPiece(playerId, piece)).map(_.lootId).max.result).flatMap { db.run(pieceLoot(LootRep.fromLoot(playerId, loot)).map(_.lootId).max.result).flatMap {
case Some(id) => db.run(lootTable.filter(_.lootId === id).delete) case Some(id) => db.run(lootTable.filter(_.lootId === id).delete)
case _ => throw new IllegalArgumentException(s"Could not find piece $piece belong to $playerId") case _ => throw new IllegalArgumentException(s"Could not find piece $loot belong to $playerId")
} }
def getPiecesById(playerId: Long): Future[Seq[Loot]] = getPiecesById(Seq(playerId)) def getPiecesById(playerId: Long): Future[Seq[Loot]] = getPiecesById(Seq(playerId))
def getPiecesById(playerIds: Seq[Long]): Future[Seq[Loot]] = def getPiecesById(playerIds: Seq[Long]): Future[Seq[Loot]] =
db.run(piecesLoot(playerIds).result).map(_.map(_.toLoot)) db.run(piecesLoot(playerIds).result).map(_.map(_.toLoot))
def insertPieceById(piece: Piece)(playerId: Long): Future[Int] = def insertPieceById(loot: Loot)(playerId: Long): Future[Int] =
db.run(lootTable.insertOrUpdate(LootRep.fromPiece(playerId, piece))) db.run(lootTable.insertOrUpdate(LootRep.fromLoot(playerId, loot)))
private def pieceLoot(piece: LootRep) = private def pieceLoot(piece: LootRep) =
piecesLoot(Seq(piece.playerId)).filter(_.piece === piece.piece) piecesLoot(Seq(piece.playerId)).filter(_.piece === piece.piece)

View File

@ -13,9 +13,11 @@ import org.flywaydb.core.Flyway
import org.flywaydb.core.api.configuration.ClassicConfiguration import org.flywaydb.core.api.configuration.ClassicConfiguration
import scala.concurrent.Future import scala.concurrent.Future
import scala.util.Try
class Migration(config: Config) { class Migration(config: Config) {
def performMigration(): Future[Int] = {
def performMigration(): Try[Boolean] = {
val section = DatabaseProfile.getSection(config) val section = DatabaseProfile.getSection(config)
val url = section.getString("db.url") val url = section.getString("db.url")
@ -32,10 +34,11 @@ class Migration(config: Config) {
flywayConfiguration.setDataSource(url, username, password) flywayConfiguration.setDataSource(url, username, password)
val flyway = new Flyway(flywayConfiguration) val flyway = new Flyway(flywayConfiguration)
Future.successful(flyway.migrate()) Try(flyway.migrate().success)
} }
} }
object Migration { object Migration {
def apply(config: Config): Future[Int] = new Migration(config).performMigration()
def apply(config: Config): Try[Boolean] = new Migration(config).performMigration()
} }

View File

@ -0,0 +1,49 @@
/*
* Copyright (c) 2020 Evgeniy Alekseev.
*
* This file is part of ffxivbis
* (see https://github.com/arcan1s/ffxivbis).
*
* License: 3-clause BSD, see https://opensource.org/licenses/BSD-3-Clause
*/
package me.arcanis.ffxivbis.storage
import me.arcanis.ffxivbis.models.PartyDescription
import scala.concurrent.Future
trait PartyProfile { this: DatabaseProfile =>
import dbConfig.profile.api._
case class PartyRep(partyId: Option[Long], partyName: String,
partyAlias: Option[String]) {
def toDescription: PartyDescription = PartyDescription(partyName, partyAlias)
}
object PartyRep {
def fromDescription(party: PartyDescription, id: Option[Long]): PartyRep =
PartyRep(id, party.partyId, party.partyAlias)
}
class Parties(tag: Tag) extends Table[PartyRep](tag, "parties") {
def partyId: Rep[Long] = column[Long]("party_id", O.AutoInc, O.PrimaryKey)
def partyName: Rep[String] = column[String]("party_name")
def partyAlias: Rep[Option[String]] = column[Option[String]]("party_alias")
def * =
(partyId.?, partyName, partyAlias) <> ((PartyRep.apply _).tupled, PartyRep.unapply)
}
def getPartyDescription(partyId: String): Future[PartyDescription] =
db.run(partyDescription(partyId).result.headOption.map(_.map(_.toDescription).getOrElse(PartyDescription.empty(partyId))))
def getUniquePartyId(partyId: String): Future[Option[Long]] =
db.run(partyDescription(partyId).map(_.partyId).result.headOption)
def insertPartyDescription(partyDescription: PartyDescription): Future[Int] =
getUniquePartyId(partyDescription.partyId).flatMap {
case Some(id) => db.run(partiesTable.update(PartyRep.fromDescription(partyDescription, Some(id))))
case _ => db.run(partiesTable.insertOrUpdate(PartyRep.fromDescription(partyDescription, None)))
}
private def partyDescription(partyId: String) =
partiesTable.filter(_.partyName === partyId)
}

View File

@ -15,10 +15,11 @@ import scala.concurrent.Future
trait PlayersProfile { this: DatabaseProfile => trait PlayersProfile { this: DatabaseProfile =>
import dbConfig.profile.api._ import dbConfig.profile.api._
case class PlayerRep(partyId: String, playerId: Option[Long], created: Long, nick: String, case class PlayerRep(partyId: String, playerId: Option[Long], created: Long,
job: String, link: Option[String], priority: Int) { nick: String, job: String, link: Option[String], priority: Int) {
def toPlayer: Player = def toPlayer: Player =
Player(partyId, Job.withName(job), nick, BiS(Seq.empty), List.empty, link, priority) Player(playerId.getOrElse(-1), partyId, Job.withName(job), nick,
BiS.empty, Seq.empty, link, priority)
} }
object PlayerRep { object PlayerRep {
def fromPlayer(player: Player, id: Option[Long]): PlayerRep = def fromPlayer(player: Player, id: Option[Long]): PlayerRep =
@ -39,7 +40,6 @@ trait PlayersProfile { this: DatabaseProfile =>
(partyId, playerId.?, created, nick, job, bisLink, priority) <> ((PlayerRep.apply _).tupled, PlayerRep.unapply) (partyId, playerId.?, created, nick, job, bisLink, priority) <> ((PlayerRep.apply _).tupled, PlayerRep.unapply)
} }
def deletePlayer(playerId: PlayerId): Future[Int] = db.run(player(playerId).delete) def deletePlayer(playerId: PlayerId): Future[Int] = db.run(player(playerId).delete)
def getParty(partyId: String): Future[Map[Long, Player]] = def getParty(partyId: String): Future[Map[Long, Player]] =
db.run(players(partyId).result).map(_.foldLeft(Map.empty[Long, Player]) { db.run(players(partyId).result).map(_.foldLeft(Map.empty[Long, Player]) {
@ -53,10 +53,10 @@ trait PlayersProfile { this: DatabaseProfile =>
def getPlayers(partyId: String): Future[Seq[Long]] = def getPlayers(partyId: String): Future[Seq[Long]] =
db.run(players(partyId).map(_.playerId).result) db.run(players(partyId).map(_.playerId).result)
def insertPlayer(playerObj: Player): Future[Int] = def insertPlayer(playerObj: Player): Future[Int] =
getPlayer(playerObj.playerId).map { getPlayer(playerObj.playerId).flatMap {
case Some(id) => db.run(playersTable.update(PlayerRep.fromPlayer(playerObj, Some(id)))) case Some(id) => db.run(playersTable.update(PlayerRep.fromPlayer(playerObj, Some(id))))
case _ => db.run(playersTable.insertOrUpdate(PlayerRep.fromPlayer(playerObj, None))) case _ => db.run(playersTable.insertOrUpdate(PlayerRep.fromPlayer(playerObj, None)))
}.flatten }
private def player(playerId: PlayerId) = private def player(playerId: PlayerId) =
playersTable playersTable

View File

@ -16,8 +16,8 @@ import scala.concurrent.Future
trait UsersProfile { this: DatabaseProfile => trait UsersProfile { this: DatabaseProfile =>
import dbConfig.profile.api._ import dbConfig.profile.api._
case class UserRep(partyId: String, userId: Option[Long], username: String, password: String, case class UserRep(partyId: String, userId: Option[Long], username: String,
permission: String) { password: String, permission: String) {
def toUser: User = User(partyId, username, password, Permission.withName(permission)) def toUser: User = User(partyId, username, password, Permission.withName(permission))
} }
object UserRep { object UserRep {
@ -49,10 +49,10 @@ trait UsersProfile { this: DatabaseProfile =>
def getUsers(partyId: String): Future[Seq[User]] = def getUsers(partyId: String): Future[Seq[User]] =
db.run(user(partyId, None).result).map(_.map(_.toUser)) db.run(user(partyId, None).result).map(_.map(_.toUser))
def insertUser(userObj: User): Future[Int] = def insertUser(userObj: User): Future[Int] =
db.run(user(userObj.partyId, Some(userObj.username)).map(_.userId).result.headOption).map { db.run(user(userObj.partyId, Some(userObj.username)).map(_.userId).result.headOption).flatMap {
case Some(id) => db.run(usersTable.insertOrUpdate(UserRep.fromUser(userObj, Some(id)))) case Some(id) => db.run(usersTable.insertOrUpdate(UserRep.fromUser(userObj, Some(id))))
case _ => db.run(usersTable.insertOrUpdate(UserRep.fromUser(userObj, None))) case _ => db.run(usersTable.insertOrUpdate(UserRep.fromUser(userObj, None)))
}.flatten }
private def user(partyId: String, username: Option[String]) = private def user(partyId: String, username: Option[String]) =
usersTable usersTable

View File

@ -17,6 +17,7 @@ import scala.concurrent.duration.FiniteDuration
import scala.language.implicitConversions import scala.language.implicitConversions
object Implicits { object Implicits {
implicit def getBooleanFromOptionString(maybeYes: Option[String]): Boolean = maybeYes.map(_.toLowerCase) match { implicit def getBooleanFromOptionString(maybeYes: Option[String]): Boolean = maybeYes.map(_.toLowerCase) match {
case Some("yes" | "on") => true case Some("yes" | "on") => true
case _ => false case _ => false

View File

@ -5,33 +5,49 @@ import me.arcanis.ffxivbis.models._
object Fixtures { object Fixtures {
lazy val bis: BiS = BiS( lazy val bis: BiS = BiS(
Seq( Seq(
Weapon(isTome = false ,Job.DNC), Weapon(pieceType = PieceType.Savage ,Job.DNC),
Head(isTome = false, Job.DNC), Head(pieceType = PieceType.Savage, Job.DNC),
Body(isTome = false, Job.DNC), Body(pieceType = PieceType.Savage, Job.DNC),
Hands(isTome = true, Job.DNC), Hands(pieceType = PieceType.Tome, Job.DNC),
Waist(isTome = true, Job.DNC), Legs(pieceType = PieceType.Tome, Job.DNC),
Legs(isTome = true, Job.DNC), Feet(pieceType = PieceType.Savage, Job.DNC),
Feet(isTome = false, Job.DNC), Ears(pieceType = PieceType.Savage, Job.DNC),
Ears(isTome = false, Job.DNC), Neck(pieceType = PieceType.Tome, Job.DNC),
Neck(isTome = true, Job.DNC), Wrist(pieceType = PieceType.Savage, Job.DNC),
Wrist(isTome = false, Job.DNC), Ring(pieceType = PieceType.Tome, Job.DNC, "left ring"),
Ring(isTome = true, Job.DNC, "left ring"), Ring(pieceType = PieceType.Tome, Job.DNC, "right ring")
Ring(isTome = true, Job.DNC, "right ring") )
)
lazy val bis2: BiS = BiS(
Seq(
Weapon(pieceType = PieceType.Savage ,Job.DNC),
Head(pieceType = PieceType.Tome, Job.DNC),
Body(pieceType = PieceType.Savage, Job.DNC),
Hands(pieceType = PieceType.Tome, Job.DNC),
Legs(pieceType = PieceType.Savage, Job.DNC),
Feet(pieceType = PieceType.Tome, Job.DNC),
Ears(pieceType = PieceType.Savage, Job.DNC),
Neck(pieceType = PieceType.Savage, Job.DNC),
Wrist(pieceType = PieceType.Savage, Job.DNC),
Ring(pieceType = PieceType.Tome, Job.DNC, "left ring"),
Ring(pieceType = PieceType.Savage, Job.DNC, "right ring")
) )
) )
lazy val link: String = "https://ffxiv.ariyala.com/19V5R" lazy val link: String = "https://ffxiv.ariyala.com/19V5R"
lazy val link2: String = "https://ffxiv.ariyala.com/1A0WM" lazy val link2: String = "https://ffxiv.ariyala.com/1A0WM"
lazy val link3: String = "https://etro.gg/gearset/26a67536-b4ce-4adc-a46a-f70e348bb138"
lazy val link4: String = "https://etro.gg/gearset/865fc886-994f-4c28-8fc1-4379f160a916"
lazy val lootWeapon: Piece = Weapon(isTome = true, Job.AnyJob) lazy val lootWeapon: Piece = Weapon(pieceType = PieceType.Tome, Job.AnyJob)
lazy val lootBody: Piece = Body(isTome = false, Job.AnyJob) lazy val lootBody: Piece = Body(pieceType = PieceType.Savage, Job.AnyJob)
lazy val lootHands: Piece = Hands(isTome = true, Job.AnyJob) lazy val lootBodyCrafted: Piece = Body(pieceType = PieceType.Crafted, Job.AnyJob)
lazy val lootWaist: Piece = Waist(isTome = true, Job.AnyJob) lazy val lootHands: Piece = Hands(pieceType = PieceType.Tome, Job.AnyJob)
lazy val lootLegs: Piece = Legs(isTome = false, Job.AnyJob) lazy val lootLegs: Piece = Legs(pieceType = PieceType.Savage, Job.AnyJob)
lazy val lootEars: Piece = Ears(isTome = false, Job.AnyJob) lazy val lootEars: Piece = Ears(pieceType = PieceType.Savage, Job.AnyJob)
lazy val lootRing: Piece = Ring(isTome = true, Job.AnyJob) lazy val lootRing: Piece = Ring(pieceType = PieceType.Tome, Job.AnyJob)
lazy val lootLeftRing: Piece = Ring(isTome = true, Job.AnyJob, "left ring") lazy val lootLeftRing: Piece = Ring(pieceType = PieceType.Tome, Job.AnyJob, "left ring")
lazy val lootRightRing: Piece = Ring(isTome = true, Job.AnyJob, "right ring") lazy val lootRightRing: Piece = Ring(pieceType = PieceType.Tome, Job.AnyJob, "right ring")
lazy val lootUpgrade: Piece = BodyUpgrade lazy val lootUpgrade: Piece = BodyUpgrade
lazy val loot: Seq[Piece] = Seq(lootBody, lootHands, lootLegs, lootUpgrade) lazy val loot: Seq[Piece] = Seq(lootBody, lootHands, lootLegs, lootUpgrade)
@ -39,7 +55,7 @@ object Fixtures {
lazy val partyId2: String = Party.randomPartyId lazy val partyId2: String = Party.randomPartyId
lazy val playerEmpty: Player = lazy val playerEmpty: Player =
Player(partyId, Job.DNC, "Siuan Sanche", BiS(), Seq.empty, Some(link)) Player(1, partyId, Job.DNC, "Siuan Sanche", BiS.empty, Seq.empty, Some(link))
lazy val playerWithBiS: Player = playerEmpty.copy(bis = bis) lazy val playerWithBiS: Player = playerEmpty.copy(bis = bis)
lazy val userPassword: String = "password" lazy val userPassword: String = "password"

View File

@ -1,50 +1,65 @@
package me.arcanis.ffxivbis.http.api.v1 package me.arcanis.ffxivbis.http.api.v1
import akka.actor.ActorRef import akka.actor.testkit.typed.scaladsl.ActorTestKit
import akka.actor.typed.scaladsl.AskPattern.Askable
import akka.http.scaladsl.model.{StatusCodes, Uri} import akka.http.scaladsl.model.{StatusCodes, Uri}
import akka.http.scaladsl.model.headers.{Authorization, BasicHttpCredentials} import akka.http.scaladsl.model.headers.{Authorization, BasicHttpCredentials}
import akka.http.scaladsl.testkit.{RouteTestTimeout, ScalatestRouteTest} import akka.http.scaladsl.testkit.{RouteTestTimeout, ScalatestRouteTest}
import akka.http.scaladsl.server._
import akka.pattern.ask
import akka.testkit.TestKit import akka.testkit.TestKit
import com.typesafe.config.Config import com.typesafe.config.Config
import me.arcanis.ffxivbis.{Fixtures, Settings} import me.arcanis.ffxivbis.{Fixtures, Settings}
import me.arcanis.ffxivbis.http.api.v1.json._ import me.arcanis.ffxivbis.http.api.v1.json._
import me.arcanis.ffxivbis.models.BiS import me.arcanis.ffxivbis.messages.{AddPlayer, AddUser}
import me.arcanis.ffxivbis.service.{Ariyala, PartyService, impl} import me.arcanis.ffxivbis.models.{BiS, Job}
import me.arcanis.ffxivbis.service.bis.BisProvider
import me.arcanis.ffxivbis.service.{Database, PartyService}
import me.arcanis.ffxivbis.storage.Migration import me.arcanis.ffxivbis.storage.Migration
import org.scalatest.{Matchers, WordSpec} import me.arcanis.ffxivbis.utils.Compare
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike
import scala.concurrent.Await import scala.concurrent.Await
import scala.concurrent.duration._ import scala.concurrent.duration._
import scala.language.postfixOps import scala.language.postfixOps
class BiSEndpointTest extends WordSpec class BiSEndpointTest extends AnyWordSpecLike with Matchers with ScalatestRouteTest with JsonSupport {
with Matchers with ScalatestRouteTest with JsonSupport {
private val auth: Authorization = private val testKit = ActorTestKit(Settings.withRandomDatabase)
override val testConfig: Config = testKit.system.settings.config
private val auth =
Authorization(BasicHttpCredentials(Fixtures.userAdmin.username, Fixtures.userPassword)) Authorization(BasicHttpCredentials(Fixtures.userAdmin.username, Fixtures.userPassword))
private val endpoint: Uri = Uri(s"/party/${Fixtures.partyId}/bis") private val endpoint = Uri(s"/party/${Fixtures.partyId}/bis")
private val playerId = PlayerIdResponse.fromPlayerId(Fixtures.playerEmpty.playerId) private val playerId = PlayerIdResponse.fromPlayerId(Fixtures.playerEmpty.playerId)
private val timeout: FiniteDuration = 60 seconds private val askTimeout = 60 seconds
implicit private val routeTimeout: RouteTestTimeout = RouteTestTimeout(timeout) implicit private val routeTimeout: RouteTestTimeout = RouteTestTimeout(askTimeout)
private val storage: ActorRef = system.actorOf(impl.DatabaseImpl.props) private val storage = testKit.spawn(Database())
private val ariyala: ActorRef = system.actorOf(Ariyala.props) private val provider = testKit.spawn(BisProvider())
private val party: ActorRef = system.actorOf(PartyService.props(storage)) private val party = testKit.spawn(PartyService(storage))
private val route: Route = new BiSEndpoint(party, ariyala)(timeout).route private val route = new BiSEndpoint(party, provider)(askTimeout, testKit.scheduler).route
override def testConfig: Config = Settings.withRandomDatabase override def beforeAll(): Unit = {
super.beforeAll()
override def beforeAll: Unit = { Migration(testConfig)
Await.result(Migration(system.settings.config), timeout) Await.result(storage.ask(AddUser(Fixtures.userAdmin, isHashedPassword = true, _))(askTimeout, testKit.scheduler), askTimeout)
Await.result((storage ? impl.DatabaseUserHandler.AddUser(Fixtures.userAdmin, isHashedPassword = true))(timeout).mapTo[Int], timeout) Await.result(storage.ask(AddPlayer(Fixtures.playerEmpty, _))(askTimeout, testKit.scheduler), askTimeout)
Await.result((storage ? impl.DatabasePartyHandler.AddPlayer(Fixtures.playerEmpty))(timeout).mapTo[Int], timeout)
} }
override def afterAll: Unit = { override def afterAll(): Unit = {
Settings.clearDatabase(testConfig)
TestKit.shutdownActorSystem(system) TestKit.shutdownActorSystem(system)
Settings.clearDatabase(system.settings.config) testKit.shutdownTestKit()
super.afterAll()
}
private def compareBiSResponse(actual: PlayerResponse, expected: PlayerResponse): Unit = {
actual.partyId shouldEqual expected.partyId
actual.nick shouldEqual expected.nick
actual.job shouldEqual expected.job
Compare.seqEquals(actual.bis.get, expected.bis.get) shouldEqual true
actual.link shouldEqual expected.link
actual.priority shouldEqual expected.priority
} }
"api v1 bis endpoint" must { "api v1 bis endpoint" must {
@ -60,17 +75,19 @@ class BiSEndpointTest extends WordSpec
"return best in slot set" in { "return best in slot set" in {
val uri = endpoint.withQuery(Uri.Query(Map("nick" -> playerId.nick, "job" -> playerId.job))) val uri = endpoint.withQuery(Uri.Query(Map("nick" -> playerId.nick, "job" -> playerId.job)))
val response = Seq(PlayerResponse.fromPlayer(Fixtures.playerEmpty.withBiS(Some(Fixtures.bis)))) val response = PlayerResponse.fromPlayer(Fixtures.playerEmpty.withBiS(Some(Fixtures.bis)))
Get(uri).withHeaders(auth) ~> route ~> check { Get(uri).withHeaders(auth) ~> route ~> check {
status shouldEqual StatusCodes.OK status shouldEqual StatusCodes.OK
responseAs[Seq[PlayerResponse]] shouldEqual response val actual = responseAs[Seq[PlayerResponse]]
actual.length shouldEqual 1
actual.foreach(compareBiSResponse(_, response))
} }
} }
"remove item from best in slot set" in { "remove item from best in slot set" in {
val piece = PieceResponse.fromPiece(Fixtures.lootBody) val piece = PieceResponse.fromPiece(Fixtures.lootBody)
val entity = PieceActionResponse(ApiAction.remove, piece, playerId) val entity = PieceActionResponse(ApiAction.remove, piece, playerId, None)
Post(endpoint, entity).withHeaders(auth) ~> route ~> check { Post(endpoint, entity).withHeaders(auth) ~> route ~> check {
status shouldEqual StatusCodes.Accepted status shouldEqual StatusCodes.Accepted
@ -79,17 +96,19 @@ class BiSEndpointTest extends WordSpec
val uri = endpoint.withQuery(Uri.Query(Map("nick" -> playerId.nick, "job" -> playerId.job))) val uri = endpoint.withQuery(Uri.Query(Map("nick" -> playerId.nick, "job" -> playerId.job)))
val bis = BiS(Fixtures.bis.pieces.filterNot(_ == Fixtures.lootBody)) val bis = BiS(Fixtures.bis.pieces.filterNot(_ == Fixtures.lootBody))
val response = Seq(PlayerResponse.fromPlayer(Fixtures.playerEmpty.withBiS(Some(bis)))) val response = PlayerResponse.fromPlayer(Fixtures.playerEmpty.withBiS(Some(bis)))
Get(uri).withHeaders(auth) ~> route ~> check { Get(uri).withHeaders(auth) ~> route ~> check {
status shouldEqual StatusCodes.OK status shouldEqual StatusCodes.OK
responseAs[Seq[PlayerResponse]] shouldEqual response val actual = responseAs[Seq[PlayerResponse]]
actual.length shouldEqual 1
actual.foreach(compareBiSResponse(_, response))
} }
} }
"add item to best in slot set" in { "add item to best in slot set" in {
val piece = PieceResponse.fromPiece(Fixtures.lootBody) val piece = PieceResponse.fromPiece(Fixtures.lootBody)
val entity = PieceActionResponse(ApiAction.add, piece, playerId) val entity = PieceActionResponse(ApiAction.add, piece, playerId, None)
Post(endpoint, entity).withHeaders(auth) ~> route ~> check { Post(endpoint, entity).withHeaders(auth) ~> route ~> check {
status shouldEqual StatusCodes.Accepted status shouldEqual StatusCodes.Accepted
@ -97,11 +116,102 @@ class BiSEndpointTest extends WordSpec
} }
val uri = endpoint.withQuery(Uri.Query(Map("nick" -> playerId.nick, "job" -> playerId.job))) val uri = endpoint.withQuery(Uri.Query(Map("nick" -> playerId.nick, "job" -> playerId.job)))
val response = Seq(PlayerResponse.fromPlayer(Fixtures.playerEmpty.withBiS(Some(Fixtures.bis)))) val response = PlayerResponse.fromPlayer(Fixtures.playerEmpty.withBiS(Some(Fixtures.bis)))
Get(uri).withHeaders(auth) ~> route ~> check { Get(uri).withHeaders(auth) ~> route ~> check {
status shouldEqual StatusCodes.OK status shouldEqual StatusCodes.OK
responseAs[Seq[PlayerResponse]] shouldEqual response val actual = responseAs[Seq[PlayerResponse]]
actual.length shouldEqual 1
actual.foreach(compareBiSResponse(_, response))
}
}
"do not allow to add same item to best in slot set" in {
val piece = PieceResponse.fromPiece(Fixtures.lootBody.withJob(Job.DNC))
val entity = PieceActionResponse(ApiAction.add, piece, playerId, None)
Post(endpoint, entity).withHeaders(auth) ~> route ~> check {
status shouldEqual StatusCodes.Accepted
responseAs[String] shouldEqual ""
}
val uri = endpoint.withQuery(Uri.Query(Map("nick" -> playerId.nick, "job" -> playerId.job)))
val response = PlayerResponse.fromPlayer(Fixtures.playerEmpty.withBiS(Some(Fixtures.bis)))
Get(uri).withHeaders(auth) ~> route ~> check {
status shouldEqual StatusCodes.OK
val actual = responseAs[Seq[PlayerResponse]]
actual.length shouldEqual 1
actual.foreach(compareBiSResponse(_, response))
}
}
"allow to add item with another type to best in slot set" in {
val piece = PieceResponse.fromPiece(Fixtures.lootBodyCrafted.withJob(Job.DNC))
val entity = PieceActionResponse(ApiAction.add, piece, playerId, None)
Post(endpoint, entity).withHeaders(auth) ~> route ~> check {
status shouldEqual StatusCodes.Accepted
responseAs[String] shouldEqual ""
}
val uri = endpoint.withQuery(Uri.Query(Map("nick" -> playerId.nick, "job" -> playerId.job)))
val bis = Fixtures.bis.withPiece(piece.toPiece)
val response = PlayerResponse.fromPlayer(Fixtures.playerEmpty.withBiS(Some(bis)))
Get(uri).withHeaders(auth) ~> route ~> check {
status shouldEqual StatusCodes.OK
val actual = responseAs[Seq[PlayerResponse]]
actual.length shouldEqual 1
actual.foreach(compareBiSResponse(_, response))
}
}
"remove only specific item from best in slot set" in {
val piece = PieceResponse.fromPiece(Fixtures.lootBodyCrafted.withJob(Job.DNC))
val entity = PieceActionResponse(ApiAction.remove, piece, playerId, None)
Post(endpoint, entity).withHeaders(auth) ~> route ~> check {
status shouldEqual StatusCodes.Accepted
responseAs[String] shouldEqual ""
}
val uri = endpoint.withQuery(Uri.Query(Map("nick" -> playerId.nick, "job" -> playerId.job)))
val response = PlayerResponse.fromPlayer(Fixtures.playerEmpty.withBiS(Some(Fixtures.bis)))
Get(uri).withHeaders(auth) ~> route ~> check {
status shouldEqual StatusCodes.OK
val actual = responseAs[Seq[PlayerResponse]]
actual.length shouldEqual 1
actual.foreach(compareBiSResponse(_, response))
}
}
"totaly replace player bis" in {
// add random item first
val piece = PieceResponse.fromPiece(Fixtures.lootBodyCrafted.withJob(Job.DNC))
val entity = PieceActionResponse(ApiAction.add, piece, playerId, None)
Post(endpoint, entity).withHeaders(auth) ~> route ~> check {
status shouldEqual StatusCodes.Accepted
responseAs[String] shouldEqual ""
}
val bisEntity = PlayerBiSLinkResponse(Fixtures.link, playerId)
Put(endpoint, bisEntity).withHeaders(auth) ~> route ~> check {
status shouldEqual StatusCodes.Created
responseAs[String] shouldEqual ""
}
val uri = endpoint.withQuery(Uri.Query(Map("nick" -> playerId.nick, "job" -> playerId.job)))
val response = PlayerResponse.fromPlayer(Fixtures.playerEmpty.withBiS(Some(Fixtures.bis)))
Get(uri).withHeaders(auth) ~> route ~> check {
status shouldEqual StatusCodes.OK
val actual = responseAs[Seq[PlayerResponse]]
actual.length shouldEqual 1
actual.foreach(compareBiSResponse(_, response))
} }
} }

View File

@ -1,55 +1,61 @@
package me.arcanis.ffxivbis.http.api.v1 package me.arcanis.ffxivbis.http.api.v1
import akka.actor.ActorRef import java.time.Instant
import akka.actor.testkit.typed.scaladsl.ActorTestKit
import akka.actor.typed.scaladsl.AskPattern.Askable
import akka.http.scaladsl.model.{StatusCodes, Uri} import akka.http.scaladsl.model.{StatusCodes, Uri}
import akka.http.scaladsl.model.headers.{Authorization, BasicHttpCredentials} import akka.http.scaladsl.model.headers.{Authorization, BasicHttpCredentials}
import akka.http.scaladsl.testkit.{RouteTestTimeout, ScalatestRouteTest} import akka.http.scaladsl.testkit.{RouteTestTimeout, ScalatestRouteTest}
import akka.http.scaladsl.server._
import akka.pattern.ask
import akka.testkit.TestKit import akka.testkit.TestKit
import com.typesafe.config.Config import com.typesafe.config.Config
import me.arcanis.ffxivbis.{Fixtures, Settings} import me.arcanis.ffxivbis.{Fixtures, Settings}
import me.arcanis.ffxivbis.http.api.v1.json._ import me.arcanis.ffxivbis.http.api.v1.json._
import me.arcanis.ffxivbis.service.{PartyService, impl} import me.arcanis.ffxivbis.messages.{AddPlayer, AddUser}
import me.arcanis.ffxivbis.service.{Database, PartyService}
import me.arcanis.ffxivbis.storage.Migration import me.arcanis.ffxivbis.storage.Migration
import org.scalatest.{Matchers, WordSpec} import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike
import scala.concurrent.Await import scala.concurrent.Await
import scala.concurrent.duration._ import scala.concurrent.duration._
import scala.language.postfixOps import scala.language.postfixOps
class LootEndpointTest extends WordSpec class LootEndpointTest extends AnyWordSpecLike with Matchers with ScalatestRouteTest with JsonSupport {
with Matchers with ScalatestRouteTest with JsonSupport {
private val auth: Authorization = private val testKit = ActorTestKit(Settings.withRandomDatabase)
override val testConfig: Config = testKit.system.settings.config
private val auth =
Authorization(BasicHttpCredentials(Fixtures.userAdmin.username, Fixtures.userPassword)) Authorization(BasicHttpCredentials(Fixtures.userAdmin.username, Fixtures.userPassword))
private val endpoint: Uri = Uri(s"/party/${Fixtures.partyId}/loot") private val endpoint = Uri(s"/party/${Fixtures.partyId}/loot")
private val playerId = PlayerIdResponse.fromPlayerId(Fixtures.playerEmpty.playerId) private val playerId = PlayerIdResponse.fromPlayerId(Fixtures.playerEmpty.playerId)
private val timeout: FiniteDuration = 60 seconds private val askTimeout = 60 seconds
implicit private val routeTimeout: RouteTestTimeout = RouteTestTimeout(timeout) implicit private val routeTimeout: RouteTestTimeout = RouteTestTimeout(askTimeout)
private val storage: ActorRef = system.actorOf(impl.DatabaseImpl.props) private val storage = testKit.spawn(Database())
private val party: ActorRef = system.actorOf(PartyService.props(storage)) private val party = testKit.spawn(PartyService(storage))
private val route: Route = new LootEndpoint(party)(timeout).route private val route = new LootEndpoint(party)(askTimeout, testKit.scheduler).route
override def testConfig: Config = Settings.withRandomDatabase override def beforeAll(): Unit = {
super.beforeAll()
override def beforeAll: Unit = { Migration(testConfig)
Await.result(Migration(system.settings.config), timeout) Await.result(storage.ask(AddUser(Fixtures.userAdmin, isHashedPassword = true, _))(askTimeout, testKit.scheduler), askTimeout)
Await.result((storage ? impl.DatabaseUserHandler.AddUser(Fixtures.userAdmin, isHashedPassword = true))(timeout).mapTo[Int], timeout) Await.result(storage.ask(AddPlayer(Fixtures.playerEmpty, _))(askTimeout, testKit.scheduler), askTimeout)
Await.result((storage ? impl.DatabasePartyHandler.AddPlayer(Fixtures.playerEmpty))(timeout).mapTo[Int], timeout)
} }
override def afterAll: Unit = { override def afterAll(): Unit = {
Settings.clearDatabase(testConfig)
TestKit.shutdownActorSystem(system) TestKit.shutdownActorSystem(system)
Settings.clearDatabase(system.settings.config) testKit.shutdownTestKit()
super.afterAll()
} }
"api v1 loot endpoint" must { "api v1 loot endpoint" must {
"add item to loot" in { "add item to loot" in {
val piece = PieceResponse.fromPiece(Fixtures.lootBody) val piece = PieceResponse.fromPiece(Fixtures.lootBody)
val entity = PieceActionResponse(ApiAction.add, piece, playerId) val entity = PieceActionResponse(ApiAction.add, piece, playerId, Some(false))
Post(endpoint, entity).withHeaders(auth) ~> route ~> check { Post(endpoint, entity).withHeaders(auth) ~> route ~> check {
status shouldEqual StatusCodes.Accepted status shouldEqual StatusCodes.Accepted
@ -58,18 +64,23 @@ class LootEndpointTest extends WordSpec
} }
"return looted items" in { "return looted items" in {
import me.arcanis.ffxivbis.utils.Converters._
val uri = endpoint.withQuery(Uri.Query(Map("nick" -> playerId.nick, "job" -> playerId.job))) val uri = endpoint.withQuery(Uri.Query(Map("nick" -> playerId.nick, "job" -> playerId.job)))
val response = Seq(PlayerResponse.fromPlayer(Fixtures.playerEmpty.withLoot(Fixtures.lootBody))) val response = Seq(PlayerResponse.fromPlayer(Fixtures.playerEmpty.withLoot(Fixtures.lootBody)))
Get(uri).withHeaders(auth) ~> route ~> check { Get(uri).withHeaders(auth) ~> route ~> check {
status shouldEqual StatusCodes.OK status shouldEqual StatusCodes.OK
responseAs[Seq[PlayerResponse]] shouldEqual response val withEmptyTimestamp = responseAs[Seq[PlayerResponse]].map { player =>
player.copy(loot = player.loot.map(_.map(_.copy(timestamp = Instant.ofEpochMilli(0)))))
}
withEmptyTimestamp shouldEqual response
} }
} }
"remove item from loot" in { "remove item from loot" in {
val piece = PieceResponse.fromPiece(Fixtures.lootBody) val piece = PieceResponse.fromPiece(Fixtures.lootBody)
val entity = PieceActionResponse(ApiAction.remove, piece, playerId) val entity = PieceActionResponse(ApiAction.remove, piece, playerId, Some(false))
Post(endpoint, entity).withHeaders(auth) ~> route ~> check { Post(endpoint, entity).withHeaders(auth) ~> route ~> check {
status shouldEqual StatusCodes.Accepted status shouldEqual StatusCodes.Accepted

View File

@ -1,60 +1,74 @@
package me.arcanis.ffxivbis.http.api.v1 package me.arcanis.ffxivbis.http.api.v1
import akka.actor.ActorRef import akka.actor.testkit.typed.scaladsl.ActorTestKit
import akka.actor.typed.scaladsl.AskPattern.Askable
import akka.http.scaladsl.model.{StatusCodes, Uri} import akka.http.scaladsl.model.{StatusCodes, Uri}
import akka.http.scaladsl.model.headers.{Authorization, BasicHttpCredentials} import akka.http.scaladsl.model.headers.{Authorization, BasicHttpCredentials}
import akka.http.scaladsl.testkit.{RouteTestTimeout, ScalatestRouteTest} import akka.http.scaladsl.testkit.{RouteTestTimeout, ScalatestRouteTest}
import akka.http.scaladsl.server._
import akka.pattern.ask
import akka.testkit.TestKit import akka.testkit.TestKit
import com.typesafe.config.Config import com.typesafe.config.Config
import me.arcanis.ffxivbis.{Fixtures, Settings} import me.arcanis.ffxivbis.{Fixtures, Settings}
import me.arcanis.ffxivbis.http.api.v1.json._ import me.arcanis.ffxivbis.http.api.v1.json._
import me.arcanis.ffxivbis.service.{Ariyala, PartyService, impl} import me.arcanis.ffxivbis.messages.AddUser
import me.arcanis.ffxivbis.models.PartyDescription
import me.arcanis.ffxivbis.service.bis.BisProvider
import me.arcanis.ffxivbis.service.{Database, PartyService}
import me.arcanis.ffxivbis.storage.Migration import me.arcanis.ffxivbis.storage.Migration
import org.scalatest.{Matchers, WordSpec} import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike
import scala.concurrent.Await import scala.concurrent.Await
import scala.concurrent.duration._ import scala.concurrent.duration._
import scala.language.postfixOps import scala.language.postfixOps
class PartyEndpointTest extends WordSpec class PartyEndpointTest extends AnyWordSpecLike with Matchers with ScalatestRouteTest with JsonSupport {
with Matchers with ScalatestRouteTest with JsonSupport {
private val auth: Authorization = private val testKit = ActorTestKit(Settings.withRandomDatabase)
override val testConfig: Config = testKit.system.settings.config
private val auth =
Authorization(BasicHttpCredentials(Fixtures.userAdmin.username, Fixtures.userPassword)) Authorization(BasicHttpCredentials(Fixtures.userAdmin.username, Fixtures.userPassword))
private val endpoint: Uri = Uri(s"/party/${Fixtures.partyId}") private val endpoint = Uri(s"/party/${Fixtures.partyId}/description")
private val playerId = PlayerIdResponse.fromPlayerId(Fixtures.playerEmpty.playerId) private val askTimeout = 60 seconds
private val timeout: FiniteDuration = 60 seconds implicit private val routeTimeout: RouteTestTimeout = RouteTestTimeout(askTimeout)
implicit private val routeTimeout: RouteTestTimeout = RouteTestTimeout(timeout)
private val storage: ActorRef = system.actorOf(impl.DatabaseImpl.props) private val storage = testKit.spawn(Database())
private val ariyala: ActorRef = system.actorOf(Ariyala.props) private val provider = testKit.spawn(BisProvider())
private val party: ActorRef = system.actorOf(PartyService.props(storage)) private val party = testKit.spawn(PartyService(storage))
private val route: Route = new PlayerEndpoint(party, ariyala)(timeout).route private val route = new PartyEndpoint(party, provider)(askTimeout, testKit.scheduler).route
override def testConfig: Config = Settings.withRandomDatabase override def beforeAll(): Unit = {
super.beforeAll()
override def beforeAll: Unit = { Migration(testConfig)
Await.result(Migration(system.settings.config), timeout) Await.result(storage.ask(AddUser(Fixtures.userAdmin, isHashedPassword = true, _))(askTimeout, testKit.scheduler), askTimeout)
Await.result((storage ? impl.DatabaseUserHandler.AddUser(Fixtures.userAdmin, isHashedPassword = true))(timeout).mapTo[Int], timeout)
Await.result((storage ? impl.DatabasePartyHandler.AddPlayer(Fixtures.playerEmpty))(timeout).mapTo[Int], timeout)
} }
override def afterAll: Unit = { override def afterAll(): Unit = {
Settings.clearDatabase(testConfig)
TestKit.shutdownActorSystem(system) TestKit.shutdownActorSystem(system)
Settings.clearDatabase(system.settings.config) testKit.shutdownTestKit()
super.afterAll()
} }
"api v1 party endpoint" must { "api v1 party endpoint" must {
"get users" in { "get empty party description" in {
val uri = endpoint.withQuery(Uri.Query(Map("nick" -> playerId.nick, "job" -> playerId.job))) Get(endpoint).withHeaders(auth) ~> route ~> check {
val response = Seq(PlayerResponse.fromPlayer(Fixtures.playerEmpty)) status shouldEqual StatusCodes.OK
responseAs[PartyDescriptionResponse].toDescription shouldEqual PartyDescription.empty(Fixtures.partyId)
}
}
"update party description" in {
val entity = PartyDescriptionResponse(Fixtures.partyId, Some("random party name"))
Post(endpoint, entity).withHeaders(auth) ~> route ~> check {
status shouldEqual StatusCodes.Accepted
}
Get(endpoint).withHeaders(auth) ~> route ~> check { Get(endpoint).withHeaders(auth) ~> route ~> check {
status shouldEqual StatusCodes.OK status shouldEqual StatusCodes.OK
responseAs[Seq[PlayerResponse]] shouldEqual response responseAs[PartyDescriptionResponse].toDescription shouldEqual entity.toDescription
} }
} }

View File

@ -0,0 +1,65 @@
package me.arcanis.ffxivbis.http.api.v1
import akka.actor.testkit.typed.scaladsl.ActorTestKit
import akka.actor.typed.scaladsl.AskPattern.Askable
import akka.http.scaladsl.model.{StatusCodes, Uri}
import akka.http.scaladsl.model.headers.{Authorization, BasicHttpCredentials}
import akka.http.scaladsl.testkit.{RouteTestTimeout, ScalatestRouteTest}
import akka.testkit.TestKit
import com.typesafe.config.Config
import me.arcanis.ffxivbis.{Fixtures, Settings}
import me.arcanis.ffxivbis.http.api.v1.json._
import me.arcanis.ffxivbis.messages.{AddPlayer, AddUser}
import me.arcanis.ffxivbis.service.bis.BisProvider
import me.arcanis.ffxivbis.service.{Database, PartyService}
import me.arcanis.ffxivbis.storage.Migration
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike
import scala.concurrent.Await
import scala.concurrent.duration._
import scala.language.postfixOps
class PlayerEndpointTest extends AnyWordSpecLike with Matchers with ScalatestRouteTest with JsonSupport {
private val testKit = ActorTestKit(Settings.withRandomDatabase)
override val testConfig: Config = testKit.system.settings.config
private val auth =
Authorization(BasicHttpCredentials(Fixtures.userAdmin.username, Fixtures.userPassword))
private val endpoint = Uri(s"/party/${Fixtures.partyId}")
private val askTimeout = 60 seconds
implicit private val routeTimeout: RouteTestTimeout = RouteTestTimeout(askTimeout)
private val storage = testKit.spawn(Database())
private val provider = testKit.spawn(BisProvider())
private val party = testKit.spawn(PartyService(storage))
private val route = new PlayerEndpoint(party, provider)(askTimeout, testKit.scheduler).route
override def beforeAll(): Unit = {
super.beforeAll()
Migration(testConfig)
Await.result(storage.ask(AddUser(Fixtures.userAdmin, isHashedPassword = true, _))(askTimeout, testKit.scheduler), askTimeout)
Await.result(storage.ask(AddPlayer(Fixtures.playerEmpty, _))(askTimeout, testKit.scheduler), askTimeout)
}
override def afterAll(): Unit = {
Settings.clearDatabase(testConfig)
TestKit.shutdownActorSystem(system)
testKit.shutdownTestKit()
super.afterAll()
}
"api v1 player endpoint" must {
"get users" in {
val response = Seq(PlayerResponse.fromPlayer(Fixtures.playerEmpty))
Get(endpoint).withHeaders(auth) ~> route ~> check {
status shouldEqual StatusCodes.OK
responseAs[Seq[PlayerResponse]] shouldEqual response
}
}
}
}

View File

@ -2,21 +2,21 @@ package me.arcanis.ffxivbis.http.api.v1
import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.testkit.ScalatestRouteTest import akka.http.scaladsl.testkit.ScalatestRouteTest
import akka.http.scaladsl.server._
import com.typesafe.config.Config import com.typesafe.config.Config
import me.arcanis.ffxivbis.Settings import me.arcanis.ffxivbis.Settings
import me.arcanis.ffxivbis.http.api.v1.json._ import me.arcanis.ffxivbis.http.api.v1.json._
import me.arcanis.ffxivbis.models.{Job, Party, Permission, Piece} import me.arcanis.ffxivbis.models.{Job, Party, Permission, Piece, PieceType}
import org.scalatest.{Matchers, WordSpec} import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike
import scala.language.postfixOps import scala.language.postfixOps
class TypesEndpointTest extends WordSpec class TypesEndpointTest extends AnyWordSpecLike
with Matchers with ScalatestRouteTest with JsonSupport { with Matchers with ScalatestRouteTest with JsonSupport {
private val route: Route = new TypesEndpoint(testConfig).route override val testConfig: Config = Settings.withRandomDatabase
override def testConfig: Config = Settings.withRandomDatabase private val route = new TypesEndpoint(testConfig).route
"api v1 types endpoint" must { "api v1 types endpoint" must {
@ -41,6 +41,13 @@ class TypesEndpointTest extends WordSpec
} }
} }
"return all available piece types" in {
Get("/types/pieces/types") ~> route ~> check {
status shouldEqual StatusCodes.OK
responseAs[Seq[String]] shouldEqual PieceType.available.map(_.toString)
}
}
"return current priority" in { "return current priority" in {
Get("/types/priority") ~> route ~> check { Get("/types/priority") ~> route ~> check {
status shouldEqual StatusCodes.OK status shouldEqual StatusCodes.OK

View File

@ -1,45 +1,48 @@
package me.arcanis.ffxivbis.http.api.v1 package me.arcanis.ffxivbis.http.api.v1
import akka.actor.ActorRef import akka.actor.testkit.typed.scaladsl.ActorTestKit
import akka.http.scaladsl.model.{StatusCodes, Uri} import akka.http.scaladsl.model.{StatusCodes, Uri}
import akka.http.scaladsl.model.headers.{Authorization, BasicHttpCredentials} import akka.http.scaladsl.model.headers.{Authorization, BasicHttpCredentials}
import akka.http.scaladsl.testkit.{RouteTestTimeout, ScalatestRouteTest} import akka.http.scaladsl.testkit.{RouteTestTimeout, ScalatestRouteTest}
import akka.http.scaladsl.server._
import akka.testkit.TestKit import akka.testkit.TestKit
import com.typesafe.config.Config import com.typesafe.config.Config
import me.arcanis.ffxivbis.{Fixtures, Settings} import me.arcanis.ffxivbis.{Fixtures, Settings}
import me.arcanis.ffxivbis.http.api.v1.json._ import me.arcanis.ffxivbis.http.api.v1.json._
import me.arcanis.ffxivbis.service.{PartyService, impl} import me.arcanis.ffxivbis.service.{Database, PartyService}
import me.arcanis.ffxivbis.storage.Migration import me.arcanis.ffxivbis.storage.Migration
import org.scalatest.{Matchers, WordSpec} import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike
import scala.concurrent.Await import scala.concurrent.Await
import scala.concurrent.duration._ import scala.concurrent.duration._
import scala.language.postfixOps import scala.language.postfixOps
class UserEndpointTest extends WordSpec class UserEndpointTest extends AnyWordSpecLike with Matchers with ScalatestRouteTest with JsonSupport {
with Matchers with ScalatestRouteTest with JsonSupport {
private val auth: Authorization = private val testKit = ActorTestKit(Settings.withRandomDatabase)
override val testConfig: Config = testKit.system.settings.config
private val auth =
Authorization(BasicHttpCredentials(Fixtures.userAdmin.username, Fixtures.userPassword)) Authorization(BasicHttpCredentials(Fixtures.userAdmin.username, Fixtures.userPassword))
private def endpoint: Uri = Uri(s"/party/$partyId/users") private def endpoint = Uri(s"/party/$partyId/users")
private val timeout: FiniteDuration = 60 seconds private val askTimeout = 60 seconds
implicit private val routeTimeout: RouteTestTimeout = RouteTestTimeout(timeout) implicit private val routeTimeout: RouteTestTimeout = RouteTestTimeout(askTimeout)
private var partyId: String = Fixtures.partyId private var partyId = Fixtures.partyId
private val storage: ActorRef = system.actorOf(impl.DatabaseImpl.props) private val storage = testKit.spawn(Database())
private val party: ActorRef = system.actorOf(PartyService.props(storage)) private val party = testKit.spawn(PartyService(storage))
private val route: Route = new UserEndpoint(party)(timeout).route private val route = new UserEndpoint(party)(askTimeout, testKit.scheduler).route
override def testConfig: Config = Settings.withRandomDatabase override def beforeAll(): Unit = {
super.beforeAll()
override def beforeAll: Unit = { Migration(testConfig)
Await.result(Migration(system.settings.config), timeout)
} }
override def afterAll: Unit = { override def afterAll(): Unit = {
Settings.clearDatabase(testConfig)
TestKit.shutdownActorSystem(system) TestKit.shutdownActorSystem(system)
Settings.clearDatabase(system.settings.config) testKit.shutdownTestKit()
super.afterAll()
} }
"api v1 users endpoint" must { "api v1 users endpoint" must {

View File

@ -2,9 +2,10 @@ package me.arcanis.ffxivbis.models
import me.arcanis.ffxivbis.Fixtures import me.arcanis.ffxivbis.Fixtures
import me.arcanis.ffxivbis.utils.Compare import me.arcanis.ffxivbis.utils.Compare
import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpecLike} import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike
class BiSTest extends WordSpecLike with Matchers with BeforeAndAfterAll { class BiSTest extends AnyWordSpecLike with Matchers {
"bis model" must { "bis model" must {
@ -29,18 +30,14 @@ class BiSTest extends WordSpecLike with Matchers with BeforeAndAfterAll {
val bis = BiS(Seq(Fixtures.lootLegs)) val bis = BiS(Seq(Fixtures.lootLegs))
val newBis = bis.withPiece(Fixtures.lootHands) val newBis = bis.withPiece(Fixtures.lootHands)
newBis.legs shouldEqual Some(Fixtures.lootLegs) newBis shouldEqual BiS(Seq(Fixtures.lootLegs, Fixtures.lootHands))
newBis.hands shouldEqual Some(Fixtures.lootHands)
newBis.pieces.length shouldEqual 2
} }
"create copy without piece" in { "create copy without piece" in {
val bis = BiS(Seq(Fixtures.lootHands, Fixtures.lootLegs)) val bis = BiS(Seq(Fixtures.lootHands, Fixtures.lootLegs))
val newBis = bis.withoutPiece(Fixtures.lootHands) val newBis = bis.withoutPiece(Fixtures.lootHands)
newBis.legs shouldEqual Some(Fixtures.lootLegs) newBis shouldEqual BiS(Seq(Fixtures.lootLegs))
newBis.hands shouldEqual None
newBis.pieces.length shouldEqual 1
} }
"ignore upgrade on modification" in { "ignore upgrade on modification" in {
@ -49,7 +46,7 @@ class BiSTest extends WordSpecLike with Matchers with BeforeAndAfterAll {
} }
"return upgrade list" in { "return upgrade list" in {
Compare.mapEquals(Fixtures.bis.upgrades, Map[PieceUpgrade, Int](BodyUpgrade -> 2, AccessoryUpgrade -> 4)) shouldEqual true Compare.mapEquals(Fixtures.bis.upgrades, Map[PieceUpgrade, Int](BodyUpgrade -> 2, AccessoryUpgrade -> 3)) shouldEqual true
} }
} }

View File

@ -1,8 +1,9 @@
package me.arcanis.ffxivbis.models package me.arcanis.ffxivbis.models
import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpecLike} import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike
class JobTest extends WordSpecLike with Matchers with BeforeAndAfterAll { class JobTest extends AnyWordSpecLike with Matchers {
"job model" must { "job model" must {
@ -22,7 +23,7 @@ class JobTest extends WordSpecLike with Matchers with BeforeAndAfterAll {
"equal AnyJob to others" in { "equal AnyJob to others" in {
Job.available.foreach { job => Job.available.foreach { job =>
Job.AnyJob shouldBe job Job.AnyJob shouldEqual job
} }
} }

View File

@ -2,27 +2,29 @@ package me.arcanis.ffxivbis.models
import me.arcanis.ffxivbis.Fixtures import me.arcanis.ffxivbis.Fixtures
import me.arcanis.ffxivbis.utils.Compare import me.arcanis.ffxivbis.utils.Compare
import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpecLike} import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike
class PartyTest extends WordSpecLike with Matchers with BeforeAndAfterAll { class PartyTest extends AnyWordSpecLike with Matchers {
private val partyDescription = PartyDescription.empty(Fixtures.partyId)
private val party = private val party =
Party(Fixtures.partyId, Seq.empty, Map(Fixtures.playerEmpty.playerId -> Fixtures.playerEmpty)) Party(partyDescription, Seq.empty, Map(Fixtures.playerEmpty.playerId -> Fixtures.playerEmpty))
"party model" must { "party model" must {
"accept player with same party id" in { "accept player with same party id" in {
noException should be thrownBy noException should be thrownBy
Party(Fixtures.partyId, Seq.empty, Map(Fixtures.playerEmpty.playerId -> Fixtures.playerEmpty)) Party(partyDescription, Seq.empty, Map(Fixtures.playerEmpty.playerId -> Fixtures.playerEmpty))
} }
"fail on multiple party ids" in { "fail on multiple party ids" in {
val anotherPlayer = Fixtures.playerEmpty.copy(partyId = Fixtures.partyId2) val anotherPlayer = Fixtures.playerEmpty.copy(partyId = Fixtures.partyId2)
an [IllegalArgumentException] should be thrownBy an [IllegalArgumentException] should be thrownBy
Party(Fixtures.partyId, Seq.empty, Map(anotherPlayer.playerId -> anotherPlayer)) Party(partyDescription, Seq.empty, Map(anotherPlayer.playerId -> anotherPlayer))
an [IllegalArgumentException] should be thrownBy an [IllegalArgumentException] should be thrownBy
Party(Fixtures.partyId, Seq.empty, Map(Fixtures.playerEmpty.playerId -> Fixtures.playerEmpty, anotherPlayer.playerId -> anotherPlayer)) Party(partyDescription, Seq.empty, Map(Fixtures.playerEmpty.playerId -> Fixtures.playerEmpty, anotherPlayer.playerId -> anotherPlayer))
} }
"return player list" in { "return player list" in {
@ -38,7 +40,7 @@ class PartyTest extends WordSpecLike with Matchers with BeforeAndAfterAll {
} }
"add new player" in { "add new player" in {
val newParty = Party(Fixtures.partyId, Seq.empty, Map.empty) val newParty = Party(partyDescription, Seq.empty, Map.empty)
newParty.withPlayer(Fixtures.playerEmpty) shouldEqual party newParty.withPlayer(Fixtures.playerEmpty) shouldEqual party
} }

Some files were not shown because too many files have changed in this diff Show More