Home Posts Tags Post Search Tag Search

Post 15

Game Site Journal - 06

Published on: 2025-04-14 Tags: elixir, Blog, Side Project, LiveView, Game Site
This is a blog about the issues that I had with the question that a player was seeing for the first time when loading Math Live was not the question that the back end was seeing.

For this to work I had to be sure that I only sent the question and answer as the form. This insures that the function that test for a correct answer will only test what the Player sees.

<.simple_form id="answer-form" for={@form} phx-submit="answer">
      <.input type="hidden" field={@form[:question]} value={@question} />
      <.input type="hidden" field={@form[:answer]} value={@answer} />
      <.input type="number" field={@form[:guess]} label="Guess" value={@form.params["guess"]} />
...
      <:actions>
        <.button>Answer</.button>
      </:actions>
    </.simple_form>

As you can see I added in the 2 hidden .inputs

once that was done I needed to be sure that I only used the params for answer and question going forward.

I wanted to be sure that I didnt have to have a bunch of socket.assigns.variable or params["value"] so I made a helper function to set all the needed variables into 1 map.

defp set_event_info(socket, %{"wager" => wager, "guess" => guess, "answer" => answer}) do
    parsed_wager =
      Helper.add_subtract_wager(wager, guess, answer)

    %{
      current_score: socket.assigns.score + parsed_wager,
      highest_score: socket.assigns.highest_score,
      guess: guess,
      wager: wager,
      correct: guess == answer
    }
  end

This way all the logic is hidden and when I make any assigns(socket, ...) calls it's a lot cleaner.