breakout-custom/app/controllers/GameController.scala
2024-09-06 19:53:24 -04:00

30 lines
No EOL
894 B
Scala

package controllers
import javax.inject._
import play.api.mvc._
import play.api.libs.json._
import models.Brick
@Singleton
class GameController @Inject()(val controllerComponents: ControllerComponents) extends BaseController {
implicit val brickWrites: Writes[Brick] = Json.writes[Brick]
implicit val brickReads: Reads[Brick] = Json.reads[Brick]
def index(): Action[AnyContent] = Action { implicit request: Request[AnyContent] =>
Ok(views.html.game())
}
def getBricks(file: String): Action[AnyContent] = Action {
val filePath = s"conf/$file"
try {
val source = scala.io.Source.fromFile(filePath)
val jsonString = try source.mkString finally source.close()
val bricks = Json.parse(jsonString).as[List[Brick]]
Ok(Json.toJson(bricks))
} catch {
case e: Exception =>
BadRequest(s"Error reading file: ${e.getMessage}")
}
}
}