Skip to content

Instantly share code, notes, and snippets.

@ujihisa
Last active January 19, 2019 02:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ujihisa/597e09096c4ccdbdc9dd64572ef2fa89 to your computer and use it in GitHub Desktop.
Save ujihisa/597e09096c4ccdbdc9dd64572ef2fa89 to your computer and use it in GitHub Desktop.
#!ruby -v
ALL_PERKS = {
PER: ['Gunslinger', 'Shotgun Messiah', 'Automatic Weapons', 'Dead Eye', 'Archery', 'Explosive Weapons', 'Boom! Headshot!', 'Run And Gun', 'Lucky Looter', 'Salvage Operations'],
STR: ['Wrecking Crew', 'Sexual Tyrannosaurus', 'Flurry of Blows', 'Deep Cuts', 'Stay Down!', 'Heavy Metal', 'Skull Crusher', 'Miner 69\'er', 'Pack Mule', 'Mother Lode'],
FRT: ['Heavy Armor', 'The Huntsman', 'Intrinsic Immunity', 'Well Insulated', 'Living Off The Land', 'Pain Tolerance', 'Healing Factor', 'Fully Hydrated', 'Slow Metabolism', 'Self Medicated'],
AGI: ['Rule 1: Cardio', 'Light Armor', 'Charging Bull', 'Parkour', 'Olympic Swimmer', 'Ninja Movement', 'Hidden Strike', 'From Shadows'],
INT: ['Better Barter', 'The Daring Adventurer', 'Charismatic Nature', 'Hammer & Forge', 'Grease Monkey', 'Advanced Engineering', 'Yeah, Science!', 'Physician', 'Master Chef']
}
# Available == !insufficiencies(...)
def insufficiencies(perk, player_attrs, player_level)
perk_name, perk_level = perk
case perk_name
when *ALL_PERKS[:PER]
player_attrs[:PER] < [0, 1, 3, 5, 7, 10][perk_level] and 'Insufficient PER'
when *ALL_PERKS[:STR]
player_attrs[:STR] < [0, 1, 3, 5, 7, 10][perk_level] and 'Insufficient STR'
when *ALL_PERKS[:FRT]
player_attrs[:FRT] < [0, 1, 3, 5, 7, 10][perk_level] and 'Insufficient FRT'
when *ALL_PERKS[:AGI]
player_attrs[:AGI] < [0, 1, 3, 5, 7, 10][perk_level] and 'Insufficient AGI'
when 'Better Barter', 'The Daring Adventurer'
player_attrs[:INT] < [0, 1, 3, 5, 7, 10][perk_level] and 'Insufficient INT'
when 'Charismatic Nature'
player_attrs[:INT] < [0, 1, 3, 5, 7][perk_level] and 'Insufficient INT'
when 'Hammer & Forge'
(player_attrs[:INT] < [0, 4, 5, 6, 8, 9][perk_level] and 'Insufficient INT') or
(player_level < [0, 10, 20, 35, 70, 100][perk_level] and 'Insufficient player level')
when 'Grease Monkey'
(player_attrs[:INT] < [0, 4, 6, 7, 9, 10][perk_level] and 'Insufficient INT') or
(player_level < [0, 1, 25, 50, 85, 120][perk_level] and 'Insufficient player level')
when 'Advanced Engineering'
(player_attrs[:INT] < [0, 6, 7, 8, 9, 10][perk_level] and 'Insufficient INT') or
(player_level < [0, 25, 40, 60, 80, 100][perk_level] and 'Insufficient player level')
when 'Yeah, Science!'
player_attrs[:INT] < [0, 6, 7, 8, 9, 10][perk_level] and 'Insufficient INT'
when 'Physician'
player_attrs[:INT] < [0, 4, 6, 8, 9, 10][perk_level] and 'Insufficient INT'
when 'Master Chef'
player_attrs[:INT] < [0, 1, 3, 5, 7, 10][perk_level] and 'Insufficient INT'
else
raise ArgumentError, 'No such perk'
end
end
Player = Struct.new(:level, :skill_point, :attrs, :perks) do
def level_up(n = 1)
Player.new(level + n, skill_point + n, attrs, perks)
end
def take_attr(attr_name)
new_attr_level = attrs[attr_name] + 1
if new_attr_level > 10
warn('Attr level beyond 10')
return nil
end
new_skill_point = skill_point - [0, 1, 1, 1, 2, 2, 3, 3, 4, 5][new_attr_level]
if new_skill_point < 0
warn('Insufficient skill point')
return nil
end
Player.new(level, new_skill_point, attrs.merge(attr_name => new_attr_level), perks)
end
def take_perk(perk_name)
if self.skill_point <= 0
warn 'Insufficient skill point'
return nil
end
perk_level = perks[perk_name] || 0
x = insufficiencies([perk_name, perk_level], self.attrs, self.level)
if x
warn(x)
nil
else
Player.new(level, skill_point - 1, attrs, perks.merge(perk_name => perk_level + 1))
end
end
end
player = Player.new(1, 4, {PER: 1, STR: 1, FRT: 1, AGI: 1, INT: 1}, {})
p player
p player = player&.take_perk('Gunslinger')
p player = player&.take_perk('Gunslinger')
p player = player&.take_perk('Skull Crusher')
p player = player&.take_attr(:PER)
p player = player&.take_perk('Gunslinger')
p player = player&.take_perk('Gunslinger')
# ruby 2.6.0p0 (2018-12-25 revision 66547) [x86_64-linux]
# #<struct Player level=1, skill_point=4, attrs={:PER=>1, :STR=>1, :FRT=>1, :AGI=>1, :INT=>1}, perks={}>
# #<struct Player level=1, skill_point=3, attrs={:PER=>1, :STR=>1, :FRT=>1, :AGI=>1, :INT=>1}, perks={"Gunslinger"=>1}>
# #<struct Player level=1, skill_point=2, attrs={:PER=>1, :STR=>1, :FRT=>1, :AGI=>1, :INT=>1}, perks={"Gunslinger"=>2}>
# #<struct Player level=1, skill_point=1, attrs={:PER=>1, :STR=>1, :FRT=>1, :AGI=>1, :INT=>1}, perks={"Gunslinger"=>2, "Skull Crusher"=>1}>
# nil
# nil
# Insufficient PER
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment