Template:Recipe

From StarMade Wiki

Revision as of 16:01, 8 July 2016 by DukeofRealms (talk | contribs) (2 revisions imported)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Error: Unknown block

[view] [edit] [history] [[[:Template:Purge]]] 50px Template documentation

Creates manufacturing trees.

Usage

{{recipe|grey hull}}
{{recipe|blue advanced armor wedge}}
  • Blue Advanced Armor Wedge.png
    • Blue Advanced Armor.png
{{recipe|blue advanced armor wedge|recursive=yes}}


Editing

Template was created using the following script:

#!/usr/bin/env ruby
#
# Author: Sebastian Dufner
# License: I don't care, consider it public domain.
#
# Usage example: mw-recipe-template BLOCKCONFIG | xclip -sel clipboard

require 'rexml/document'

abort "Usage: mw-recipe-template BLOCKCONFIG" unless ARGV.size == 1

class Block
  Dependency = Struct.new(:block, :count)

  def self.blocks(block_config)
    blocks = {}
    doc = REXML::Document.new(File.open(block_config))

    REXML::XPath::each(doc, '//Block') do |node|
      block = from_xml(node)
      blocks[block.type] = block
    end

    blocks.values.each do |block|
      block.dependencies.map! {|dep| Dependency.new(blocks[dep[0]], dep[1])}
    end
  end

  def self.from_xml(node)
    type = node.attributes['type']
    name = node.attributes['name']
    deps = node.find {|child| child.name == 'Consistence' if child.is_a? REXML::Element}.map do |item|
      [item.text, item.attributes['count']] if item.is_a? REXML::Element
    end

    Block.new(type, name, deps.compact)
  end
  private_class_method :from_xml

  def initialize(type, name, dependencies = [])
    @type, @name, @dependencies = type, name, dependencies
  end
  attr_accessor :type, :name, :dependencies

  def to_s(depth: 0, horizontal: true, note: nil)
    result = "{{tree|[[File:#{name}.png]]"
    result.concat '|root' if depth == 0
    result.concat "|note=#{note}" if note
    result.concat '|class='
    result.concat 'manufacturing_tree ' if depth == 0

    if dependencies.size > 0
      result.concat 'horizontal ' if horizontal
      result.concat 'single ' if
        dependencies.size == 1 && dependencies[0].block.dependencies.size == 0
      result.concat '{{#ifeq: {{{recursive|no}}}|yes||single}}' if
        dependencies.size == 1
      result.concat '|children='
      result.concat '{{#ifeq: {{{recursive|no}}}|yes|' if depth == 1

      dependencies.each do |dep|
        result.concat(dep.block.to_s(
              depth: depth + 1,
              horizontal: !horizontal,
              note: (dep.count == '1' ? nil : dep.count)
            ))
      end

      result.concat '|}}' if depth == 1
    end

    result.concat '}}'
  end
end

template = '{{#switch: {{lc: {{{1}}}}}'
Block.blocks(ARGV[0]).each do |block|
  template.concat "|#{block.name.downcase}=#{block.to_s}"
end
template.concat '|#default=Error: Unknown block}}'

puts template