Skip to content

Commit

Permalink
Refactor a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
zspencer committed Feb 7, 2024
1 parent 867a8dc commit cae8f69
Show file tree
Hide file tree
Showing 14 changed files with 124 additions and 20 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -593,6 +595,7 @@ DEPENDENCIES
square.rb
standard (~> 1.33)
stimulus-rails
store_model
strip_attributes (~> 1.13)
stripe
strong_migrations (~> 1.7)
Expand Down
6 changes: 6 additions & 0 deletions app/furniture/slipvector.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
class Slipvector
ELMOS = {
energy: "⚡️ Energy",
life: "🧬 Life",
matter: "⚛️ Matter",
oddities: "⁉️ Oddity"
}
end
16 changes: 8 additions & 8 deletions app/furniture/slipvector/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 9 additions & 2 deletions app/furniture/slipvector/data_level.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion app/furniture/slipvector/star_system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 40 additions & 0 deletions app/furniture/slipvector/star_system/experience.rb
Original file line number Diff line number Diff line change
@@ -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
21 changes: 19 additions & 2 deletions app/furniture/slipvector/star_systems/_star_system.html.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
<%= render CardComponent.new do |card| %>
<%- card.with_header do %>
<h2><%= star_system.name %></h2>

<div class="mt-2 grid grid-cols-2 sm:grid-cols-4 gap-3">
<%- star_system.experience.pools.each do |pool| %>
<div class="flex flex-wrap justify-between">
<span><%= pool.label %></span>
<span>
<%= pool.xp %> of <%= pool.next_level_xp %>
</span>
<%- 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 %>
</div>
<%- end %>
</div>

<%- end %>

<%- @pagy, @surveys = pagy(policy_scope(star_system.surveys.complete.recent), items: 8) %>

<div class="grid sm:grid-cols-2">
<%= render @surveys %>
<div class="grid gap-3 grid-cols-2 sm:grid-cols-4">
<%- @surveys.each do |survey| %>
<%= link_to(survey.name, survey.location) %>
<%- end %>
</div>

<%== pagy_nav(@pagy, nav_extra: 'flex justify-between') %>
Expand Down
24 changes: 24 additions & 0 deletions app/furniture/slipvector/survey.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<div class="flex flex-col gap-y-3">
<h2>Authorized Star Systems</h2>
<div class="grid grid-cols-1 sm:grid-cols-2 gap-1 sm:gap-5">
<%- @pagy, @star_systems = pagy(policy_scope(surveyors_guild.star_systems), items: 4) %>
<%- surveyors_guild.star_systems.each do |star_system| %>
Expand Down
3 changes: 0 additions & 3 deletions app/furniture/slipvector/surveys/_survey.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
<div>
<h3><%= survey.id %></h3>
</div>
8 changes: 8 additions & 0 deletions app/furniture/slipvector/surveys/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
<p class="text-sm">Surveying <%= survey.star_system.name %></p>
<%- end %>

<p>Gathered...
<ul>
<%- survey.results.each do |result| %>
<li><%= result.xp %> <%= result.label %></li>
<%- end %>
</ul>


<%- 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 } })%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -281,15 +281,14 @@
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"
end

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
Expand Down

0 comments on commit cae8f69

Please sign in to comment.