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

Solve Zad4 #131

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def index
warriors = clan.warriors

if params.key?(:alive)
if params[:alive].to_i == 0
if params[:alive].to_i.zero?
render json: warriors.dead
else
render json: warriors.alive
Expand Down
30 changes: 11 additions & 19 deletions lecture_4/homework/app/controllers/mercenaries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,22 @@
# This controller is written badly on purpose. Please refactor this
class MercenariesController < ApplicationController
def index
render json: Mercenary.where('available_from < ?', Time.now).all
render json: Mercenary.available
end

def show
render json: mercenary, include: [:warrior]
end

def employ_best
mercenary = Mercenary.where('available_from < ?', Time.now).order(price: :asc).first # TODO: what about experience?
clan = find_clan
building = find_building
warrior_class = clan.warriors.select('type, count(type) as warriors_count').group(:type).order('warriors_count ASC').first.class
warrior = warrior_class.create!(name: mercenary.name, clan: clan, building: building, preferred_weapon_kind: mercenary.preferred_weapon_kind, mercenary: mercenary)
create_good_weapon(mercenary)
mercenary = best
warrior = WarriorRecruiter.new(mercenary: mercenary, params: mercenary_params).call
render json: warrior, include: [:mercenary], status: 201
end

def employ
return unless mercenary.available_from < Time.now

clan = find_clan
building = find_building
warrior_class = clan.warriors.select('type, count(type) as warriors_count').group(:type).order('warriors_count ASC').first.class
Expand All @@ -37,9 +34,7 @@ def mercenary
end

def find_building
if params[:building_id]
Building.find(params[:building_id])
end
Building.find(params[:building_id]) if params[:building_id]
end

def find_clan
Expand All @@ -50,14 +45,11 @@ def find_clan
end
end

def create_good_weapon(mercenary)
case mercenary.preferred_weapon_kind
when :melee
Weapons::Katana.create!(warrior: mercenary.warrior, range: 2, damage: 25)
when :ranged
Weapons::Musket.create!(warrior: mercenary.warrior, range: 40, damage: 10)
else
# TODO: some default?
end
def best
Mercenary.available.cheapest
end

def mercenary_params
params.permit(:building_id, :clan_id)
end
end
2 changes: 2 additions & 0 deletions lecture_4/homework/app/models/mercenary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class Mercenary < ApplicationRecord

validates :warrior, uniqueness: { allow_nil: true }

scope :available, -> { MercenariesQuery.available }
scope :cheapest, -> { MercenariesQuery.cheapest }
scope :alive, -> { where('death_date IS NULL') }
scope :dead, -> { where('death_date IS NOT NULL') }
end
2 changes: 1 addition & 1 deletion lecture_4/homework/app/models/warriors/hussar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Hussar < Warrior
attribute :preferred_weapon_kind, :string, default: :ranged

def attack
"Hussar #{name} charged while yielding #{weapon.type.demodulize}"
"Hussar #{name} charged while yielding #{(weapon || NullWeapon.new).type.demodulize}"
end
end
end
2 changes: 1 addition & 1 deletion lecture_4/homework/app/models/warriors/samurai.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Samurai < Warrior
attribute :preferred_weapon_kind, :string, default: :melee

def attack
"Samurai #{name} attacked with #{weapon.type.demodulize}"
"Samurai #{name} attacked with #{(weapon || NullWeapon.new).type.demodulize}"
end
end
end
11 changes: 11 additions & 0 deletions lecture_4/homework/app/models/weapons/rod.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module Weapons
class Rod < Weapon
attribute :kind, :string, default: :magical

def calculate_damage(distance)
distance < range ? damage * 2 : damage / 2 + 42
end
end
end
7 changes: 7 additions & 0 deletions lecture_4/homework/app/null_objects/weapon.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class NullWeapon
def type
'Null Weapon'
end
end
11 changes: 11 additions & 0 deletions lecture_4/homework/app/queries/mercenaries_query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class MercenariesQuery
def self.available(relation: Mercenary)
relation.where(warrior_id: nil).where('available_from < ?', Time.now)
end

def self.cheapest(relation: Mercenary)
relation.order(price: :asc).first
end
end
48 changes: 48 additions & 0 deletions lecture_4/homework/app/services/warrior_recruiter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

class WarriorRecruiter
attr_accessor :mercenary, :params
def initialize(mercenary:, params:)
@mercenary = mercenary
@params = params
end

def call
clan = find_clan
building = find_building
warrior_class = find_class(clan)

warrior = warrior_class.create!(
name: mercenary.name,
clan: clan,
building: building,
preferred_weapon_kind: mercenary.preferred_weapon_kind,
mercenary: @mercenary
)
create_good_weapon(@mercenary)
warrior
end

private

def find_class(clan)
clan.warriors.select('type, count(type) as warriors_count')
.group(:type).order('warriors_count ASC').first.class
end

def find_building
Building.find(@params[:building_id]) if @params[:building_id]
end

def find_clan
if @params[:clan_id]
Clan.find(@params[:clan_id])
else
Clan.order(warriors_count: :desc).first
end
end

def create_good_weapon(mercenary)
WeaponCreator.new(mercenary: mercenary).call
end
end
20 changes: 20 additions & 0 deletions lecture_4/homework/app/services/weapon_creator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

class WeaponCreator
attr_accessor :mercenary

def initialize(mercenary:)
@mercenary = mercenary
end

def call(mercenary)
case mercenary.preferred_weapon_kind
when :melee
Weapons::Katana.create!(warrior: mercenary.warrior, range: 2, damage: 25)
when :ranged
Weapons::Musket.create!(warrior: mercenary.warrior, range: 40, damage: 10)
else
Weapons::Rod.create!(warrior: mercenary.warrior, range: 42, damage: 10)
end
end
end
22 changes: 20 additions & 2 deletions recruitment_task/converter.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
require_relative 'price'

class Converter
# TODO: Implement me
class Converter < Price

EXCHANGE = {
eur: { pln: 4.32, eur: 1.00, usd: 1.13 },
usd: { pln: 3.81, eur: 0.88, usd: 1.00 },
pln: { pln: 1.00, eur: 0.23, usd: 0.26 }
}.freeze

def initialize(price)
@price = price
end

def convert_to(currency)
validate_currency(currency)
(@price.amount * EXCHANGE[@price.currency][currency]).round(2)
end

def validate_currency(currency)
raise InvalidCurrency unless SUPPORTED_CURRENCIES.include?(currency)
end
end