Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ feat: Creature Recruitment | Dwelling + slice: BuildDwelling -> DwellingBuilt #4

Merged
merged 18 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions heroesofddd_rails_application/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# 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`
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.

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Heroes
module CreatureRecruitment
class Configuration
def call(event_store, command_bus) end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module Heroes
module CreatureRecruitment
module Dwelling
NotBuilt = Data.define
Built = Data.define(:dwelling_id, :creature_id, :cost_per_troop, :available_creatures)

class << self
def decide(command, state)
case command
when BuildDwelling
build(command, state)
else
raise "Unknown command"
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: SharedKernel::Resources::Amount.of(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
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module Heroes
module SharedKernel
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

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
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require "minitest/autorun"
require_relative "../../../../lib/heroes/creature_recruitment/build_dwelling"
require_relative "../../../../lib/heroes/creature_recruitment/dwelling"
require_relative "../../../../lib/heroes/shared_kernel/resources"


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::Resources::Cost.resources([ :GOLD, 3000 ], [ :CRYSTAL, 1 ])

def test_given_not_built_dwelling_when_build_dwelling_then_dwelling_built
# given
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
command = Heroes::CreatureRecruitment::BuildDwelling.new(@dwelling_id, @creature_id, @cost_per_troop)
result = decide(given_events, command)

# then
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