Thread Tools Display Modes
12-28-13, 06:59 AM   #1
Caellian
A Frostmaul Preserver
 
Caellian's Avatar
Join Date: May 2006
Posts: 281
Help with calendar stuff

I have this little function that allows me to automatically accept events if they're created by a member of my guild.

Code:
function Management:ManageCalendar()
	for month = 0, 12 do
		for day = 1, 31 do
			for eventIndex = 1, CalendarGetNumDayEvents(month, day) do
				local _, _, _, calendarType, _, _, _, _, _, invitedBy = CalendarGetDayEvent(month, day, eventIndex)
				if calendarType == "PLAYER" and CalendarContextInviteIsPending(month, day, eventIndex) then
					if caelUI.isGuild(invitedBy) then -- Check this first so you don't have to check it twice.
						CalendarContextInviteAvailable(month, day, eventIndex)
--					elseif invitedBy == "" then -- or (invitedBy ~= caelUI.playerName and not caelUI.isFriend(invitedBy) and not not caelUI.isInGroup(invitedBy)) then
--						CalendarContextInviteRemove(month, day, eventIndex)
					end
				end
			end
		end
	end
end
But i'd like to also make it automatically remove events that happened before the current day, if that's clear enough

Any idea how ?
__________________
if (sizeof(workload) > sizeof(brain_capacity)) { die('System Overload'); }
  Reply With Quote
12-28-13, 08:45 AM   #2
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Get the current day:

Code:
local _, thisMonth, thisDay, thisYear = CalendarGetDate()
...and then compare those values against the iterator values inside your loops.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
12-28-13, 09:57 AM   #3
Caellian
A Frostmaul Preserver
 
Caellian's Avatar
Join Date: May 2006
Posts: 281
I'm not sure i understand what to compare with "thisDay"

I mean, how to get the day of all the events and compare them with "thisDay"

Or should i just do

Code:
function Management:ManageCalendar()
	for month = 0, 12 do
		for day = 1, 31 do
			for eventIndex = 1, CalendarGetNumDayEvents(month, day) do
				local title, _, _, calendarType, _, _, _, _, _, invitedBy = CalendarGetDayEvent(month, day, eventIndex)

				local _, thisMonth, thisDay, thisYear = CalendarGetDate()

				if calendarType == "PLAYER" then
					if day < thisDay then
						CalendarContextInviteRemove(month, day, eventIndex)
					elseif CalendarContextInviteIsPending(month, day, eventIndex) then
						if caelUI.isGuild(invitedBy) then -- Check this first so you don't have to check it twice.
							CalendarContextInviteAvailable(month, day, eventIndex)
--						elseif invitedBy == "" then -- or (invitedBy ~= caelUI.playerName and not caelUI.isFriend(invitedBy) and not not caelUI.isInGroup(invitedBy)) then
--							CalendarContextInviteRemove(month, day, eventIndex)
						end
					end
				end
			end
		end
	end
end
Problem is, i can't really try it
__________________
if (sizeof(workload) > sizeof(brain_capacity)) { die('System Overload'); }

Last edited by Caellian : 12-28-13 at 10:31 AM.
  Reply With Quote
12-28-13, 11:44 AM   #4
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
(1) You only need to look up CalendarGetDate() once, not 400 times.

(2) The month returned by CalendarGetDate() is probably not actually important, since the month iterator is an offset; just remove events where month == 0 and day < thisDay. If you wanted to go back through previous months you'd need to start iterating from a negative number instead of 0.

(3) I don't know what your caelUI.isGuild function is doing, but assuming you just want to check whether someone is in your guild, there is a dedicated API function for that -- UnitIsInMyGuild(unitID or unitName). Similarly, you can use UnitInRaid(unit) or UnitInParty(unit) instead of your custom caelUI.isGroup function.

(4) I can't test either, but this should work:

Code:
function Management:ManageCalendar()
	local _, thisMonth, thisDay, thisYear = CalendarGetDate()
	for month = 0, 12 do
		for day = 1, 31 do
			for eventIndex = 1, CalendarGetNumDayEvents(month, day) do
				local title, _, _, calendarType, _, _, _, _, _, invitedBy = CalendarGetDayEvent(month, day, eventIndex)
				if calendarType == "PLAYER" then
					if month == 0 and day < thisDay then
						CalendarContextInviteRemove(month, day, eventIndex)
					elseif CalendarContextInviteIsPending(month, day, eventIndex) then
						if UnitIsInMyGuild(invitedBy) then -- Check this first so you don't have to check it twice.
							CalendarContextInviteAvailable(month, day, eventIndex)
--						elseif invitedBy == "" then -- or (invitedBy ~= caelUI.playerName and not caelUI.isFriend(invitedBy) and (UnitInRaid(invitedBy) or UnitInGroup(invitedBy))) then
--							CalendarContextInviteRemove(month, day, eventIndex)
						end
					end
				end
			end
		end
	end
end
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.

Last edited by Phanx : 12-28-13 at 11:47 AM.
  Reply With Quote
12-28-13, 12:25 PM   #5
Caellian
A Frostmaul Preserver
 
Caellian's Avatar
Join Date: May 2006
Posts: 281
Why the "month == 0" check, it limits you to the current month instead of doing them all, or am i mistaken ?

And, this is my guild function:

Code:
caelUI.isGuild = function(unit)
	local numGuildMembers = GetNumGuildMembers()

	for i = 1, numGuildMembers do
		local member = GetGuildRosterInfo(i)

		member = Ambiguate(member, "guild")

		if unit == member then
			return true
		end
	end

	return false
end
__________________
if (sizeof(workload) > sizeof(brain_capacity)) { die('System Overload'); }
  Reply With Quote
