class ParaDice::Results::CancelOpposing

Results class to have some faces cancel other faces out of the results,

can also be used to drop faces entirely by defining them as their own opposite

Constants

DEFAULT_OPPOSING

set empty here so it will work if no values passed to it.

Attributes

default_opposing[RW]

@!attribute rw default_opposing

default to DEFAULT_OPPOSING = []
@return [Array<Array>] list of pairs of faces that cancel each other out
  use [['blank','blank']] as opposing to eliminate blank results
  use [['good','bad'], ['hero','villain']] to have good and bad, heroes
  villains to cancel each other out

Public Class Methods

new(default_opposing = DEFAULT_OPPOSING) click to toggle source

@param [Array<Array>] opposing defaults to DEFAULT_OPPOSING=[]

# File lib/para_dice/results/cancel_opposing.rb, line 19
def initialize(default_opposing = DEFAULT_OPPOSING)
  @default_opposing = default_opposing
end

Public Instance Methods

resolve(faces, opposing = default_opposing) click to toggle source

@param [Array<#to_s>] faces @param [Array<Array>] opposing defaults to default_opposing @return [Array<String>]

# File lib/para_dice/results/cancel_opposing.rb, line 26
def resolve(faces, opposing = default_opposing)
  summary = Utility.to_summary(faces)
  opposing.each do |one_face, another_face|
    one_face     = one_face.to_s
    another_face = another_face.to_s
    next unless summary.key?(one_face) && summary.key?(another_face)
    if summary[one_face] == summary[another_face]
      summary.delete(one_face)
      summary.delete(another_face)
    else
      low, high     = [one_face, another_face].sort_by { |e| summary[e] }
      summary[high] -= summary[low]
      summary.delete(low)
    end
  end

  Utility.from_summary(summary)
end