summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Gay <david@davidgay.org>2021-05-27 19:24:12 -0400
committerDavid Gay <david@davidgay.org>2021-05-27 19:24:12 -0400
commit09dbf09b5fa6d106a57cdb601c6ed6c23e00f7dd (patch)
tree26ff703ba99ef0821f0f74539378e40e18dd85af
parentd9f829e7a723ae36367280950dcbb7edf394e9a2 (diff)
Make timer more robust, and `stop_activity` if you can't do it
-rw-r--r--app/controllers/game_controller.rb9
-rw-r--r--app/javascript/controllers/timer_controller.js24
-rw-r--r--app/views/look/_results.html.erb2
-rw-r--r--app/views/look/show.html.erb21
4 files changed, 41 insertions, 15 deletions
diff --git a/app/controllers/game_controller.rb b/app/controllers/game_controller.rb
index 40d8991..886b37a 100644
--- a/app/controllers/game_controller.rb
+++ b/app/controllers/game_controller.rb
@@ -9,7 +9,14 @@ class GameController < ApplicationController
return unless current_char.activity_time_remaining <= 0
activity = current_char.activity
- return unless current_char.can_do_activity?(activity) # TODO: Add error message
+ unless current_char.can_do_activity?(activity)
+ message = "You can't do this right now."
+ message += " (requires #{activity.requirements&.join(", ")})" if activity.requirements.any?
+ message += " (costs #{activity.costs&.join(", ")})" if activity.costs.any?
+ @results.replace([{ type: "error", message: message }])
+ current_char.stop_activity
+ return
+ end
Character.transaction do
current_char.pay_cost_for(activity)
diff --git a/app/javascript/controllers/timer_controller.js b/app/javascript/controllers/timer_controller.js
index 7714bad..5190a79 100644
--- a/app/javascript/controllers/timer_controller.js
+++ b/app/javascript/controllers/timer_controller.js
@@ -12,6 +12,7 @@ export default class extends Controller {
initialize() {
this.counter = this.startValue;
this.timerTarget.textContent = this.counter;
+ this.postFailures = 0;
}
connect() {
@@ -23,14 +24,27 @@ export default class extends Controller {
}
startUpdating() {
+ let controller = this;
this.timerInterval = setInterval(() => {
- if (this.counter > 0) {
- this.timerTarget.textContent = this.counter.toString();
- this.counter--;
- } else if (this.counter === 0) {
+ if (controller.postFailures >= 5) {
+ alert("An error occurred. Please submit a bug report, explaining what you were doing when it occurred.");
+ clearInterval(controller.timerInterval);
+ return;
+ }
+
+ if (controller.counter > 0) {
+ controller.timerTarget.textContent = controller.counter.toString();
+ controller.counter--;
+ } else if (this.counter <= 0) {
Rails.ajax({
type: "POST",
- url: this.postUrlValue,
+ url: controller.postUrlValue,
+ success: () => {
+ controller.postFailures = 0;
+ },
+ error: () => {
+ controller.postFailures++;
+ },
});
}
}, 1000);
diff --git a/app/views/look/_results.html.erb b/app/views/look/_results.html.erb
index 36e8103..8b5466e 100644
--- a/app/views/look/_results.html.erb
+++ b/app/views/look/_results.html.erb
@@ -21,7 +21,7 @@
<% when "message" %>
<p><%= result[:body] %></p>
<% when "error" %>
- <p><%= result[:message] %></p>
+ <p class="text-red-500"><%= result[:message] %></p>
<% end %>
<% end %>
</div>
diff --git a/app/views/look/show.html.erb b/app/views/look/show.html.erb
index a0c1bd7..f5ae417 100644
--- a/app/views/look/show.html.erb
+++ b/app/views/look/show.html.erb
@@ -1,10 +1,15 @@
-<h1 class="text-2xl"><%= current_char.activity.name %></h1>
-<p><%= current_char.activity.description %></p>
+<% if current_char.activity %>
+ <h1 class="text-2xl"><%= current_char.activity.name %></h1>
+ <p><%= current_char.activity.description %></p>
-<div class="min-w-full my-2 px-1 overflow-auto text-sm border-2 border-gray-800 rounded"
- style="height: 30rem;" id="result_output">
-</div>
+ <div class="min-w-full my-2 px-1 overflow-auto text-sm border-2 border-gray-800 rounded"
+ style="height: 30rem;" id="result_output">
+ </div>
-<div id="result_controls">
- <%= render "timer" %>
-</div>
+ <div id="result_controls">
+ <%= render "timer" %>
+ </div>
+<% else %>
+ <p>You're not currently doing anything. Maybe you'd like to
+ <%= link_to "go somewhere", locations_path %>?</p>
+<% end %>