From 42f9a9dde46641a24f2df4524370730049832ebc Mon Sep 17 00:00:00 2001 From: Mateusz Nowak Date: Wed, 14 Aug 2024 15:58:12 +0200 Subject: [PATCH 01/17] wip --- .../heroes/creature_recruitment/module.rb | 8 ++++++++ .../heroes/creature_recruitment/write/setup_dwelling.rb | 5 +++++ 2 files changed, 13 insertions(+) create mode 100644 heroesofddd_rails_application/heroes/creature_recruitment/module.rb create mode 100644 heroesofddd_rails_application/heroes/creature_recruitment/write/setup_dwelling.rb diff --git a/heroesofddd_rails_application/heroes/creature_recruitment/module.rb b/heroesofddd_rails_application/heroes/creature_recruitment/module.rb new file mode 100644 index 0000000..fcdbce0 --- /dev/null +++ b/heroesofddd_rails_application/heroes/creature_recruitment/module.rb @@ -0,0 +1,8 @@ +module Heroes + module CreatureRecruitment + class Configuration + def call(event_store, command_bus) end + end + + end +end diff --git a/heroesofddd_rails_application/heroes/creature_recruitment/write/setup_dwelling.rb b/heroesofddd_rails_application/heroes/creature_recruitment/write/setup_dwelling.rb new file mode 100644 index 0000000..5a4f6e8 --- /dev/null +++ b/heroesofddd_rails_application/heroes/creature_recruitment/write/setup_dwelling.rb @@ -0,0 +1,5 @@ +module Heroes + module CreatureRecruitment + + end +end From 74b2f14629fc4793eee0c6fd05ffecdcda550c96 Mon Sep 17 00:00:00 2001 From: Mateusz Nowak Date: Thu, 15 Aug 2024 00:40:40 +0200 Subject: [PATCH 02/17] move heroes directory to lib --- .../{ => lib}/heroes/creature_recruitment/module.rb | 0 .../{ => lib}/heroes/creature_recruitment/write/setup_dwelling.rb | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename heroesofddd_rails_application/{ => lib}/heroes/creature_recruitment/module.rb (100%) rename heroesofddd_rails_application/{ => lib}/heroes/creature_recruitment/write/setup_dwelling.rb (100%) diff --git a/heroesofddd_rails_application/heroes/creature_recruitment/module.rb b/heroesofddd_rails_application/lib/heroes/creature_recruitment/module.rb similarity index 100% rename from heroesofddd_rails_application/heroes/creature_recruitment/module.rb rename to heroesofddd_rails_application/lib/heroes/creature_recruitment/module.rb diff --git a/heroesofddd_rails_application/heroes/creature_recruitment/write/setup_dwelling.rb b/heroesofddd_rails_application/lib/heroes/creature_recruitment/write/setup_dwelling.rb similarity index 100% rename from heroesofddd_rails_application/heroes/creature_recruitment/write/setup_dwelling.rb rename to heroesofddd_rails_application/lib/heroes/creature_recruitment/write/setup_dwelling.rb From 0a2ea82ed3cafbcdfe36c43f341dd04d9172b9b1 Mon Sep 17 00:00:00 2001 From: Mateusz Nowak Date: Thu, 29 Aug 2024 01:34:30 +0200 Subject: [PATCH 03/17] fix: robocop --- .../lib/heroes/creature_recruitment/module.rb | 1 - .../lib/heroes/creature_recruitment/write/setup_dwelling.rb | 1 - 2 files changed, 2 deletions(-) diff --git a/heroesofddd_rails_application/lib/heroes/creature_recruitment/module.rb b/heroesofddd_rails_application/lib/heroes/creature_recruitment/module.rb index fcdbce0..ace5197 100644 --- a/heroesofddd_rails_application/lib/heroes/creature_recruitment/module.rb +++ b/heroesofddd_rails_application/lib/heroes/creature_recruitment/module.rb @@ -3,6 +3,5 @@ module CreatureRecruitment class Configuration def call(event_store, command_bus) end end - end end diff --git a/heroesofddd_rails_application/lib/heroes/creature_recruitment/write/setup_dwelling.rb b/heroesofddd_rails_application/lib/heroes/creature_recruitment/write/setup_dwelling.rb index 5a4f6e8..58edb72 100644 --- a/heroesofddd_rails_application/lib/heroes/creature_recruitment/write/setup_dwelling.rb +++ b/heroesofddd_rails_application/lib/heroes/creature_recruitment/write/setup_dwelling.rb @@ -1,5 +1,4 @@ module Heroes module CreatureRecruitment - end end From 0b22ff2cd20ad81be2c66e826cdc714c1c85351b Mon Sep 17 00:00:00 2001 From: Mateusz Nowak Date: Thu, 29 Aug 2024 02:16:21 +0200 Subject: [PATCH 04/17] minitest --- .../config/environments/production.rb | 2 +- .../write/setup_dwelling.rb | 4 - .../modules/creature_recruitment/dwelling.rb | 124 ++++++++++++++++++ .../creature_recruitment/module.rb | 0 .../write_build_dwelling.rb | 6 + .../creature_recruitment/dwelling_test.rb | 13 ++ 6 files changed, 144 insertions(+), 5 deletions(-) delete mode 100644 heroesofddd_rails_application/lib/heroes/creature_recruitment/write/setup_dwelling.rb create mode 100644 heroesofddd_rails_application/lib/heroes/modules/creature_recruitment/dwelling.rb rename heroesofddd_rails_application/lib/heroes/{ => modules}/creature_recruitment/module.rb (100%) create mode 100644 heroesofddd_rails_application/lib/heroes/modules/creature_recruitment/write_build_dwelling.rb create mode 100644 heroesofddd_rails_application/test/lib/heroes/modules/creature_recruitment/dwelling_test.rb diff --git a/heroesofddd_rails_application/config/environments/production.rb b/heroesofddd_rails_application/config/environments/production.rb index 0f6becf..dd95acb 100644 --- a/heroesofddd_rails_application/config/environments/production.rb +++ b/heroesofddd_rails_application/config/environments/production.rb @@ -8,7 +8,7 @@ # Eager load code on boot. This eager loads most of Rails and # your application in memory, allowing both threaded web servers - # and those relying on copy on write to perform better. + # and those relying on copy on write_build_dwelling to perform better. # Rake tasks automatically ignore this option for performance. config.eager_load = true diff --git a/heroesofddd_rails_application/lib/heroes/creature_recruitment/write/setup_dwelling.rb b/heroesofddd_rails_application/lib/heroes/creature_recruitment/write/setup_dwelling.rb deleted file mode 100644 index 58edb72..0000000 --- a/heroesofddd_rails_application/lib/heroes/creature_recruitment/write/setup_dwelling.rb +++ /dev/null @@ -1,4 +0,0 @@ -module Heroes - module CreatureRecruitment - end -end diff --git a/heroesofddd_rails_application/lib/heroes/modules/creature_recruitment/dwelling.rb b/heroesofddd_rails_application/lib/heroes/modules/creature_recruitment/dwelling.rb new file mode 100644 index 0000000..c6e5cb5 --- /dev/null +++ b/heroesofddd_rails_application/lib/heroes/modules/creature_recruitment/dwelling.rb @@ -0,0 +1,124 @@ +module Heroes + module CreatureRecruitment + module Dwelling + NotBuilt = Data.define + Built = Data.define(:dwelling_id, :creature_id, :cost_per_troop, :available_creatures) + + def self.decide(command, state) + case command + when BuildDwelling + build(command, state) + else + raise "Unknown command" + end + end + + private + + def self.build(command, state) + if state.is_a?(Built) + [] + end + [DwellingBuilt.new(data: { dwelling_id: command.dwelling_id, creature_id: command.creature_id, cost_per_troop: command.cost_per_troop, available_creatures: 0 })] + end + + end + end +end +# +# module ProjectManagement +# module Issue +# InvalidTransition = Class.new(StandardError) +# State = Data.define(:id, :status) +# +# class << self +# def decide(command, state) +# case command +# when CreateIssue +# open(state) +# when ResolveIssue +# resolve(state) +# when CloseIssue +# close(state) +# when ReopenIssue +# reopen(state) +# when StartIssueProgress +# start(state) +# when StopIssueProgress +# stop(state) +# end +# end +# +# def evolve(state, event) +# case event +# when IssueOpened +# state.with(status: :open) +# when IssueResolved +# state.with(status: :resolved) +# when IssueClosed +# state.with(status: :closed) +# when IssueReopened +# state.with(status: :reopened) +# when IssueProgressStarted +# state.with(status: :in_progress) +# when IssueProgressStopped +# state.with(status: :open) +# end +# end +# +# def initial_state(id) +# State.new(id: id, status: nil) +# end +# +# private +# +# def open(state) +# if state.status +# InvalidTransition.new +# else +# IssueOpened.new(data: { issue_id: state.id }) +# end +# end +# +# def resolve(state) +# if %i[open in_progress reopened].include? state.status +# IssueResolved.new(data: { issue_id: state.id }) +# else +# InvalidTransition.new +# end +# end +# +# def close(state) +# if %i[open in_progress resolved reopened].include? state.status +# IssueClosed.new(data: { issue_id: state.id }) +# else +# InvalidTransition.new +# end +# end +# +# def reopen(state) +# if %i[resolved closed].include? state.status +# IssueReopened.new(data: { issue_id: state.id }) +# else +# InvalidTransition.new +# end +# end +# +# def stop(state) +# if %i[in_progress].include? state.status +# IssueProgressStopped.new(data: { issue_id: state.id }) +# else +# InvalidTransition.new +# end +# end +# +# def start(state) +# if %i[open reopened].include? state.status +# IssueProgressStarted.new(data: { issue_id: state.id }) +# else +# InvalidTransition.new +# end +# end +# end +# end +# end \ No newline at end of file diff --git a/heroesofddd_rails_application/lib/heroes/creature_recruitment/module.rb b/heroesofddd_rails_application/lib/heroes/modules/creature_recruitment/module.rb similarity index 100% rename from heroesofddd_rails_application/lib/heroes/creature_recruitment/module.rb rename to heroesofddd_rails_application/lib/heroes/modules/creature_recruitment/module.rb diff --git a/heroesofddd_rails_application/lib/heroes/modules/creature_recruitment/write_build_dwelling.rb b/heroesofddd_rails_application/lib/heroes/modules/creature_recruitment/write_build_dwelling.rb new file mode 100644 index 0000000..f8763ca --- /dev/null +++ b/heroesofddd_rails_application/lib/heroes/modules/creature_recruitment/write_build_dwelling.rb @@ -0,0 +1,6 @@ +module Heroes + module CreatureRecruitment + BuildDwelling = Data.define(:dwelling_id, :creature_id, :cost_per_troop) + DwellingBuilt = Data.define(:dwelling_id, :creature_id, :cost_per_troop) + end +end diff --git a/heroesofddd_rails_application/test/lib/heroes/modules/creature_recruitment/dwelling_test.rb b/heroesofddd_rails_application/test/lib/heroes/modules/creature_recruitment/dwelling_test.rb new file mode 100644 index 0000000..97a1723 --- /dev/null +++ b/heroesofddd_rails_application/test/lib/heroes/modules/creature_recruitment/dwelling_test.rb @@ -0,0 +1,13 @@ +require "minitest/autorun" + +module Heroes + module CreatureRecruitment + class DwellingTest < Minitest::Test + + def test_sum + assert_equal(0, 1) + end + + end + end +end \ No newline at end of file From 17dd07da24969ba75ee335a6174472c52e3611ac Mon Sep 17 00:00:00 2001 From: Mateusz Nowak Date: Thu, 29 Aug 2024 02:25:20 +0200 Subject: [PATCH 05/17] wip --- .../creature_recruitment/dwelling_test.rb | 13 ------------ .../write_build_dwelling_test.rb | 20 +++++++++++++++++++ 2 files changed, 20 insertions(+), 13 deletions(-) delete mode 100644 heroesofddd_rails_application/test/lib/heroes/modules/creature_recruitment/dwelling_test.rb create mode 100644 heroesofddd_rails_application/test/lib/heroes/modules/creature_recruitment/write_build_dwelling_test.rb diff --git a/heroesofddd_rails_application/test/lib/heroes/modules/creature_recruitment/dwelling_test.rb b/heroesofddd_rails_application/test/lib/heroes/modules/creature_recruitment/dwelling_test.rb deleted file mode 100644 index 97a1723..0000000 --- a/heroesofddd_rails_application/test/lib/heroes/modules/creature_recruitment/dwelling_test.rb +++ /dev/null @@ -1,13 +0,0 @@ -require "minitest/autorun" - -module Heroes - module CreatureRecruitment - class DwellingTest < Minitest::Test - - def test_sum - assert_equal(0, 1) - end - - end - end -end \ No newline at end of file diff --git a/heroesofddd_rails_application/test/lib/heroes/modules/creature_recruitment/write_build_dwelling_test.rb b/heroesofddd_rails_application/test/lib/heroes/modules/creature_recruitment/write_build_dwelling_test.rb new file mode 100644 index 0000000..fb8cb7a --- /dev/null +++ b/heroesofddd_rails_application/test/lib/heroes/modules/creature_recruitment/write_build_dwelling_test.rb @@ -0,0 +1,20 @@ +require "minitest/autorun" + +module Heroes + module CreatureRecruitment + class BuildDwellingTest < Minitest::Test + + def test_given_not_built_dwelling_when_build_dwelling_then_dwelling_built + # given + state = Heroes::CreatureRecruitment::Dwelling::NotBuilt.new + # when + command = Heroes::CreatureRecruitment::BuildDwelling.new(dwelling_id: "1", creature_id: "1", cost_per_troop: 1) + result = Heroes::CreatureRecruitment::Dwelling.decide(command, state) + # then + + assert_equal([Heroes::CreatureRecruitment::DwellingBuilt.new(dwelling_id: "1", creature_id: "1", cost_per_troop: 1)], result) + end + + end + end +end \ No newline at end of file From 876e7f149f5e7cfa355c9d1f9faa429053acc5d8 Mon Sep 17 00:00:00 2001 From: Mateusz Nowak Date: Thu, 29 Aug 2024 02:28:23 +0200 Subject: [PATCH 06/17] first test --- .../lib/heroes/modules/creature_recruitment/dwelling.rb | 5 ++--- .../creature_recruitment/write_build_dwelling_test.rb | 9 +++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/heroesofddd_rails_application/lib/heroes/modules/creature_recruitment/dwelling.rb b/heroesofddd_rails_application/lib/heroes/modules/creature_recruitment/dwelling.rb index c6e5cb5..c0be09e 100644 --- a/heroesofddd_rails_application/lib/heroes/modules/creature_recruitment/dwelling.rb +++ b/heroesofddd_rails_application/lib/heroes/modules/creature_recruitment/dwelling.rb @@ -19,9 +19,8 @@ def self.build(command, state) if state.is_a?(Built) [] end - [DwellingBuilt.new(data: { dwelling_id: command.dwelling_id, creature_id: command.creature_id, cost_per_troop: command.cost_per_troop, available_creatures: 0 })] + [ DwellingBuilt.new(dwelling_id: command.dwelling_id, creature_id: command.creature_id, cost_per_troop: command.cost_per_troop) ] end - end end end @@ -121,4 +120,4 @@ def self.build(command, state) # end # end # end -# end \ No newline at end of file +# end diff --git a/heroesofddd_rails_application/test/lib/heroes/modules/creature_recruitment/write_build_dwelling_test.rb b/heroesofddd_rails_application/test/lib/heroes/modules/creature_recruitment/write_build_dwelling_test.rb index fb8cb7a..57b91b9 100644 --- a/heroesofddd_rails_application/test/lib/heroes/modules/creature_recruitment/write_build_dwelling_test.rb +++ b/heroesofddd_rails_application/test/lib/heroes/modules/creature_recruitment/write_build_dwelling_test.rb @@ -1,9 +1,11 @@ require "minitest/autorun" +require_relative "../../../../../lib/heroes/modules/creature_recruitment/dwelling" +require_relative "../../../../../lib/heroes/modules/creature_recruitment/write_build_dwelling" + module Heroes module CreatureRecruitment class BuildDwellingTest < Minitest::Test - def test_given_not_built_dwelling_when_build_dwelling_then_dwelling_built # given state = Heroes::CreatureRecruitment::Dwelling::NotBuilt.new @@ -12,9 +14,8 @@ def test_given_not_built_dwelling_when_build_dwelling_then_dwelling_built result = Heroes::CreatureRecruitment::Dwelling.decide(command, state) # then - assert_equal([Heroes::CreatureRecruitment::DwellingBuilt.new(dwelling_id: "1", creature_id: "1", cost_per_troop: 1)], result) + assert_equal([ Heroes::CreatureRecruitment::DwellingBuilt.new(dwelling_id: "1", creature_id: "1", cost_per_troop: 1) ], result) end - end end -end \ No newline at end of file +end From 67f20f983fd6545ae66d32803f06d32741744509 Mon Sep 17 00:00:00 2001 From: Mateusz Nowak Date: Thu, 29 Aug 2024 02:33:02 +0200 Subject: [PATCH 07/17] first testtest format --- .../modules/creature_recruitment/write_build_dwelling_test.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/heroesofddd_rails_application/test/lib/heroes/modules/creature_recruitment/write_build_dwelling_test.rb b/heroesofddd_rails_application/test/lib/heroes/modules/creature_recruitment/write_build_dwelling_test.rb index 57b91b9..33a82e4 100644 --- a/heroesofddd_rails_application/test/lib/heroes/modules/creature_recruitment/write_build_dwelling_test.rb +++ b/heroesofddd_rails_application/test/lib/heroes/modules/creature_recruitment/write_build_dwelling_test.rb @@ -9,11 +9,12 @@ class BuildDwellingTest < Minitest::Test def test_given_not_built_dwelling_when_build_dwelling_then_dwelling_built # given state = Heroes::CreatureRecruitment::Dwelling::NotBuilt.new + # when command = Heroes::CreatureRecruitment::BuildDwelling.new(dwelling_id: "1", creature_id: "1", cost_per_troop: 1) result = Heroes::CreatureRecruitment::Dwelling.decide(command, state) - # then + # then assert_equal([ Heroes::CreatureRecruitment::DwellingBuilt.new(dwelling_id: "1", creature_id: "1", cost_per_troop: 1) ], result) end end From 728b94482d47b7860592f3896b3607f8bbf96669 Mon Sep 17 00:00:00 2001 From: Mateusz Nowak Date: Thu, 29 Aug 2024 02:50:20 +0200 Subject: [PATCH 08/17] build dwelling --- .../heroes/modules/shared_kernel/resources.rb | 41 +++++++++++++++++++ .../write_build_dwelling_test.rb | 9 +++- 2 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 heroesofddd_rails_application/lib/heroes/modules/shared_kernel/resources.rb diff --git a/heroesofddd_rails_application/lib/heroes/modules/shared_kernel/resources.rb b/heroesofddd_rails_application/lib/heroes/modules/shared_kernel/resources.rb new file mode 100644 index 0000000..c262ae2 --- /dev/null +++ b/heroesofddd_rails_application/lib/heroes/modules/shared_kernel/resources.rb @@ -0,0 +1,41 @@ +module Heroes + module SharedKernel + ResourceType = [ :GOLD, :WOOD, :ORE, :MERCURY, :SULFUR, :CRYSTAL, :GEMS ] + + Amount = Data.define(:raw) do + def self.of(raw) + raise ArgumentError, "Amount cannot be negative" if raw < 0 + + new(raw) + end + + def self.zero + new(0) + end + + def <=>(other) + raw <=> other.raw + end + + def +(other) + Amount.new(raw + other.raw) + end + + def -(other) + Amount.new(raw - other.raw) + end + end + + Cost = Data.define(:resources) do + def self.resources(*resources) + resource_map = resources.to_h { |type, amount| [ type, Amount.new(amount) ] } + new(resource_map) + end + + def *(multiplier) + resource_map = resources.transform_values { |amount| Amount.new(amount.raw * multiplier) } + self.class.new(resource_map) + end + end + end +end diff --git a/heroesofddd_rails_application/test/lib/heroes/modules/creature_recruitment/write_build_dwelling_test.rb b/heroesofddd_rails_application/test/lib/heroes/modules/creature_recruitment/write_build_dwelling_test.rb index 33a82e4..4994de8 100644 --- a/heroesofddd_rails_application/test/lib/heroes/modules/creature_recruitment/write_build_dwelling_test.rb +++ b/heroesofddd_rails_application/test/lib/heroes/modules/creature_recruitment/write_build_dwelling_test.rb @@ -1,6 +1,7 @@ require "minitest/autorun" require_relative "../../../../../lib/heroes/modules/creature_recruitment/dwelling" require_relative "../../../../../lib/heroes/modules/creature_recruitment/write_build_dwelling" +require_relative "../../../../../lib/heroes/modules/shared_kernel/resources" module Heroes @@ -11,11 +12,15 @@ def test_given_not_built_dwelling_when_build_dwelling_then_dwelling_built state = Heroes::CreatureRecruitment::Dwelling::NotBuilt.new # when - command = Heroes::CreatureRecruitment::BuildDwelling.new(dwelling_id: "1", creature_id: "1", cost_per_troop: 1) + dwelling_id = "portal_of_glory" + creature_id = "angel" + cost_per_troop = Heroes::SharedKernel::Cost.resources([ :GOLD, 3000 ], [ :CRYSTAL, 1 ]) + command = Heroes::CreatureRecruitment::BuildDwelling.new(dwelling_id, creature_id, cost_per_troop) result = Heroes::CreatureRecruitment::Dwelling.decide(command, state) # then - assert_equal([ Heroes::CreatureRecruitment::DwellingBuilt.new(dwelling_id: "1", creature_id: "1", cost_per_troop: 1) ], result) + expected_events = [ Heroes::CreatureRecruitment::DwellingBuilt.new(dwelling_id, creature_id, cost_per_troop) ] + assert_equal(expected_events, result) end end end From cad4c42b2cd20bfae0516e3bff8a2256672bb256 Mon Sep 17 00:00:00 2001 From: Mateusz Nowak Date: Thu, 29 Aug 2024 02:52:34 +0200 Subject: [PATCH 09/17] use self --- .../modules/creature_recruitment/dwelling.rb | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/heroesofddd_rails_application/lib/heroes/modules/creature_recruitment/dwelling.rb b/heroesofddd_rails_application/lib/heroes/modules/creature_recruitment/dwelling.rb index c0be09e..57fa5e3 100644 --- a/heroesofddd_rails_application/lib/heroes/modules/creature_recruitment/dwelling.rb +++ b/heroesofddd_rails_application/lib/heroes/modules/creature_recruitment/dwelling.rb @@ -4,22 +4,24 @@ module Dwelling NotBuilt = Data.define Built = Data.define(:dwelling_id, :creature_id, :cost_per_troop, :available_creatures) - def self.decide(command, state) - case command - when BuildDwelling - build(command, state) - else - raise "Unknown command" + class << self + def decide(command, state) + case command + when BuildDwelling + build(command, state) + else + raise "Unknown command" + end end - end - private + private - def self.build(command, state) - if state.is_a?(Built) - [] + def build(command, state) + if state.is_a?(Built) + [] + end + [ DwellingBuilt.new(dwelling_id: command.dwelling_id, creature_id: command.creature_id, cost_per_troop: command.cost_per_troop) ] end - [ DwellingBuilt.new(dwelling_id: command.dwelling_id, creature_id: command.creature_id, cost_per_troop: command.cost_per_troop) ] end end end From aca9a734ff74487879f065c5c4aab9613a232154 Mon Sep 17 00:00:00 2001 From: Mateusz Nowak Date: Wed, 18 Sep 2024 12:54:28 +0200 Subject: [PATCH 10/17] README.md description --- heroesofddd_rails_application/README.md | 8 ++++++++ .../config/environments/production.rb | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/heroesofddd_rails_application/README.md b/heroesofddd_rails_application/README.md index 7db80e4..2dcd88d 100644 --- a/heroesofddd_rails_application/README.md +++ b/heroesofddd_rails_application/README.md @@ -1,5 +1,13 @@ # README +## How to run? +0. `bundle install` +1. `docker compose up` +2. `rails db:create` +3. `rails db:migrate` +4. `rails server` + + This README would normally document whatever steps are necessary to get the application up and running. diff --git a/heroesofddd_rails_application/config/environments/production.rb b/heroesofddd_rails_application/config/environments/production.rb index dd95acb..0f6becf 100644 --- a/heroesofddd_rails_application/config/environments/production.rb +++ b/heroesofddd_rails_application/config/environments/production.rb @@ -8,7 +8,7 @@ # Eager load code on boot. This eager loads most of Rails and # your application in memory, allowing both threaded web servers - # and those relying on copy on write_build_dwelling to perform better. + # and those relying on copy on write to perform better. # Rake tasks automatically ignore this option for performance. config.eager_load = true From d3266639010867976ba9566fbbee1da78f8be9f7 Mon Sep 17 00:00:00 2001 From: Mateusz Nowak Date: Wed, 18 Sep 2024 13:54:54 +0200 Subject: [PATCH 11/17] decider test --- heroesofddd_rails_application/README.md | 4 ++ .../modules/creature_recruitment/dwelling.rb | 15 ++++++- .../write_build_dwelling_test.rb | 42 +++++++++++++++---- 3 files changed, 53 insertions(+), 8 deletions(-) diff --git a/heroesofddd_rails_application/README.md b/heroesofddd_rails_application/README.md index 2dcd88d..5b82219 100644 --- a/heroesofddd_rails_application/README.md +++ b/heroesofddd_rails_application/README.md @@ -1,6 +1,10 @@ # README ## How to run? + +Install ruby on your machine and Rails: +`gem install rails` + 0. `bundle install` 1. `docker compose up` 2. `rails db:create` diff --git a/heroesofddd_rails_application/lib/heroes/modules/creature_recruitment/dwelling.rb b/heroesofddd_rails_application/lib/heroes/modules/creature_recruitment/dwelling.rb index 57fa5e3..2fa7793 100644 --- a/heroesofddd_rails_application/lib/heroes/modules/creature_recruitment/dwelling.rb +++ b/heroesofddd_rails_application/lib/heroes/modules/creature_recruitment/dwelling.rb @@ -14,11 +14,24 @@ def decide(command, state) end end + def evolve(state, event) + case event + when DwellingBuilt + Built.new(dwelling_id: event.dwelling_id, creature_id: event.creature_id, cost_per_troop: event.cost_per_troop, available_creatures: 0) + else + raise "Unknown event" + end + end + + def initial_state + NotBuilt.new + end + private def build(command, state) if state.is_a?(Built) - [] + return [] end [ DwellingBuilt.new(dwelling_id: command.dwelling_id, creature_id: command.creature_id, cost_per_troop: command.cost_per_troop) ] end diff --git a/heroesofddd_rails_application/test/lib/heroes/modules/creature_recruitment/write_build_dwelling_test.rb b/heroesofddd_rails_application/test/lib/heroes/modules/creature_recruitment/write_build_dwelling_test.rb index 4994de8..719be79 100644 --- a/heroesofddd_rails_application/test/lib/heroes/modules/creature_recruitment/write_build_dwelling_test.rb +++ b/heroesofddd_rails_application/test/lib/heroes/modules/creature_recruitment/write_build_dwelling_test.rb @@ -6,22 +6,50 @@ module Heroes module CreatureRecruitment + DECIDER = Heroes::CreatureRecruitment::Dwelling class BuildDwellingTest < Minitest::Test + # givens + @dwelling_id = "portal_of_glory" + @creature_id = "angel" + @cost_per_troop = Heroes::SharedKernel::Cost.resources([ :GOLD, 3000 ], [ :CRYSTAL, 1 ]) + def test_given_not_built_dwelling_when_build_dwelling_then_dwelling_built # given - state = Heroes::CreatureRecruitment::Dwelling::NotBuilt.new + given_events = [] + + # when + command = Heroes::CreatureRecruitment::BuildDwelling.new(@dwelling_id, @creature_id, @cost_per_troop) + result = decide(given_events, command) + + # then + expected_events = [ Heroes::CreatureRecruitment::DwellingBuilt.new(@dwelling_id, @creature_id, @cost_per_troop) ] + assert_equal(expected_events, result) + end + + def test_given_built_dwelling_when_build_dwelling_then_nothing + # given + given_events = [ + Heroes::CreatureRecruitment::DwellingBuilt.new(@dwelling_id, @creature_id, @cost_per_troop) + ] # when - dwelling_id = "portal_of_glory" - creature_id = "angel" - cost_per_troop = Heroes::SharedKernel::Cost.resources([ :GOLD, 3000 ], [ :CRYSTAL, 1 ]) - command = Heroes::CreatureRecruitment::BuildDwelling.new(dwelling_id, creature_id, cost_per_troop) - result = Heroes::CreatureRecruitment::Dwelling.decide(command, state) + command = Heroes::CreatureRecruitment::BuildDwelling.new(@dwelling_id, @creature_id, @cost_per_troop) + result = decide(given_events, command) # then - expected_events = [ Heroes::CreatureRecruitment::DwellingBuilt.new(dwelling_id, creature_id, cost_per_troop) ] + expected_events = [] assert_equal(expected_events, result) end + + private + + def decide(given_events, command) + DECIDER.decide(command, state_from(given_events)) + end + + def state_from(events) + events.reduce(DECIDER.initial_state) { |state, event| DECIDER.evolve(state, event) } + end end end end From 5a07789890d37e9df580582727fa1241efb995f9 Mon Sep 17 00:00:00 2001 From: Mateusz Nowak Date: Wed, 18 Sep 2024 14:08:43 +0200 Subject: [PATCH 12/17] wip --- .../modules/creature_recruitment/dwelling.rb | 101 +----------------- 1 file changed, 2 insertions(+), 99 deletions(-) diff --git a/heroesofddd_rails_application/lib/heroes/modules/creature_recruitment/dwelling.rb b/heroesofddd_rails_application/lib/heroes/modules/creature_recruitment/dwelling.rb index 2fa7793..8ff4cef 100644 --- a/heroesofddd_rails_application/lib/heroes/modules/creature_recruitment/dwelling.rb +++ b/heroesofddd_rails_application/lib/heroes/modules/creature_recruitment/dwelling.rb @@ -17,7 +17,7 @@ def decide(command, state) def evolve(state, event) case event when DwellingBuilt - Built.new(dwelling_id: event.dwelling_id, creature_id: event.creature_id, cost_per_troop: event.cost_per_troop, available_creatures: 0) + Built.new(dwelling_id: event.dwelling_id, creature_id: event.creature_id, cost_per_troop: event.cost_per_troop, available_creatures: SharedKernel::Amount.of(0)) else raise "Unknown event" end @@ -38,101 +38,4 @@ def build(command, state) end end end -end -# -# module ProjectManagement -# module Issue -# InvalidTransition = Class.new(StandardError) -# State = Data.define(:id, :status) -# -# class << self -# def decide(command, state) -# case command -# when CreateIssue -# open(state) -# when ResolveIssue -# resolve(state) -# when CloseIssue -# close(state) -# when ReopenIssue -# reopen(state) -# when StartIssueProgress -# start(state) -# when StopIssueProgress -# stop(state) -# end -# end -# -# def evolve(state, event) -# case event -# when IssueOpened -# state.with(status: :open) -# when IssueResolved -# state.with(status: :resolved) -# when IssueClosed -# state.with(status: :closed) -# when IssueReopened -# state.with(status: :reopened) -# when IssueProgressStarted -# state.with(status: :in_progress) -# when IssueProgressStopped -# state.with(status: :open) -# end -# end -# -# def initial_state(id) -# State.new(id: id, status: nil) -# end -# -# private -# -# def open(state) -# if state.status -# InvalidTransition.new -# else -# IssueOpened.new(data: { issue_id: state.id }) -# end -# end -# -# def resolve(state) -# if %i[open in_progress reopened].include? state.status -# IssueResolved.new(data: { issue_id: state.id }) -# else -# InvalidTransition.new -# end -# end -# -# def close(state) -# if %i[open in_progress resolved reopened].include? state.status -# IssueClosed.new(data: { issue_id: state.id }) -# else -# InvalidTransition.new -# end -# end -# -# def reopen(state) -# if %i[resolved closed].include? state.status -# IssueReopened.new(data: { issue_id: state.id }) -# else -# InvalidTransition.new -# end -# end -# -# def stop(state) -# if %i[in_progress].include? state.status -# IssueProgressStopped.new(data: { issue_id: state.id }) -# else -# InvalidTransition.new -# end -# end -# -# def start(state) -# if %i[open reopened].include? state.status -# IssueProgressStarted.new(data: { issue_id: state.id }) -# else -# InvalidTransition.new -# end -# end -# end -# end -# end +end \ No newline at end of file From e95ca4e73cafa9a272071c85f3af5df6a99da76d Mon Sep 17 00:00:00 2001 From: Mateusz Nowak Date: Wed, 18 Sep 2024 14:09:56 +0200 Subject: [PATCH 13/17] fix namespaces - remove modules module --- .../heroes/{modules => }/creature_recruitment/dwelling.rb | 0 .../lib/heroes/{modules => }/creature_recruitment/module.rb | 0 .../creature_recruitment/write_build_dwelling.rb | 0 .../lib/heroes/{modules => }/shared_kernel/resources.rb | 0 .../creature_recruitment/write_build_dwelling_test.rb | 6 +++--- 5 files changed, 3 insertions(+), 3 deletions(-) rename heroesofddd_rails_application/lib/heroes/{modules => }/creature_recruitment/dwelling.rb (100%) rename heroesofddd_rails_application/lib/heroes/{modules => }/creature_recruitment/module.rb (100%) rename heroesofddd_rails_application/lib/heroes/{modules => }/creature_recruitment/write_build_dwelling.rb (100%) rename heroesofddd_rails_application/lib/heroes/{modules => }/shared_kernel/resources.rb (100%) rename heroesofddd_rails_application/test/lib/heroes/{modules => }/creature_recruitment/write_build_dwelling_test.rb (86%) diff --git a/heroesofddd_rails_application/lib/heroes/modules/creature_recruitment/dwelling.rb b/heroesofddd_rails_application/lib/heroes/creature_recruitment/dwelling.rb similarity index 100% rename from heroesofddd_rails_application/lib/heroes/modules/creature_recruitment/dwelling.rb rename to heroesofddd_rails_application/lib/heroes/creature_recruitment/dwelling.rb diff --git a/heroesofddd_rails_application/lib/heroes/modules/creature_recruitment/module.rb b/heroesofddd_rails_application/lib/heroes/creature_recruitment/module.rb similarity index 100% rename from heroesofddd_rails_application/lib/heroes/modules/creature_recruitment/module.rb rename to heroesofddd_rails_application/lib/heroes/creature_recruitment/module.rb diff --git a/heroesofddd_rails_application/lib/heroes/modules/creature_recruitment/write_build_dwelling.rb b/heroesofddd_rails_application/lib/heroes/creature_recruitment/write_build_dwelling.rb similarity index 100% rename from heroesofddd_rails_application/lib/heroes/modules/creature_recruitment/write_build_dwelling.rb rename to heroesofddd_rails_application/lib/heroes/creature_recruitment/write_build_dwelling.rb diff --git a/heroesofddd_rails_application/lib/heroes/modules/shared_kernel/resources.rb b/heroesofddd_rails_application/lib/heroes/shared_kernel/resources.rb similarity index 100% rename from heroesofddd_rails_application/lib/heroes/modules/shared_kernel/resources.rb rename to heroesofddd_rails_application/lib/heroes/shared_kernel/resources.rb diff --git a/heroesofddd_rails_application/test/lib/heroes/modules/creature_recruitment/write_build_dwelling_test.rb b/heroesofddd_rails_application/test/lib/heroes/creature_recruitment/write_build_dwelling_test.rb similarity index 86% rename from heroesofddd_rails_application/test/lib/heroes/modules/creature_recruitment/write_build_dwelling_test.rb rename to heroesofddd_rails_application/test/lib/heroes/creature_recruitment/write_build_dwelling_test.rb index 719be79..6489d86 100644 --- a/heroesofddd_rails_application/test/lib/heroes/modules/creature_recruitment/write_build_dwelling_test.rb +++ b/heroesofddd_rails_application/test/lib/heroes/creature_recruitment/write_build_dwelling_test.rb @@ -1,7 +1,7 @@ require "minitest/autorun" -require_relative "../../../../../lib/heroes/modules/creature_recruitment/dwelling" -require_relative "../../../../../lib/heroes/modules/creature_recruitment/write_build_dwelling" -require_relative "../../../../../lib/heroes/modules/shared_kernel/resources" +require_relative "../../../../lib/heroes/creature_recruitment/write_build_dwelling" +require_relative "../../../../lib/heroes/creature_recruitment/dwelling" +require_relative "../../../../lib/heroes/shared_kernel/resources" module Heroes From 3e760c1bebb62e71c3006ca88aa783a803ce4d22 Mon Sep 17 00:00:00 2001 From: Mateusz Nowak Date: Wed, 18 Sep 2024 14:13:31 +0200 Subject: [PATCH 14/17] fix format errors --- heroesofddd_rails_application/README.md | 2 ++ .../lib/heroes/creature_recruitment/dwelling.rb | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/heroesofddd_rails_application/README.md b/heroesofddd_rails_application/README.md index 5b82219..6eb84cd 100644 --- a/heroesofddd_rails_application/README.md +++ b/heroesofddd_rails_application/README.md @@ -11,6 +11,8 @@ Install ruby on your machine and Rails: 3. `rails db:migrate` 4. `rails server` +Fixing format errors: +`rubocop --auto-correct` This README would normally document whatever steps are necessary to get the application up and running. diff --git a/heroesofddd_rails_application/lib/heroes/creature_recruitment/dwelling.rb b/heroesofddd_rails_application/lib/heroes/creature_recruitment/dwelling.rb index 8ff4cef..23aacd8 100644 --- a/heroesofddd_rails_application/lib/heroes/creature_recruitment/dwelling.rb +++ b/heroesofddd_rails_application/lib/heroes/creature_recruitment/dwelling.rb @@ -38,4 +38,4 @@ def build(command, state) end end end -end \ No newline at end of file +end From f55143590a42ee6c2ba607d734a59076423eb251 Mon Sep 17 00:00:00 2001 From: Mateusz Nowak Date: Wed, 18 Sep 2024 14:17:37 +0200 Subject: [PATCH 15/17] configuration name --- .../heroes/creature_recruitment/{module.rb => configuration.rb} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename heroesofddd_rails_application/lib/heroes/creature_recruitment/{module.rb => configuration.rb} (100%) diff --git a/heroesofddd_rails_application/lib/heroes/creature_recruitment/module.rb b/heroesofddd_rails_application/lib/heroes/creature_recruitment/configuration.rb similarity index 100% rename from heroesofddd_rails_application/lib/heroes/creature_recruitment/module.rb rename to heroesofddd_rails_application/lib/heroes/creature_recruitment/configuration.rb From cba7b8133054fc7254f6bcf9ef3dcbeec9cc6ceb Mon Sep 17 00:00:00 2001 From: Mateusz Nowak Date: Wed, 18 Sep 2024 14:26:04 +0200 Subject: [PATCH 16/17] build dwelling --- .../{write_build_dwelling.rb => build_dwelling.rb} | 0 .../{write_build_dwelling_test.rb => build_dwelling_test.rb} | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename heroesofddd_rails_application/lib/heroes/creature_recruitment/{write_build_dwelling.rb => build_dwelling.rb} (100%) rename heroesofddd_rails_application/test/lib/heroes/creature_recruitment/{write_build_dwelling_test.rb => build_dwelling_test.rb} (95%) diff --git a/heroesofddd_rails_application/lib/heroes/creature_recruitment/write_build_dwelling.rb b/heroesofddd_rails_application/lib/heroes/creature_recruitment/build_dwelling.rb similarity index 100% rename from heroesofddd_rails_application/lib/heroes/creature_recruitment/write_build_dwelling.rb rename to heroesofddd_rails_application/lib/heroes/creature_recruitment/build_dwelling.rb diff --git a/heroesofddd_rails_application/test/lib/heroes/creature_recruitment/write_build_dwelling_test.rb b/heroesofddd_rails_application/test/lib/heroes/creature_recruitment/build_dwelling_test.rb similarity index 95% rename from heroesofddd_rails_application/test/lib/heroes/creature_recruitment/write_build_dwelling_test.rb rename to heroesofddd_rails_application/test/lib/heroes/creature_recruitment/build_dwelling_test.rb index 6489d86..f43db65 100644 --- a/heroesofddd_rails_application/test/lib/heroes/creature_recruitment/write_build_dwelling_test.rb +++ b/heroesofddd_rails_application/test/lib/heroes/creature_recruitment/build_dwelling_test.rb @@ -1,5 +1,5 @@ require "minitest/autorun" -require_relative "../../../../lib/heroes/creature_recruitment/write_build_dwelling" +require_relative "../../../../lib/heroes/creature_recruitment/build_dwelling" require_relative "../../../../lib/heroes/creature_recruitment/dwelling" require_relative "../../../../lib/heroes/shared_kernel/resources" From 366446f2fad0a1bfdb90e92915dad647e0d70283 Mon Sep 17 00:00:00 2001 From: Mateusz Nowak Date: Wed, 18 Sep 2024 14:30:33 +0200 Subject: [PATCH 17/17] fix sharedkernel resources --- .../heroes/creature_recruitment/dwelling.rb | 2 +- .../lib/heroes/shared_kernel/resources.rb | 54 ++++++++++--------- .../build_dwelling_test.rb | 2 +- 3 files changed, 30 insertions(+), 28 deletions(-) diff --git a/heroesofddd_rails_application/lib/heroes/creature_recruitment/dwelling.rb b/heroesofddd_rails_application/lib/heroes/creature_recruitment/dwelling.rb index 23aacd8..2802a8d 100644 --- a/heroesofddd_rails_application/lib/heroes/creature_recruitment/dwelling.rb +++ b/heroesofddd_rails_application/lib/heroes/creature_recruitment/dwelling.rb @@ -17,7 +17,7 @@ def decide(command, state) def evolve(state, event) case event when DwellingBuilt - Built.new(dwelling_id: event.dwelling_id, creature_id: event.creature_id, cost_per_troop: event.cost_per_troop, available_creatures: SharedKernel::Amount.of(0)) + Built.new(dwelling_id: event.dwelling_id, creature_id: event.creature_id, cost_per_troop: event.cost_per_troop, available_creatures: SharedKernel::Resources::Amount.of(0)) else raise "Unknown event" end diff --git a/heroesofddd_rails_application/lib/heroes/shared_kernel/resources.rb b/heroesofddd_rails_application/lib/heroes/shared_kernel/resources.rb index c262ae2..b248fe8 100644 --- a/heroesofddd_rails_application/lib/heroes/shared_kernel/resources.rb +++ b/heroesofddd_rails_application/lib/heroes/shared_kernel/resources.rb @@ -1,40 +1,42 @@ module Heroes module SharedKernel - ResourceType = [ :GOLD, :WOOD, :ORE, :MERCURY, :SULFUR, :CRYSTAL, :GEMS ] + module Resources + Type = [ :GOLD, :WOOD, :ORE, :MERCURY, :SULFUR, :CRYSTAL, :GEMS ] - Amount = Data.define(:raw) do - def self.of(raw) - raise ArgumentError, "Amount cannot be negative" if raw < 0 + Amount = Data.define(:raw) do + def self.of(raw) + raise ArgumentError, "Amount cannot be negative" if raw < 0 - new(raw) - end + new(raw) + end - def self.zero - new(0) - end + def self.zero + new(0) + end - def <=>(other) - raw <=> other.raw - end + def <=>(other) + raw <=> other.raw + end - def +(other) - Amount.new(raw + other.raw) - end + def +(other) + Amount.new(raw + other.raw) + end - def -(other) - Amount.new(raw - other.raw) + def -(other) + Amount.new(raw - other.raw) + end end - end - Cost = Data.define(:resources) do - def self.resources(*resources) - resource_map = resources.to_h { |type, amount| [ type, Amount.new(amount) ] } - new(resource_map) - end + Cost = Data.define(:resources) do + def self.resources(*resources) + resource_map = resources.to_h { |type, amount| [ type, Amount.new(amount) ] } + new(resource_map) + end - def *(multiplier) - resource_map = resources.transform_values { |amount| Amount.new(amount.raw * multiplier) } - self.class.new(resource_map) + def *(multiplier) + resource_map = resources.transform_values { |amount| Amount.new(amount.raw * multiplier) } + self.class.new(resource_map) + end end end end diff --git a/heroesofddd_rails_application/test/lib/heroes/creature_recruitment/build_dwelling_test.rb b/heroesofddd_rails_application/test/lib/heroes/creature_recruitment/build_dwelling_test.rb index f43db65..dd1d6c7 100644 --- a/heroesofddd_rails_application/test/lib/heroes/creature_recruitment/build_dwelling_test.rb +++ b/heroesofddd_rails_application/test/lib/heroes/creature_recruitment/build_dwelling_test.rb @@ -11,7 +11,7 @@ class BuildDwellingTest < Minitest::Test # givens @dwelling_id = "portal_of_glory" @creature_id = "angel" - @cost_per_troop = Heroes::SharedKernel::Cost.resources([ :GOLD, 3000 ], [ :CRYSTAL, 1 ]) + @cost_per_troop = Heroes::SharedKernel::Resources::Cost.resources([ :GOLD, 3000 ], [ :CRYSTAL, 1 ]) def test_given_not_built_dwelling_when_build_dwelling_then_dwelling_built # given