-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
140 changed files
with
9,494 additions
and
1,096 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package controllers | ||
|
||
import play.api.libs.json.* | ||
import play.api.i18n.Lang | ||
import play.api.mvc.* | ||
import play.api.data.* | ||
import play.api.data.Forms.* | ||
import views.* | ||
|
||
import lila.app.{ given, * } | ||
import lila.common.Json.given | ||
import lila.user.User | ||
import lila.rating.{ Perf, PerfType } | ||
import lila.security.Permission | ||
import lila.local.{ GameSetup, AssetType } | ||
|
||
final class Local(env: Env) extends LilaController(env): | ||
def index = Open: | ||
for | ||
bots <- env.local.repo.getLatestBots() | ||
page <- renderPage(indexPage(bots, none)) | ||
yield Ok(page).enforceCrossSiteIsolation.withHeaders("Service-Worker-Allowed" -> "/") | ||
|
||
def bots = Open: | ||
env.local.repo | ||
.getLatestBots() | ||
.map: bots => | ||
JsonOk(Json.obj("bots" -> bots)) | ||
|
||
def assetKeys = Open: // for service worker | ||
JsonOk(env.local.api.assetKeys) | ||
|
||
def devIndex = Auth: _ ?=> | ||
for | ||
bots <- env.local.repo.getLatestBots() | ||
assets <- devGetAssets | ||
page <- renderPage(indexPage(bots, assets.some)) | ||
yield Ok(page).enforceCrossSiteIsolation.withHeaders("Service-Worker-Allowed" -> "/") | ||
|
||
def devAssets = Auth: ctx ?=> | ||
devGetAssets.map(JsonOk) | ||
|
||
def devBotHistory(botId: Option[String]) = Auth: _ ?=> | ||
env.local.repo | ||
.getVersions(botId.map(UserId.apply)) | ||
.map: history => | ||
JsonOk(Json.obj("bots" -> history)) | ||
|
||
def devPostBot = SecureBody(parse.json)(_.BotEditor) { ctx ?=> me ?=> | ||
ctx.body.body | ||
.validate[JsObject] | ||
.fold( | ||
err => BadRequest(Json.obj("error" -> err.toString)), | ||
bot => | ||
env.local.repo | ||
.putBot(bot, me.userId) | ||
.map: updatedBot => | ||
JsonOk(updatedBot) | ||
) | ||
} | ||
|
||
def devNameAsset(key: String, name: String) = Secure(_.BotEditor): _ ?=> | ||
env.local.repo | ||
.nameAsset(none, key, name, none) | ||
.flatMap(_ => devGetAssets.map(JsonOk)) | ||
|
||
def devDeleteAsset(key: String) = Secure(_.BotEditor): _ ?=> | ||
env.local.repo | ||
.deleteAsset(key) | ||
.flatMap(_ => devGetAssets.map(JsonOk)) | ||
|
||
def devPostAsset(notAString: String, key: String) = SecureBody(parse.multipartFormData)(_.BotEditor) { | ||
ctx ?=> | ||
val tpe: AssetType = notAString.asInstanceOf[AssetType] | ||
val author: Option[String] = ctx.body.body.dataParts.get("author").flatMap(_.headOption) | ||
val name = ctx.body.body.dataParts.get("name").flatMap(_.headOption).getOrElse(key) | ||
ctx.body.body | ||
.file("file") | ||
.map: file => | ||
env.local.api | ||
.storeAsset(tpe, key, file) | ||
.flatMap: | ||
case Left(error) => InternalServerError(Json.obj("error" -> error.toString)).as(JSON) | ||
case Right(assets) => | ||
env.local.repo | ||
.nameAsset(tpe.some, key, name, author) | ||
.flatMap(_ => (JsonOk(Json.obj("key" -> key, "name" -> name)))) | ||
.getOrElse(fuccess(BadRequest(Json.obj("error" -> "missing file")).as(JSON))) | ||
} | ||
|
||
private def indexPage(bots: JsArray, devAssets: Option[JsObject] = none)(using | ||
ctx: Context | ||
) = | ||
given setupFormat: Format[GameSetup] = Json.format[GameSetup] | ||
views.local.index( | ||
Json | ||
.obj("pref" -> pref, "bots" -> bots) | ||
.add("assets", devAssets) | ||
.add("userId", ctx.me.map(_.userId)) | ||
.add("username", ctx.me.map(_.username)) | ||
.add("canPost", isGrantedOpt(_.BotEditor)), | ||
if devAssets.isDefined then "local.dev" else "local" | ||
) | ||
|
||
private def devGetAssets = | ||
env.local.repo.getAssets.map: m => | ||
JsObject: | ||
env.local.api.assetKeys | ||
.as[JsObject] | ||
.fields | ||
.collect: | ||
case (category, JsArray(keys)) => | ||
category -> JsArray: | ||
keys.collect: | ||
case JsString(key) if m.contains(key) => | ||
Json.obj("key" -> key, "name" -> m(key)) | ||
|
||
private def pref(using ctx: Context) = | ||
lila.pref.JsonView | ||
.write(ctx.pref, false) | ||
.add("animationDuration", ctx.pref.animationMillis.some) | ||
.add("enablePremove", ctx.pref.premove.some) | ||
|
||
private def optTrue(s: Option[String]) = | ||
s.exists(v => v == "" || v == "1" || v == "true") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package lila.local | ||
|
||
import com.softwaremill.macwire.* | ||
import play.api.Configuration | ||
|
||
import lila.common.autoconfig.{ *, given } | ||
import lila.core.config.* | ||
|
||
@Module | ||
final private class LocalConfig( | ||
@ConfigName("asset_path") val assetPath: String | ||
) | ||
|
||
@Module | ||
final class Env( | ||
appConfig: Configuration, | ||
db: lila.db.Db, | ||
getFile: (String => java.io.File) | ||
)(using | ||
Executor, | ||
akka.stream.Materializer | ||
)(using mode: play.api.Mode, scheduler: Scheduler): | ||
|
||
private val config: LocalConfig = appConfig.get[LocalConfig]("local")(AutoConfig.loader) | ||
|
||
val repo = LocalRepo(db(CollName("local_bots")), db(CollName("local_assets"))) | ||
|
||
val api: LocalApi = wire[LocalApi] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package lila.local | ||
|
||
import java.nio.file.{ Files as NioFiles, Paths } | ||
import play.api.libs.json.* | ||
import play.api.libs.Files | ||
import play.api.mvc.* | ||
import akka.stream.scaladsl.{ FileIO, Source } | ||
import akka.util.ByteString | ||
|
||
// this stuff is for bot devs | ||
|
||
final private class LocalApi(config: LocalConfig, repo: LocalRepo, getFile: (String => java.io.File))(using | ||
Executor, | ||
akka.stream.Materializer | ||
): | ||
|
||
@volatile private var cachedAssets: Option[JsObject] = None | ||
|
||
def storeAsset( | ||
tpe: AssetType, | ||
name: String, | ||
file: MultipartFormData.FilePart[Files.TemporaryFile] | ||
): Fu[Either[String, JsObject]] = | ||
FileIO | ||
.fromPath(file.ref.path) | ||
.runWith(FileIO.toPath(getFile(s"public/lifat/bots/${tpe}/$name").toPath)) | ||
.map: result => | ||
if result.wasSuccessful then Right(updateAssets) | ||
else Left(s"Error uploading asset $tpe $name") | ||
.recover: | ||
case e: Exception => Left(s"Exception: ${e.getMessage}") | ||
|
||
def assetKeys: JsObject = cachedAssets.getOrElse(updateAssets) | ||
|
||
private def listFiles(tpe: String, ext: String): List[String] = | ||
val path = getFile(s"public/lifat/bots/${tpe}") | ||
if !path.exists() then | ||
NioFiles.createDirectories(path.toPath) | ||
Nil | ||
else | ||
path | ||
.listFiles() | ||
.toList | ||
.map(_.getName) | ||
.filter(_.endsWith(s".${ext}")) | ||
|
||
def updateAssets: JsObject = | ||
val newAssets = Json.obj( | ||
"image" -> listFiles("image", "webp"), | ||
"net" -> listFiles("net", "pb"), | ||
"sound" -> listFiles("sound", "mp3"), | ||
"book" -> listFiles("book", "png") | ||
.map(_.dropRight(4)) | ||
) | ||
cachedAssets = newAssets.some | ||
newAssets |
Oops, something went wrong.