Advertisement
darsovit

items_to_chars_builder.lua

Feb 23rd, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.90 KB | None | 0 0
  1. require('chat')
  2. require('lists')
  3. require('sets')
  4. require('tables')
  5. require('strings')
  6.  
  7. local json = require('json')
  8. local file = require('files')
  9. local slips = require('slips')
  10. local res   = require('resources')
  11. local texts = require('texts')
  12.  
  13. local relative_depth_to_addons_findAll = '../../..'
  14.  
  15. local storages_path       = relative_depth_to_addons_findAll..'/addons/findAll/data/storages.json'
  16. local items_to_chars_path = relative_depth_to_addons_findAll..'/addons/findAll/data/items_to_chars.json'
  17.  
  18. local storages_file = file.new(storages_path)
  19. if not storages_file:exists() then
  20.     windower.add_to_chat( 8, "Unable to find storages path: "..storages_path )
  21.     return false
  22. else
  23.     windower.add_to_chat( 8, "Found storages path: "..storages_path )
  24. end
  25.  
  26. local global_storages = json.read( storages_file )
  27.  
  28. if global_storages == nil then
  29.     windower.add_to_chat(8, "Unable to load json file" )
  30.     return false
  31. end
  32.  
  33. local item_to_char_storage_table = T{}
  34.  
  35. for character_name, storages in pairs(global_storages) do
  36.     for storage_name, storage_list in pairs(storages) do
  37.         if ( storage_name ~= 'gil' ) then
  38.             for item_id, item_quantity in pairs(storage_list) do
  39.                 if item_to_char_storage_table[item_id] == nil then
  40.                     item_to_char_storage_table[item_id] = T{}
  41.                     item_to_char_storage_table[item_id][character_name] = T{}
  42.                     item_to_char_storage_table[item_id][character_name][storage_name] = item_quantity
  43.                 else
  44.                     if item_to_char_storage_table[item_id][character_name] == nil then
  45.                         item_to_char_storage_table[item_id][character_name] = T{}
  46.                         item_to_char_storage_table[item_id][character_name][storage_name] = item_quantity
  47.                     else
  48.                         if item_to_char_storage_table[item_id][character_name][storage_name] == nil then
  49.                             item_to_char_storage_table[item_id][character_name][storage_name] = item_quantity
  50.                         else
  51.                             item_to_char_storage_table[item_id][character_name][storage_name] = item_quantity + item_to_char_storage_table[item_id][character_name][storage_name]
  52.                         end
  53.                     end
  54.                 end
  55.             end
  56.         end
  57.     end
  58. end
  59.  
  60. local items_to_chars_json = L{}
  61.  
  62. for item_id, character_storage_tables in pairs(item_to_char_storage_table) do
  63.     -- magic +0 to convert string to number, for indexing res.items
  64.     local resource_item = res.items[ item_id + 0 ]
  65.     local characters_json = L{}
  66.     for character, storage_tables in pairs( character_storage_tables ) do
  67.         local storages_json = L{}
  68.         for storage, quantity in pairs( storage_tables ) do
  69.             storages_json:append('"'..storage..'":'..quantity)
  70.         end
  71.         characters_json:append('"'..character..'":{'..storages_json:concat(',')..'}')
  72.     end
  73.     items_to_chars_json:append('"'..resource_item['en']..'":{'..characters_json:concat(',')..'}')
  74. end
  75.  
  76. local new_data_file = file.new( items_to_chars_path )
  77.  
  78. if not new_data_file:exists() then
  79.     new_data_file:create()
  80. end
  81.  
  82. new_data_file:write('{'..items_to_chars_json:concat(',\n')..'}')
  83.  
  84. return true
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement