Модуль:TableOfChemicals: различия между версиями

мНет описания правки
м Серная кислота не существует в раздатчике химикатов. Это миф
Метки: с мобильного устройства из мобильной версии
 
(не показана 71 промежуточная версия 1 участника)
Строка 1: Строка 1:
p = {}
p = {}
p.chem = mw.text.jsonDecode(mw.title.new("User:IanComradeBot/chem_prototypes.json"):getContent())
p.react = mw.text.jsonDecode(mw.title.new("User:IanComradeBot/react_prototypes.json"):getContent())


local prototypes = mw.loadData("Module:Chemistry Lookup/data")
p.fillReactTable = function(frame)
p.chem = prototypes.chem
p.react = prototypes.react
 
p.fillTable = function(frame)
local out = {}
local out = ""
local out = ""
local group = frame.args.group
local templateArgs = {}
for _, reactPrototype in pairs(p.react) do
for _, chemPrototype in pairs(p.chem) do
if tablelength(reactPrototype.effects) ~= 0 then
local reactants = {}
if group == nil or chemPrototype.group == group then
local reactantTemplate = "%s [[#chem_%s|%s]]"
templateArgs.id = chemPrototype.id
templateArgs.name = chemPrototype.name
templateArgs.description = chemPrototype.desc .. " На вид " .. chemPrototype.physicalDesc .. "."
templateArgs.recipes_count = tablelength(chemPrototype.recipes)
for reactantId, reactantValue in pairs(reactPrototype.reactants) do
local firstReact = true
for _, reactId in pairs(chemPrototype.recipes) do
local reactPrototype = p.react[reactId]
local reactantChemData = p.chem[reactantId]
local reactants = {}
local reactantText = string.format(reactantTemplate, reactantValue.amount, reactantChemData.id,  reactantChemData.name)
for reactantId, reactantValue in pairs(reactPrototype.reactants) do
if reactantValue.catalyst then
reactantText = reactantText .. " (катализатор)"
local reactantChemData = p.chem[reactantId]
local reactantText = reactantValue.amount .. " " .. reactantChemData.name
table.insert(reactants, reactantText)
end
end
templateArgs.reactants = table.concat(reactants, "<br>")
table.insert(reactants, reactantText)
end
local templateArgs = {}
templateArgs.reactants = table.concat(reactants, "<br>")
local products = {}
local productTemplate = "%s [[#chem_%s|%s]]"
for productId, productAmount in pairs(reactPrototype.products) do
local products = {}
local productChemData = p.chem[productId]
for productId, productAmount in pairs(reactPrototype.products) do
local productText = string.format(productTemplate, productAmount, productChemData.id,  productChemData.name)
table.insert(products, productText)
local productChemData = p.chem[productId]
end
local productText = productAmount .. " " .. productChemData.name  
-- Эффекты реакции
table.insert(products, productText)
if tablelength(reactPrototype.effects) then
end
for _, effect in pairs(reactPrototype.effects) do
templateArgs.products = table.concat(products, "<br>")
if effect.description ~= "" then
table.insert(products, effect.description)
templateArgs.action = "смешать"
end
local template = "Строка_химического_вещества"
if firstReact then
template = "Первая_строка_химического_вещества"
firstReact = false
end
end
out = out .. frame:expandTemplate{ title = template, args = templateArgs}
end
end
templateArgs.products = table.concat(products, "<br>")
templateArgs.actions = getActions(reactPrototype)
local template = "Строка_реакции"
out = out .. frame:expandTemplate{ title = template, args = templateArgs}
end
end
end
end
return out
return out
end
end


p.fillEffectsTable = function(frame)
p.fillChemTable = function(frame)
local chemPrototypeId = frame.args.id
local out = ""
 
local group = frame.args.group
local out = "{|" -- Объявление таблицы (Условие | Эффект)
local groups
local rowTemplate = "|-\n| %s || %s" -- Шаблон строки таблицы
if group ~= nil and group ~= "" then
groups = mw.text.split(group, ",")
end
local effects = getEffects(chemPrototypeId)
local additional = frame.args.additional
if additional ~= nil and additional ~= "" then
local additionalChemPrototypeIds = mw.text.split(additional, ",")
for _, chemPrototypeId in pairs(additionalChemPrototypeIds) do
out = out .. fillChemistryRow(chemPrototypeId, frame)  
end
end
for _, effect in pairs(effects) do
for _, chemPrototype in pairs(p.chem) do
out = attachAsNewLine(out, string.format(rowTemplate, effect.condition, effect.effect))
if group == nil or group == "" or contains(groups, chemPrototype.group) then
out = out .. fillChemistryRow(chemPrototype.id, frame)  
end
end
end
out = attachAsNewLine(out, "|}") -- Конец таблицы
mw.log(out)
return out
return out
end
end


function getEffects(chemPrototypeId)
p.fillContentOfDispencer = function(frame)
local dispencer = frame.args.dispencer
local chemPrototype = p.chem[chemPrototypeId]
local effects = {}
local effect = {}
effect.condition = ""
effect.effect = p.geneffects(chemPrototypeId)
table.insert(effects, effect)
return effects
 
end
 
function tablelength(T)
  local count = 0
  for _ in pairs(T) do count = count + 1 end
  return count
end
 
function attachAsNewLine(originalStr, joinStr)
return originalStr .. "\n" .. joinStr
end
 
-- НЕ МОЕ
function p.geneffects(chemPrototypeId)
local frame = mw.getCurrentFrame()
local met = p.chem[chemPrototypeId].metabolisms
if met == nil then
return ""
end
local out = ""
local out = ""
for k, v in pairs(met) do
for _, chemId in pairs(dispencersContent()[dispencer]) do
out = out .. "<b>" .. k .. "</b> (" .. v.rate .. " единиц в секунду)\n" .. p.geneffectlist(v.effects, frame, v.rate)
out = out .. frame:expandTemplate{ title = "Кнопка_реагента", args = {chemId, "y"}}
end
end
return out
return out
end
end


function p.geneffectlist(effects, frame, rate)
function dispencersContent()
local out = ""
local content = {
for l, w in pairs(effects) do
["Химикатов"]={
-- Popup Message is ignored on purpose
"Aluminium","Carbon","Chlorine","Copper","Ethanol","Fluorine","Hydrogen","Iodine",
if w.id == "HealthChange" then
"Iron","Lithium","Mercury","Nitrogen","Oxygen","Phosphorus","Potassium","Radium",
out = out .. ":" .. p.genhealthchange(w, rate, frame) .. "\n"
"Silicon","Sodium","Sugar","Sulfur"
elseif w.id == "AdjustReagent" then
},
out = out .. ":" .. p.genadjustreagent(w, rate, frame) .. "\n"
["Соды"]={
elseif w.id == "FlammableReaction" then
"Water","Ice","Coffee","Cream","Tea","GreenTea","IcedTea","IcedGreenTea",
out = out .. ":" .. p.genflammablereaction(w, frame) .. "\n"
"Cola","SpaceMountainWind","DrGibb","SpaceUp","TonicWater","SodaWater","LemonLime","Sugar",
elseif w.id == "AdjustTemperature" then
"JuiceOrange","JuiceLime","JuiceWatermelon"
out = out .. ":" .. p.genadjusttemperature(w, frame) .. "\n"
},
elseif w.id == "GenericStatusEffect" then
["Алкоголя"]={
out = out .. ":" .. p.gengenericstatuseffect(w, frame) .. "\n"
"LemonLime","Sugar","JuiceOrange","JuiceLime","SodaWater","TonicWater","Beer","CoffeeLiqueur",
elseif w.id == "ExplosionReactionEffect" then
"Whiskey","Wine","Vodka","Gin","Rum","Tequila","Vermouth","Cognac",
out = out .. ":" .. p.genexplosionreactioneffect(w, frame) .. "\n"
"Ale","Mead"
elseif w.id == "FoamAreaReactionEffect" then
}
out = out .. ":" .. p.genfoamareareactioneffect(w, frame) .. "\n"
}
elseif w.id == "SmokeAreaReactionEffect" then
return content
out = out .. ":" .. p.gensmokeareareactioneffect(w, frame) .. "\n"
elseif w.id == "ModifyBleedAmount" then
out = out .. ":" .. p.genmodifybleedamount(w, frame) .. "\n"
end
end
return out
end
end


function p.gensmokeareareactioneffect(r, frame)
if r.conditions ~= nil then
conds = p.genconds(r.conditions, frame)
end
return frame:expandTemplate{ title = "SmokeAreaReactionEffect", args = { when = conds, prob = r.probability }}
end


function p.genfoamareareactioneffect(r, frame)
p.readArg = function(frame)
if r.conditions ~= nil then
return mw.text.nowiki(p.chem[frame.args[1]][frame.args[2]])
conds = p.genconds(r.conditions, frame)
end
return frame:expandTemplate{ title = "FoamAreaReactionEffect", args = { when = conds, prob = r.probability }}
end
end