12-29-13, 05:11 AM   #6
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Yeah, you don't need that function. Just use UnitIsInMyGuild.

The month == 0 check is there because you said you only wanted to remove events that have already occurred. The month value you pass to CalendarGetNumDayEvents is an offset from the current month, not a calendar month number. So, month 0 = this month, month 1 = next month, month 12 = this month next year, etc. If you wanted to go into the past, you'd need to start the iterator from a negative value, since last month = -1, etc.

If you didn't have that check, then on the 29th of every month (using today's date as an example) you would be removing every event from every month -- including future months -- that occurred before the 29th of the month.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
12-29-13, 05:26 AM   #7
Caellian
A Frostmaul Preserver
 
Caellian's Avatar
Join Date: May 2006
Posts: 281
Originally Posted by Phanx View Post
If you didn't have that check, then on the 29th of every month (using today's date as an example) you would be removing every event from every month -- including future months -- that occurred before the 29th of the month.
Ahh, now this made perfect sense, thanks
__________________
if (sizeof(workload) > sizeof(brain_capacity)) { die('System Overload'); }
  Reply With Quote
12-30-13, 05:23 PM   #8
Caellian
A Frostmaul Preserver
 
Caellian's Avatar
Join Date: May 2006
Posts: 281
Actually, there is a minor issue, taking today as example, if i switch to the January page, it will delete all the events between today and the 31 of January. Is there any way to prevent that ?
__________________
if (sizeof(workload) > sizeof(brain_capacity)) { die('System Overload'); }
  Reply With Quote
12-30-13, 06:03 PM   #9
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Probably, but I'll have to investigate further. I actually have work to do at the moment (shocking, I know!) but I'll get back to you.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
01-07-14, 01:23 AM   #10
Caellian
A Frostmaul Preserver
 
Caellian's Avatar
Join Date: May 2006
Posts: 281
Originally Posted by Phanx View Post
Probably, but I'll have to investigate further. I actually have work to do at the moment (shocking, I know!) but I'll get back to you.
Did you have the chance to take a look yet ?
__________________
if (sizeof(workload) > sizeof(brain_capacity)) { die('System Overload'); }
  Reply With Quote
01-07-14, 05:59 PM   #11
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Just did some investigation... I have to say the calendar API is quite possibly the worst thing Blizzard has ever written. Why the hell is everything relative to the month being viewed instead of the current actual month?! Anyway, I think the easiest solution is going to be to just run the scan only if you're viewing the current month (eg. when you first open the calendar):

Code:
local current = date("*t")

local f = CreateFrame("Frame")
f:RegisterEvent("CALENDAR_UPDATE_EVENT_LIST")
f:SetScript("OnEvent", function(self, event, ...)
	local viewedMonth, viewedYear = CalendarGetMonth()
	if viewedMonth ~= current.month or viewedYear ~= current.year then
		return print("Not viewing current month. Skipping scan.")
	end
	print("Scanning...")
	for monthOffset = -12, 12 do
		local scanMonth, scanYear, numDays = CalendarGetMonth(monthOffset)
		for day = 1, numDays do
			for eventIndex = 1, CalendarGetNumDayEvents(monthOffset, day) do
				local title, _, _, calendarType, _, _, _, _, _, invitedBy = CalendarGetDayEvent(monthOffset, day, eventIndex)
				if calendarType == "PLAYER" then
					if monthOffset < 0 or (monthOffset == 0 and day < current.day) then
						-- Remove the event
						CalendarContextInviteRemove(monthOffset, day, eventIndex)
					elseif CalendarContextInviteIsPending(monthOffset, day, eventIndex) then
						-- Accept the event if it's from a friend or guildmate
						if UnitIsInMyGuild(invitedBy) then
							print(format("Accept guild event: %s from %s on %d-%d-%d", title, invitedBy, day, scanMonth, scanYear))
							--CalendarContextInviteAvailable(monthOffset, day, eventIndex)
						elseif invitedBy == "" then
							print(format("Remove event: %s on %d-%d-%d", title, day, scanMonth, scanYear))
							--CalendarContextInviteRemove(monthOffset, day, eventIndex)
						end
					end
				end
			end
		end
	end
	print("Done.")
end)
Once it's "working" un-comment the CalendarContextInviteAvailable and CalendarContextInviteRemove lines (right after the print lines) to let it actually work, and comment out all the print lines if you want it to be silent.

I sort of tested it, but since I don't have a guild, or any friends who still play, or even any spammers invite me to events, there wasn't much real testing I could do.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
01-10-14, 01:47 AM   #12
Caellian
A Frostmaul Preserver
 
Caellian's Avatar
Join Date: May 2006
Posts: 281
Ok well, i've been testing it for 2 days now on several toons and it works just fine, only i had to completely disable the last "elseif invitedBy == "" then" part because, if i open the calendar right after logging in, i guess a "GUILD_ROSTER_UPDATE" event didn't happen yet and everyone is considered being ""

Anyway, that was supposed to work as some kind of anti Calandar spam on paper, but it doesn't work well enough.
__________________
if (sizeof(workload) > sizeof(brain_capacity)) { die('System Overload'); }
  Reply With Quote
01-12-14, 10:47 AM   #13
pelf
Sentient Plasmoid
 
pelf's Avatar
Premium Member
Join Date: May 2008
Posts: 133
Originally Posted by Phanx View Post
Why the hell is everything relative to the month being viewed instead of the current actual month?!
Maybe because they were trying to avoid taking year values? I can't guess what their motivation for that might've been; but, maybe they had some design reason. Without justification, it seems a lot nicer to just ask for the year and default to the current year when it's not specified.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Help with calendar stuff


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