Skip to content

Commit

Permalink
Abstract Platforms to PlatformCategories for future Platforms
Browse files Browse the repository at this point in the history
If/when we want to add Bukkit/Spigot/Paper or whatever else, adding a
new platform is super simple.

Signed-off-by: Jadon Fowler <[email protected]>
  • Loading branch information
phase committed Aug 16, 2018
1 parent 5fadf19 commit 17a0eb8
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 85 deletions.
4 changes: 2 additions & 2 deletions OreTestPlugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ apply plugin: 'signing'
// id 'org.spongepowered.plugin' version '0.8.2-SNAPSHOT'
//}

group = 'se.walkercrou'
group = 'org.spongepowered.ore'
version = '1.0.1'
description = 'Ore test plugin'

Expand Down Expand Up @@ -60,5 +60,5 @@ oreDeploy {
}

dependencies {
compile 'org.spongepowered:spongeapi:6.0.0-SNAPSHOT'
compile 'org.spongepowered:spongeapi:7.0.0'
}
1 change: 0 additions & 1 deletion app/controllers/Application.scala
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ final class Application @Inject()(data: DataHelper,
platform: Option[String]): Action[AnyContent] = OreAction async { implicit request =>
// Get categories and sorting strategy


val canHideProjects = request.data.globalPerm(HideProjects)
val currentUserId = request.data.currentUser.flatMap(_.id).getOrElse(-1)

Expand Down
28 changes: 26 additions & 2 deletions app/models/project/Tag.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ package models.project

import java.sql.Timestamp

import db.Named
import db.impl.OrePostgresDriver.api._
import db.impl.model.OreModel
import db.impl.table.ModelKeys._
import db.impl.{OrePostgresDriver, TagTable}
import db.table.MappedType
import db.{ModelService, Named}
import models.project.TagColors.TagColor
import slick.jdbc.JdbcType

import scala.concurrent.{ExecutionContext, Future}

case class Tag(override val id: Option[Int] = None,
private var _versionIds: List[Int],
name: String,
Expand All @@ -23,14 +26,35 @@ case class Tag(override val id: Option[Int] = None,

def versionIds: List[Int] = this._versionIds

def addVersionId(versionId: Int) = {
def addVersionId(versionId: Int): Unit = {
this._versionIds = this._versionIds :+ versionId
if (isDefined) {
update(TagVersionIds)
}
}

def copyWith(id: Option[Int], theTime: Option[Timestamp]): Tag = this.copy(id = id)

/**
* Used to convert a ghost tag to a normal tag
* @author phase
*/
def getFilledTag(service: ModelService)(implicit ex: ExecutionContext): Future[Tag] = {
val access = service.access(classOf[Tag])
for {
tagsWithVersion <- access.filter(t => t.name === this.name && t.data === this.data)
tag <- {
if(tagsWithVersion.isEmpty) {
access.add(this)
} else {
Future.successful(tagsWithVersion.head)
}
}
} yield {
tag
}
}

}

object TagColors extends Enumeration {
Expand Down
2 changes: 1 addition & 1 deletion app/models/project/Version.scala
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ case class Version(override val id: Option[Int] = None,
if(isDefined) update(TagIds)
}

def addTag(tag: Tag) = {
def addTag(tag: Tag): Unit = {
this._tagIds = this._tagIds :+ tag.id.get
if (isDefined) {
update(TagIds)
Expand Down
83 changes: 70 additions & 13 deletions app/ore/Platforms.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,70 @@
package ore

import scala.language.implicitConversions

object Platforms extends Enumeration {

val Sponge = Platform(0, "Sponge")
val Forge = Platform(1, "Forge")

case class Platform(override val id: Int, name: String) extends super.Val(id, name)
implicit def convert(v: Value): Platform = v.asInstanceOf[Platform]

}
package ore

import models.project.{Tag, TagColors}
import models.project.TagColors.TagColor
import ore.project.Dependency

import scala.language.implicitConversions

/**
* The Platform a plugin/mod runs on
*
* @author phase
*/
object Platforms extends Enumeration {

val Sponge = Platform(0, "Sponge", SpongeCategory, 0, Dependency.SpongeApiId, TagColors.Sponge)
val SpongeForge = Platform(2, "SpongeForge", SpongeCategory, 2, Dependency.SpongeForgeId, TagColors.Sponge)
val SpongeVanilla = Platform(3, "SpongeVanilla", SpongeCategory, 1, Dependency.SpongeVanillaId, TagColors.Sponge)
val Forge = Platform(1, "Forge", ForgeCategory, 0, Dependency.ForgeId, TagColors.Forge)

case class Platform(override val id: Int,
name: String,
platformCategory: PlatformCategory,
priority: Int,
dependencyId: String,
tagColor: TagColor
) extends super.Val(id, name) {

def toGhostTag(version: String): Tag = Tag(None, List(), name, version, tagColor)

}

implicit def convert(v: Value): Platform = v.asInstanceOf[Platform]

def getPlatforms(dependencyIds: List[String]): List[Platform] = {
Platforms.values
.filter(p => dependencyIds.contains(p.dependencyId))
.groupBy[PlatformCategory](p => p.platformCategory)
.map(map => map._2.toList.maxBy(p => p.priority))
.map(p => p.asInstanceOf[Platform])
.toList
}

def getPlatformGhostTags(dependencies: List[Dependency]): List[Tag] = {
Platforms.values
.filter(p => dependencies.map(_.pluginId).contains(p.dependencyId))
.groupBy[PlatformCategory](p => p.platformCategory)
.map(map => map._2.toList.maxBy(p => p.priority))
.map(p => p.toGhostTag(dependencies.find(d => d.pluginId == p.dependencyId).get.version))
.toList
}

}

/**
* The category of a platform.
* Examples would be
*
* Sponge <- SpongeAPI, SpongeForge, SpongeVanilla
* Forge <- Forge (maybe Rift if that doesn't die?)
* Bukkit <- Bukkit, Spigot, Paper
* Canary <- Canary, Neptune
*
* @author phase
*/
sealed trait PlatformCategory

case object SpongeCategory extends PlatformCategory

case object ForgeCategory extends PlatformCategory
17 changes: 3 additions & 14 deletions app/ore/project/factory/PendingVersion.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package ore.project.factory

import db.impl.access.ProjectBase
import models.project._
import ore.Cacheable
import ore.Colors.Color
import ore.project.Dependency
import ore.project.factory.TagAlias.ProjectTag
import ore.project.io.PluginFile
import ore.{Cacheable, Platforms}
import play.api.cache.SyncCacheApi

import scala.concurrent.{ExecutionContext, Future}
Expand Down Expand Up @@ -51,17 +50,7 @@ case class PendingVersion(projects: ProjectBase,
override def key: String = this.project.url + '/' + this.underlying.versionString

def dependenciesAsGhostTags: Seq[Tag] = {
var ghostFlags: Seq[Tag] = Seq()
for (dependency <- this.underlying.dependencies) {
if (factory.dependencyVersionRegex.pattern.matcher(dependency.version).matches()) {
if (dependency.pluginId.equalsIgnoreCase(Dependency.SpongeApiId)) {
ghostFlags = ghostFlags :+ Tag(None, List(), "Sponge", dependency.version, TagColors.Sponge)
}
if (dependency.pluginId.equalsIgnoreCase(Dependency.ForgeId)) {
ghostFlags = ghostFlags :+ Tag(None, List(), "Forge", dependency.version, TagColors.Forge)
}
}
}
ghostFlags
Platforms.getPlatformGhostTags(this.underlying.dependencies)
}

}
53 changes: 7 additions & 46 deletions app/ore/project/factory/ProjectFactory.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import models.project._
import models.user.role.ProjectRole
import models.user.{Notification, User}
import ore.Colors.Color
import ore.{OreConfig, OreEnv}
import ore.{OreConfig, OreEnv, Platforms}
import ore.permission.role.RoleTypes
import ore.project.Dependency
import ore.project.{NotifyWatchersTask, ProjectMember}
Expand All @@ -36,7 +36,6 @@ import scala.concurrent.{ExecutionContext, Future}
import scala.concurrent.duration.Duration
import scala.util.Try
import scala.util.matching.Regex

import db.impl.{ProjectMembersTable, ProjectRoleTable}
import ore.user.MembershipDossier

Expand Down Expand Up @@ -378,54 +377,16 @@ trait ProjectFactory {

private def addDependencyTags(version: Version)(implicit ec: ExecutionContext): Future[Seq[ProjectTag]] = {
for {
spongeForgeTag <- addTag(version, Dependency.SpongeForgeId, "SpongeForge", TagColors.Sponge).value
spongeVanillaTag <- addTag(version, Dependency.SpongeVanillaId, "SpongeVanilla", TagColors.Sponge).value
spongeTag <- addTag(version, Dependency.SpongeApiId, "Sponge", TagColors.Sponge).value
forgeTag <- addTag(version, Dependency.ForgeId, "Forge", TagColors.Forge).value
tags <- Future.sequence(Platforms.getPlatformGhostTags(version.dependencies).map(tag => tag.getFilledTag(service)))
} yield {
if (spongeForgeTag.isDefined) {
Seq(spongeForgeTag.get)
} else if (spongeVanillaTag.isDefined) {
Seq(spongeVanillaTag.get)
} else {
(spongeTag ++ forgeTag).toSeq
}
tags.map(tag => {
tag.addVersionId(version.id.get)
version.addTag(tag)
tag
})
}
}

private def addTag(newVersion: Version, dependencyName: String, tagName: String, tagColor: TagColor)(implicit ec: ExecutionContext): OptionT[Future, ProjectTag] = {
val dependenciesMatchingName = newVersion.dependencies.filter(_.pluginId.equalsIgnoreCase(dependencyName))
OptionT.fromOption[Future](dependenciesMatchingName.headOption)
.filter(dep => dependencyVersionRegex.pattern.matcher(dep.version).matches())
.semiFlatMap { dep =>
for {
tagsWithVersion <- service.access(classOf[ProjectTag])
.filter(t => t.name === tagName && t.data === dep.version)
tag <- {
if (tagsWithVersion.isEmpty) {
val tag = Tag(
_versionIds = List(newVersion.id.get),
name = tagName,
data = dep.version,
color = tagColor
)
val newTag = service.access(classOf[ProjectTag]).add(tag)
newTag.map(newVersion.addTag)
newTag

} else {
val tag = tagsWithVersion.head
tag.addVersionId(newVersion.id.get)
Future.successful(tag)
}
}
} yield {
tag
}
}
}


private def getOrCreateChannel(pending: PendingVersion, project: Project)(implicit ec: ExecutionContext) = {
project.channels.find(equalsIgnoreCase(_.name, pending.channelName))
.getOrElseF(createChannel(project, pending.channelName, pending.channelColor, nonReviewed = false))
Expand Down
7 changes: 1 addition & 6 deletions app/views/projects/list.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
@import models.project.{Project, Tag, Version}
@import models.user.User
@import ore.OreConfig
@import ore.project.{Categories, Dependency}
@import ore.project.Categories
@import views.html.utils
@(models: Seq[(Project, User, Version, Seq[Tag])], page: Int, pageSize: Int, call: Int => Call)(implicit messages: Messages, flash: Flash, config: OreConfig)

Expand Down Expand Up @@ -39,11 +39,6 @@
@rv.name
</a>
</span>
@rv.dependencies.find(_.pluginId.equals(Dependency.MinecraftId)).map { dep =>
<span class="stat" title="Minecraft version">
<strong>MC @dep.version</strong>
</span>
}

<span class="stat" title="Views"><i class="fa fa-eye"></i> @project.viewCount</span>
<span class="stat" title="Download"><i class="fa fa-download"></i> @project.downloadCount</span>
Expand Down

0 comments on commit 17a0eb8

Please sign in to comment.