0% found this document useful (0 votes)
49 views6 pages

Questlog Lua

Uploaded by

joaodeborah084
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views6 pages

Questlog Lua

Uploaded by

joaodeborah084
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

questLogButton = nil

questTrackerButton = nil
window = nil
trackerWindow = nil
settings = {}

local callDelay = 1000 -- each call delay is also increased by random values (0-
callDelay/2)
local dispatcher = {}
local refreshEvent = nil

function init()
g_ui.importStyle('questlogwindow')

window = g_ui.createWidget('QuestLogWindow', rootWidget)


window:hide()
trackerWindow = g_ui.createWidget('QuestTracker',
modules.game_interface.getRightPanel())
trackerWindow:setup()
trackerWindow:hide()

if not g_app.isMobile() then


questLogButton = modules.client_topmenu.addLeftGameButton('questLogButton',
tr('Quest Log'), '/images/topbuttons/questlog', function() g_game.requestQuestLog()
end, false, 8)
questTrackerButton =
modules.client_topmenu.addLeftGameButton('questTrackerButton', tr('Quest Tracker'),
'/images/topbuttons/quest_tracker', toggle, false, 9)
end

connect(g_game, { onQuestLog = onGameQuestLog,


onQuestLine = onGameQuestLine,
onGameEnd = offline,
onGameStart = online})
if g_game.isOnline() then
online()
end
end

function terminate()
disconnect(g_game, { onQuestLog = onGameQuestLog,
onQuestLine = onGameQuestLine,
onGameEnd = offline,
onGameStart = online})

if g_game.isOnline() then
offline()
end
if questLogButton then
questLogButton:destroy()
end
if questTrackerButton then
questTrackerButton:destroy()
end

if refreshEvent then
removeEvent(refreshEvent)
refreshEvent = nil
end
end

function toggle()
if trackerWindow:isVisible() then
trackerWindow:hide()
else
trackerWindow:show()
end
end

function offline()
if window then
window:hide()
end
if trackerWindow then
trackerWindow:hide()
end
save()
-- reset tracker
[Link]:destroyChildren()
[Link]:setHeight(0)

if refreshEvent then
removeEvent(refreshEvent)
refreshEvent = nil
end
end

function online()
local playerName = g_game.getCharacterName()
if not playerName then return end -- just to be sure
load()
refreshQuests()
refreshTrackerWidgets()

local playerName = g_game.getCharacterName()


settings[playerName] = settings[playerName] or {}
local settings = settings[playerName]
local missionList = [Link]
local track = [Link]
local missionDescription = [Link]

connect(missionList, {
onChildFocusChange = function(self, focusedChild)
if focusedChild == nil then return end
missionDescription:setText([Link])
if focusedChild:isVisible() then
track:setEnabled(true)
end
track:setChecked(settings[[Link]])
end
}
)
end

function show(questlog)
if questlog then
window:raise()
window:show()
window:focus()
[Link] = nil -- reset current quest
[Link]:setVisible(true)
[Link]:setVisible(false)
[Link]:setText('Close')
[Link]:setVisible(true)
[Link]:setEnabled(false)
[Link]:setChecked(false)
[Link]:setText('')
else
[Link]:setVisible(false)
[Link]:setVisible(true)
[Link]:setText('Back')
[Link]:setVisible(false)
end
end

function back()
if window:isVisible() then
if [Link]:isVisible() then
window:hide()
else
show(true)
end
end
end

function showQuestLine()
local questList = [Link]
local child = questList:getFocusedChild()

g_game.requestQuestLine([Link])
[Link]:setText([Link])
[Link] = [Link]
end

function onGameQuestLog(quests)
show(true)

local questList = [Link]

questList:destroyChildren()
for i,questEntry in pairs(quests) do
local id, name, completed = unpack(questEntry)

local questLabel = g_ui.createWidget('QuestLabel', questList)


questLabel:setChecked(i % 2 == 0)
[Link] = id -- for quest tracker
[Link] = name
name = completed and name.." (completed)" or name
questLabel:setText(name)
[Link] = function()
[Link] = id
g_game.requestQuestLine(id)
[Link]:setText([Link])
end
end
questList:focusChild(questList:getFirstChild())
end
function onGameQuestLine(questId, questMissions)
if questId == [Link] then
show(false)
end

local missionList = [Link]


missionList:destroyChildren()
for i,questMission in pairs(questMissions) do
local name, description = unpack(questMission)

--questlog
local missionLabel = g_ui.createWidget('QuestLabel', missionList)
local widgetId = questId..'.'..i
missionLabel:setChecked(i % 2 == 0)
missionLabel:setId(widgetId)
[Link] = questId
[Link] = widgetId
missionLabel:setText(name)
[Link] = description
missionLabel:setVisible(questId == [Link])

--tracker
local trackerLabel = [Link][widgetId]
trackerLabel = trackerLabel or g_ui.createWidget('QuestTrackerLabel',
[Link])
trackerLabel:setId(widgetId)
[Link]:setText(description)
local data = settings[g_game.getCharacterName()]
trackerLabel:setVisible(description:len() > 0 and data[widgetId])
end
local focusTarget = missionList:getFirstChild()
if focusTarget and focusTarget:isVisible() then
missionList:focusChild(focusTarget)
end
end

function onTrackOptionChange(checkbox)
local newStatus = not checkbox:isChecked()
checkbox:setChecked(newStatus)

local missionList = [Link]


local focused = missionList:getFocusedChild()
if not focused then return end
local settings = settings[g_game.getCharacterName()]
local trackdata = [Link]

-- settings
settings[trackdata] = newStatus

local trackerWidget = [Link][trackdata]


if trackerWidget then
trackerWidget:setVisible(newStatus)
end

save()
end

function refreshQuests()
if not g_game.isOnline() then return end
local data = settings[g_game.getCharacterName()]
data = data or {}

-- do not execute when questlost is in use


if not window:isVisible() then
for questData, track in pairs(data) do
local id = [Link](questData, ".")[1]

if not track then


dispatcher[questData] = nil -- remove from dispatcher if no longer tracked
else
dispatcher[questData] = dispatcher[questData] or g_clock.millis()
end

if dispatcher[questData] and g_clock.millis() > dispatcher[questData] +


callDelay + [Link](callDelay/2) then
dispatcher[questData] = g_clock.millis()
scheduleEvent(function()
g_game.requestQuestLine(id) -- request update
end, [Link](callDelay/2) )
end
end
end

refreshEvent = scheduleEvent(refreshQuests, callDelay)


end

function refreshTrackerWidgets()
if not g_game.isOnline() then return end
local data = settings[g_game.getCharacterName()]
data = data or {}

for questData, enabled in pairs(data) do


local data = [Link](questData, ".")
local id = tonumber(data[1])

local widget = [Link][questData]


if not widget then
g_game.requestQuestLine(id)
end
end
end

-- json handlers
function load()
local file = "/settings/[Link]"
if g_resources.fileExists(file) then
local status, result = pcall(function()
return [Link](g_resources.readFileContents(file))
end)
if not status then
return g_logger.error(
"Error while reading profiles file. To fix this problem you can
delete [Link]. Details: " ..
result)
end
settings = result
end
end

function save()
local file = "/settings/[Link]"
local status, result = pcall(function() return [Link](settings, 2) end)
if not status then
return g_logger.error(
"Error while saving profile settings. Data won't be saved.
Details: " ..
result)
end
if result:len() > 100 * 1024 * 1024 then
return g_logger.error(
"Something went wrong, file is above 100MB, won't be saved")
end
g_resources.writeFileContents(file, result)
end

You might also like