function p.genexplosionreactioneffect(r, frame)
if r.conditions ~= nil then
conds = p.genconds(r.conditions, frame)
end
return frame:expandTemplate{ title = "ExplosionReactionEffect", args = { when = conds, prob = r.probability }}
end


function p.gengenericstatuseffect(r, frame)
function contains(list, x)
if r.conditions ~= nil then  
for _, v in ipairs(list) do
conds = p.genconds(r.conditions, frame)
if v == x then return true end
end
end
return frame:expandTemplate{ title = "GenericStatusEffect", args = { key = r.Key, type = r.Type, time = r.Time, refresh = r.Refresh, when = conds, prob = r.probability }}
return false
end
end


function p.genadjusttemperature(r, frame)
fillChemistryRow = function(chemPrototypeId, frame)
if r.conditions ~= nil then  
conds = p.genconds(r.conditions, frame)
local out = ""
local chemPrototype = p.chem[chemPrototypeId]
local templateArgs = {}
templateArgs.id = chemPrototype.id
templateArgs.name = chemPrototype.name
templateArgs.description = chemPrototype.desc .. " На вид " .. chemPrototype.physicalDesc .. "."
templateArgs.color = chemPrototype.color
templateArgs.textColor = chemPrototype.textColor
templateArgs.effects = getEffects(chemPrototype.id)
templateArgs.recipes_count = tablelength(chemPrototype.recipes)
templateArgs.reactants = ""
templateArgs.products = ""
templateArgs.action = ""
if templateArgs.recipes_count == 0 then
templateArgs.recipes_count = 1 -- Для заполнения параметра rowspan строки
out = out .. frame:expandTemplate{ title = "Первая_строка_химического_вещества", args = templateArgs}
else
local firstReact = true
for _, reactId in pairs(chemPrototype.recipes) do
local reactPrototype = p.react[reactId]
local reactants = {}
local reactantTemplate = "%s [[#chem_%s|%s]]"
for reactantId, reactantValue in pairs(reactPrototype.reactants) do
local reactantChemData = p.chem[reactantId]
local reactantText = string.format(reactantTemplate, reactantValue.amount, reactantChemData.id,  reactantChemData.name)
if reactantValue.catalyst then
reactantText = reactantText .. " (катализатор)"
end
table.insert(reactants, reactantText)
end
templateArgs.reactants = table.concat(reactants, "<br>")
local products = {}
local productTemplate = "%s [[#chem_%s|%s]]"
for productId, productAmount in pairs(reactPrototype.products) do
local productChemData = p.chem[productId]
local productText = string.format(productTemplate, productAmount, productChemData.id, productChemData.name)
table.insert(products, productText)
end
-- Эффекты реакции
if tablelength(reactPrototype.effects) then
for _, effect in pairs(reactPrototype.effects) do
if effect.description ~= "" then
table.insert(products, effect.description)
end
end
end
templateArgs.products = table.concat(products, "<br>")
templateArgs.actions = getActions(reactPrototype)
local template = "Строка_химического_вещества"
if firstReact then
template = "Первая_строка_химического_вещества"
firstReact = false
end
out = out .. frame:expandTemplate{ title = template, args = templateArgs}
end
end
end
return frame:expandTemplate{ title = "AdjustTemperature", args = { amount = r.Amount, when = conds, prob = r.probability }}
return out
end
end


function p.genflammablereaction(r, frame)
getActions = function(reactPrototype)
if r.conditions ~= nil then  
conds = p.genconds(r.conditions, frame)
local actions = {}
-- Температура
local minMaxTemplate = "выше %sК и ниже %sК"
local minTemplate = "выше %sК"
local maxTemplate = "ниже %sК"
local hasMin = reactPrototype.minTemp ~= 0
local hasMax = reactPrototype.hasMax
local tempString = ""
if hasMax and hasMin then
tempString = string.format(minMaxTemplate, reactPrototype.minTemp, reactPrototype.maxTemp)
elseif hasMin then
tempString = string.format(minTemplate, reactPrototype.minTemp)
elseif hasMax then
tempString = string.format(maxTemplate, reactPrototype.maxTemp)
end
end
return frame:expandTemplate{ title = "FlammableReaction", args = { multiplier = r.Multiplier, when = conds, prob = r.probability }}
end
for _, mixingCategory in pairs(reactPrototype.mixingCategories) do
 
