SHARE
TWEET

upgradesystem.lua

a guest Jun 5th, 2019 64 in 319 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --PERFECT UPGRADE SYSTEM
  2. UpgradeHandler = {
  3.     nameLv = {
  4.         [1] = "rare",
  5.         [2] = "epic",
  6.         [3] = "legendary"
  7.     },
  8.     levels = {
  9.           [1] = {50, false, false},
  10.           [2] = {30, false, false},
  11.           [3] = {15, true, true}
  12.     },
  13.     broadcast = 3,
  14.     attributes = {
  15.           ["attack"] = 3,
  16.           ["defense"] = 2,
  17.           ["armor"] = 1
  18.     },
  19.     message = {
  20.           console = "Trying to refine %s to level +%s with %s%% success rate.",
  21.           success = "You have upgraded %s to level +%s",
  22.           fail = "You have failed in upgrade of %s to level +%s",
  23.           downgrade = "The upgrade level of %s has downgraded to +%s",
  24.           erase = "The upgrade level of %s has been erased.",
  25.           maxlevel = "The targeted %s is already on max upgrade level.",
  26.           notupgradeable = "This item is not upgradeable.",
  27.           broadcast = "The player %s was successful in upgrading %s to level +%s.\nCongratulations!!",
  28.           invalidtool = "This is not a valid upgrade tool.",
  29.           toolrange = "This upgrade tool can only be used in items with level between +%s and +%s"
  30.     },
  31.     tools = {
  32.         [8306] = {range = {0, 10}, info = {chance = 0, removeable = true}},
  33.         [8300] = {range = {0, 10}, info = {chance = 10, removeable = true}}
  34.     },
  35.     isEquipment = function(self)
  36.         local weaponType = self:getItemWeaponType()
  37.         return ((weaponType > 0 and weaponType < 8) or self.item.armor ~= 0)
  38.     end,
  39.     setItemName = function(self, name)
  40.         return doItemSetAttribute(self.item.uid, "name", name)
  41.     end,
  42.     chance = function(self)
  43.         local chances = {}
  44.         chances.upgrade = (self.levels[self.item.level + 1][1] or 100)
  45.         chances.downgrade = (self.item.level * 5)
  46.         chances.erase = (self.item.level * 3)
  47.         return chances
  48.     end
  49. }
  50. function UpgradeHandler:new(item)
  51.     local obj, ret = {}
  52.     obj.item = {}
  53.     obj.item.level = 0
  54.     obj.item.uid = item.uid
  55.     for key, value in pairs(getItemInfo(item.itemid)) do
  56.         obj.item[key] = value
  57.     end
  58.     ret = setmetatable(obj, {
  59.         __index = function(self, index)
  60.             if _G[index] then
  61.                 return (setmetatable({callback = _G[index]},
  62.                     {__call = function(self, ...)
  63.                         return self.callback(item.uid, ...)
  64.                     end}
  65.                 ))
  66.             else
  67.                 return UpgradeHandler[index]
  68.             end
  69.     end})
  70.     if ret:isEquipment() then
  71.         ret:update()
  72.         return ret
  73.     end
  74.     return false
  75. end
  76. function UpgradeHandler:update()
  77.    -- this will return the level by the quality or 0 if it has no quality.
  78.     self.item.level = 0
  79.     for r, v in ipairs(self.nameLv) do
  80.         if self:getItemName():find(v) then
  81.             self.item.level = r
  82.         end
  83.     end
  84. end
  85. function UpgradeHandler:refine(uid, item)
  86.     if not self.item then
  87.         doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_BLUE, self.message.notupgradeable)
  88.         return "miss"
  89.     end
  90.     local tool = self.tools[item.itemid]
  91.     if(tool == nil) then
  92.         doPlayerSendTextMessage(uid, MESSAGE_EVENT_DEFAULT, self.message.invalidtool)
  93.         return "miss"
  94.     end
  95.     if(self.item.level > #self.levels) then
  96.         doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_RED, self.message.maxlevel:format(self.item.name))
  97.         return "miss"
  98.     end
  99.     if(tool.range[1] and self.item.level < tool.range[1]) or (tool.range[2] and self.item.level >= tool.range[2]) then
  100.         doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_RED, self.message.toolrange:format(unpack(tool.range)))
  101.         return "miss"
  102.     end
  103.     local chance = (self:chance().upgrade + tool.info.chance)
  104.     doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_BLUE, self.message.console:format(self.item.name, (self.item.level + 1), math.min(100, chance)))
  105.     if(tool.info.removeable == true) then
  106.         doRemoveItem(item.uid, 1)
  107.     end
  108.     if chance * 100 > math.random(1, 10000) then
  109.         doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_ORANGE, self.message.success:format(self.item.name, (self.item.level + 1)))
  110.         if (self.item.level + 1) >= self.broadcast then
  111.             doBroadcastMessage(self.message.broadcast:format(getCreatureName(uid), self.item.name, (self.item.level + 1)))
  112.         end
  113.         -- it says if the item's level is greater then 0 (meaning is level equal to 1 or more) if it is add 1 more
  114.         -- if the current level equals 0 add 1 to it
  115.         --self:setItemName(self.item.level > 0 and (self:getItemName():gsub(self.nameLv[self.item.level], ""))..self.nameLv[self.item.level + 1] or self:getItemName().." "..self.nameLv[1])
  116.         self:setItemName(self.item.level > 0 and self.nameLv[self.item.level + 1]..(self:getItemName():gsub(self.nameLv[self.item.level], "")) or self.nameLv[1].." "..self:getItemName())
  117.         for key, value in pairs(self.attributes) do
  118.             if getItemAttribute(self.item.uid, key) ~= nil or self.item[key] ~= 0 then
  119.                 doItemSetAttribute(self.item.uid, key, (self.item.level > 0 and getItemAttribute(self.item.uid, key) or self.item[key]) + value)
  120.             end
  121.         end
  122.         return "success"
  123.     else
  124.         if item.itemid == 8300 then
  125.             if self.item.level > 0 then
  126.                 -- this will remove any number with a + sign in front of it from the string
  127.                 --self:setItemName(self:getItemName():gsub((" "..self.nameLv[self.item.level]), ""))
  128.                 self:setItemName(self:getItemName():gsub((self.nameLv[self.item.level].." "), ""))
  129.                 for key, value in pairs(self.attributes) do
  130.                     if getItemAttribute(self.item.uid, key) ~= nil or self.item[key] ~= 0 then
  131.                         doItemSetAttribute(self.item.uid, key, getItemAttribute(self.item.uid, key) - self.item.level * value)
  132.                     end
  133.                 end
  134.             end
  135.         else
  136.             doRemoveItem(self.item.uid, 1)
  137.         end
  138.         doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_BLUE, item.itemid == 8300 and "Your item level has been reseted." or "You have broken your item while trying to upgrade it.")
  139.     end
  140. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top