Advertisement
Michcol

CreateStats

Dec 13th, 2023
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.46 KB | Source Code | 0 0
  1. function CreateStats()
  2.     local createStats = {
  3.         loadedVersionItems = 0;
  4.         ownParser = false;
  5.  
  6.         _loadClientVersionItems = nil;
  7.         _checkLootTextMessage = nil;
  8.  
  9.         init = function(self)
  10.             self._loadClientVersionItems = function() self:loadClientVersionItems() end
  11.             self._checkLootTextMessage = function(messageMode, message) self:checkLootTextMessage(messageMode, message) end
  12.  
  13.             connect(g_game, { onClientVersionChange = self._loadClientVersionItems })
  14.             connect(g_game, { onTextMessage = self._checkLootTextMessage })
  15.  
  16.             if (self.loadedVersionItems == 0 and g_game.getClientVersion() ~= 0) or (g_game.getClientVersion() ~= 0 and self.loadedVersionItems ~= g_game.getClientVersion()) then
  17.                 self:loadClientVersionItems()
  18.             end
  19.         end;
  20.  
  21.         terminate = function(self)
  22.             disconnect(g_game, { onClientVersionChange = self._loadClientVersionItems })
  23.             disconnect(g_game, { onTextMessage = self._checkLootTextMessage })
  24.         end;
  25.  
  26.         -- Load items
  27.  
  28.         loadClientVersionItems = function(self)
  29.             local version = g_game.getClientVersion()
  30.  
  31.             if version ~= self.loadedVersionItems then
  32.                 if not directoryExists('items_versions/' .. version) then
  33.                     pwarning("Directory: items_versions/" .. version .. "/ doesn't exist!")
  34.                     pwarning("Add " .. version .. " directory to items_versions/ with correct version items.otb and items.xml!")
  35.                     self.loadedVersionItems = 0
  36.                     g_modules.getModule('loot_stats'):unload()
  37.                     modules.client_modulemanager.refreshLoadedModules()
  38.                     return
  39.                 end
  40.  
  41.                 if not fileExists('items_versions/' .. version .. '/items.otb') then
  42.                     pwarning("File: items_versions/" .. version .. "/ doesn't exist!")
  43.                     pwarning("Add correct version items.otb to items_versions/" .. version .. "/!")
  44.                     self.loadedVersionItems = 0
  45.                     g_modules.getModule('loot_stats'):unload()
  46.                     modules.client_modulemanager.refreshLoadedModules()
  47.                     return
  48.                 end
  49.  
  50.                 if not fileExists('items_versions/' .. version .. '/items.xml') then
  51.                     pwarning("File: items_versions/" .. version .. " doesn't exist!")
  52.                     pwarning("Add correct version items.xml to items_versions/" .. version .. "/!")
  53.                     self.loadedVersionItems = 0
  54.                     g_modules.getModule('loot_stats'):unload()
  55.                     modules.client_modulemanager.refreshLoadedModules()
  56.                     return
  57.                 end
  58.  
  59.                 loadOtb('items_versions/' .. version .. '/items.otb')
  60.                 loadXml('items_versions/' .. version .. '/items.xml')
  61.                 self:checkParserType()
  62.  
  63.                 self.loadedVersionItems = version
  64.             end
  65.         end;
  66.  
  67.         checkParserType = function(self)
  68.             if g_things.findItemTypeByPluralName then
  69.                 self.ownParser = false
  70.             else
  71.                 self.ownParser = ItemsXML()
  72.                 self.ownParser:parseItemsXML('items_versions/' .. g_game.getClientVersion() .. '/items.xml')
  73.             end
  74.         end;
  75.  
  76.         -- Convert plural to singular
  77.  
  78.         convertPluralToSingular = function(self, searchWord)
  79.             if not self.ownParser then
  80.                 local item = g_things.findItemTypeByPluralName(searchWord)
  81.                 if not item:isNull() then
  82.                     return item:getName()
  83.                 else
  84.                     return false
  85.                 end
  86.             else
  87.                 return self.ownParser:convertPluralToSingular(searchWord)
  88.             end
  89.         end;
  90.  
  91.         -- Parse message logs
  92.  
  93.         returnPluralNameFromLoot = function(lootMonsterName, itemWord)
  94.             for a,b in pairs(store.lootStatsTable[lootMonsterName].loot) do
  95.                 if b.plural == itemWord then
  96.                     return a
  97.                 end
  98.             end
  99.  
  100.             return false
  101.         end;
  102.  
  103.         checkLootTextMessage = function(self, messageMode, message)
  104.             if self.loadedVersionItems == 0 then
  105.                 return
  106.             end
  107.  
  108.             local fromLootValue, toLootValue = string.find(message, 'Loot of ')
  109.             if toLootValue then
  110.                 -- Return monster
  111.                 local lootMonsterName = string.sub(message, toLootValue + 1, string.find(message, ':') - 1)
  112.                 local isAFromLootValue, isAToLootValue = string.find(lootMonsterName, 'a ')
  113.                 if isAToLootValue then
  114.                     lootMonsterName = string.sub(lootMonsterName, isAToLootValue + 1, string.len(lootMonsterName))
  115.                 end
  116.  
  117.                 local isANFromLootValue, isANToLootValue = string.find(lootMonsterName, 'an ')
  118.                 if isANToLootValue then
  119.                     lootMonsterName = string.sub(lootMonsterName, isANToLootValue + 1, string.len(lootMonsterName))
  120.                 end
  121.  
  122.                 -- If no monster then add monster to table
  123.                 if not store.lootStatsTable[lootMonsterName] then
  124.                     store.lootStatsTable[lootMonsterName] = { loot = {}, count = 0 }
  125.                 end
  126.  
  127.                 -- Update monster kill count information
  128.                 store.lootStatsTable[lootMonsterName].count = store.lootStatsTable[lootMonsterName].count + 1
  129.  
  130.                 -- Return Loot
  131.                 local lootString = string.sub(message, string.find(message, ': ') + 2, string.len(message))
  132.  
  133.                 -- If dot at the ned of sentence (OTS only), delete it
  134.                 if not store:getIgnoreLastSignWhenDot() then
  135.                     if string.sub(lootString, string.len(lootString)) == '.' then
  136.                         lootString = string.sub(lootString, 0, string.len(lootString) - 1)
  137.                     end
  138.                 end
  139.  
  140.                 local lootToScreen = {}
  141.                 for word in string.gmatch(lootString, '([^,]+)') do
  142.                     -- Delete first space
  143.                     if string.sub(word, 0, 1) == ' ' then
  144.                         word = string.sub(word, 2, string.len(word))
  145.                     end
  146.  
  147.                     -- Delete 'a ' / 'an '
  148.                     local isAToLootValue, isAFromLootValue = string.find(word, 'a ')
  149.                     if isAFromLootValue then
  150.                         word = string.sub(word, isAFromLootValue + 1, string.len(word))
  151.                     end
  152.  
  153.                     local isANToLootValue, isANFromLootValue = string.find(word, 'an ')
  154.                     if isANFromLootValue then
  155.                         word = string.sub(word, isANFromLootValue + 1, string.len(word))
  156.                     end
  157.  
  158.                     -- Check is first sign is number
  159.                     if type(tonumber(string.sub(word, 0, 1))) == 'number' then
  160.                         local itemCount = tonumber(string.match(word, "%d+"))
  161.                         local delFN, delLN = string.find(word, itemCount)
  162.                         local itemWord = string.sub(word, delLN + 2)
  163.                         local isPluralNameInLoot = self.returnPluralNameFromLoot(lootMonsterName, itemWord)
  164.  
  165.                         if isPluralNameInLoot then
  166.                             if not store.lootStatsTable[lootMonsterName].loot[isPluralNameInLoot] then
  167.                                 store.lootStatsTable[lootMonsterName].loot[isPluralNameInLoot] = {}
  168.                                 store.lootStatsTable[lootMonsterName].loot[isPluralNameInLoot].count = 0
  169.                             end
  170.  
  171.                             if not lootToScreen[isPluralNameInLoot] then
  172.                                 lootToScreen[isPluralNameInLoot] = {}
  173.                                 lootToScreen[isPluralNameInLoot].count = 0
  174.                             end
  175.  
  176.                             store.lootStatsTable[lootMonsterName].loot[isPluralNameInLoot].count = store.lootStatsTable[lootMonsterName].loot[isPluralNameInLoot].count + itemCount
  177.                             lootToScreen[isPluralNameInLoot].count = lootToScreen[isPluralNameInLoot].count + itemCount
  178.                         else
  179.                             local pluralNameToSingular = self:convertPluralToSingular(itemWord)
  180.                             if pluralNameToSingular then
  181.                                 if not store.lootStatsTable[lootMonsterName].loot[pluralNameToSingular] then
  182.                                     store.lootStatsTable[lootMonsterName].loot[pluralNameToSingular] = {}
  183.                                     store.lootStatsTable[lootMonsterName].loot[pluralNameToSingular].count = 0
  184.                                 end
  185.  
  186.                                 if not store.lootStatsTable[lootMonsterName].loot[pluralNameToSingular].plural then
  187.                                     store.lootStatsTable[lootMonsterName].loot[pluralNameToSingular].plural = itemWord
  188.                                 end
  189.  
  190.                                 if not lootToScreen[pluralNameToSingular] then
  191.                                     lootToScreen[pluralNameToSingular] = {}
  192.                                     lootToScreen[pluralNameToSingular].count = 0
  193.                                 end
  194.  
  195.                                 store.lootStatsTable[lootMonsterName].loot[pluralNameToSingular].count = store.lootStatsTable[lootMonsterName].loot[pluralNameToSingular].count + itemCount
  196.                                 lootToScreen[pluralNameToSingular].count = lootToScreen[pluralNameToSingular].count + itemCount
  197.                             else
  198.                                 if not store.lootStatsTable[lootMonsterName].loot[word] then
  199.                                     store.lootStatsTable[lootMonsterName].loot[word] = {}
  200.                                     store.lootStatsTable[lootMonsterName].loot[word].count = 0
  201.                                 end
  202.  
  203.                                 if not lootToScreen[word] then
  204.                                     lootToScreen[word] = {}
  205.                                     lootToScreen[word].count = 0
  206.                                 end
  207.  
  208.                                 store.lootStatsTable[lootMonsterName].loot[word].count = store.lootStatsTable[lootMonsterName].loot[word].count + 1
  209.                                 lootToScreen[word].count = lootToScreen[word].count + 1
  210.                             end
  211.                         end
  212.                     else
  213.                         if not store.lootStatsTable[lootMonsterName].loot[word] then
  214.                             store.lootStatsTable[lootMonsterName].loot[word] = {}
  215.                             store.lootStatsTable[lootMonsterName].loot[word].count = 0
  216.                         end
  217.  
  218.                         if not lootToScreen[word] then
  219.                             lootToScreen[word] = {}
  220.                             lootToScreen[word].count = 0
  221.                         end
  222.  
  223.                         store.lootStatsTable[lootMonsterName].loot[word].count = store.lootStatsTable[lootMonsterName].loot[word].count + 1
  224.                         lootToScreen[word].count = lootToScreen[word].count + 1
  225.                     end
  226.                 end
  227.  
  228.                 store:addLootLog(lootToScreen)
  229.                 lootToScreen = {}
  230.             end
  231.  
  232.             store:refreshLootStatsTable()
  233.         end;
  234.     }
  235.  
  236.     return createStats
  237. end
  238.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement