From dcbb8efcde93af6452af77fea6b1038784db477e Mon Sep 17 00:00:00 2001 From: David Gay Date: Mon, 31 May 2021 16:51:47 -0400 Subject: Character resting --- app/models/character.rb | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) (limited to 'app/models') diff --git a/app/models/character.rb b/app/models/character.rb index 56be0c0..5b3a4ab 100644 --- a/app/models/character.rb +++ b/app/models/character.rb @@ -15,11 +15,14 @@ class Character < ApplicationRecord has_many :chat_messages has_many :bazaar_orders validates :name, presence: true + # TODO: Make defaults better. This has to allow nil so the `attribute` default works, and I don't like that. + validates :rested_duration, numericality: { greater_than_or_equal_to: 0, only_integer: true }, allow_nil: true validates_length_of :name, maximum: 15, message: "can't be longer than 15 characters" validates_uniqueness_of :name, message: "is already being used" validates_format_of :name, with: /\A[a-z]+\z/i, message: "must consist of letters only" attribute :wounds, :integer, default: 0 + attribute :rested_duration, :integer, default: 0 enum offensive_style: [:balanced, :precise, :brutal], _default: "balanced" enum defensive_style: [:centered, :elusive, :protective], _default: "centered" @@ -180,6 +183,12 @@ class Character < ApplicationRecord end def activity_time_remaining + time = duration_of_activity - (Time.now - self.activity_started_at) + time -= rested_duration_to_spend_on_activity if self.rested_duration > 0 + time + end + + def duration_of_activity return nil unless self.activity duration_data = self.activity.whatnot[:duration] duration = duration_data[:base] @@ -193,8 +202,12 @@ class Character < ApplicationRecord raise "Invalid scaling type." # TODO: Improve this end end - duration = [duration, duration_data[:minimum] || 10].max - duration - (Time.now - self.activity_started_at) + [duration, duration_data[:minimum] || 10].max + end + + def rested_duration_to_spend_on_activity + return nil unless self.activity + [duration_of_activity - 10, self.rested_duration].min end def can_do_activity?(activity, ignore_cost: false, ignore_requirements: false) @@ -238,6 +251,26 @@ class Character < ApplicationRecord self.update(activity: nil, activity_started_at: nil, queued_actions: nil) end + def resting? + self.started_resting_at + end + + def start_resting + return false if self.started_resting_at + self.update(started_resting_at: Time.now) + end + + def stop_resting + return false unless self.started_resting_at + Character.transaction do + byebug + seconds_of_this_rest = (Time.now - self.started_resting_at).to_i + # Maximum rested duration is 10 days. + new_rested_duration = [(seconds_of_this_rest + self.rested_duration), (60 * 60 * 24 * 10)].min + self.update(started_resting_at: nil, rested_duration: new_rested_duration) + end + end + def award_title(title) title = Title.find_by_gid(title) if title.is_a? String # TODO: Simplify these lines? -- cgit v1.2.3