about summary refs log tree commit diff stats
path: root/app/views/puzzles/_handle_puzzle.html.erb
diff options
context:
space:
mode:
Diffstat (limited to 'app/views/puzzles/_handle_puzzle.html.erb')
-rw-r--r--app/views/puzzles/_handle_puzzle.html.erb76
1 files changed, 76 insertions, 0 deletions
diff --git a/app/views/puzzles/_handle_puzzle.html.erb b/app/views/puzzles/_handle_puzzle.html.erb new file mode 100644 index 0000000..f0e3227 --- /dev/null +++ b/app/views/puzzles/_handle_puzzle.html.erb
@@ -0,0 +1,76 @@
1<script type="text/javascript">
2var totalSeconds = 0
3var startTime = 0
4var timerInterval
5
6function pad(val) {
7 var valString = val + ""
8 if (valString.length < 2)
9 {
10 return "0" + valString
11 } else {
12 return valString
13 }
14}
15function setTime() {
16 totalSeconds = Math.floor((Date.now() - startTime) / 1000)
17 $("#seconds").text(pad(totalSeconds%60))
18 $("#minutes").text(pad(parseInt(totalSeconds/60)))
19}
20
21window.onload = function() {
22 <% if @playable or @puzzle.latest? %>
23 $("#sens").val(window.settings.sensitivity)
24 $("#sens").on("change", function() {
25 window.settings.sensitivity = this.value
26 })
27 $("#volume").val(parseFloat(window.settings.volume))
28 $("#volume").on("change", function() {
29 window.settings.volume = this.value
30 })
31 <% end %>
32
33 <% if @playable %>
34 <% if @already_started %>
35 var puzzle = window.deserializePuzzle("<%= @puzzle.data %>")
36 draw(puzzle)
37 <% else %>
38 $("#activation-button button").on("click", function() {
39 var puzzle = window.deserializePuzzle("<%= @puzzle.data %>")
40 draw(puzzle)
41
42 $("#activation-button").hide()
43
44 startTime = Date.now()
45 timerInterval = setInterval(setTime, 1000);
46
47 $.ajax({
48 type: "POST",
49 url: "<%= start_puzzle_path(@puzzle, format: :js) %>"
50 })
51 })
52 <% end %>
53 <% else %>
54 var puzzle = window.deserializePuzzle("<%= @puzzle.data %>")
55 draw(puzzle)
56 drawPath(puzzle, JSON.parse("<%= escape_javascript(sanitize @solution) %>"))
57 <% unless @puzzle.latest? %>
58 window.trace = function() {}
59 <% end %>
60 <% end %>
61}
62
63<% if @playable %>
64window.TRACE_COMPLETION_FUNC = function(puzzle, rawPath) {
65 clearInterval(timerInterval)
66 setTime()
67
68 $.ajax({
69 type: "POST",
70 url: "<%= solve_puzzle_path(@puzzle, format: :js) %>",
71 data: { solved: JSON.stringify(rawPath) <% unless @already_started %> , time: totalSeconds <% end %> }
72 })
73}
74
75<% end %>
76</script>