Advertisement
Guest User

Account Manager

a guest
Aug 11th, 2014
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.25 KB | None | 0 0
  1. local CHECK_TIME    = 60 * 60   -- 1 hr, interval between check SAME character again.
  2. local MIN_STAMINA   = 17        -- hours, min stamina to go hunt when checking.
  3. local LOG_STAMINA   = 14        -- stamina to leave spawn and logout.
  4. -- Limit time!
  5. HUNT_TIME = "07:15:00"
  6.  
  7. ACCOUNTS = {
  8.     [1] = {acc = 'myacc1',  psw = 'mypass1'},   -- my bot acc number 1
  9.     [2] = {acc = 'myacc2',  psw = 'mypass2'}    -- my bot acc number 2
  10. }
  11.  
  12. CHARACTERS = {
  13.     [1] = {'Bototo', 'Botin'},                  -- my characters in account 1, id should match!
  14.     [2] = {'Botoneitor', 'Bots of haste'}       -- my characters in account 2, id should match!
  15. }
  16.  
  17. -- Not to edit, internal use.
  18. TRAINERS = {
  19.         [1] = {name = 'sword',      itemid = 16198},
  20.         [2] = {name = 'axe',        itemid = 16199},
  21.         [3] = {name = 'club',       itemid = 16200},
  22.         [4] = {name = 'distance',   itemid = 16201},
  23.         [5] = {name = 'magic',      itemid = 16202}
  24.     }
  25.    
  26. -- Class AccountManager
  27. AccountManager = {}
  28. AccountManager.__index = AccountManager
  29.  
  30. function AccountManager.new(names)
  31.     return setmetatable({names = names}, AccountManager)
  32. end
  33.  
  34. function AccountManager.getAccount(name)
  35.     for k, v in pairs(CHARACTERS) do
  36.         if isInArray(v, name) then
  37.             local info = ACCOUNTS[k]
  38.             if (info ~= nil) then
  39.                 return info.acc, info.psw
  40.             end
  41.         end
  42.     end
  43.     return nil
  44. end
  45.  
  46. function AccountManager.getSkill()
  47.     local max_skill = {id = 0, value = 0}
  48.    
  49.     for id, value in pairs({sword, axe, club, distance, mlevel}) do
  50.         if (value > max_skill.value) then
  51.             max_skill.id = id
  52.             max_skill.value = value
  53.         end
  54.     end
  55.    
  56.     -- We need a default skill to train incase we don't find our best skill.
  57.     -- Magic level is useful no matter what vocation we are.
  58.     if (max_skill.value == 10) and (max_skill.id ~= 5) then     -- Probably new character
  59.         max_skill.id = 5
  60.         max_skill.value = 0     -- not really matters at this point.
  61.     end
  62.     return max_skill
  63. end
  64.    
  65. function AccountManager.useTrainer(name)
  66.     print('Attemping to use trainers: ' .. name)
  67.     updateworld()
  68.     wait(2500, 4500)
  69.    
  70.    
  71.     -- Assuming we are already on the previous waypoint.
  72.     -- This should be the central SQM in trainers.
  73.     wait(500, 700)
  74.     updateworld()
  75.    
  76.     -- Lets get our best skill
  77.     local toTrain = TRAINERS[AccountManager.getSkill().id]
  78.     local __toPos = nil
  79.    
  80.     for _, pos in pairs(buildArea(myPosition(), 6)) do
  81.         for _, item in pairs(getTileItems(pos)) do
  82.             if (item.id == toTrain.itemid) then
  83.                 __toPos = pos
  84.                 break
  85.             end
  86.         end
  87.     end
  88.  
  89.     if (__toPos ~= nil) then
  90.         print('Using training statue for skill: ' .. toTrain.name .. ' -> character: ' .. name)
  91.         reachlocation(__toPos.x, __toPos.y, __toPos.z)
  92.         wait(1000, 1500)
  93.         useitem(toTrain.itemid, __toPos.x, __toPos.y, __toPos.z)
  94.     else
  95.         print('Can\'t find training statue for skill: ' .. toTrain.name)
  96.         -- We execute a normal logout.
  97.         logout()
  98.     end
  99. end
  100.  
  101. -- in my case I use my own functions to handle backpacks, you'll need take a look here.
  102. function AccountManager:setBackpacks(backpacks)
  103.     self.backpacks = backpacks
  104. end
  105.  
  106. function AccountManager:setLabel(label)
  107.     self.label = label
  108. end
  109.  
  110. function AccountManager:removeUser(name)
  111.     local index = nil
  112.     for k, v in pairs(self.names) do
  113.         if (v == name) then
  114.             index = k
  115.             break
  116.         end
  117.     end
  118.    
  119.     if (index ~= nil) then
  120.         table.remove(self.names, index)
  121.         print('Error: removed user ' .. name .. '.')
  122.     else
  123.         print('Error: remove, user ' .. name .. ' not found.')
  124.     end
  125. end
  126.  
  127. function AccountManager:removeCurrentUser()
  128.     self:removeUser(self.online)
  129.     self.last = self.online
  130.     self.online = nil
  131. end
  132.  
  133. function AccountManager:getLastCheck(name)
  134.     if (self.check == nil) then
  135.         return nil
  136.     end
  137.    
  138.     return self.check[name]
  139. end
  140.  
  141. function AccountManager:setLastCheck(name)
  142.     if (self.check == nil) then
  143.         self.check = {}
  144.     end
  145.    
  146.     self.check[name] = os.time()
  147. end
  148.  
  149. function AccountManager:ignoreHuntingTime(bool)
  150.     setGlobalVariable("ignoretime_" .. getscriptname(), bool)
  151. end
  152.  
  153. function AccountManager:checkStamina()
  154.     local ignore = getBooleanFromGlobal("ignoretime_" .. getscriptname())
  155.     if (timehunt >= HUNT_TIME) and not ignore then
  156.         return false
  157.     end
  158.     return (fromStamina() > LOG_STAMINA)
  159. end
  160.  
  161. function AccountManager:think()
  162.     local nextReady = getBooleanFromGlobal("next_" .. getscriptname())
  163.     if nextReady then
  164.         self:setLastCheck(name)
  165.         self.last = name
  166.         self.online = nil
  167.        
  168.         print('Moving to next user ...')
  169.         setGlobalVariable("next_" .. getscriptname(), false)
  170.         wait(3000)
  171.        
  172.         if isInTrainers() then
  173.             AccountManager.useTrainer(name)
  174.         else
  175.             logout()
  176.         end
  177.         return false
  178.     end
  179.    
  180.     if (self.online == nil) then
  181.         if not connected then  
  182.             -- Let's decide who should login ...
  183.             if (self.last == nil) then
  184.                 -- First login attemp?
  185.                 local newUser = self.names[1]
  186.                 local acc, pass = AccountManager.getAccount(newUser)
  187.                 if (acc == nil) then
  188.                     print('Error: no account found, removing: ' .. newUser)
  189.                     self:removeUser(newUser)
  190.                     return false
  191.                 end
  192.                
  193.                 print('Attemping to connect: ' .. newUser)
  194.                 focusclient()
  195.                 connect(acc, pass, newUser)
  196.                 wait(2000)
  197.                 updateworld()
  198.                
  199.                 if connected then
  200.                     if pzone and not isInSafePosition() then
  201.                         print('User ' .. newUser .. ' did die, removing from list ...')
  202.                         logout()
  203.                         self:removeUser(newUser)   
  204.                         return false
  205.                     end
  206.                    
  207.                     -- Check stamina
  208.                     wait(1775, 2135)
  209.                     if (stamina >= toStamina(MIN_STAMINA)) then
  210.                    
  211.                 --      PAY ATTENTION HERE, YOU SHOULD IMPLEMENT YOUR BACKPACK HANDLER HERE.
  212.                 --      if not checkBackpacks(self.backpacks) then
  213.                             reOpenBackpacks(self.backpacks)
  214.                 --      end
  215.                         print('Character ' .. newUser .. ' is now the system\'s user :)')
  216.                         self.online = newUser
  217.                        
  218.                         -- -- -- --
  219.                         if not cavebot then
  220.                             setcavebot("yes")
  221.                         end
  222.                         -- Labels ~
  223.                         gotolabel(self.label)
  224.                         -- -- -- --
  225.                     else
  226.                         print('Character ' .. newUser .. ' has not enough stamina, let\'s check next ...')
  227.                         self:setLastCheck(newUser)
  228.                         self.last = newUser
  229.                         if isInTrainers() then
  230.                             AccountManager.useTrainer(newUser)
  231.                         else
  232.                             logout()
  233.                         end
  234.                     end
  235.                 end
  236.             else
  237.                 -- Get next user ...
  238.                 local index = nil
  239.                 for k, v in pairs(self.names) do
  240.                     if (v == self.last) then
  241.                         index = k
  242.                         break
  243.                     end
  244.                 end
  245.                
  246.                 if (index == nil) then
  247.                     print('Fatal error: can not find index for user: ' .. self.last .. ', set self.last to nil.')
  248.                     self.last = nil
  249.                 else
  250.                     local i = ((index < #self.names) and (index + 1) or 1)
  251.                     local newUser = self.names[i]
  252.                     local checkTime = self:getLastCheck(newUser)
  253.                     if (checkTime ~= nil) then
  254.                         if ((os.time() - checkTime) < CHECK_TIME) then
  255.         --                  print('Skip user: ' .. newUser .. ' it was checked recently.')
  256.                             self.last = newUser
  257.                             return false
  258.                         end
  259.                     end
  260.                    
  261.                     local acc, pass = AccountManager.getAccount(newUser)
  262.                     if (acc == nil) then
  263.                         print('Error: no account found, removing: ' .. newUser)
  264.                         self:removeUser(newUser)
  265.                         self.last = nil
  266.                         return false
  267.                     end
  268.                    
  269.                     print('Attemping to connect: ' .. newUser)
  270.                    
  271.                     focusclient()
  272.                     connect(acc, pass, newUser)
  273.                     wait(2000)
  274.                     updateworld()
  275.                    
  276.                     if connected then
  277.                         if pzone and not isInSafePosition() then
  278.                             print('User ' .. newUser .. ' did die, removing from list ...')
  279.                             logout()
  280.                             self:removeUser(newUser)   
  281.                             self.last = nil
  282.                             return false
  283.                         end
  284.                        
  285.                         -- Check stamina
  286.                         wait(1775, 2135)
  287.                         if (stamina >= toStamina(MIN_STAMINA)) then
  288.                     --      PAY ATTENTION HERE, YOU SHOULD IMPLEMENT YOUR BACKPACK HANDLER HERE.
  289.                     --      if not checkBackpacks(self.backpacks) then
  290.                                 reOpenBackpacks(self.backpacks)
  291.                     --      end
  292.                             print('Character ' .. newUser .. ' is now the system\'s user :)')
  293.                             self.online = newUser
  294.                            
  295.                             -- -- -- --
  296.                             if not cavebot then
  297.                                 setcavebot("yes")
  298.                             end
  299.                             -- Labels ~
  300.                             gotolabel(self.label)
  301.                             -- -- -- --
  302.                             resethud()
  303.                         else
  304.                             print('Character ' .. newUser .. ' has not enough stamina, let\'s check next ...')
  305.                             self:setLastCheck(newUser)
  306.                             self.last = newUser
  307.                             if isInTrainers() then
  308.                                 AccountManager.useTrainer(newUser)
  309.                             else
  310.                                 logout()
  311.                             end
  312.                         end
  313.                     end
  314.                 end
  315.             end
  316.         else   
  317.             -- A character is online let's set it as current user
  318.             if isInArray(self.names, name) then
  319.                 self.online = name
  320.                 print('A character is online let\'s set it as current user: ' .. name)
  321.             else
  322.                 print('Error: unknown user online: ' .. name)
  323.             end
  324.         end
  325.     else
  326.         if not connected then   -- user was probably kicked, server save, etc ...
  327.             local acc, pass = AccountManager.getAccount(self.online)
  328.             if (acc == nil) then
  329.                 print('Error: no account found, removing: ' .. self.online)
  330.                 self:removeCurrentUser()
  331.                 return false
  332.             end
  333.            
  334. --          print('Attemping to RE-connect: ' .. self.online)
  335.            
  336.             focusclient()
  337.             connect(acc, pass, self.online)
  338.             wait(2000)
  339.             updateworld()
  340.            
  341.             if connected then
  342.                 if pzone and not isInSafePosition() then
  343.                     print('User ' .. self.online .. ' did die, removing from list ...')
  344.                     logout()
  345.                     self:removeCurrentUser()                   
  346.                     return false
  347.                 end
  348.                
  349.                 wait(1775, 2135)
  350.         --      PAY ATTENTION HERE, YOU SHOULD IMPLEMENT YOUR BACKPACK HANDLER HERE.
  351.         --      if not checkBackpacks(self.backpacks) then
  352.                     reOpenBackpacks(self.backpacks)
  353.         --      end
  354.                 print('User ' .. self.online .. ' is back online :)')
  355.             end
  356.         end
  357.     end
  358. end
  359.  
  360. function AccountManager:next()
  361.     print("Finishing " .. name)
  362.     setcavebot("no")
  363.     setGlobalVariable("next_" .. getscriptname(), true)
  364. end
  365.  
  366. -- -- -- -- -- -- --
  367. -- Shared set users
  368. -- -- -- -- -- -- --
  369. local SHARED_SETS = {
  370.     -- Shared set example
  371.     {
  372.         users = {'Botin', 'Botoneitor'},
  373.         set = {
  374.             helmet  = 10385,    -- Zaoan Helmet
  375.             amulet  = 3055,     -- Platinum Amulet
  376.             back    = 9601,     -- Demon Backpack
  377.             armor   = 3366,     -- Magic Plate Armor
  378.             shield  = 3414,     -- Mastermind Shield
  379.             weapon  = 16160,    -- Crystalline Sword
  380.             legs    = 10387,    -- Zaoan Legs
  381.             boots   = 3079      -- Boots of Haste
  382.         },
  383.         pos = {x = 32237, y = 31150, z = 7}
  384.     }
  385. }
  386.  
  387. function isSharedSetUser()
  388.     for k, v in pairs(SHARED_SETS) do
  389.         if isInArray(v.users, name) then
  390.             return v
  391.         end
  392.     end
  393.     return false
  394. end
  395.  
  396. function getCurrentSet()
  397.     return {helmet = helmet, amulet = amulet, back = back, armor = armor, shield = shield, weapon = weapon, legs = legs, boots = boots}
  398. end
  399.  
  400. function AccountManager.checkSet()
  401.     local info = isSharedSetUser()
  402.     if (info ~= false) then
  403.         updateworld()
  404.        
  405.         local set = getCurrentSet()
  406.         for k, v in pairs(info.set) do
  407.             local vType = type(v)
  408.             if (((vType == 'number') and (v ~= set[k])) or ((vType == 'table') and not isInArray(v, set[k]))) and (tostring(k) ~= "auxbp") then
  409.                 return false
  410.             end
  411.         end
  412.     else
  413.         print("Error: " .. name .. " is not shared set user.")
  414.     end
  415.     return true
  416. end
  417.  
  418. function AccountManager.checkEmptySet_noBp()
  419.     local info = isSharedSetUser()
  420.     if (info ~= false) then
  421.         updateworld()
  422.        
  423.         for k, v in pairs(getCurrentSet()) do
  424.             if (tostring(k) ~= "back") then
  425.                 if (v ~= 0) then
  426.                     return false
  427.                 end
  428.             end
  429.         end
  430.     else
  431.         print("Error: " .. name .. " is not shared set user.")
  432.     end
  433.     return true
  434. end
  435.  
  436. function AccountManager.checkEmptySet()
  437.     local info = isSharedSetUser()
  438.     if (info ~= false) then
  439.         updateworld()
  440.        
  441.         for k, v in pairs(getCurrentSet()) do
  442.             if (v ~= 0) then
  443.                 return false
  444.             end
  445.         end
  446.     else
  447.         print("Error: " .. name .. " is not shared set user.")
  448.     end
  449.     return true
  450. end
  451.  
  452. function needSet()
  453.     if not isSharedSetUser() then
  454.         return false
  455.     end
  456.    
  457.     return AccountManager.checkEmptySet()
  458. end
  459.  
  460. function needSaveSet()
  461.     if not isSharedSetUser() then
  462.         return false
  463.     end
  464.    
  465.     return AccountManager.checkSet()
  466. end
  467.  
  468. function AccountManager.findSet()
  469.     if not isInHouse() then
  470.         return print("Error AccountManager.findSet() is not in house.")
  471.     end
  472.    
  473.     local info = isSharedSetUser()
  474.     if (info ~= false) then
  475.     --  setsettings("Settings\\Actions\\List\\Open BPS\\Enabled", "no")
  476.         updateworld()
  477.         local set = info.set
  478.         if (back ~= set.back) then
  479.             print("Reach backpack position ...")
  480.             if reachlocation(info.pos.x, info.pos.y, info.pos.z) then
  481.                 print("Attemping to get backpack ...")
  482.                 wait(2000, 3000)
  483.                 openbrowsefield(info.pos.x, info.pos.y, info.pos.z)
  484.                 wait(1000, 2000)
  485.                 moveitems(set.back, ground(info.pos.x, info.pos.y, info.pos.z), 'back', 1)
  486.             end
  487.         else
  488.             if not getcontainer(set.back).open then
  489.                 print("Opening backpack ...")
  490.                 openitem(set.back, 'back', false)
  491.                 wait(500, 900)
  492.                 resizewindow(set.back, 4)
  493.                 wait(1000, 1200)
  494.                
  495.                 if (set.auxbp ~= nil) then
  496.                     openitem(set.auxbp, set.back, true)
  497.                     wait(500, 900)
  498.                     resizewindow(set.auxbp, 4)
  499.                 end
  500.             end
  501.            
  502.             local cset = getCurrentSet()
  503.             for k, v in pairs(set) do
  504.                 if (tostring(k) ~= "back") and (tostring(k) ~= "auxbp") then
  505.                     local vType = type(v)
  506.                     if ((vType == 'number') and (v ~= cset[k])) or ((vType == 'table') and not isInArray(v, cset[k])) then
  507.                         print("Attemping to equip: " .. tostring(k))
  508.                        
  509.                         if (vType == 'table') then
  510.                             local toEquip = v[1]
  511.                             local vOrigin = nil
  512.                             if (itemcount(toEquip, set.auxbp) > 0) then
  513.                                 vOrigin = set.auxbp
  514.                             elseif (itemcount(toEquip, set.back) > 0) then
  515.                                 vOrigin = set.back
  516.                             end
  517.                            
  518.                             if (vOrigin ~= nil) then
  519.                                 moveitems(toEquip, vOrigin, tostring(k), 1)
  520.                             else
  521.                                 closetibia()
  522.                                 return print("Fatal error: " .. name .. " error on equiping set, code: 256.")
  523.                             end
  524.                         else
  525.                             moveitems(v, set.back, tostring(k), 1)
  526.                         end
  527.                         wait(3000)
  528.                     end
  529.                 end
  530.             end
  531.         end
  532.     else
  533.         return print("Error: " .. name .. " is not shared set user.")
  534.     end
  535.        
  536.     if not AccountManager.checkSet() then
  537.         print("Checking set ...")
  538.         wait(2000, 3000)
  539.         AccountManager.findSet()
  540.         return false
  541.     else
  542.         print("Set equiped !!")
  543. --      setsettings("Settings\\Actions\\List\\Open BPS\\Enabled", "yes")
  544.     end
  545.     return true
  546. end
  547.  
  548. function AccountManager.saveSet()
  549.     if not isInHouse() then
  550.         return print("Error AccountManager.saveSet() is not in house.")
  551.     end
  552.    
  553.     local info = isSharedSetUser()
  554.     if (info ~= false) then
  555.     --  setsettings("Settings\\Actions\\List\\Open BPS\\Enabled", "no")
  556.         updateworld()
  557.         closewindows()
  558.         wait(3000)
  559.         updateworld()
  560.        
  561.         local set = info.set
  562.         if (back == set.back) then
  563.             if not getcontainer(set.back).open then
  564.                 print("Attemping to open backpack ...")
  565.                 openitem(set.back, 'back', false)
  566.                 wait(500, 900)
  567.                 resizewindow(set.back, 4)
  568.                 wait(1000, 1200)
  569.                
  570.                 if (set.auxbp ~= nil) then
  571.                     openitem(set.auxbp, set.back, true)
  572.                     wait(500, 900)
  573.                     resizewindow(set.auxbp, 4)
  574.                 end
  575.             end
  576.            
  577.             if not getcontainer(set.back).open then
  578.                 return AccountManager.saveSet()
  579.             end
  580.            
  581.             local cset = getCurrentSet()
  582.             for k, v in pairs(set) do
  583.                 if (tostring(k) ~= "back") and (tostring(k) ~= "auxbp") then
  584.                     local vType = type(v)
  585.                     if ((vType == 'number') and (v == cset[k])) or ((vType == 'table') and isInArray(v, cset[k])) then
  586.                         print("Attemping to de-equip: " .. tostring(k))
  587.                        
  588.                         if (vType == 'table') then
  589.                             moveitems(cset[k], tostring(k), set.auxbp, 1)
  590.                         else
  591.                             moveitems(cset[k], tostring(k), set.back, 1)
  592.                         end
  593.                         wait(3000)
  594.                     end
  595.                 end
  596.             end
  597.         end
  598.     else
  599.         return print("Error: " .. name .. " is not shared set user.")
  600.     end
  601.        
  602.     if not AccountManager.checkEmptySet_noBp() then
  603.         print("Still have parts of set on ... ")
  604.         wait(2000, 3000)
  605.         return AccountManager.saveSet()
  606.     else
  607.         print("Set saved to backpack ...")
  608.     end
  609.    
  610.     print("Attemping to save backpack ...")
  611.     if (back == info.set.back) then
  612.         wait(2000)
  613.         if reachlocation(info.pos.x, info.pos.y, info.pos.z) then
  614.             moveitems(info.set.back, "back", ground(info.pos.x, info.pos.y, info.pos.z), 1)
  615.             wait(2000)
  616.         end
  617.        
  618.         updateworld()
  619.         if not AccountManager.checkEmptySet() then
  620.             print("Failed to move backpack to save position ...")
  621.             return AccountManager.saveSet()
  622.         else
  623.             print("Set saved in house :)")
  624.             wait(3000)
  625.         --  setsettings("Settings\\Actions\\List\\Open BPS\\Enabled", "yes")
  626.         end
  627.     end
  628.     return true
  629. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement