From e558a8531bae9c26b4256e91fc2017a4f3ed1afa Mon Sep 17 00:00:00 2001 From: "Jill \"oatmealine\" Monoids" Date: Fri, 2 Jun 2023 13:44:00 +0300 Subject: [PATCH] configurable buddy types!!! --- src/buddies.cr | 16 ++++++++++++++++ src/configman.cr | 5 ++++- src/funfriend.cr | 4 +++- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/buddies.cr b/src/buddies.cr index a7b05be..468db40 100644 --- a/src/buddies.cr +++ b/src/buddies.cr @@ -99,4 +99,20 @@ module Funfriend SoundMan.play_sound("assets/sfx/talk_god#{(1..8).sample}.ogg") end end + + BUDDIES = { + "funfriend" => FunfriendBuddy, + "catfriend" => CatfriendBuddy, + "god" => GodBuddy, + } + + def self.make_buddy(name : String) : Buddy + buddy_type = BUDDIES[name]? + + if buddy_type + return buddy_type.new + else + raise IndexError.new(message: "Invalid buddy type \"#{name}\". Supported buddies: #{BUDDIES.keys.map { |k| "\"#{k}\""} .join(", ")}") + end + end end diff --git a/src/configman.cr b/src/configman.cr index 936fcd1..4d1d76a 100644 --- a/src/configman.cr +++ b/src/configman.cr @@ -18,7 +18,10 @@ module Funfriend::ConfigMan }, "sound" => { "volume" => 0.2.as(ConfigValue) - } + }, + "buddies" => { + "types" => "funfriend".as(ConfigValue) + }, }.as(Config) def get_config_path : Path diff --git a/src/funfriend.cr b/src/funfriend.cr index 2863849..e7297ae 100644 --- a/src/funfriend.cr +++ b/src/funfriend.cr @@ -35,7 +35,9 @@ module Funfriend end def self.init_contexts - add_context(BuddyContext.new(FunfriendBuddy.new)) + ConfigMan.config["buddies"]["types"].as(String).split(",").each do |buddy_name| + add_context(BuddyContext.new(make_buddy(buddy_name))) + end @@main_context = @@contexts[0] at_exit { contexts.each &.destroy } end