itemUtil.lua 913 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. --[[
  2. Descripttion:
  3. version:
  4. Author: Neo,Huang
  5. Date: 2022-07-04 11:24:54
  6. LastEditors: Neo,Huang
  7. LastEditTime: 2022-07-05 10:11:23
  8. --]]
  9. local root = {}
  10. -- 合并列表 - 物品ID不重复
  11. function root:items_merge(dst, src)
  12. if is_empty(src) then
  13. return dst
  14. end
  15. for k, v in ipairs(src) do
  16. dst = root.add_item_to_items(dst, v)
  17. end
  18. return dst
  19. end
  20. -- 合并 - 物品ID不重复
  21. function root:add_item_to_items(dst, item)
  22. if is_empty(item) then
  23. return dst
  24. end
  25. local items = table.copy(dst or {}, true)
  26. local isMatch = false
  27. for k, v in ipairs(items) do
  28. if v.id == item.id then
  29. if v.count == nil then
  30. v.count = 0
  31. end
  32. v.count = v.count + item.count
  33. isMatch = true
  34. end
  35. end
  36. if not isMatch then
  37. table.insert(items, item)
  38. end
  39. return items
  40. end
  41. return root