From cae8f698b0a3a8468f2e15da1b63737187f50103 Mon Sep 17 00:00:00 2001 From: Zee Spencer <50284+zspencer@users.noreply.github.com> Date: Tue, 6 Feb 2024 19:47:37 -0800 Subject: [PATCH] Refactor a bit --- Gemfile | 3 ++ Gemfile.lock | 3 ++ app/furniture/slipvector.rb | 6 +++ app/furniture/slipvector/README.md | 16 ++++---- app/furniture/slipvector/data_level.rb | 11 ++++- app/furniture/slipvector/star_system.rb | 2 +- .../slipvector/star_system/experience.rb | 40 +++++++++++++++++++ .../star_systems/_star_system.html.erb | 21 +++++++++- app/furniture/slipvector/survey.rb | 24 +++++++++++ .../_surveyors_guild.html.erb | 1 + .../slipvector/surveys/_survey.html.erb | 3 -- .../slipvector/surveys/show.html.erb | 8 ++++ ...42142_create_slipvector_surveyors_guild.rb | 3 +- db/schema.rb | 3 +- 14 files changed, 124 insertions(+), 20 deletions(-) create mode 100644 app/furniture/slipvector/star_system/experience.rb diff --git a/Gemfile b/Gemfile index 2100e43d4..5db833553 100644 --- a/Gemfile +++ b/Gemfile @@ -68,6 +68,9 @@ gem "strong_migrations", "~> 1.7" # Soft Deletion gem "discard", "~> 1.2" +# JSON models +gem "store_model" + # ActiveModel extension to remove extra whitespace from attributes gem "strip_attributes", "~> 1.13" diff --git a/Gemfile.lock b/Gemfile.lock index 0cec88ce7..002c4a1cc 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -495,6 +495,8 @@ GEM rubocop-performance (~> 1.20.1) stimulus-rails (1.3.3) railties (>= 6.0.0) + store_model (1.2.0) + activerecord (>= 5.2) stringio (3.1.0) strip_attributes (1.13.0) activemodel (>= 3.0, < 8.0) @@ -593,6 +595,7 @@ DEPENDENCIES square.rb standard (~> 1.33) stimulus-rails + store_model strip_attributes (~> 1.13) stripe strong_migrations (~> 1.7) diff --git a/app/furniture/slipvector.rb b/app/furniture/slipvector.rb index 3ef65cf5a..a50a9416f 100644 --- a/app/furniture/slipvector.rb +++ b/app/furniture/slipvector.rb @@ -1,2 +1,8 @@ class Slipvector + ELMOS = { + energy: "⚡️ Energy", + life: "🧬 Life", + matter: "⚛️ Matter", + oddities: "⁉️ Oddity" + } end diff --git a/app/furniture/slipvector/README.md b/app/furniture/slipvector/README.md index 2e144f0a2..a4fae1d06 100644 --- a/app/furniture/slipvector/README.md +++ b/app/furniture/slipvector/README.md @@ -59,14 +59,14 @@ Binds [Star Systems](#star-systems) together. By spending considerable [Energy]( Every [Star System](#star-systems) in Slipvector has Potential and Kinetic ELMO -- [Energy](#energy) -- [Matter](#matter) -- [Life](#life) -- [Oddity](#oddity) +- [⚡️ Energy](#energy) +- [⚛️ Matter](#matter) +- [🧬 Life](#life) +- [⁉️ Oddity](#oddity) -#### Energy -#### Matter +#### ⚡️ Energy +#### ⚛️ Matter -#### Life +#### 🧬 Life -#### Oddity +#### ⁉️ Oddity diff --git a/app/furniture/slipvector/data_level.rb b/app/furniture/slipvector/data_level.rb index 95bb9fe7e..6bd0a6cdf 100644 --- a/app/furniture/slipvector/data_level.rb +++ b/app/furniture/slipvector/data_level.rb @@ -4,7 +4,14 @@ class DataLevel include ActiveModel::Attributes include ActiveModel::Validations - attribute :observes, :string - validates :observes, inclusion: {in: [:energy, :life, :materials, :oddities]} + attribute :elmo, :string + validates :elmo, inclusion: {in: [:energy, :life, :materials, :oddities]} + + attribute :xp, :integer + validates :xp, numericality: true + + def label + ELMOS.fetch(elmo.to_sym) + end end end diff --git a/app/furniture/slipvector/star_system.rb b/app/furniture/slipvector/star_system.rb index 219fc98a1..d0858bb7d 100644 --- a/app/furniture/slipvector/star_system.rb +++ b/app/furniture/slipvector/star_system.rb @@ -5,7 +5,7 @@ class StarSystem < Record belongs_to :surveyors_guild, inverse_of: :star_systems location(parent: :surveyors_guild) - attribute :data_levels, DataLevelType.new + attribute :experience, Experience.to_type, default: -> { Experience.new } has_many :surveys, inverse_of: :star_system, dependent: :destroy has_many :active_surveys, -> { active }, class_name: :Survey diff --git a/app/furniture/slipvector/star_system/experience.rb b/app/furniture/slipvector/star_system/experience.rb new file mode 100644 index 000000000..a1a2dc4e2 --- /dev/null +++ b/app/furniture/slipvector/star_system/experience.rb @@ -0,0 +1,40 @@ +class Slipvector + class StarSystem::Experience + include StoreModel::Model + + attribute :energy, :integer, default: 0 + attribute :energy_level, :integer, default: 1 + attribute :life, :integer, default: 0 + attribute :life_level, :integer, default: 1 + attribute :matter, :integer, default: 0 + attribute :matter_level, :integer, default: 1 + attribute :oddities, :integer, default: 0 + attribute :oddities_level, :integer, default: 1 + + def bump(result) + assign_attributes({result.elmo => attribute(result.elmo) + result.xp}) + end + + def pools + @experience ||= [ + Level.new(elmo: :energy, xp: energy, level: energy_level), + Level.new(elmo: :life, xp: life, level: life_level), + Level.new(elmo: :matter, xp: matter, level: matter_level), + Level.new(elmo: :oddities, xp: oddities, level: oddities_level) + ] + end + + class Level < DataLevel + attribute :level, :integer, default: 0 + + def next_level_xp + level * 125 + end + + def ready_for_level_up? + xp > next_level_xp + end + + end + end +end diff --git a/app/furniture/slipvector/star_systems/_star_system.html.erb b/app/furniture/slipvector/star_systems/_star_system.html.erb index d43c62365..8684e9973 100644 --- a/app/furniture/slipvector/star_systems/_star_system.html.erb +++ b/app/furniture/slipvector/star_systems/_star_system.html.erb @@ -1,12 +1,29 @@ <%= render CardComponent.new do |card| %> <%- card.with_header do %>

