summaryrefslogtreecommitdiff
path: root/logic/cartridge
diff options
context:
space:
mode:
authoricebaker <icebaker@proton.me>2023-05-13 18:51:02 -0300
committericebaker <icebaker@proton.me>2023-05-13 18:51:02 -0300
commit1fbdb24fff7cd84b5506cb0c7092fdcdc867ec12 (patch)
treed5d815cae646002f4833439d07c36bf9c1f68ed7 /logic/cartridge
parent62939e4baafde86d0ef599f1c7dc07cce95b8a73 (diff)
adding missing files
Diffstat (limited to 'logic/cartridge')
-rw-r--r--logic/cartridge/adapters.rb30
-rw-r--r--logic/cartridge/affixes.rb33
-rw-r--r--logic/cartridge/default.rb34
-rw-r--r--logic/cartridge/interaction.rb38
-rw-r--r--logic/cartridge/streaming.rb25
5 files changed, 160 insertions, 0 deletions
diff --git a/logic/cartridge/adapters.rb b/logic/cartridge/adapters.rb
new file mode 100644
index 0000000..bc8ddaf
--- /dev/null
+++ b/logic/cartridge/adapters.rb
@@ -0,0 +1,30 @@
+# frozen_string_literal: true
+
+require_relative '../helpers/hash'
+require_relative './default'
+
+module NanoBot
+ module Logic
+ module Cartridge
+ module Adapter
+ def self.expression(cartridge, interface, direction, language)
+ adapter = [
+ {
+ exists: (Helpers::Hash.fetch(cartridge, [:interfaces, direction, :adapter]) || {}).key?(language),
+ value: Helpers::Hash.fetch(cartridge, [:interfaces, direction, :adapter, language])
+ },
+ {
+ exists: (Helpers::Hash.fetch(cartridge,
+ [:interfaces, interface, direction, :adapter]) || {}).key?(language),
+ value: Helpers::Hash.fetch(cartridge, [:interfaces, interface, direction, :adapter, language])
+ }
+ ].filter { |candidate| candidate[:exists] }.last
+
+ return nil if adapter.nil?
+
+ adapter[:value]
+ end
+ end
+ end
+ end
+end
diff --git a/logic/cartridge/affixes.rb b/logic/cartridge/affixes.rb
new file mode 100644
index 0000000..0b40fa2
--- /dev/null
+++ b/logic/cartridge/affixes.rb
@@ -0,0 +1,33 @@
+# frozen_string_literal: true
+
+require_relative '../helpers/hash'
+require_relative './default'
+
+module NanoBot
+ module Logic
+ module Cartridge
+ module Affixes
+ def self.get(cartridge, interface, direction, kind)
+ affix = [
+ {
+ exists: (Helpers::Hash.fetch(cartridge, [:interfaces, direction]) || {}).key?(kind),
+ value: Helpers::Hash.fetch(cartridge, [:interfaces, direction, kind])
+ },
+ {
+ exists: (Helpers::Hash.fetch(cartridge, [:interfaces, interface, direction]) || {}).key?(kind),
+ value: Helpers::Hash.fetch(cartridge, [:interfaces, interface, direction, kind])
+ }
+ ].filter { |candidate| candidate[:exists] }.last
+
+ if affix.nil?
+ return Helpers::Hash.fetch(
+ Default.instance.values, [:interfaces, interface, direction, kind]
+ )
+ end
+
+ affix[:value]
+ end
+ end
+ end
+ end
+end
diff --git a/logic/cartridge/default.rb b/logic/cartridge/default.rb
new file mode 100644
index 0000000..43d45d4
--- /dev/null
+++ b/logic/cartridge/default.rb
@@ -0,0 +1,34 @@
+# frozen_string_literal: true
+
+require 'yaml'
+require 'singleton'
+
+require_relative '../helpers/hash'
+
+module NanoBot
+ module Logic
+ module Cartridge
+ class Default
+ include Singleton
+
+ def values
+ return @values if @values
+
+ path = File.expand_path('../../static/cartridges/default.yml', __dir__)
+ cartridge = YAML.safe_load(File.read(path), permitted_classes: [Symbol])
+ @values = Logic::Helpers::Hash.symbolize_keys(cartridge)
+ @values
+ end
+
+ def baseline
+ return @baseline if @baseline
+
+ path = File.expand_path('../../static/cartridges/baseline.yml', __dir__)
+ cartridge = YAML.safe_load(File.read(path), permitted_classes: [Symbol])
+ @baseline = Logic::Helpers::Hash.symbolize_keys(cartridge)
+ @baseline
+ end
+ end
+ end
+ end
+end
diff --git a/logic/cartridge/interaction.rb b/logic/cartridge/interaction.rb
new file mode 100644
index 0000000..160d818
--- /dev/null
+++ b/logic/cartridge/interaction.rb
@@ -0,0 +1,38 @@
+# frozen_string_literal: true
+
+require 'sweet-moon'
+
+require_relative './affixes'
+require_relative './adapters'
+
+module NanoBot
+ module Logic
+ module Cartridge
+ module Interaction
+ def self.input(cartridge, interface, content)
+ lua = Adapter.expression(cartridge, interface, :input, :lua)
+ fennel = Adapter.expression(cartridge, interface, :input, :fennel)
+
+ prefix = Affixes.get(cartridge, interface, :input, :prefix)
+ suffix = Affixes.get(cartridge, interface, :input, :suffix)
+
+ { content:, prefix:, suffix:, lua:, fennel: }
+ end
+
+ def self.output(cartridge, interface, result, streaming, _finished)
+ if streaming
+ result[:message] = { content: result[:message], lua: nil, fennel: nil }
+ return result
+ end
+
+ lua = Adapter.expression(cartridge, interface, :output, :lua)
+ fennel = Adapter.expression(cartridge, interface, :output, :fennel)
+
+ result[:message] = { content: result[:message], lua:, fennel: }
+
+ result
+ end
+ end
+ end
+ end
+end
diff --git a/logic/cartridge/streaming.rb b/logic/cartridge/streaming.rb
new file mode 100644
index 0000000..a0f8700
--- /dev/null
+++ b/logic/cartridge/streaming.rb
@@ -0,0 +1,25 @@
+# frozen_string_literal: true
+
+require_relative '../helpers/hash'
+
+module NanoBot
+ module Logic
+ module Cartridge
+ module Streaming
+ def self.enabled?(cartridge, interface)
+ return false if Helpers::Hash.fetch(cartridge, %i[provider settings stream]) == false
+
+ specific_interface = Helpers::Hash.fetch(cartridge, [:interfaces, interface, :output, :stream])
+
+ return specific_interface unless specific_interface.nil?
+
+ interface = Helpers::Hash.fetch(cartridge, %i[interfaces output stream])
+
+ return interface unless interface.nil?
+
+ true
+ end
+ end
+ end
+ end
+end