Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
MateuszNaKodach committed Sep 27, 2024
1 parent a63841d commit cb860a6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,30 @@

if Rails.env.development?
Rails.application.config.after_initialize do
# game_id = SecureRandom.uuid
# metadata = ::BuildingBlocks::Application::AppContext.for_game(game_id)
# dwelling_id = SecureRandom.uuid
# creature_id = "angel"
# cost_per_troop = Heroes::SharedKernel::Resources::Cost.resources([ :GOLD, 3000 ], [ :GEM, 1 ])
#
# # Build Dwelling
# build_dwelling_command = Heroes::CreatureRecruitment::BuildDwelling.new(
# dwelling_id,
# creature_id,
# cost_per_troop
# )
# Rails.configuration.command_bus.call(build_dwelling_command, metadata)
# puts "🏠 Dwelling built with ID: #{dwelling_id}"
#
# # Increase Available Creatures
# increase_creatures_command = Heroes::CreatureRecruitment::IncreaseAvailableCreatures.new(
# dwelling_id,
# creature_id,
# 10
# )
# Rails.configuration.command_bus.call(increase_creatures_command, metadata)
# puts "👼 Increased available creatures by 10"
#
# puts "✅ Development seed completed successfully!"
game_id = SecureRandom.uuid
metadata = ::BuildingBlocks::Application::AppContext.for_game(game_id)
dwelling_id = SecureRandom.uuid
creature_id = "angel"
cost_per_troop = Heroes::SharedKernel::Resources::Cost.resources([ :GOLD, 3000 ], [ :GEM, 1 ])

# Build Dwelling
build_dwelling_command = Heroes::CreatureRecruitment::BuildDwelling.new(
dwelling_id,
creature_id,
cost_per_troop
)
Rails.configuration.command_bus.call(build_dwelling_command, metadata)
puts "🏠 Dwelling built with ID: #{dwelling_id}"

# Increase Available Creatures
increase_creatures_command = Heroes::CreatureRecruitment::IncreaseAvailableCreatures.new(
dwelling_id,
creature_id,
10
)
Rails.configuration.command_bus.call(increase_creatures_command, metadata)
puts "👼 Increased available creatures by 10"

puts "✅ Development seed completed successfully!"
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class State < ApplicationRecord
self.table_name = "current_date_read_models"

validates :game_id, presence: true, uniqueness: true
validates :month, presence: true, numericality: { greater_than_or_equal_to: 1 }
validates :week, presence: true, numericality: { greater_than_or_equal_to: 1 }
validates :day, presence: true, numericality: { greater_than_or_equal_to: 1 }
validates :month, presence: true
validates :week, presence: true
validates :day, presence: true
end

class Projection
Expand All @@ -31,9 +31,9 @@ def call(event_store)
CurrentDateReadModel::State
.find_or_create_by!(game_id: event.metadata[:game_id])
.update!(
month: event.year[:month],
week: event.year[:week],
day: event.year[:day],
month: event.data[:month],
week: event.data[:week],
day: event.data[:day],
)
},
to: [ ::EventStore::Heroes::Calendar::DayStarted ])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,73 +14,61 @@ def setup

def test_create_on_first_day_started
# given
given_domain_event(@stream_name, DayStarted.new(year: 1, month: 1, day: 1))
given_domain_event(@stream_name, DayStarted.new(month: 1, week: 1, day: 1))

# when
state = CurrentDateReadModel::State.find_by(game_id: @game_id)

# then
expected_state = CurrentDateReadModel::State.new(
game_id: @game_id,
year: 1,
month: 1,
day: 1
)
assert_equal expected_state.attributes, state.attributes
assert_equal @game_id, state.game_id
assert_equal 1, state.month
assert_equal 1, state.week
assert_equal 1, state.day
end

def test_update_on_next_day_started
# given
given_domain_event(@stream_name, DayStarted.new(year: 1, month: 1, day: 1))
given_domain_event(@stream_name, DayStarted.new(year: 1, month: 1, day: 2))
given_domain_event(@stream_name, DayStarted.new(month: 1, week: 1, day: 1))
given_domain_event(@stream_name, DayStarted.new(month: 1, week: 1, day: 2))

# when
state = CurrentDateReadModel::State.find_by(game_id: @game_id)

# then
expected_state = CurrentDateReadModel::State.new(
game_id: @game_id,
year: 1,
month: 1,
day: 2
)
assert_equal expected_state.attributes, state.attributes
assert_equal @game_id, state.game_id
assert_equal 1, state.month
assert_equal 1, state.week
assert_equal 2, state.day
end

def test_update_on_new_month_started
def test_update_on_new_week_started
# given
given_domain_event(@stream_name, DayStarted.new(year: 1, month: 1, day: 30))
given_domain_event(@stream_name, DayStarted.new(year: 1, month: 2, day: 1))
given_domain_event(@stream_name, DayStarted.new(month: 1, week: 1, day: 7))
given_domain_event(@stream_name, DayStarted.new(month: 1, week: 2, day: 1))

# when
state = CurrentDateReadModel::State.find_by(game_id: @game_id)

# then
expected_state = CurrentDateReadModel::State.new(
game_id: @game_id,
year: 1,
month: 2,
day: 1
)
assert_equal expected_state.attributes, state.attributes
assert_equal @game_id, state.game_id
assert_equal 1, state.month
assert_equal 2, state.week
assert_equal 1, state.day
end

def test_update_on_new_year_started
def test_update_on_new_month_started
# given
given_domain_event(@stream_name, DayStarted.new(year: 1, month: 12, day: 31))
given_domain_event(@stream_name, DayStarted.new(year: 2, month: 1, day: 1))
given_domain_event(@stream_name, DayStarted.new(month: 1, week: 4, day: 7))
given_domain_event(@stream_name, DayStarted.new(month: 2, week: 1, day: 1))

# when
state = CurrentDateReadModel::State.find_by(game_id: @game_id)

# then
expected_state = CurrentDateReadModel::State.new(
game_id: @game_id,
year: 2,
month: 1,
day: 1
)
assert_equal expected_state.attributes, state.attributes
assert_equal @game_id, state.game_id
assert_equal 2, state.month
assert_equal 1, state.week
assert_equal 1, state.day
end

def default_app_context
Expand Down

0 comments on commit cb860a6

Please sign in to comment.