-
Notifications
You must be signed in to change notification settings - Fork 25
/
candybot.lua
260 lines (207 loc) · 5.65 KB
/
candybot.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
--[[
@Authors: Ben Dol (BeniS)
@Details: Otclient module entry point. This handles
main bot controls and functionality.
]]
CandyBot = extends(UIWidget, "CandyBot")
CandyBot.window = nil
CandyBot.defaultOptions = {
["LoggerType"] = 1,
["PrintLogs"] = false
}
dofile('consts.lua')
dofile('helper.lua')
dofile('logger.lua')
dofile('modules.lua')
dofile('events.lua')
dofile('listeners.lua')
dofiles('classes')
dofiles('classes/ui')
dofiles('extensions')
local botButton
local botTabBar
local enabled = false
local writeDir = "/candybot"
local function setupDefaultOptions()
for _, module in pairs(Modules.getOptions()) do
for k, option in pairs(module) do
CandyBot.defaultOptions[k] = option
end
end
end
local function loadModules()
Modules.init()
-- setup the default options
setupDefaultOptions()
end
function init()
CandyBot.window = g_ui.displayUI('candybot.otui')
CandyBot.window:setVisible(false)
botButton = modules.client_topmenu.addRightGameToggleButton(
'botButton', 'Bot (Ctrl+Shift+B)', 'candybot', CandyBot.toggle)
botButton:setOn(false)
botTabBar = CandyBot.window:getChildById('botTabBar')
botTabBar:setContentWidget(CandyBot.window:getChildById('botContent'))
botTabBar:setTabSpacing(-1)
-- setup resources
if not g_resources.directoryExists(writeDir) then
g_resources.makeDir(writeDir)
end
-- load modules
loadModules()
-- init bot logger
BotLogger.init()
-- hook functions
connect(g_game, {
onGameStart = CandyBot.online,
onGameEnd = CandyBot.offline
})
-- get bot settings
CandyBot.options = g_settings.getNode('Bot') or {}
if g_game.isOnline() then
CandyBot.online()
end
end
function terminate()
CandyBot.hide()
disconnect(g_game, {
onGameStart = CandyBot.online,
onGameEnd = CandyBot.offline
})
if g_game.isOnline() then
CandyBot.offline()
end
Modules.terminate()
if botButton then
botButton:destroy()
botButton = nil
end
CandyBot.window:destroy()
end
function CandyBot.online()
g_game.enableFeature(GameKeepUnawareTiles)
CandyBot.loadOptions()
-- bind keys
g_keyboard.bindKeyDown('Ctrl+Shift+B', CandyBot.toggle)
end
function CandyBot.offline()
Modules.stop()
CandyBot.hide()
-- unbind keys
g_keyboard.unbindKeyDown('Ctrl+Shift+B')
end
function CandyBot.toggle()
if CandyBot.window:isVisible() then
CandyBot.hide()
else
CandyBot.show()
CandyBot.window:focus()
end
end
function CandyBot.show()
if g_game.isOnline() then
CandyBot.window:show()
botButton:setOn(true)
end
end
function CandyBot.hide()
CandyBot.window:hide()
botButton:setOn(false)
end
function CandyBot.enable(state)
enabled = state
if not state then Modules.stop() end
end
function CandyBot.isEnabled()
return enabled
end
function CandyBot.getIcon()
return botIcon
end
function CandyBot.getUI()
return CandyBot.window
end
function CandyBot.getParent()
return CandyBot.window:getParent() -- main window
end
function CandyBot.getWriteDir()
return writeDir
end
function CandyBot.getOptions()
local char = g_game.getCharacterName() .. '@' .. tostring(G.host) .. '@' .. tostring(G.port)
return CandyBot.options and CandyBot.options[char] or CandyBot.defaultOptions
end
function CandyBot.getOption(key)
return CandyBot.getOptions()[key]
end
function CandyBot.loadOptions()
local char = g_game.getCharacterName()
local server = tostring(G.host) .. '@' .. tostring(G.port)
if CandyBot.options[char .. '@' .. server] ~= nil then
for i, v in pairs(CandyBot.options[char .. '@' .. server]) do
addEvent(function() CandyBot.changeOption(i, v, true) end)
end
elseif CandyBot.options[char] ~= nil then
for i, v in pairs(CandyBot.options[char]) do
addEvent(function() CandyBot.changeOption(i, v, true) end)
end
else
for i, v in pairs(CandyBot.defaultOptions) do
addEvent(function() CandyBot.changeOption(i, v, true) end)
end
end
end
function CandyBot.changeOption(key, state, loading)
local loading = loading or false
if state == nil then
return
end
if not CandyBot.options then -- bug with setting bot node to empty on loading
return
end
if CandyBot.defaultOptions[key] == nil then
CandyBot.options[key] = nil
return
end
if g_game.isOnline() then
local panel = CandyBot.window
if loading then
local widget
for k, p in pairs(Modules.getPanels()) do
widget = p:recursiveGetChildById(key)
if widget then break end
end
if not widget then
widget = panel:recursiveGetChildById(key)
if not widget then
BotLogger.warning("CandyBot: no widget found with name '"..key.."'")
return
end
end
local style = widget:getStyle().__class
if style == 'UITextEdit' then
widget:setText(state)
elseif style == 'UIComboBox' then
widget:setCurrentOption(state)
elseif style == 'UICheckBox' then
widget:setChecked(state)
elseif style == 'UIItem' then
widget:setItemId(state)
elseif style == 'UIScrollBar' then
local value = tonumber(state)
if value then widget:setValue(value) end
elseif style == 'UIScrollArea' then
local child = widget:getChildById(state)
if child then widget:focusChild(child, MouseFocusReason) end
end
end
Modules.notifyChange(key, state)
local char = g_game.getCharacterName() .. '@' .. tostring(G.host) .. '@' .. tostring(G.port)
if CandyBot.options[char] == nil then
CandyBot.options[char] = {}
end
CandyBot.options[char][key] = state
-- Update the settings
g_settings.setNode('Bot', CandyBot.options)
end
end