progs/display/MinimalApplication.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Tue, 06 Oct 2020 00:39:34 +0100
changeset 775 16aafc7691d8
parent 738 084e2843f478
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
738
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
package app
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
import scalatags.Text.all._
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
import scala.util.Random
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
object MinimalApplication extends cask.MainRoutes{
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
  val bootstrap = "https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.css"
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
  var x = Random.between(0, 7)
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
  var y = Random.between(0, 7) 
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
  var z = Random.between(0, 7) 
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
  abstract class Answer
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
  case object No extends Answer
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
  case object Correct extends Answer
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
  case class Wrong(n: Int) extends Answer
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
  var ans : Answer = No
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
  var cnt = 1
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
  val heart = raw("&#10084;&#65039;")
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
  val unicorn = raw("&#129412;")
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
  var nms = List("Christiane", "Sebastian")
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
  @cask.get("/")
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
  def hello() = 
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
      doctype("html")(
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
      html(
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
        head(link(rel := "stylesheet", href := bootstrap)), 
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
        body(
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
          div(cls := "container")( 
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
            //h1(s"Hello ${nms((cnt / 3) % 2)}!"), 
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
            h1(s"Hello Christiane"),  
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
            h1(s"$x + $y = ??"),  
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
	    ans match {
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
              case No => ""
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
              case Correct => i(color.green)("Correct!")
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
              case Wrong(n) => i(color.red)(s"$n is wrong!")
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
	    },
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
            form(action := "/", method := "post")(
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
	      frag(
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
		for (i <- 0 to 12) yield button(`type` := "submit", name := "name", value := i.toString)(i.toString)  
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
              )
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
            ),
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
            frag(
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
              for (i <- 0 until cnt) yield 
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
                if ((i + 1) % 10 != 0) heart else unicorn
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
            )
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
          )
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
        )
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
      )
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
  )
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
  @cask.postForm("/")
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
  def answer(name: String) = {
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
    if (x + y == name.toInt) {
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
      ans = Correct
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
      x = Random.between(0, 7)
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
      y = Random.between(0, 7)
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
      z = Random.between(0, 7)
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
      cnt = cnt + 1
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
    }
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
    else 
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
      ans = Wrong(name.toInt)
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
    hello()
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
  }
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    71
  initialize()
084e2843f478 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    72
}