LifePex/src/achievements/achievement.rb

56 lines
1.8 KiB
Ruby

require_relative "./dsl"
module LifePex::AchievementDSL::Includer
extend LifePex::AchievementDSL
SUCCESS_LIST = []
SUCCESS_LIST << success("Marked a perk regularly") do
description "This success search for a serie of pexs in a span of time"
parameter "DaysAmountTotal" do
type :Integer
required
description "Duration in days to eval if the criteria are valids"
end
parameter "DaysAmountValidity" do
type :Integer
required
description "Amount of day over the total to validate the success"
end
parameter "MinimumAmountMarkedByDay" do
type :Integer
required
description "Minimum amount of mark for a day to be validated"
end
# parameter "PexIds" do
# type :Array
# description "List of pex that validates"
# end
parameter "Category" do
type :String
description "A category that validates"
end
to_complete do |user_pexs, pexs|
allowed_pex_ids = given(:PexIds)
allowed_pex_ids = pexs.filter { |pex| pex[:category] == given(:Category) }.map { |pex| pex[:id] } if given(:Category)
applicable = user_pexs.filter { |up| allowed_pex_ids.include?(up[:pex_id]) }
is_valid = false
counter = []
applicable.each do |up|
while !counter.empty? && (up[:created_at] - counter.first[:created_at]) > given(:DaysAmountTotal)
counter.shift
end unless counter.empty?
counter << up
grouped = counter.group_by { |c| c[:created_at] }
if grouped.values.filter { |tuple| tuple.size >= given(:MinimumAmountMarkedByDay) }.size >= given(:DaysAmountValidity)
is_valid = true
break
end
end
is_valid
end
end
SUCCESS_INDEX = SUCCESS_LIST.map { |s| [s.name, s] }.to_h
end