FANDOM


-- Created by Spottra, 9 Oct 2015
local p = {}
 
local troopStats      = require( 'Module:TroopStatistics' )
local spellStats      = require( 'Module:SpellStatistics' )
local formatNum       = require( 'Module:FormatNum' ).formatNum
local pluralize       = require( 'Module:Plural' ).pluralize
local icon            = require( 'Module:Icon' ).icon
local titleCase       = require( 'Module:Title Case' ).titleCase
local pageCreator     = require( 'Module:Page Creator' ).main
local troopStatistics = troopStats.troopStatistics
local troopData       = troopStats.troopData
local troops          = troopStats.troops
local spellStatistics = spellStats.spellStatistics
local spellData       = spellStats.spellData
local spells          = spellStats.spells
 
function p.createAttackStrategy( args )
   local mintownhall = tonumber( args[ 'mintownhall' ] )
   local maxtownhall = tonumber( args[ 'maxtownhall' ] )
   local townhall    = ''
   local mintrophies = tonumber( args[ 'mintrophies' ] )
   local maxtrophies = tonumber( args[ 'maxtrophies' ] )
   local trophies    = ''
 
   if not args[ 'mintownhall' ] and not args[ 'maxtownhall' ] then
      townhall = 'Any'
   elseif ( args[ 'mintownhall' ] and not mintownhall ) or
      ( args[ 'maxtownhall' ] and not maxtownhall ) then
      townhall = 'Error'
   elseif not mintownhall then
      townhall = '0-' .. maxtownhall
   elseif not maxtownhall then
      townhall = mintownhall .. '+'
   elseif mintownhall > maxtownhall then
      townhall = 'Error'
   elseif mintownhall == maxtownhall then
      townhall = mintownhall
   else
      townhall = mintownhall .. '-' .. maxtownhall
   end
 
   if not args[ 'mintrophies' ] and not args[ 'maxtrophies' ] then
      trophies = 'Any'
   elseif ( args[ 'mintrophies' ] and not mintrophies ) or
          ( args[ 'maxtrophies' ] and not maxtrophies ) then
      trophies = 'Error'
   elseif not mintrophies then
      trophies = '0-' .. maxtrophies
   elseif not maxtrophies then
      trophies = mintrophies .. '+'
   elseif mintrophies > maxtrophies then
      trophies = 'Error'
   elseif mintrophies == maxtrophies then
      trophies = mintrophies
   else
      trophies = mintrophies .. '-' .. maxtrophies
   end
 
   local troopTbl       = {}
   local totalResources = {}
   local totalHousing   = 0
   local totalspellHousing   = 0
   local idx            = 0
 
   for i = 1, #troops do
      local troop        = troops[ i ]
      local troopName    = troopStatistics( troop, 'name' )
      local troopQty     = args[ pluralize( troop ) ] or 0
      local troopLvl     = args[ troop .. 'level' ] or 1
      local housing      = 0
      local cost         = 0
      local resource     = troopStatistics( troop, 'resource' )
 
      if type( totalResources ) == 'table' and tonumber( troopQty ) and tonumber( troopLvl ) then
         housing = tonumber( troopQty ) * troopStatistics( troop, 'housing space' )
         local troopCost = troopStatistics( troop, 'training cost', tonumber( troopLvl ) )
 
         if troopCost == '' then
            troopCost = 0
         end
 
         cost = tonumber( troopQty ) * troopCost
 
         if type( totalResources ) == 'table' then
            if totalResources[ resource ] == nil then
               totalResources[ resource ] = 0
            end
 
            totalResources[ resource ] = totalResources[ resource ] + cost
            totalHousing = totalHousing + housing
         end
      else
         totalResources = 'Invalid'
         totalHousing   = 'Invalid'
      end
 
      if type( troopQty ) ~= 'number' or troopQty > 0 then
         idx = idx + 1
         troopTbl[ idx ] = {
            [ 'name'    ] = '[[' .. troopName .. '|' .. pluralize( troopName ) .. ']]',
            [ 'qty'     ] = formatNum( troopQty ),
            [ 'lvl'     ] = (tonumber( troopLvl ) == 1 and 'Any' or troopLvl),
            [ 'housing' ] = formatNum( housing ),
            [ 'cost'    ] = formatNum( cost ) .. ' ' .. icon( resource )
         }
      end
   end
 
   for i = 1, #spells do
      local spell        = spells[ i ]
      local spellName    = spellStatistics( spell, 'name' )
      local spellQty     = args[ pluralize( spell .. 'spell' ) ] or 0
      local spellLvl     = args[ spell .. 'spelllevel' ] or 1
      local spellhousing      = 0
      local cost         = 0
      local resource     = spellStatistics( spell, 'resource' )
 
      if type( totalResources ) == 'table' and tonumber( spellQty ) and tonumber( spellLvl ) then
         spellhousing = tonumber( spellQty ) * spellStatistics( spell, 'housing space' )
         local spellCost = spellStatistics( spell, 'creation cost', tonumber( spellLvl ) )
 
         if spellCost == '' then
            spellCost = 0
         end
 
         cost = tonumber( spellQty ) * spellCost
 
         if type( totalResources ) == 'table' then
            if totalResources[ resource ] == nil then
               totalResources[ resource ] = 0
            end
 
            totalResources[ resource ] = totalResources[ resource ] + cost
            totalspellHousing = totalspellHousing + spellhousing
         end
      else
         totalResources = 'Invalid'
         totalspellHousing   = 'Invalid'
      end
 
      if type( spellQty ) ~= 'number' or spellQty > 0 then
         idx = idx + 1
         troopTbl[ idx ] = {
            [ 'name'    ] = '[[' .. spellName .. '|' .. pluralize( spellName ) .. ']]',
            [ 'qty'     ] = formatNum( spellQty ),
            [ 'lvl'     ] = (tonumber( spellLvl ) == 1 and 'Any' or spellLvl),
            [ 'housing' ] = formatNum( spellhousing ),
            [ 'cost'    ] = formatNum( cost ) .. ' ' .. icon( resource )
         }
      end
   end
 
   -- Should we print out the totals or not?
   local validTotals = type( totalResources ) == 'table' and
      type( totalHousing ) == 'number' and
      totalHousing > 0 or type( totalspellHousing ) == 'number' and
      totalspellHousing > 0
 
   if tonumber( totalHousing ) then
      totalHousing = formatNum( totalHousing )
   end
 
   if tonumber( totalspellHousing ) then
      totalspellHousing = formatNum( totalspellHousing )
   end
 
   local resourceStr = ''
 
   if type( totalResources ) == 'table' then
      local tbl = mw.html.create( 'div' )
 
      for resType, resAmt in pairs( totalResources ) do
         if resAmt > 0 then
            tbl
               :tag( 'div' )
               :wikitext( formatNum( resAmt ) .. ' ' .. icon( resType ) )
               :done()
         end
      end
 
      resourceStr = tostring( tbl )
   else
      resourceStr = tostring( totalResources )
   end
 
   local troopTable = mw.html.create( 'table' )
 
   troopTable
      :addClass( 'coc-attack-strategy-trooptable' )
 
   local tr = troopTable:tag( 'tr' )
 
   tr
      :tag( 'th' )
         :attr( 'data-type', 'troop type' )
         :wikitext( '[[Troops|Troop]] and [[Spells|Spell]] Type' )
         :done()
      :tag( 'th' )
         :attr( 'data-type', 'quantity' )
         :wikitext( 'Quantity' )
         :done()
      :tag( 'th' )
         :attr( 'data-type', 'min level' )
         :wikitext( 'Min Lvl' )
         :done()
 
   if validTotals then
      tr
         :tag( 'th' )
            :attr( 'data-type', 'housing' )
            :wikitext( 'Housing' )
            :done()
         :tag( 'th' )
            :attr( 'data-type', 'cost' )
            :wikitext( 'Cost' )
            :done()
         :done()
   end
 
   for i = 1, #troopTbl do
      local troop = troopTbl[ i ]
 
      tr = troopTable:tag( 'tr' )
 
      tr
         :tag( 'td' )
            :attr( 'data-type', 'troop type' )
            :wikitext( troop[ 'name' ] )
            :done()
         :tag( 'td' )
            :attr( 'data-type', 'quantity' )
            :wikitext( troop[ 'qty' ] )
            :done()
         :tag( 'td' )
            :attr( 'data-type', 'min level' )
            :wikitext( troop[ 'lvl' ] )
            :done()
 
      if validTotals then
         tr
            :tag( 'td' )
               :attr( 'data-type', 'housing' )
               :wikitext( troop[ 'housing' ] )
               :done()
            :tag( 'td' )
               :attr( 'data-type', 'cost' )
               :wikitext( troop[ 'cost' ] )
               :done()
            :done()
      end
   end
 
   if validTotals then
      tr = troopTable:tag( 'tr' )
 
      tr
         :addClass( 'totals' )
         :tag( 'td' )
            :attr( 'data-type', 'troop type' )
            :wikitext( 'Totals' )
            :done()
         :tag( 'td' )
            :attr( 'data-type', 'quantity' )
            :wikitext( '-' )
            :done()
         :tag( 'td' )
            :attr( 'data-type', 'min level' )
            :wikitext( '-' )
            :done()
         :tag( 'td' )
            :attr( 'data-type', 'housing' )
            :wikitext( totalHousing, '+', totalspellHousing)
            :done()
         :tag( 'td' )
            :attr( 'data-type', 'cost' )
            :wikitext( resourceStr )
            :done()
         :done()
   end
 
   local executionSteps = mw.html.create('ol')
   executionSteps:addClass( 'coc-attack-strategy-execution' )
 
   -- Look for all 'stepX' arguments
   local orderedKeys = {}
 
   for k, v in pairs( args ) do
      if string.match( k, '^step%d+$' ) then
         table.insert( orderedKeys, tonumber( string.sub( k, 5 ) ) )
      end
   end
 
   table.sort( orderedKeys )
 
   for i = 1, #orderedKeys do
      executionSteps
         :tag( 'li' )
         :attr( 'value', orderedKeys[ i ] )
         :wikitext( args[ 'step' .. orderedKeys[ i ] ] )
         :done()
   end
 
   local output = mw.html.create( 'div' )
 
   output
      :addClass( 'coc-attack-strategy-main' )
      :css( { [ 'border-color' ] = color } )
      -- Main Title
      :tag( 'div' )
         :addClass( 'coc-attack-strategy-title' )
         :wikitext( mw.title.getCurrentTitle().subpageText )
         :tag( 'span' )
            :addClass( 'coc-attack-strategy-subtitle' )
            :wikitext( ' (TH: ' .. townhall .. ', Trophies: ' .. trophies .. ')' )
            :done()
         :tag( 'span' )
            :addClass( 'coc-attack-strategy-instructions' )
            :wikitext( '[[Template:Attack Strategy/doc|(Instructions)]]' )
            :done()
         :done()
      -- Subtitle (creator, Town Hall level, trophy level)
      :tag( 'table' )
         :addClass( 'coc-attack-strategy-table' )
         :tag( 'tr' )
            :tag( 'th' )
               :wikitext( 'By:' )
               :done()
            :tag( 'td' )
               :wikitext( args[ 'wikiname' ] )
               :done()
            :done()
         :tag( 'tr' )
            :tag( 'th' )
               :wikitext( 'Town Hall Level:' )
               :done()
            :tag( 'td' )
               :wikitext( townhall )
               :done()
            :done()
         :tag( 'tr' )
            :tag( 'th' )
               :wikitext( 'Trophy Level:' )
               :done()
            :tag( 'td' )
               :wikitext( trophies )
               :done()
            :done()
         :done()
 
   -- Introduction
   if args[ 'introduction' ] then
      output
         :tag( 'table' )
            :addClass( 'coc-attack-strategy-table' )
            :tag( 'tr' )
               :tag( 'th' )
                  :wikitext( 'Introduction:' )
                  :done()
               :tag( 'td' )
                  :wikitext( args[ 'introduction' ] or '(No introduction)' )
                  :done()
               :done()
            :done()
   end
 
   -- Army composition
   if not args[ 'suppressarmycomposition' ] then
      local introClass   = args[ 'armyintro'  ] and '' or 'hidden'
      local housingClass = args[ 'minhousing' ] and '' or 'hidden'
      local spellhousingClass = args[ 'minspellhousing' ] and '' or 'hidden'
      local notesClass   = args[ 'armynotes'  ] and '' or 'hidden'
 
      output
         :tag( 'table' )
            :addClass( 'coc-attack-strategy-table' )
            :tag( 'tr' )
               :tag( 'th' )
                  :wikitext( 'Army Composition:' )
                  :done()
               :tag( 'td' )
                  -- Army composition intro, if any
                  :tag( 'div' )
                     :addClass( 'coc-attack-strategy-note' )
                     :addClass( introClass )
                     :wikitext( args[ 'armyintro' ] or '(No army intro)' )
                     :done()
                  -- Minimum housing space required, if specified
                  :tag( 'div' )
                     :addClass( 'coc-attack-strategy-note' )
                     :tag( 'span' )
                        :addClass( 'coc-attack-strategy-subheader' )
                        :addClass( housingClass )
                        :wikitext( 'Minimum Troop Housing Space Required: ' )
                        :done()
                     :tag( 'span' )
                        :addClass( 'coc-attack-strategy-entry' )
                        :addClass( housingClass )
                        :wikitext( args[ 'minhousing' ] or '(not specified)' )
                        :done()
                     :tag( 'span' )
                        :addClass( 'coc-attack-strategy-subheader' )
                        :addClass( spellhousingClass )
                        :wikitext( '<br/>Minimum Spell Housing Space Required: ' )
                        :done()
                     :tag( 'span' )
                        :addClass( 'coc-attack-strategy-entry' )
                        :addClass( spellhousingClass )
                        :wikitext( args[ 'minspellhousing' ] or '(not specified)' )
                        :done()
                     :done()
                  -- Insert army composition table here
                  :node( troopTable )
                  -- Army composition notes, if any
                  :tag( 'div' )
                     :addClass( 'coc-attack-strategy-note' )
                     :addClass( notesClass )
                     :wikitext( args[ 'armynotes' ] or '(No army notes)' )
                     :done()
                  :done()
               :done()
            :done()
   end
 
   -- Execution
   if not args[ 'suppressexecution' ] then
      local introClass   = args[ 'executionintro'  ] and '' or 'hidden'
      local notesClass   = args[ 'executionnotes'  ] and '' or 'hidden'
 
      output
         :tag( 'table' )
            :addClass( 'coc-attack-strategy-table' )
            :tag( 'tr' )
               :tag( 'th' )
                  :wikitext( 'Execution:' )
                  :done()
               :tag( 'td' )
                  -- Execution intro, if any
                  :tag( 'div' )
                     :addClass( 'coc-attack-strategy-note' )
                     :addClass( introClass )
                     :wikitext( args[ 'executionintro' ] or '(No execution intro)' )
                     :done()
                  -- Insert execution steps here
                  :node( executionSteps )
                  -- Execution notes, if any
                  :tag( 'div' )
                     :addClass( 'coc-attack-strategy-note' )
                     :addClass( notesClass )
                     :wikitext( args[ 'executionnotes' ] or '(No execution notes)' )
                     :done()
                  :done()
               :done()
            :done()
   end
 
   -- Conclusion
   if args[ 'conclusion' ] then
      output
         :tag( 'table' )
            :addClass( 'coc-attack-strategy-table' )
            :tag( 'tr' )
               :tag( 'th' )
                  :wikitext( 'Conclusion:' )
                  :done()
               :tag( 'td' )
                  :wikitext( args[ 'conclusion' ] or '(No conclusion)' )
                  :done()
               :done()
            :done()
   end
 
   --return frame:preprocess( '<nowiki>' .. tostring( output ) .. '</nowiki>' )
   return tostring( output )
end
 
function p.main( frame )
   local origArgs = frame:getParent().args
   local args = {}
   local name = ''
 
   p.frame = frame
 
   for k, v in pairs( origArgs ) do
      v = v:match( '^%s*(.-)%s*$' )
 
      if v ~= '' then
         args[ k ] = v
      end
   end
 
   if not args[ 'wikiname' ] then
      name = pageCreator( frame )
   else
      name = args[ 'wikiname' ]
   end
 
   if string.lower( name ) ~= 'anonymous' then
      name = '[[User:' .. name .. '|' .. name .. ']]'
   else
      name = titleCase( name )
   end
 
   args[ 'wikiname' ] = name
 
   if not args[ 'framecolorHTMLcolorcode' ] then
      args[ 'framecolorHTMLcolorcode' ] = '#6666BB'
   end
 
   return p.createAttackStrategy( args )
end
 
return p

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.