about summary refs log tree commit diff stats
path: root/app/views/puzzles/_handle_puzzle.html.erb
blob: f0e322787a5c33c70d577247da533a1db005ecc8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<script type="text/javascript">
var totalSeconds = 0
var startTime = 0
var timerInterval

function pad(val) {
  var valString = val + ""
  if (valString.length < 2)
  {
    return "0" + valString
  } else {
    return valString
  }
}
function setTime() {
  totalSeconds = Math.floor((Date.now() - startTime) / 1000)
  $("#seconds").text(pad(totalSeconds%60))
  $("#minutes").text(pad(parseInt(totalSeconds/60)))
}

window.onload = function() {
  <% if @playable or @puzzle.latest? %>
    $("#sens").val(window.settings.sensitivity)
    $("#sens").on("change", function() {
      window.settings.sensitivity = this.value
    })
    $("#volume").val(parseFloat(window.settings.volume))
    $("#volume").on("change", function() {
      window.settings.volume = this.value
    })
  <% end %>

  <% if @playable %>
    <% if @already_started %>
      var puzzle = window.deserializePuzzle("<%= @puzzle.data %>")
      draw(puzzle)
    <% else %>
      $("#activation-button button").on("click", function() {
        var puzzle = window.deserializePuzzle("<%= @puzzle.data %>")
        draw(puzzle)

        $("#activation-button").hide()

        startTime = Date.now()
        timerInterval = setInterval(setTime, 1000);

        $.ajax({
          type: "POST",
          url: "<%= start_puzzle_path(@puzzle, format: :js) %>"
        })
      })
    <% end %>
  <% else %>
    var puzzle = window.deserializePuzzle("<%= @puzzle.data %>")
    draw(puzzle)
    drawPath(puzzle, JSON.parse("<%= escape_javascript(sanitize @solution) %>"))
    <% unless @puzzle.latest? %>
      window.trace = function() {}
    <% end %>
  <% end %>
}

<% if @playable %>
window.TRACE_COMPLETION_FUNC = function(puzzle, rawPath) {
  clearInterval(timerInterval)
  setTime()

  $.ajax({
    type: "POST",
    url: "<%= solve_puzzle_path(@puzzle, format: :js) %>",
    data: { solved: JSON.stringify(rawPath) <% unless @already_started %> , time: totalSeconds <% end %> }
  })
}

<% end %>
</script>