From 03f44c53e35fc1643a39a1e31141ead8c275a3a0 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Sun, 29 Oct 2023 21:04:43 -0400 Subject: disable sending a time if you refresh the page --- app/controllers/wittle/puzzles_controller.rb | 8 +++ app/views/wittle/puzzles/_handle_puzzle.html.erb | 75 ++++++++++++++---------- app/views/wittle/puzzles/show.html.haml | 13 ++-- config/routes.rb | 1 + 4 files changed, 59 insertions(+), 38 deletions(-) diff --git a/app/controllers/wittle/puzzles_controller.rb b/app/controllers/wittle/puzzles_controller.rb index ed7087a..2949f94 100644 --- a/app/controllers/wittle/puzzles_controller.rb +++ b/app/controllers/wittle/puzzles_controller.rb @@ -12,6 +12,14 @@ module Wittle def show @puzzle = Puzzle.find(params[:id]) @playable = @puzzle.latest? && !((session[:played_puzzles] || []).include? @puzzle.id) + @already_started = ((session[:started_puzzles] || []).include? @puzzle.id) + end + + def start + @puzzle = Puzzle.find(params[:id]) + + session[:started_puzzles] ||= [] + session[:started_puzzles] << @puzzle.id end def solve diff --git a/app/views/wittle/puzzles/_handle_puzzle.html.erb b/app/views/wittle/puzzles/_handle_puzzle.html.erb index 3d593a4..be9166a 100644 --- a/app/views/wittle/puzzles/_handle_puzzle.html.erb +++ b/app/views/wittle/puzzles/_handle_puzzle.html.erb @@ -4,41 +4,52 @@ var timerInterval window.onload = function() { <% if @playable %> - function pad(val) { - var valString = val + "" - if (valString.length < 2) - { - return "0" + valString - } else { - return valString + function pad(val) { + var valString = val + "" + if (valString.length < 2) + { + return "0" + valString + } else { + return valString + } } - } - function setTime() { - ++totalSeconds - $("#seconds").text(pad(totalSeconds%60)) - $("#minutes").text(pad(parseInt(totalSeconds/60))) - } - $("#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 - }) - $("#activation-button button").on("click", function() { - var puzzle = window.deserializePuzzle("<%= @puzzle.data %>") - draw(puzzle) + function setTime() { + ++totalSeconds + $("#seconds").text(pad(totalSeconds%60)) + $("#minutes").text(pad(parseInt(totalSeconds/60))) + } + $("#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 + }) - $("#activation-button").hide() + <% 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) - timerInterval = setInterval(setTime, 1000); - }) + $("#activation-button").hide() + + 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 @puzzle.solved_data) %>")) - window.trace = function() {} + var puzzle = window.deserializePuzzle("<%= @puzzle.data %>") + draw(puzzle) + drawPath(puzzle, JSON.parse("<%= escape_javascript(sanitize @puzzle.solved_data) %>")) + window.trace = function() {} <% end %> } @@ -49,7 +60,7 @@ window.TRACE_COMPLETION_FUNC = function(puzzle, rawPath) { $.ajax({ type: "POST", url: "<%= solve_puzzle_path(@puzzle, format: :js) %>", - data: { solved: JSON.stringify(rawPath), time: totalSeconds } + data: { solved: JSON.stringify(rawPath) <% unless @already_started %> , time: totalSeconds <% end %> } }) } diff --git a/app/views/wittle/puzzles/show.html.haml b/app/views/wittle/puzzles/show.html.haml index deda344..c46e5ce 100644 --- a/app/views/wittle/puzzles/show.html.haml +++ b/app/views/wittle/puzzles/show.html.haml @@ -3,12 +3,13 @@ %svg#puzzle{ style: "pointer-events: auto"} #submission-form - if @playable - #activation-button - %button{ type: "button" } Reveal Puzzle - #timer - %label#minutes 00 - %label#colon : - %label#seconds 00 + - unless @already_started + #activation-button + %button{ type: "button" } Reveal Puzzle + #timer + %label#minutes 00 + %label#colon : + %label#seconds 00 %details#trace-settings %summary Settings .things diff --git a/config/routes.rb b/config/routes.rb index 8cae7ec..346315e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,6 +2,7 @@ Wittle::Engine.routes.draw do root to: 'puzzles#about' get 'archive' => 'puzzles#index' get ':id' => 'puzzles#show', as: 'puzzle' + post ':id/start' => 'puzzles#start', as: 'start_puzzle' post ':id/solve' => 'puzzles#solve', as: 'solve_puzzle' post ':id/submit' => 'puzzles#submit', as: 'submit_puzzle' end -- cgit 1.4.1