Skip to content

Commit

Permalink
Add deactivation
Browse files Browse the repository at this point in the history
  • Loading branch information
outkine committed Mar 10, 2024
1 parent 539abce commit 8ad337c
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 21 deletions.
26 changes: 26 additions & 0 deletions app/controllers/RobotController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,32 @@ class RobotController @Inject()(
}
}

def deactivate(_username: String, name: String) =
auth.actionForceLI { user => implicit request =>
robotsRepo.findBare(user.id, name)(LoggedIn(user)) map {
case Some(robot) =>
Ok(
views.html.robot.deactivate(robot, assetsFinder)
)
case None =>
NotFound("404")
}
}

def postDeactivate(id: Long) =
auth.actionForceLI { user => implicit request =>
robotsRepo.findBare(RobotId(id))(LoggedIn(user)) flatMap {
case Some(robot) =>
robotsRepo.deactivate(robot.id) map { _ =>
Redirect(
routes.RobotController.view(user.username, robot.name)
)
}
case None =>
Future successful NotFound("404")
}
}

def viewById(id: Long) = auth.action { visitor => implicit request =>
robotsRepo.find(RobotId(id))(visitor) map {
case Some(FullRobot(robot, user)) =>
Expand Down
3 changes: 3 additions & 0 deletions app/models/Robots.scala
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ class Robots @Inject()(
def update(id: RobotId, name: String, bio: String, openSource: Boolean): Future[Long] =
run(robots.by(id).update(_.name -> lift(name), _.bio -> lift(bio), _.openSource -> lift(openSource)))

def deactivate(id: RobotId): Future[Long] =
run(robots.by(id).update(_.active -> false, _.deactivationReason -> lift(Some(DeactivationReason.Manual): Option[DeactivationReason])))

def updateAfterBattle(
id: RobotId,
prId: PRobotId,
Expand Down
1 change: 1 addition & 0 deletions app/models/Schema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ object Schema {
with QuillEnum[DeactivationReason] {
case object Errored extends DeactivationReason
case object Inactivity extends DeactivationReason
case object Manual extends DeactivationReason
val values = findValues
}

Expand Down
2 changes: 1 addition & 1 deletion app/views/board/publish.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
@main(s"Publish to ${board.name}", "publish", assetsFinder) {
<h1>Publish to <a href="@routes.BoardController.view(board.id.id)">@board.name</a></h1>

<p class="mb-3">Once you publish, your robot will be seen to every other user on this site, and will be queued for battles to establish your rating. You can only publish once every <b>@board.formatPublishCooldown()</b>, so make sure to only do so when your robot is ready to meet the competition.
<p class="mb-3">Once you publish, your robot will be seen to every other user on this site, and will be queued for battles to establish your rating. Please note that you cannot unpublish a robot, although you can pause matchmaking for that robot by deactivating it. Also, you can only publish once every <b>@board.formatPublishCooldown()</b> on this board, so make sure to only do so when your robot is ready to meet the competition.
<a href="https://rr-docs.readthedocs.io/en/latest/publishing.html" target="_blank">Learn more</a>
</p>

Expand Down
11 changes: 11 additions & 0 deletions app/views/robot/deactivate.scala.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@import models.Schema._
@(robot: Robot, assetsFinder: AssetsFinder)(implicit request: MessagesRequestHeader)

@main("Deactivate robot", "deactivate-robot", assetsFinder) {
<p class="mb-3">Deactivating a robot prevents it from being involved in matchmaking on any board that it is currently published to. Please note, however, that both this robot and any of its battles will still be visible on the site.</p>

@helper.form(routes.RobotController.postDeactivate(robot.id.id)) {
@helper.CSRF.formField
<button class="button">deactivate</button>
}
}
48 changes: 28 additions & 20 deletions app/views/robot/view.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,6 @@
<h1>Robot @fullRobot.robot.name</h1>
} else {
<h1 class="inactive">Robot @fullRobot.robot.name (inactive)</h1>
@if(isOwner) {
<p class="error mb-3">Deactivation reason:
@fullRobot.robot.deactivationReason match {
case Some(DeactivationReason.Errored) => {
Your robot errored too many times in a row
}
case Some(DeactivationReason.Inactivity) => {
Your robot has not been updated in a long time
}
case None => {
Unknown
}
}
</p>
}
}
<h3 class="mb-4">@if(fullRobot.robot.openSource) { Open Source } @fullRobot.robot.lang robot by <a href="@routes.UserController.view(fullRobot.user.username)">@fullRobot.user.username</a></h3>

Expand All @@ -63,16 +48,39 @@ <h3 class="mb-4">@if(fullRobot.robot.openSource) { Open Source } @fullRobot.robo
<img src="@assetsFinder.path("images/edit.svg")" alt="">
update name/bio/open source
</a>

@if(fullRobot.robot.active) {
<a href="@routes.RobotController.deactivate(fullRobot.user.username, fullRobot.robot.name)">
<img src="@assetsFinder.path("images/deactivate.svg")" alt="">
deactivate
</a>
}
}
</div>

@if(isOwner && fullRobot.robot.errorCount > 0) {
<p class="mt-3 error">
@if(isOwner && !fullRobot.robot.active) {
<p class=@{if (fullRobot.robot.deactivationReason != Some(DeactivationReason.Manual)) "error mt-3" else "mt-3"}>Your robot is no longer active, which means it is not involved in matchmaking. You can reactivate it by publishing to a board. Deactivation reason:
@fullRobot.robot.deactivationReason match {
case Some(DeactivationReason.Errored) => {
Your robot errored @fullRobot.robot.errorCount times in a row
}
case Some(DeactivationReason.Inactivity) => {
Your robot has not been updated in a long time
}
case Some(DeactivationReason.Manual) => {
Your robot has been manually deactivated
}
case None => {
Unknown
}
} <a href="https://rr-docs.readthedocs.io/en/latest/publishing.html#deactivation" target="_blank">(learn more)</a>
</p>
}

@if(isOwner && fullRobot.robot.errorCount > 0 && (fullRobot.robot.active || fullRobot.robot.deactivationReason != Some(DeactivationReason.Errored))) {
<p class="error mt-3">
<img src="@assetsFinder.path("images/error.svg")" alt="">
Your robot errored @fullRobot.robot.errorCount time@{if (fullRobot.robot.errorCount > 1) "s"} in recent battles.
@if(!fullRobot.robot.active) {
It has been automatically deactivated. Matchmaking will resume once you re-publish with corrected code. <a href="https://rr-docs.readthedocs.io/en/latest/publishing.html#deactivation" target="_blank">Learn more</a>
}
</p>
}

Expand Down
2 changes: 2 additions & 0 deletions conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ GET /assets/*file controllers.Assets.at(file)
GET /robots/create controllers.RobotController.create
POST /robots/create controllers.RobotController.postCreate
POST /robots/update/:robotId controllers.RobotController.postUpdate(robotId: Long)
POST /robots/deactivate/:robotId controllers.RobotController.postDeactivate(robotId: Long)

GET /:user controllers.UserController.view(user)
GET /:user/ controllers.UserController.view(user)
GET /:user/:robot controllers.RobotController.view(user, robot)
GET /:user/:robot/ controllers.RobotController.view(user, robot)
GET /:user/:robot/edit controllers.RobotController.edit(user, robot)
GET /:user/:robot/update controllers.RobotController.update(user, robot)
GET /:user/:robot/deactivate controllers.RobotController.deactivate(user, robot)
GET /:user/:robot/view-code controllers.RobotController.viewPublishedCode(user, robot)
64 changes: 64 additions & 0 deletions public/images/deactivate.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8ad337c

Please sign in to comment.