<%= star_system.name %>

+ +
+ <%- star_system.experience.pools.each do |pool| %> +
+ <%= pool.label %> + + <%= pool.xp %> of <%= pool.next_level_xp %> + + <%- if pool.ready_for_level_up? %> + <%= link_to("⬆️ Level Up!", "#", class: "w-full text-center p-2 rounded bg-yellow-100 no-underline border-yellow-500") %> + <%- end %> +
+ <%- end %> +
+ <%- end %> <%- @pagy, @surveys = pagy(policy_scope(star_system.surveys.complete.recent), items: 8) %> -
- <%= render @surveys %> +
+ <%- @surveys.each do |survey| %> + <%= link_to(survey.name, survey.location) %> + <%- end %>
<%== pagy_nav(@pagy, nav_extra: 'flex justify-between') %> diff --git a/app/furniture/slipvector/survey.rb b/app/furniture/slipvector/survey.rb index 312c96f71..e74be463a 100644 --- a/app/furniture/slipvector/survey.rb +++ b/app/furniture/slipvector/survey.rb @@ -8,6 +8,30 @@ class Survey < Record scope :recent, -> { order(updated_at: :desc) } + after_commit :update_star_system + + def update_star_system + if status_previously_changed?(to: "complete") + results.each do |data_level| + star_system.experience.bump(data_level) + end + star_system.save + end + end + + def name + "Survey #{id.slice(-6,6)}" + end + + def results + rng = Random.new(id.hash) + + rng.rand(6).times.map do + elmo = ELMOS.keys.sample(random: rng) + DataLevel.new(elmo:, xp: rng.rand(3).times.sum { rng.rand(20) }) + end + end + enum status: { preparing: "preparing", active: "active", diff --git a/app/furniture/slipvector/surveyors_guilds/_surveyors_guild.html.erb b/app/furniture/slipvector/surveyors_guilds/_surveyors_guild.html.erb index 813b2238e..423ffa535 100644 --- a/app/furniture/slipvector/surveyors_guilds/_surveyors_guild.html.erb +++ b/app/furniture/slipvector/surveyors_guilds/_surveyors_guild.html.erb @@ -1,4 +1,5 @@
+

Authorized Star Systems

<%- @pagy, @star_systems = pagy(policy_scope(surveyors_guild.star_systems), items: 4) %> <%- surveyors_guild.star_systems.each do |star_system| %> diff --git a/app/furniture/slipvector/surveys/_survey.html.erb b/app/furniture/slipvector/surveys/_survey.html.erb index 7fdb0db80..e69de29bb 100644 --- a/app/furniture/slipvector/surveys/_survey.html.erb +++ b/app/furniture/slipvector/surveys/_survey.html.erb @@ -1,3 +0,0 @@ -
-

<%= survey.id %>

-
diff --git a/app/furniture/slipvector/surveys/show.html.erb b/app/furniture/slipvector/surveys/show.html.erb index e96cbde98..311750c78 100644 --- a/app/furniture/slipvector/surveys/show.html.erb +++ b/app/furniture/slipvector/surveys/show.html.erb @@ -8,6 +8,14 @@

Surveying <%= survey.star_system.name %>

<%- end %> +

Gathered... +

    + <%- survey.results.each do |result| %> +
  • <%= result.xp %> <%= result.label %>
  • + <%- end %> +
+ + <%- card.with_footer(variant: :action_bar) do %> <%- if survey.active? %> <%= button_to("Complete Survey", survey.location, form_class: "w-full", class: "w-full", params: { survey: { status: :complete } })%> diff --git a/db/migrate/20230419042142_create_slipvector_surveyors_guild.rb b/db/migrate/20230419042142_create_slipvector_surveyors_guild.rb index 0cfa91fb6..5c6e323a3 100644 --- a/db/migrate/20230419042142_create_slipvector_surveyors_guild.rb +++ b/db/migrate/20230419042142_create_slipvector_surveyors_guild.rb @@ -3,7 +3,7 @@ def change create_table :slipvector_star_systems, id: :uuid do |t| t.references :surveyors_guild, type: :uuid, foreign_key: {to_table: :furnitures} t.string :name - t.jsonb :data_levels + t.jsonb :experience t.timestamps end @@ -15,7 +15,6 @@ def change create_table :slipvector_surveys, id: :uuid do |t| t.references :star_system, type: :uuid, foreign_key: {to_table: :slipvector_star_systems} - t.jsonb :data_levels t.enum :status, default: "preparing", null: false, enum_type: :slipvector_survey_status t.timestamps end diff --git a/db/schema.rb b/db/schema.rb index 4e72e7036..dc5320acd 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -281,7 +281,7 @@ create_table "slipvector_star_systems", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| t.uuid "surveyors_guild_id" t.string "name" - t.jsonb "data_levels" + t.jsonb "experience" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["surveyors_guild_id"], name: "index_slipvector_star_systems_on_surveyors_guild_id" @@ -289,7 +289,6 @@ create_table "slipvector_surveys", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| t.uuid "star_system_id" - t.jsonb "data_levels" t.enum "status", default: "preparing", null: false, enum_type: "slipvector_survey_status" t.datetime "created_at", null: false t.datetime "updated_at", null: false