local image = getMixingImage(mixingCategory.id)
function p.genflammablereaction(r, frame)
if image ~= nil then
if r.conditions ~= nil then
table.insert(actions, string.format("[[File:%s|32px|link=]]", image)) -- Картинка
conds = p.genconds(r.conditions, frame)
end
table.insert(actions, mixingCategory.name .. " " .. tempString) -- Название
end
end
return frame:expandTemplate{ title = "FlammableReaction", args = { multiplier = r.Multiplier, when = conds, prob = r.probability }}
return table.concat(actions, "<br>")
end
end


function p.genadjustreagent(r, rate, frame)
getMixingImage = function(mixingId)
if r.conditions ~= nil then
conds = p.genconds(r.conditions, frame)
local images = {
end
["DummyMix"] = "Beaker.png", -- Смешивание
return frame:expandTemplate{ title = "AdjustReagent", args = { amount = r.Amount, reagent = r.Reagent, when = conds, prob = r.probability }}
["DummyGrind"] = "Blender.png", -- Измельчение
["DummyJuice"] = "Blender.png", -- Выжимка
["DummyCondense"] = "Конденсатор газа.png",  
["Centrifuge"] = "Центрифуга.png",
["Electrolysis"] = "Электролизер.png",
["Holy"] = "Библия.png"
}
return images[mixingId]
end
end


function p.genmodifybleedamount(r, frame)
function getEffects(chemPrototypeId)
if r.conditions ~= nil then  
conds = p.genconds(r.conditions, frame)
local effects = ""
local chemPrototype = p.chem[chemPrototypeId]
if chemPrototype.metabolisms == nil then
return effects
end
end
return frame:expandTemplate{ title = "ModifyBleedAmount", args = { amount = r.Amount, when = conds, prob = r.probability }}
end
for metabolismGroupKey, metabolismGroup in pairs(chemPrototype.metabolisms) do
 
effects = attachAsNewLine(effects, string.format("* %s (%s единиц в секунду)", metabolismGroupKey, metabolismGroup.rate))
function p.genhealthchange(h, rate, frame)
for _, effect in pairs(metabolismGroup.effects) do
local healst = {}
if effect.description ~= "" then
local dealst = {}
effects = attachAsNewLine(effects, "** "..effect.description)
local r = 1.0 / rate
end
if h.damage.types ~= nil then
for k, v in pairs(h.damage.types) do
if v < 0 then
healst[k] = v * r
else
dealst[k] = v * r
end
end
end
end
end
if h.damage.groups ~= nil then
for k, v in pairs(h.damage.groups) do
if v < 0 then
healst[k] = v * r
else
dealst[k] = v * r
end
end
end
local heals = hchangelist(healst, frame)
return effects
local deals = hchangelist(dealst, frame)
 
local conds = nil
if h.conditions ~= nil then
conds = p.genconds(h.conditions, frame)
end
return frame:expandTemplate{ title = "HealthChange", args = { heals = heals, deals = deals, when = conds, prob = h.probability }}
end
end


function hchangelist(l, frame)
function tablelength(T)
out = ""
  local count = 0
local len = tablelength(l)
  for _ in pairs(T) do count = count + 1 end
local i = 0
  return count
for k, v in pairs(l) do
i = i + 1
if len == i and i ~= 1 then
out = out .. ", and "
elseif i ~= 1 then
out = out .. ", "
end
out = out .. hchange(k, v, frame)
end
return out
end
end


-- So we can make it fancy later
function attachAsNewLine(originalStr, joinStr)
function hchange(ty, amnt, frame)
return originalStr .. "\n" .. joinStr
return frame:expandTemplate{ title = "HealthModifier", args = { adj = amnt, kind = ty } }
end
 
function p.genconds(conds, frame)
out = ""
local len = tablelength(conds)
local i = 0
for k, v in pairs(conds) do
i = i + 1
if len == i and i ~= 1 then
out = out .. ", and "
elseif i ~= 1 then
out = out .. ", "
end
out = out .. p.gencond(v, frame)
end
return out
end
 
function p.gencond(c, frame)
if c.id == "TotalDamage" then
return frame:expandTemplate{ title = "TotalDamage", args = { min = c.Min, max = c.Max } }
elseif c.id == "ReagentThreshold" then
return frame:expandTemplate{ title = "ReagentThreshold", args = { min = c.Min, max = c.Max, reagent = c.Reagent } }
elseif c.id == "OrganType" then
return frame:expandTemplate{ title = "OrganType", args = { shouldhave = c.ShouldHave, type = c.Type } }
elseif c.id == "Temperature" then
return frame:expandTemplate{ title = "Temperature", args = {  min = c.Min, max = c.Max } }
end
return ""
end
end
-- НЕ МОЕ


return p
return p