Skip to content

Commit

Permalink
Added basic UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter van 't Hof committed Aug 19, 2018
1 parent ee6d9b1 commit 1051022
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 15 deletions.
101 changes: 95 additions & 6 deletions app/controllers/HomeController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,117 @@ import play.api.data.Forms._
import twentyone.utils.Game

import scala.collection.mutable
import scala.collection.mutable.ListBuffer
import scala.concurrent.{ExecutionContext, Future}

@Singleton
class HomeController @Inject()(cc: ControllerComponents)(implicit ec: ExecutionContext) extends AbstractController(cc) {

def index = Action { request =>
Ok(views.html.index(HomeController.games.toMap))
Ok(views.html.index(HomeController.games.toList))
}

def game(gameId: Int) = Action { request =>
HomeController.games.get(gameId) match {
case Some(game) => Ok("")
HomeController.games.lift(gameId) match {
case Some(game) => Ok(views.html.game(game, gameId, None, game.playerTurn))
case _ => NotFound("Game does not exists")
}
}

def player(gam: Int, player: Int) = Action { request =>
Ok("")
def player(gameId: Int, playerId: Int) = Action { request =>
HomeController.games.lift(gameId) match {
case Some(game) =>
Ok(views.html.game(game, gameId, Some(playerId), game.playerTurn))
case _ => NotFound("Game does not exists")
}
}

def createGame(playerName: String) = Action { request =>
require(playerName.nonEmpty, "Player can't be a empty string")
val game = Game(1)
game.addPlayer(playerName)
HomeController.games += game
Redirect(s"/game/${HomeController.games.size - 1}/player/0")
}

def joinGame(gameId: Int, playerName: String) = Action { request =>
require(playerName.nonEmpty, "Player can't be a empty string")
HomeController.games.lift(gameId) match {
case Some(game) =>
game.addPlayer(playerName)
Redirect(s"/game/$gameId/player/${game.numberPlayers - 1}")
case _ => NotFound("Game does not exists")
}
}

def startGame(gameId: Int, playerId: Option[Int]) = Action {
HomeController.games.lift(gameId) match {
case Some(game) =>
game.start()
playerId match {
case Some(p) => Redirect(s"/game/$gameId/player/$p")
case _ => Redirect(s"/game/$gameId")
}
case _ => NotFound("Game does not exists")
}
}

def bet(gameId: Int, playerId: Int, bet: Int) = Action {
HomeController.games.lift(gameId) match {
case Some(game) =>
game.playerAddBet(playerId, bet)
Redirect(s"/game/$gameId/player/$playerId")
case _ => NotFound("Game does not exists")
}
}

def hit(gameId: Int, playerId: Int) = Action {
HomeController.games.lift(gameId) match {
case Some(game) =>
game.playerHit(playerId)
Redirect(s"/game/$gameId/player/$playerId")
case _ => NotFound("Game does not exists")
}
}

def hold(gameId: Int, playerId: Int) = Action {
HomeController.games.lift(gameId) match {
case Some(game) =>
game.playerHold(playerId)
Redirect(s"/game/$gameId/player/$playerId")
case _ => NotFound("Game does not exists")
}
}

def split(gameId: Int, playerId: Int) = Action {
HomeController.games.lift(gameId) match {
case Some(game) =>
game.playerSplit(playerId)
Redirect(s"/game/$gameId/player/$playerId")
case _ => NotFound("Game does not exists")
}
}

def splitHit(gameId: Int, playerId: Int) = Action {
HomeController.games.lift(gameId) match {
case Some(game) =>
game.playerSplitHit(playerId)
Redirect(s"/game/$gameId/player/$playerId")
case _ => NotFound("Game does not exists")
}
}

def splitHold(gameId: Int, playerId: Int) = Action {
HomeController.games.lift(gameId) match {
case Some(game) =>
game.playerSplitHold(playerId)
Redirect(s"/game/$gameId/player/$playerId")
case _ => NotFound("Game does not exists")
}
}

}

object HomeController {
private val games: mutable.Map[Int, Game] = mutable.Map()
private val games: ListBuffer[Game] = ListBuffer()
}
88 changes: 87 additions & 1 deletion app/views/game.scala.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,95 @@
@import twentyone.utils.Game
@(game: Game)
@import twentyone.utils.Player
@(game: Game, gameId: Int, playerId: Option[Int], playerTurn: Option[Int])

@main("Twenty One") {

<a href="/">Home</a>
<h1>Twenty One</h1>
<table>
<tr><th>State</th></tr>
<tr>
<td>@game.state</td>
@if(game.state == Game.State.OpenToJoin) {
<td><form action="/start">
<input type="hidden", name="gameId", value="@gameId">
@playerId match {
case Some(id) => {
<input type="hidden" name="playerId", value="@{playerId}">
}
case _ => {}
}
<input type="submit" value="Start game">
</form></td>
}
</tr>
</table>

<br>

<table>
<tr><th>Name</th><th>Bet</th><th>Points</th><th>Actions<th><th>Cards</th>
@if(game.state == Game.State.Done) {
<th></th>
}
</tr>
<tr><td>Bank</td><td></td><td>@game.bank.totalPoints</td><td></td><td>@game.bank.cards</td>
@if(game.state == Game.State.Done) {
<td></td>
}
</tr>
@for(id <- 0 until game.numberPlayers) {
@defining(game.getPlayer(id)) { player: Player =>
<tr><td>@player.name</td><td>@player.bet.getOrElse("-")</td><td>@player.totalPoints</td><td>
@if(game.state == Game.State.Betting && player.bet.isEmpty) {
<form action="/bet">
<input type="hidden", name="gameId" value="@gameId">
<input type="hidden" name="playerId" value="@{id}">
<input type="text" name="bet">
<input type="submit" value="Bet">
</form>
}
@if(game.state == Game.State.Playing && game.playerTurn == Some(id)) {
@if(player.status == Player.Status.Playing) {
<form action="/hit">
<input type="hidden", name="gameId" value="@gameId">
<input type="hidden" name="playerId" value="@{id}">
<input type="submit" value="Hit">
</form>
<form action="/hold">
<input type="hidden", name="gameId" value="@gameId">
<input type="hidden" name="playerId" value="@{id}">
<input type="submit" value="Hold">
</form>
}
@if(player.canSplit) {
<form action="/split">
<input type="hidden", name="gameId" value="@gameId">
<input type="hidden" name="playerId" value="@{id}">
<input type="submit" value="split">
</form>
}
@if(player.splitStatus == Some(Player.Status.Playing)) {
<form action="/splitHit">
<input type="hidden", name="gameId" value="@gameId">
<input type="hidden" name="playerId" value="@{id}">
<input type="submit" value="splitHit">
</form>
<form action="/splitHold">
<input type="hidden", name="gameId" value="@gameId">
<input type="hidden" name="playerId" value="@{id}">
<input type="submit" value="splitHold">
</form>
}
}
</td><td>@player.cards</td>
<td>
@if(game.state == Game.State.Done) {
@game.result(id)
}
</td>
</tr>
}
}
</table>
}
22 changes: 19 additions & 3 deletions app/views/index.scala.html
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
@import twentyone.utils.Game
@(games: Map[Int, Game])
@(games: List[Game])

@main("Twenty One") {

<h1>Twenty One</h1>

<form action="/create">
Player name <input type="text" name="name">
<input type="submit" value="Create game" required>
</form>

<h3>Games</h3>

@if(games.isEmpty) {
<b>No game yet</b>
} else {
<table>
<tr><th>ID</th><th>State</th><th>Players</th></tr>
@for((gameId, game) <- games) {
<td>@gameId</td><td>@game.state</td><td>@game.numberPlayers</td>
@for((game, gameId) <- games.zipWithIndex) {
<tr>
<td><a href="/game/@gameId">@gameId</a></td>
<td>@game.state</td>
<td>@game.numberPlayers</td>
@if(game.state == Game.State.OpenToJoin) {
<form action="/join">
<input type="hidden", name="gameId", value="@gameId">
<td>Name <input type="text" name="name" required></td>
<td><input type="submit" value="Join game"></td>
</form>
}
</tr>
}
</table>
}
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name := "Twenty-One"
name := "TwentyOne"

version := "1.0"

Expand Down
19 changes: 15 additions & 4 deletions conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,22 @@
# ~~~~

# An example controller showing a sample home page
GET / controllers.HomeController.index
GET /game/$game<[0-9]+> controllers.HomeController.game(game: Int)
GET / controllers.HomeController.index
GET /game/$game<[0-9]+> controllers.HomeController.game(game: Int)
GET /game/$game<[0-9]+>/player/$player<[0-9]+> controllers.HomeController.player(game: Int, player: Int)
GET /create controllers.HomeController.createGame(name: String)
GET /join controllers.HomeController.joinGame(gameId: Int, name: String)
GET /start controllers.HomeController.startGame(gameId: Int, playerId: Option[Int])
GET /bet controllers.HomeController.bet(gameId: Int, playerId: Int, bet: Int)

GET /hit controllers.HomeController.hit(gameId: Int, playerId: Int)
GET /hold controllers.HomeController.hold(gameId: Int, playerId: Int)
GET /split controllers.HomeController.split(gameId: Int, playerId: Int)
GET /splitHit controllers.HomeController.splitHit(gameId: Int, playerId: Int)
GET /splitHold controllers.HomeController.splitHold(gameId: Int, playerId: Int)


# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset)
GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset)

GET /swagger.json controllers.ApiHelpController.getResources
GET /swagger.json controllers.ApiHelpController.getResources

0 comments on commit 1051022

Please sign in to comment.