Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fill Type Factor Synchronization #16

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ChallengeModConfig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<IgnoredFillType>HORSE_DUN</IgnoredFillType>
<IgnoredFillType>CHICKEN</IgnoredFillType>
<IgnoredFillType>CHICKEN_ROOSTER</IgnoredFillType>
<IgnoredFillType>UNKNOWN</IgnoredFillType>
</IgnoredFillTypes>
</VictoryPoints>

Expand Down
20 changes: 13 additions & 7 deletions gui/ScoreBoardFrame.lua
Original file line number Diff line number Diff line change
Expand Up @@ -216,20 +216,20 @@ function ScoreBoardFrame:onGuiSetupFinished()
}
}
self.managers = {
function(...)
function(detailed, ...)
return self.victoryPointManager:getList(...)
end,
function(...)
function(detailed, ...)
local sx, ix = self.leftList:getSelectedPath()
return ix == 1 and self.ruleManager:getList() or self.victoryPointManager:getList()
return ix == 1 and self.ruleManager:getList() or self.victoryPointManager:getList(detailed or false)
end,
}

self.numSections = {
[self.leftList] = function() return self.NUM_LEFT_SECTIONS end,
[self.rightList] = function()
local sx, ix = self.leftList:getSelectedPath()
return self.managers[sx]():getNumberOfElements()
return self.managers[sx](true):getNumberOfElements()
end,
[self.changelogList] = function ()
return 1
Expand Down Expand Up @@ -298,6 +298,7 @@ function ScoreBoardFrame:updateMenuButtons()
end

function ScoreBoardFrame:getNumberOfSections(list)
print("number of sections: " .. tostring(self.numSections[list]()))
return self.numSections[list]()
end

Expand All @@ -320,7 +321,7 @@ function ScoreBoardFrame:getNumberOfItemsInSection(list, section)
if list:getIsVisible() then
local farmId = self:getCurrentFarmId()
local sx, ix = self.leftList:getSelectedPath()
local l = self.managers[sx](farmId)
local l = self.managers[sx](nil, farmId)

if l == nil then
CmUtil.debug("Categories for not found %d", section)
Expand All @@ -329,6 +330,7 @@ function ScoreBoardFrame:getNumberOfItemsInSection(list, section)
return 0
end

print("number of items in section " .. tostring(section) .. ": " .. tostring(l:getNumberOfElements(section)))
return l:getNumberOfElements(section)
else
return 0
Expand Down Expand Up @@ -451,7 +453,7 @@ function ScoreBoardFrame:getElement(section, index)
end
local sx, ix = self.leftList:getSelectedPath()
local farmId = self:getCurrentFarmId()
local list = self.managers[sx](farmId)
local list = self.managers[sx](nil, farmId)
if list == nil then
CmUtil.debug("Element not found for (%s|%s).", tostring(section), tostring(index))
printCallstack()
Expand Down Expand Up @@ -680,7 +682,11 @@ end
function ScoreBoardFrame:onTextInputChangeValue(text, clickOk, element)
if clickOk then
if text ~= nil and element ~= nil then
element:onTextInput(text)
local sx, ix = self.leftList:getSelectedPath()
local list = self.managers[sx]()
for _, category in pairs(list:getElements()) do
self.victoryPointManager:updateVictoryPoint(category:getName(), element:getName(), tonumber(text))
end
self:updateLists()
end
end
Expand Down
16 changes: 15 additions & 1 deletion scripts/Rules.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ local Rule_mt = Class(Rule, ScoreBoardElement)
function Rule.new(name, default, title, valuesData, custom_mt)
local self = ScoreBoardElement.new(name, title, custom_mt or Rule_mt)
self.currentIx = default or 1
self.title = title
self.values = {}
self.texts = {}
if valuesData then
Expand All @@ -27,6 +26,21 @@ function Rule.createFromXml(data)
return Rule.new(data.name, data.default, data.title, data.values)
end

function Rule:clone()
local data = {}

assert(#self.values == #self.texts)
for i = 1, #self.self.values do
data:insert({
value = self.values[i],
text = self.texts[i]
}
)
end

return Rule.new(self.name, self.currentIx, self.title, data)
end

function Rule:getText()
if next(self.texts) ~= nil and self.texts[self.currentIx] then
return self.texts[self.currentIx]
Expand Down
32 changes: 17 additions & 15 deletions scripts/ScoreBoardCategory.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ function ScoreBoardCategory:getTitle()
end

function ScoreBoardCategory:getName()
return self.name
return self.name
end

function ScoreBoardCategory:setParent(parent, id)
self.parent = parent
self.parent = parent
self.id = id
end

Expand All @@ -34,24 +34,28 @@ function ScoreBoardCategory:getParent()
end

function ScoreBoardCategory:addElement(element, ix)
if ix ~= nil then
if ix ~= nil then
table.insert(self.elements, ix, element)
else
table.insert(self.elements, element)
end
element:setParent(self, ix or #self.elements)
end

function ScoreBoardCategory:removeElement(idx)
return table.remove(self.elements, idx)
end

function ScoreBoardCategory:getElement(index)
return index ~= nil and self.elements[index] or self
end

function ScoreBoardCategory:getElementByName(name)
if name == nil then
if name == nil then
return self
end
for _, element in pairs(self.elements) do
if element:getName() == name then
for _, element in pairs(self.elements) do
if element:getName() == name then
return element
end
end
Expand All @@ -70,15 +74,13 @@ function ScoreBoardCategory:clear()
end

function ScoreBoardCategory:onTextInput(value)

end

function ScoreBoardCategory:isTextInputAllowed()
return false
end

function ScoreBoardCategory:onClick()

end

function ScoreBoardCategory:saveToXMLFile(xmlFile, baseXmlKey, ix)
Expand All @@ -103,15 +105,15 @@ end

function ScoreBoardCategory:count()
local value = 0
for _, element in pairs(self.elements) do
for _, element in pairs(self.elements) do
value = value + element:count()
end
return value
end

function ScoreBoardCategory:clone(...)
local category = ScoreBoardCategory.new(self.name, self.title)
for i, element in ipairs(self.elements) do
for i, element in ipairs(self.elements) do
local e = element:clone(...)
category:addElement(e)
end
Expand All @@ -129,23 +131,23 @@ end

function ScoreBoardCategory:applyValues(staticList)
local element = staticList:getElementByName(self.name)
if element then
for i, e in ipairs(self.elements) do
if element then
for i, e in ipairs(self.elements) do
e:applyValues(element)
end
end
end
end

function ScoreBoardCategory:writeStream(streamId, ...)
streamWriteUInt8(streamId, #self.elements)
for i, element in ipairs(self.elements) do
for i, element in ipairs(self.elements) do
streamWriteString(streamId, element:getName())
element:writeJoinStream(streamId, ...)
end
end

function ScoreBoardCategory:readStream(streamId, ...)
for i= 1, streamReadUInt8(streamId) do
for i= 1, streamReadUInt8(streamId) do
local elementName = streamReadString(streamId)
local e = self:getElementByName(elementName)
e:readJoinStream(streamId, ...)
Expand Down
4 changes: 4 additions & 0 deletions scripts/ScoreBoardElement.lua
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ function ScoreBoardElement.loadFromXMLFile(category, xmlFile, baseXmlKey)
end)
end

function ScoreBoardElement:clone()
return ScoreBoardElement.new(self.name, self.title)
end

function ScoreBoardElement:applyValues()
end

Expand Down
52 changes: 46 additions & 6 deletions scripts/ScoreBoardList.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ function ScoreBoardList.new(name, titles, elements, custom_mt)
self.titles = titles
self.name = name
self.elements = elements or {}
self.nameToIndex = {}

return self
end

Expand Down Expand Up @@ -59,27 +61,37 @@ end
function ScoreBoardList:addElement(element, ix)
if ix ~= nil then
table.insert(self.elements, ix, element)
self.nameToIndex[element:getName()] = ix
else
table.insert(self.elements, element)
self.nameToIndex[element:getName()] = #self.elements
end
element:setParent(self, ix or #self.elements)
end

function ScoreBoardList:removeElement(idx)
return table.remove(self.elements, idx)
end

function ScoreBoardList:getElement(index, ...)
return self.elements[index]:getElement(...)
end

function ScoreBoardList:getElementByName(name, ...)
for _, element in pairs(self.elements) do
for _, element in pairs(self.elements) do
if element:getName() == name then
return element:getElementByName(...)
end
end
end

function ScoreBoardList:getElementIndexByName(name)
return self.nameToIndex[name]
end

function ScoreBoardList:setElementByName(name, newCategory)
for ix, element in pairs(self.elements) do
if element:getName() == name then
for ix, element in pairs(self.elements) do
if element:getName() == name then
self.elements[ix] = newCategory
end
end
Expand All @@ -95,14 +107,42 @@ end

function ScoreBoardList:count()
local value = 0
for _, element in pairs(self.elements) do
for _, element in pairs(self.elements) do
value = value + element:count()
end
return value
end

function ScoreBoardList:applyValues(staticList)
for i, element in ipairs(self.elements) do
for i, element in ipairs(self.elements) do
element:applyValues(staticList)
end
end
end

function ScoreBoardList:clone()
local list = ScoreBoardList.new(self.name, self.titles)

for _, element in pairs(self.elements) do
local e = element:clone()
list:addElement(e)
end

return list
end

--- Combines all specified elements into newElement and add newElement to self.elements
--- @param newElement ScoreBoardCategory
--- @param ... string names of all elements to combine
function ScoreBoardList:mergeElements(newElement, ...)
for _, name in pairs({...}) do
local idx = self:getElementIndexByName(name)
local element = self:getElementByName(name)
for _, elem in pairs(element:getElements()) do
newElement:addElement(elem)
end

self.elements[idx] = nil
self.nameToIndex[name] = nil
end
self:addElement(newElement)
end
12 changes: 4 additions & 8 deletions scripts/VictoryPoint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ function VictoryPoint.new(name, value, factor, title, inputText, unitTextFunc, d
local self = ScoreBoardElement.new(name, title, custom_mt or VictoryPoint_mt)
self.value = value
self.factor = factor
self.title = title
self.inputText = inputText
self.dependency = dependency
self.unitTextFunc = unitTextFunc
Expand All @@ -34,6 +33,10 @@ function VictoryPoint.createFromXml(data, value)
return VictoryPoint.new(data.name, value, data.default, data.title, data.inputText, data.unitTextFunc, data.dependency)
end

function VictoryPoint:clone()
return VictoryPoint.new(self.name, self.value, self.factor, self.title, self.inputText, self.unitTextFunc, self.dependency)
end

function VictoryPoint:getValue()
if self.staticElement == nil and not self.dependency then
return 0
Expand Down Expand Up @@ -104,10 +107,6 @@ function VictoryPoint:isTextInputAllowed()
return not self.dependency
end

function VictoryPoint:clone(farmId, farm)

end

function VictoryPoint:__tostring()
return string.format("title: %s, value/factor: %.1f", self.title, self.value / self.factor)
end
Expand Down Expand Up @@ -140,7 +139,4 @@ function VictoryPoint.readStream(categoryName, categoryId, name, value, ix)
local element = g_victoryPointManager:getList():getElementByName(categoryName, name)
element:setFactor(value)
return element
print("getList: " .. tostring(g_victoryPointManager:getList()))
print("get element: " .. tostring(g_victoryPointManager:getList():getElementByName(categoryName, name)))
print("setFactor: " .. tostring(g_victoryPointManager:getList():getElementByName(categoryName, name):setFactor(value)))
end
19 changes: 18 additions & 1 deletion scripts/VictoryPointManager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ function VictoryPointManager:loadConfigData(xmlFile, baseXmlKey)
end)

self.staticPointList = self:getNewPointList()
self.fillTypePointList = self.staticPointList:clone()
local storageCategory = self.fillTypePointList:getElementByName("storage")
storageCategory = ScoreBoardCategory.new(storageCategory:getName(), storageCategory:getTitle())
self.fillTypePointList:mergeElements(storageCategory, "storage", "bales", "pallets")
end

function VictoryPointManager:saveToXMLFile(xmlFile, baseXmlKey)
Expand Down Expand Up @@ -235,8 +239,21 @@ function VictoryPointManager:update()
end
end

function VictoryPointManager:updateVictoryPoint(categoryName, pointName, factor)
assert(type(factor) == "number", "Error: Only numbers are allowed as factors")
local point = self.staticPointList:getElementByName(categoryName, pointName)
if point ~= nil then
point:setFactor(factor)
end

point = self.fillTypePointList:getElementByName(categoryName, pointName)
if point ~= nil then
point:setFactor(factor)
end
end

function VictoryPointManager:getList(farmId)
return farmId ~= nil and self.pointList[farmId] or self.staticPointList
return farmId ~= nil and self.pointList[farmId] or self.fillTypePointList
end

function VictoryPointManager:getListByName()
Expand Down