Reply
 
Thread Tools Display Modes
Old 02-12-12, 07:08 AM   #1
algritz
Claw of Regulos
AddOn Author - Click to view addons
Join Date: Jun 2011
Posts: 11
Quote:
Originally Posted by Zoc View Post
Thank you!

With the info in the post you provided me, I created the LibCoroutine.

It's very early and the code can be considered Alpha quality. It's included with zBag (Alpha4), in case you want to take a look on how I implemented it.

Suggestions / improvements are welcome
Glad I could help !

I'll have a look at your library. Not sure I'll implement it for my add-on since I've already done this part, but maybe for my next project
algritz is offline   Reply With Quote
Old 02-12-12, 12:41 PM   #2
Zoc
Bomani Harbinger
AddOn Author - Click to view addons
Join Date: Feb 2012
Posts: 6
Talking

Quote:
Originally Posted by algritz View Post
Glad I could help !

I'll have a look at your library. Not sure I'll implement it for my add-on since I've already done this part, but maybe for my next project
Thanks!

I've uploaded it here: http://www.riftui.com/downloads/info...Coroutine.html

It doesn't support Queue, like you used on your project ( I still need to study that part ), but in case you need it in your next project and want to improve the library, you're more than welcome to do so
Zoc is offline   Reply With Quote
Unread 02-13-12, 05:36 AM   #3
algritz
Claw of Regulos
AddOn Author - Click to view addons
Join Date: Jun 2011
Posts: 11
here's how I have combines both:

Code:
-- Coroutines management table
local function SetupCoroutineTable()
	coroutine_table = { };
end

-- adds a coroutine to the queue
local function AddCoroutine(Item)
	table.insert(coroutine_table, Item);
end

-- remove a coroutine to the queue
local function RemoveCoroutine(Index)
	table.remove(coroutine_table, Index);
end

-- resumes a coroutine while it is active
local function ResumeCoroutine(Index)
	Item = coroutine_table[Index];
	if coroutine.status(Item) ~= 'dead' then
		coroutine.resume(Item);
	end
end

-- loops through the coroutines lists and calls the resume handler
local function ResumeAllCoroutines()
	-- make sure there is something to process
	if coroutine_table ~= {} then
		for Index,Item in ipairs(coroutine_table) do
			ResumeCoroutine(Index);
		end
	else
		return
	end
end


-- pause timer
function Pause(seconds)
	local start = Inspect.Time.Frame()
	while start + seconds > Inspect.Time.Frame() do
		coroutine.yield()
	end
end


-- function that check for the global queue status
local queueStatus = false
local function QueueStatus()
	-- inspects the queue status
	queueStatus = Inspect.Queue.Status("global")
	if queueStatus then return end -- global queue is still backlogged, var is true so exit out
	queueStatus = false
end

-- small function that opens an email
local function MailOpen(k)
	-- check if the queue is available before opening
	if not QueueStatus() then
		Command.Mail.Open(k)
	else
		Pause(0.2)
		MailOpen(k)
	end
end

-- function that parses each email and store it in a emprary database for further processing
local function Mailboxparser()
	print("Starting to read emails")
	local status = Inspect.Interaction("mail")
	if status == true then
		-- get the list of email
		mailList = Inspect.Mail.List()
		-- checking how many email will be parsed (cannot  use table.getn, since this table contains key/values => only way is to iterate throught the table)
		mail_number = 0
		for k,v in pairs(mailList) do
			mail_number = mail_number + 1
		end
		-- index that stores the nuber of processed emails
		processed_mail_count = 1
		-- fetch through each emails
		for k, v in pairs(mailList) do
			-- if email hasn't been parsed previously
			if not setContains(mail_history, k) or mail_history == {} then
				-- open email to have access to details
				mailOpen(k)
				Pause(0.2)
				-- get details
				details = (Inspect.Mail.Detail(k))
				-- table that will store the mail content
				mail_details = {}
				-- feeding the table
				table.insert(mail_details, details["from"])
				table.insert(mail_details, details["subject"])
				table.insert(mail_details, details["body"])
				--table.insert(mail_details, os.date)
				-- table that will contain teh attachment list if there is any
				attachment_list = {}
				-- detecty if the is any attachment
				if tonumber(details["attachments"]) == nil and details["attachments"] ~= nil then
					-- add item ids in a table
					for ka, va in pairs(details["attachments"]) do
						table.insert(attachment_list, va)
					end
				end
				table.insert(mail_details, attachment_list)
				-- detect if mail is actually "read" (only way to declare the mail as processed)
				if details["body"] ~= nil then
					addToSet(mail_history, k, mail_details)
					processed_mail_count = processed_mail_count + 1
				end

				-- check if we're done processing
				if mail_number == processed_mail_count then
					print("Email recording complete: " .. processed_mail_count .. " entries saved")
					RemoveCoroutine(parsing_coro)
				end
			end
		end
	end
end


local function Launch_mailboxparser()
	parsing_coro = coroutine.create(Mailboxparser)
	AddCoroutine(parsing_coro)
end

SetupCoroutineTable()

table.insert(Event.Queue.Status, {QueueStatus, "aher", "Queue Status"})
table.insert(Event.System.Update.Begin, {function() ResumeAllCoroutines() end, "aher", "OnUpdate" })


launch_mailboxparser() -- just for the sake of calling the function
Re-reading this have me wondering though. "if not QueueStatus() then", I wonder if I should just call the queuestatus variable instead.

Last edited by algritz : 02-13-12 at 02:45 PM.
algritz is offline   Reply With Quote
Unread 02-13-12, 12:44 PM   #4
Zoc
Bomani Harbinger
AddOn Author - Click to view addons
Join Date: Feb 2012
Posts: 6
Arrow

How do you handle the "pause" command ? I don't see a registered coroutine.

Also, I don't see QueueStatus() returning a value, and queueStatus var is never read on mailOpen(k).

May I give you a suggestion ? Always start functions with Capital letter and avoid naming variables with the same name as functions, just differing on letter capitalization
Zoc is offline   Reply With Quote
Unread 02-13-12, 02:23 PM   #5
algritz
Claw of Regulos
AddOn Author - Click to view addons
Join Date: Jun 2011
Posts: 11
Quote:
Originally Posted by Zoc View Post
How do you handle the "pause" command ? I don't see a registered coroutine.
My bad, I forgot to include it in my code, I'll update my previous comment.


Quote:
Also, I don't see QueueStatus() returning a value, and queueStatus var is never read on mailOpen(k).
This is due to the lack of clarity of my own code (shame on me) I end up just checking the value of the "global" queueStatus (it is defined as a local, but since it is done at the root level of my add-on its ends up being accessible everywhere)

Quote:
May I give you a suggestion ? Always start functions with Capital letter and avoid naming variables with the same name as functions, just differing on letter capitalization
I totally agree, I just realized how much my code is a mess :P
algritz is offline   Reply With Quote
Reply

Go BackRiftui » AddOns, Layouts and Macros » Help/Support » How do you "wait" without crashing the game client.


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off