Advertisement
Michcol

Untitled

Dec 18th, 2023
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.34 KB | Software | 0 0
  1. ]dofile('itemsXML.lua')
  2.  
  3. function CreateStats()
  4.  
  5.     local createStats = {
  6.  
  7.         loadedVersionItems = 0;
  8.  
  9.         ownParser = false;
  10.  
  11.  
  12.  
  13.         _loadClientVersionItems = nil;
  14.  
  15.         _checkLootTextMessage = nil;
  16.  
  17.  
  18.  
  19.         init = function(self)
  20.  
  21.             self._loadClientVersionItems = function() self:loadClientVersionItems() end
  22.  
  23.             self._checkLootTextMessage = function(messageMode, message) self:checkLootTextMessage(messageMode, message) end
  24.  
  25.  
  26.  
  27.             connect(g_game, { onClientVersionChange = self._loadClientVersionItems })
  28.  
  29.             connect(g_game, { onTextMessage = self._checkLootTextMessage })
  30.  
  31.  
  32.  
  33.             if (self.loadedVersionItems == 0 and g_game.getClientVersion() ~= 0) or (g_game.getClientVersion() ~= 0 and self.loadedVersionItems ~= g_game.getClientVersion()) then
  34.  
  35.                 self:loadClientVersionItems()
  36.  
  37.             end
  38.  
  39.         end;
  40.  
  41.  
  42.  
  43.         terminate = function(self)
  44.  
  45.             disconnect(g_game, { onClientVersionChange = self._loadClientVersionItems })
  46.  
  47.             disconnect(g_game, { onTextMessage = self._checkLootTextMessage })
  48.  
  49.         end;
  50.  
  51.  
  52.  
  53.         -- Load items
  54.  
  55.  
  56.  
  57.         loadClientVersionItems = function(self)
  58.  
  59.             print("Rozpoczęcie ładowania przedmiotów wersji klienta")
  60.  
  61.             local version = g_game.getClientVersion()
  62.  
  63.             print("Aktualna wersja klienta: ", version)
  64.  
  65.  
  66.  
  67.             if version ~= self.loadedVersionItems then
  68.  
  69.                 local otbPath = '/mods/loot_stats/items_versions/' .. version .. '/items.otb'
  70.  
  71.                 local xmlPath = 'items_versions/' .. version .. '/items.xml'
  72.  
  73.  
  74.  
  75.                 if g_resources.fileExists(otbPath) then
  76.  
  77.                     g_things.loadOtb(otbPath)
  78.  
  79.                     if g_things.isOtbLoaded() then
  80.  
  81.                         print("Plik OTB załadowany.")
  82.  
  83.                         self.ownParser = ItemsXML()
  84.  
  85.                         self.ownParser:parseItemsXML(xmlPath)
  86.  
  87.                         print("Plik XML załadowany przy użyciu własnego parsera.")
  88.  
  89.                     else
  90.  
  91.                         print("Błąd przy ładowaniu pliku OTB.")
  92.  
  93.                         return
  94.  
  95.                     end
  96.  
  97.                 else
  98.  
  99.                     print("Brak wymaganych plików OTB.")
  100.  
  101.                     return
  102.  
  103.                 end
  104.  
  105.  
  106.  
  107.                 self.loadedVersionItems = version
  108.  
  109.             end
  110.  
  111.             print("Zakończenie ładowania przedmiotów wersji klienta")
  112.  
  113.         end;
  114.  
  115.  
  116.  
  117.         checkParserType = function(self)
  118.  
  119.             if g_things.findItemTypeByPluralName then
  120.  
  121.                 self.ownParser = false
  122.  
  123.             else
  124.  
  125.                 self.ownParser = ItemsXML()
  126.  
  127.                 self.ownParser:parseItemsXML('items_versions/' .. g_game.getClientVersion() .. '/items.xml')
  128.  
  129.             end
  130.  
  131.         end;
  132.  
  133.  
  134.  
  135.         -- Convert plural to singular
  136.  
  137.  
  138.  
  139.         convertPluralToSingular = function(self, searchWord)
  140.  
  141.             if not self.ownParser then
  142.  
  143.                 local item = g_things.findItemTypeByPluralName(searchWord)
  144.  
  145.                 if not item:isNull() then
  146.  
  147.                     return item:getName()
  148.  
  149.                 else
  150.  
  151.                     return false
  152.  
  153.                 end
  154.  
  155.             else
  156.  
  157.                 return self.ownParser:convertPluralToSingular(searchWord)
  158.  
  159.             end
  160.  
  161.         end;
  162.  
  163.  
  164.  
  165.         -- Parse message logs
  166.  
  167.  
  168.  
  169.         returnPluralNameFromLoot = function(lootMonsterName, itemWord)
  170.  
  171.             for a,b in pairs(store.lootStatsTable[lootMonsterName].loot) do
  172.  
  173.                 if b.plural == itemWord then
  174.  
  175.                     return a
  176.  
  177.                 end
  178.  
  179.             end
  180.  
  181.  
  182.  
  183.             return false
  184.  
  185.         end;
  186.  
  187.  
  188.  
  189.         checkLootTextMessage = function(self, messageMode, message)
  190.  
  191.             print("Checking loot text message:", message)
  192.  
  193.             if self.loadedVersionItems == 0 then
  194.  
  195.                 return
  196.  
  197.             end
  198.  
  199.  
  200.  
  201.             local fromLootValue, toLootValue = string.find(message, 'Loot of ')
  202.  
  203.             if toLootValue then
  204.  
  205.                 -- Return monster
  206.  
  207.                 local lootMonsterName = string.sub(message, toLootValue + 1, string.find(message, ':') - 1)
  208.  
  209.                 local isAFromLootValue, isAToLootValue = string.find(lootMonsterName, 'a ')
  210.  
  211.                 if isAToLootValue then
  212.  
  213.                     lootMonsterName = string.sub(lootMonsterName, isAToLootValue + 1, string.len(lootMonsterName))
  214.  
  215.                 end
  216.  
  217.  
  218.  
  219.                 local isANFromLootValue, isANToLootValue = string.find(lootMonsterName, 'an ')
  220.  
  221.                 if isANToLootValue then
  222.  
  223.                     lootMonsterName = string.sub(lootMonsterName, isANToLootValue + 1, string.len(lootMonsterName))
  224.  
  225.                 end
  226.  
  227.  
  228.  
  229.                 -- If no monster then add monster to table
  230.  
  231.                 if not store.lootStatsTable[lootMonsterName] then
  232.  
  233.                     store.lootStatsTable[lootMonsterName] = { loot = {}, count = 0 }
  234.  
  235.                 end
  236.  
  237.  
  238.  
  239.                 -- Update monster kill count information
  240.  
  241.                 store.lootStatsTable[lootMonsterName].count = store.lootStatsTable[lootMonsterName].count + 1
  242.  
  243.  
  244.  
  245.                 -- Return Loot
  246.  
  247.                 local lootString = string.sub(message, string.find(message, ': ') + 2, string.len(message))
  248.  
  249.  
  250.  
  251.                 -- If dot at the ned of sentence (OTS only), delete it
  252.  
  253.                 if not store:getIgnoreLastSignWhenDot() then
  254.  
  255.                     if string.sub(lootString, string.len(lootString)) == '.' then
  256.  
  257.                         lootString = string.sub(lootString, 0, string.len(lootString) - 1)
  258.  
  259.                     end
  260.  
  261.                 end
  262.  
  263.  
  264.  
  265.                 local lootToScreen = {}
  266.  
  267.                 for word in string.gmatch(lootString, '([^,]+)') do
  268.  
  269.                     -- Delete first space
  270.  
  271.                     if string.sub(word, 0, 1) == ' ' then
  272.  
  273.                         word = string.sub(word, 2, string.len(word))
  274.  
  275.                     end
  276.  
  277.  
  278.  
  279.                     -- Delete 'a ' / 'an '
  280.  
  281.                     local isAToLootValue, isAFromLootValue = string.find(word, 'a ')
  282.  
  283.                     if isAFromLootValue then
  284.  
  285.                         word = string.sub(word, isAFromLootValue + 1, string.len(word))
  286.  
  287.                     end
  288.  
  289.  
  290.  
  291.                     local isANToLootValue, isANFromLootValue = string.find(word, 'an ')
  292.  
  293.                     if isANFromLootValue then
  294.  
  295.                         word = string.sub(word, isANFromLootValue + 1, string.len(word))
  296.  
  297.                     end
  298.  
  299.  
  300.  
  301.                     -- Check is first sign is number
  302.  
  303.                     if type(tonumber(string.sub(word, 0, 1))) == 'number' then
  304.  
  305.                         local itemCount = tonumber(string.match(word, "%d+"))
  306.  
  307.                         local delFN, delLN = string.find(word, itemCount)
  308.  
  309.                         local itemWord = string.sub(word, delLN + 2)
  310.  
  311.                         local isPluralNameInLoot = self.returnPluralNameFromLoot(lootMonsterName, itemWord)
  312.  
  313.  
  314.  
  315.                         if isPluralNameInLoot then
  316.  
  317.                             if not store.lootStatsTable[lootMonsterName].loot[isPluralNameInLoot] then
  318.  
  319.                                 store.lootStatsTable[lootMonsterName].loot[isPluralNameInLoot] = {}
  320.  
  321.                                 store.lootStatsTable[lootMonsterName].loot[isPluralNameInLoot].count = 0
  322.  
  323.                             end
  324.  
  325.  
  326.  
  327.                             if not lootToScreen[isPluralNameInLoot] then
  328.  
  329.                                 lootToScreen[isPluralNameInLoot] = {}
  330.  
  331.                                 lootToScreen[isPluralNameInLoot].count = 0
  332.  
  333.                             end
  334.  
  335.  
  336.  
  337.                             store.lootStatsTable[lootMonsterName].loot[isPluralNameInLoot].count = store.lootStatsTable[lootMonsterName].loot[isPluralNameInLoot].count + itemCount
  338.  
  339.                             lootToScreen[isPluralNameInLoot].count = lootToScreen[isPluralNameInLoot].count + itemCount
  340.  
  341.                         else
  342.  
  343.                             local pluralNameToSingular = self:convertPluralToSingular(itemWord)
  344.  
  345.                             if pluralNameToSingular then
  346.  
  347.                                 if not store.lootStatsTable[lootMonsterName].loot[pluralNameToSingular] then
  348.  
  349.                                     store.lootStatsTable[lootMonsterName].loot[pluralNameToSingular] = {}
  350.  
  351.                                     store.lootStatsTable[lootMonsterName].loot[pluralNameToSingular].count = 0
  352.  
  353.                                 end
  354.  
  355.  
  356.  
  357.                                 if not store.lootStatsTable[lootMonsterName].loot[pluralNameToSingular].plural then
  358.  
  359.                                     store.lootStatsTable[lootMonsterName].loot[pluralNameToSingular].plural = itemWord
  360.  
  361.                                 end
  362.  
  363.  
  364.  
  365.                                 if not lootToScreen[pluralNameToSingular] then
  366.  
  367.                                     lootToScreen[pluralNameToSingular] = {}
  368.  
  369.                                     lootToScreen[pluralNameToSingular].count = 0
  370.  
  371.                                 end
  372.  
  373.  
  374.  
  375.                                 store.lootStatsTable[lootMonsterName].loot[pluralNameToSingular].count = store.lootStatsTable[lootMonsterName].loot[pluralNameToSingular].count + itemCount
  376.  
  377.                                 lootToScreen[pluralNameToSingular].count = lootToScreen[pluralNameToSingular].count + itemCount
  378.  
  379.                             else
  380.  
  381.                                 if not store.lootStatsTable[lootMonsterName].loot[word] then
  382.  
  383.                                     store.lootStatsTable[lootMonsterName].loot[word] = {}
  384.  
  385.                                     store.lootStatsTable[lootMonsterName].loot[word].count = 0
  386.  
  387.                                 end
  388.  
  389.  
  390.  
  391.                                 if not lootToScreen[word] then
  392.  
  393.                                     lootToScreen[word] = {}
  394.  
  395.                                     lootToScreen[word].count = 0
  396.  
  397.                                 end
  398.  
  399.  
  400.  
  401.                                 store.lootStatsTable[lootMonsterName].loot[word].count = store.lootStatsTable[lootMonsterName].loot[word].count + 1
  402.  
  403.                                 lootToScreen[word].count = lootToScreen[word].count + 1
  404.  
  405.                             end
  406.  
  407.                         end
  408.  
  409.                     else
  410.  
  411.                         if not store.lootStatsTable[lootMonsterName].loot[word] then
  412.  
  413.                             store.lootStatsTable[lootMonsterName].loot[word] = {}
  414.  
  415.                             store.lootStatsTable[lootMonsterName].loot[word].count = 0
  416.  
  417.                         end
  418.  
  419.  
  420.  
  421.                         if not lootToScreen[word] then
  422.  
  423.                             lootToScreen[word] = {}
  424.  
  425.                             lootToScreen[word].count = 0
  426.  
  427.                         end
  428.  
  429.  
  430.  
  431.                         store.lootStatsTable[lootMonsterName].loot[word].count = store.lootStatsTable[lootMonsterName].loot[word].count + 1
  432.  
  433.                         lootToScreen[word].count = lootToScreen[word].count + 1
  434.  
  435.                     end
  436.  
  437.                 end
  438.  
  439.  
  440.  
  441.                 store:addLootLog(lootToScreen)
  442.  
  443.                 lootToScreen = {}
  444.  
  445.             end
  446.  
  447.  
  448.  
  449.             store:refreshLootStatsTable()
  450.  
  451.         end;
  452.  
  453.     }
  454.  
  455.  
  456.  
  457.     return createStats
  458.  
  459. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement