Data:Sample:RecipeProduction

From Desynced Wiki

This page uses Template:DataRecipe to show a recipe by passing "Building 1x1 (2S)"

{{DataRecipe|Building 1x1 (2S)}}


Error: Table <div class not found.


Template:DataRecipe is the replacement for making a page like GameData:Recipe:Building_1x1_(2S). Instead of having to use python scripts to create a new robot page and upload it each time you want more information, you can create a template like Template:DataRecipe

The high-level steps are:

  1. Add a template like Template:Recipe_Production_Test, this is exactly the same as it was before
  2. Create a template like Template:DataRecipe to fill in Template:Recipe_Production_Test
    1. Fill in a bunch of boilterplate (doesn't change much between templates like cargo_query)
    2. Choose which fields you want
    3. Tell cargo to rename them to your template's arguments
    4. Point the query to the actual template you created (like Template:Recipe_Production_Test)
  3. Include that template on pages you wish

Let's take a closer look at what Template:DataRecipe is actually doing.

<includeonly>
{{#cargo_query:
tables={{GetTable|{{{1}}}}}
|fields=name,ingredient1,amount1,ingredient2,amount2,ingredient3,amount3,ingredient4,amount4,producer1,time1,producer2,time2
|where=name='{{{1}}}'
|format=template
|template=Recipe_Production_Test
|named args=yes
}}
</includeonly>

What's happening here? There is a template called Template:Recipe_Production_Test that is just like Template:Recipe_Production except for a few renamed arguments because the character -(hyphen) is banned in cargo. That template needs a bunch of things: name of the thing, ingredients, how much of those, and producers. Here is the template:

{{Recipe Production
|name
|ingredient1
|amount1
|ingredient2
|amount2
|ingredient3
|amount3
|ingredient4
|amount4
|producer1
|time1
|producer2
|time2
}}

Our goal is: I want to fill in all the arguments I need for my template so I can render it. Let's look at the query line-by-line:


<includeonly>

See https://www.mediawiki.org/wiki/Transclusion#Partial_transclusion_markup, this has to do with transclusion. It says: Only render this when you include it on a page.


{{#cargo_query:

This is just like calling a template, but instead it's going to ask cargo for information.


tables={{GetTable|{{{1}}}}}

In order to ask cargo for information, we need to know which table from Special:CargoTables it's stored in. To help with that, there is Template:GetTable which does this for you[1]. Don't sweat the details, this always stays the same if you're looking up one thing.


|fields=name,ingredient1,amount1,ingredient2,amount2,ingredient3,amount3,ingredient4,amount4,producer1,time1,producer2,time2

Ok now we know which cargo table we're looking in (entity for the one example above). Now we can go to Special:CargoTables and click on our table: Special:CargoTables/entity. On that page there is a list of fields (there will be descriptions later) which you can pick from for your template.

name - String
health - Integer
power_usage_per_second - Integer
movement_speed - Float
visibility - Float
storage - Integer
size - String
race - String (allowed values: Robot · Alien · Bug · Human · Virus · Blight)
types1 - String (allowed values: Bug · Bot · Building)
types2 - String (allowed values: Bug · Bot · Building)
large_sockets - Integer
medium_sockets - Integer
small_sockets - Integer
internal_sockets - Integer
slot_type - String (allowed values: None · Flyer · Drone · Satellite · Garage · Bughole)
ingredient1 - String
amount1 - Integer
ingredient2 - String
amount2 - Integer
ingredient3 - String
amount3 - Integer
ingredient4 - String
amount4 - Integer
producer1 - String
time1 - Integer
producer2 - String
time2 - Integer

In this case I want the name and the recipe values. Looking back at the template:

{{Recipe_Production_Test
|name
|ingredient1
|amount1
|ingredient2
|amount2
|ingredient3
|amount3
|ingredient4
|amount4
|produced-by1
|time1
|produced-by2
|time2
}}

It turns out that the field names match[3] the template exactly, so all we have to do is list them in fields. More on this in a second.


|where=name='{{{1}}}'

This tells cargo to look up the fields we just listed only for the thing we care about. We're setting the name to the first argument passed to our template (the name).


|format=template
|template=Recipe_Production_Test
|named args=yes

Lets look at the last three together because they're related. First we tell cargo to take the fields we asked for and pass them to a template (|format=template), then we need to tell it which template (|template=Recipe_Production_Test) we want it to pass the information to.

The last line |named args=yes just means: use the fields names when you pass it to the template. If youre template didn't use names and only did postitional arguments ({{{1}}}, {{{2}}}) you would leave the named args line out.

It's worth noting that cargo has lots of display formats (https://www.mediawiki.org/wiki/Extension:Cargo/Display_formats) so you don't even have to write your own template if you don't want!


[1] The TLDR on how it works is that there is a special tabled called Special:CargoTables/table_index that only has two columns: name, stored_table where name is the name of a page and stored_table is which cargo table it's stored in.

[2] Well... how do I know which table my thing is? Often it might be obvious from the Special:CargoTables (it's a component, an item), but other times it might not (a building is an entity). One way to find the table is to just toss {{GetTable|My Thing}} into sany page and preview or into Special:ExpandTemplates and it'll spit out what table it's in the preview. This is something I want to improve.

[3] It's not a problem if your fields don't match. Lets imagine they were called item_amount1 instead of amount. All we have to do is add that to our fields line: |fields=ingredient1,amount1=item_amount1,... and now it'll automatically fill them in the template for us.