Version 1.7

This commit is contained in:
OTCv8
2020-01-08 00:24:01 +01:00
parent 1d2bdf855d
commit 2a10e65ec0
42 changed files with 1157 additions and 563 deletions

View File

@@ -0,0 +1,113 @@
--[[
Config. create. load and save config file (.json)
Used by cavebot and other things
]]--
local context = G.botContext
context.Config = {}
local Config = context.Config
Config.exist = function(dir)
return g_resources.directoryExists(context.configDir .. "/" .. dir)
end
Config.create = function(dir)
g_resources.makeDir(context.configDir .. "/" .. dir)
return Config.exist(dir)
end
Config.list = function(dir)
if not Config.exist(dir) then
if not Config.create(dir) then
return contex.error("Can't create config dir: " .. context.configDir .. "/" .. dir)
end
end
return g_resources.listDirectoryFiles(context.configDir .. "/" .. dir)
end
Config.load = function(dir, name)
local file = context.configDir .. "/" .. dir .. "/" .. name .. ".json"
if g_resources.fileExists(file) then -- load json
return json.decode(g_resources.readFileContents(file))
end
file = context.configDir .. "/" .. dir .. "/" .. name .. ".cfg"
if g_resources.fileExists(file) then -- load cfg
return g_resources.readFileContents(file)
end
return context.error("Config " .. file .. " doesn't exist")
end
Config.save = function(dir, name, value)
if not Config.exist(dir) then
if not Config.create(dir) then
return contex.error("Can't create config dir: " .. context.configDir .. "/" .. dir)
end
end
local file = context.configDir .. "/" .. dir .. "/" .. name
if type(value) == 'string' then -- cfg
g_resources.writeFileContents(file .. ".cfg", value)
elseif type(value) == 'table' then -- json
g_resources.writeFileContents(file .. ".json", json.encode(value))
end
return context.error("Invalid config value type: " .. type(value))
end
Config.remove = function(dir, name)
local file = context.configDir .. "/" .. dir .. "/" .. name .. ".json"
if g_resources.fileExists(file) then
return g_resources.deleteFile(file)
end
file = context.configDir .. "/" .. dir .. "/" .. name .. ".cfg"
if g_resources.fileExists(file) then
return g_resources.deleteFile(file)
end
end
-- setup is used for BotConfig widget
-- not done yet
Config.setup = function(dir, widget, callback)
local refresh = function()
--
end
widget.switch.onClick = function()
widget.switch:setOn(not widget.switch:isOn())
end
widget.add = function()
end
widget.edit = function()
end
widget.remove = function()
end
--local configs = Config.list(dir)
--widget.list.
return {
isOn = function()
return widget.switch:isOn()
end,
isOff = function()
return not widget.switch:isOn()
end,
enable = function()
if not widget.switch:isOn() then
widget.switch:onClick()
end
end,
disable = function()
if widget.switch:isOn() then
widget.switch:onClick()
end
end,
save = function()
end
}
end

View File

@@ -0,0 +1,111 @@
local context = G.botContext
context.NPC = {}
context.NPC.talk = function(text)
if g_game.getClientVersion() >= 810 then
g_game.talkChannel(11, 0, text)
else
return context.say(text)
end
end
context.NPC.say = context.NPC.talk
context.NPC.isTrading = function()
return modules.game_npctrade.npcWindow and modules.game_npctrade.npcWindow:isVisible()
end
context.NPC.hasTrade = context.NPC.isTrading
context.NPC.hasTradeWindow = context.NPC.isTrading
context.NPC.getSellItems = function()
if not context.NPC.isTrading() then return {} end
local items = {}
for i, item in ipairs(modules.game_npctrade.tradeItems[modules.game_npctrade.SELL]) do
table.insert(items, {
id = item.ptr:getId(),
name = item.name,
count = item.ptr:getCount(),
subType = item.ptr:getSubType(),
weight = item.weight / 100,
price = item.price
})
end
return items
end
context.NPC.getBuyItems = function()
if not context.NPC.isTrading() then return {} end
for i, item in ipairs(modules.game_npctrade.tradeItems[modules.game_npctrade.BUY]) do
table.insert(items, {
id = item.ptr:getId(),
name = item.name,
count = item.ptr:getCount(),
subType = item.ptr:getSubType(),
weight = item.weight / 100,
price = item.price
})
end
return items
end
context.NPC.getSellQuantity = function(item)
if not context.NPC.isTrading() then return 0 end
if type(item) == 'number' then
item = Item.create(item)
end
return modules.game_npctrade.getSellQuantity(item)
end
context.NPC.canTradeItem = function(item)
if not context.NPC.isTrading() then return false end
if type(item) == 'number' then
item = Item.create(item)
end
return modules.game_npctrade.canTradeItem(item)
end
context.NPC.sell = function(item, count, ignoreEquipped)
if type(item) == 'number' then
item = Item.create(item)
end
if count == 0 then
count = 1
end
if count == nil or count == -1 then
count = context.NPC.getSellQuantity(item)
end
if ignoreEquipped == nil then
ignoreEquipped = true
end
g_game.sellItem(item, count, ignoreEquipped)
end
context.NPC.buy = function(item, count, ignoreCapacity, withBackpack)
if type(item) == 'number' then
item = Item.create(item)
end
if count == nil or count <= 0 then
count = 1
end
if ignoreCapacity == nil then
ignoreCapacity = false
end
if withBackpack == nil then
withBackpack = false
end
g_game.buyItem(item, count, ignoreCapacity, withBackpack)
end
context.NPC.sellAll = function()
if not context.NPC.isTrading() then return false end
modules.game_npctrade.sellAll()
end
context.NPC.closeTrade = function()
modules.game_npctrade.closeNpcTrade()
end
context.NPC.close = context.NPC.closeTrade
context.NPC.finish = context.NPC.closeTrade
context.NPC.endTrade = context.NPC.closeTrade
context.NPC.finishTrade = context.NPC.closeTrade

View File

@@ -11,7 +11,7 @@ end
context.addTab = function(name)
context.tabs:setOn(true)
return context.tabs:addTab(name, g_ui.createWidget('BotPanel')).tabPanel
return context.tabs:addTab(name, g_ui.createWidget('BotPanel')).tabPanel.content
end
context.addSwitch = function(id, text, onClickCallback, parent)