Skip to content

Commit

Permalink
Improve Tradeships.lua
Browse files Browse the repository at this point in the history
  • Loading branch information
Gliese852 committed Feb 6, 2021
1 parent 8eb0854 commit 31d8931
Show file tree
Hide file tree
Showing 9 changed files with 1,288 additions and 383 deletions.
210 changes: 210 additions & 0 deletions data/libs/MyDebug.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
local utf8 = require 'utf8'
return {
makeSummaryTable = function(tbl, sum_key, keys)
local result = {}
local ind = {}
local obj_count = {}
local obj_rem = {}
for _, row in ipairs(tbl) do
group = row[sum_key]
if not ind[group] then
ind[group] = {}
obj_count[group] = {}
obj_rem[group] = {}
end
for _, key in ipairs(keys) do
local val = row[key]
-- if it is not number, just count
if type(val) ~= "number" then
if not obj_count[group][row[key]] then
obj_count[group][row[key]] = 1
obj_rem[group][key] = val
val = 1
else
val = 0
end
end
if not ind[group][key] then
ind[group][key] = val
else
ind[group][key] = ind[group][key] + val
end
end
end
for k,v in pairs(ind) do
v[sum_key] = k
for k1, v1 in pairs(v) do
if v1 == 1 then
v[k1] = obj_rem[k][k1]
end
end
table.insert(result, v)
end
return result
end,
printDataTable = function(tbl, onlykeys, totals)
local sep = "|"
local function spaces(n)
return string.rep(" ", n)
end
if #tbl == 0 then
print "<empty table>"
return
end
local keys = {}
local tostrs = {}
if onlykeys then
for _, v in ipairs(onlykeys) do
table.insert(keys, v[1])
tostrs[v[1]] = v[2] or tostring
end
else
for k,_ in pairs(tbl[1]) do
table.insert(keys, k)
tostrs[k] = tostring
end
end

-- uniquie key to store id
-- just sum all the given keys
local uniq_id = ""
for _, key in ipairs(keys) do
uniq_id = uniq_id .. key .. "+"
end
-- adding total's column
table.insert(keys, 1, uniq_id)
tostrs[uniq_id] = tostring
for i,row in ipairs(tbl) do
row[uniq_id] = i
end

local function avr_counter()
local sum = 0
local count = 1
return function(a, b)
if count == 1 then sum = a end
count = count + 1
sum = sum + b
return sum / count
end
end

average = avr_counter()

local total_funcs = {
{"sum", function(a, b)
return a + b
end
},
{"max", math.max},
{"avr", average}}

-- calculate totals
local accums = {}
local akk_count = 0
local akk_tbl = {}
if totals then
for _, total_func in ipairs(total_funcs) do
local fn_name = total_func[1]
local fn = total_func[2]
if totals[fn_name] then
accums[fn] = {}
for _, key in ipairs(totals[fn_name]) do
accums[fn][key] = tbl[1][key] --initial value is first item in row
end
end
end
-- accumulating total values
for i = 2, #tbl do
for fn, fkeys in pairs(accums) do
for key, _ in pairs(fkeys) do
accums[fn][key] = fn(accums[fn][key], tbl[i][key])
end
end
end
-- adding totals to general table
-- order must be observed
for _, total_func in ipairs(total_funcs) do
local fn_name = total_func[1]
local fn = total_func[2]
if accums[fn] then
accums[fn][uniq_id] = fn_name
table.insert(akk_tbl, accums[fn])
akk_count = akk_count + 1
end
end
end

local texttbl = {}
-- first row - keys
texttbl[1] = {}
for _,k in ipairs(keys) do
if k == uniq_id then
texttbl[1][k] = "#"
else
texttbl[1][k] = k
end
end

-- convert to text
for _, tabl in ipairs({tbl, akk_tbl}) do
for i, _ in ipairs(tabl) do
local row = {}
for _, key in ipairs(keys) do
if tabl[i][key] then
row[key] = tostrs[key](tabl[i][key])
else
row[key] = "-"
end
end
table.insert(texttbl, row)
end
end

-- calculate widths
local widths = {}
for _,k in ipairs(keys) do
widths[k] = 0
end
for _, row in ipairs(texttbl) do
for _,k in ipairs(keys) do
widths[k] = math.max(utf8.len(row[k]), widths[k])
end
end
-- create underline
local total_width = string.len(sep)
for _,v in pairs(widths) do
total_width = total_width + v + 2 + string.len(sep)
end
local underline = string.rep("-", total_width)

-- update strings
for _, row in ipairs(texttbl) do
for _,k in ipairs(keys) do
row[k] = row[k] .. spaces(widths[k] - utf8.len(row[k]))
end
end

function print_line(i)
local line = sep
for _,k in ipairs(keys) do
line = line .. " " .. texttbl[i][k] .. " " .. sep
end
print(line)
end
-- print strings
print(underline)
print_line(1)
print(underline)
for i = 2,#texttbl - akk_count do
print_line(i)
end
print(underline)
if akk_count > 0 then
for i = #texttbl - akk_count + 1, #texttbl do
print_line(i)
end
print(underline)
end
end
}
30 changes: 30 additions & 0 deletions data/libs/Ship.lua
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,36 @@ Ship.RemoveEquip = function (self, item, count, slot)
return ret
end

--
-- Method: GetCargo
--
-- Build a table where the keys will be the names of the cargo, and the values
-- will be their number
--
-- > cargo = ship:GetCargo()
--
--
-- Return:
--
-- The table where the keys will be the names of the cargo, and the values
-- will be their number
--
-- Availability:
--
-- 2021
--
-- Status:
--
-- experimental
--
function Ship:GetCargo()
local count = {}
for _, et in pairs(self:GetEquip("cargo")) do
if not count[et] then count[et] = 0 end
count[et] = count[et]+1
end
return count
end

--
-- Method: IsHyperjumpAllowed
Expand Down
Loading

0 comments on commit 31d8931

Please sign in to comment.